Search in sources :

Example 1 with ComputeImage

use of com.emc.storageos.db.client.model.ComputeImage in project coprhd-controller by CoprHD.

the class ImageServerControllerImpl method importImageMethod.

/**
 * Method to import an image
 *
 * @param ciId {@link URI} computeImage URI
 * @param imageServer {@link ComputeImageServer} imageServer instance
 * @param opName operation Name
 * @param stepId {@link String} step Id
 */
public void importImageMethod(URI ciId, ComputeImageServer imageServer, String opName, String stepId) {
    log.info("importImageMethod importing image {} on to imageServer {}", ciId, imageServer.getId());
    ImageServerDialog d = null;
    ComputeImage ci = null;
    try {
        WorkflowStepCompleter.stepExecuting(stepId);
        ci = dbClient.queryObject(ComputeImage.class, ciId);
        SSHSession session = new SSHSession();
        session.connect(imageServer.getImageServerIp(), imageServer.getSshPort(), imageServer.getImageServerUser(), imageServer.getImageServerPassword());
        d = new ImageServerDialog(session, imageServer.getSshTimeoutMs());
        importImage(imageServer, ci, d);
        WorkflowStepCompleter.stepSucceded(stepId);
    } catch (InternalException e) {
        log.error("Exception importing image: " + e.getMessage(), e);
        updateFailedImages(imageServer.getId(), ci);
        WorkflowStepCompleter.stepFailed(stepId, e);
    } catch (Exception e) {
        log.error("Unexpected exception importing image: " + e.getMessage(), e);
        String operationName = opName;
        if (null == operationName) {
            operationName = ResourceOperationTypeEnum.IMPORT_IMAGE.getName();
        }
        updateFailedImages(imageServer.getId(), ci);
        WorkflowStepCompleter.stepFailed(stepId, ImageServerControllerException.exceptions.unexpectedException(operationName, e));
    } finally {
        try {
            if (d != null && d.isConnected()) {
                d.close();
            }
        } catch (Exception e) {
            log.error(FAILED_TO_CLOSE_STR, e);
        }
    }
}
Also used : SSHSession(com.emc.storageos.networkcontroller.SSHSession) InternalException(com.emc.storageos.svcs.errorhandling.resources.InternalException) DeviceControllerException(com.emc.storageos.exceptions.DeviceControllerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) ImageServerControllerException(com.emc.storageos.imageservercontroller.exceptions.ImageServerControllerException) ComputeImage(com.emc.storageos.db.client.model.ComputeImage) InternalException(com.emc.storageos.svcs.errorhandling.resources.InternalException)

Example 2 with ComputeImage

use of com.emc.storageos.db.client.model.ComputeImage in project coprhd-controller by CoprHD.

the class ImageServerControllerImpl method installOperatingSystem.

/**
 * Install OS
 * @param task {@link AsyncTask}
 * @param computeImageJob {@link URI} compute imageJob id
 * @throws InternalException
 */
@Override
public void installOperatingSystem(AsyncTask task, URI computeImageJob) throws InternalException {
    log.info("installOperatingSystem");
    Host host = dbClient.queryObject(Host.class, task._id);
    ComputeElement ce = dbClient.queryObject(ComputeElement.class, host.getComputeElement());
    ComputeSystem cs = dbClient.queryObject(ComputeSystem.class, ce.getComputeSystem());
    ComputeImageJob job = dbClient.queryObject(ComputeImageJob.class, computeImageJob);
    ComputeImageServer imageServer = dbClient.queryObject(ComputeImageServer.class, job.getComputeImageServerId());
    ComputeImage img = dbClient.queryObject(ComputeImage.class, job.getComputeImageId());
    TaskCompleter completer = null;
    try {
        completer = new OsInstallCompleter(host.getId(), task._opId, job.getId(), EVENT_SERVICE_TYPE);
        boolean imageServerVerified = verifyImageServer(imageServer);
        if (!imageServerVerified) {
            throw ImageServerControllerException.exceptions.imageServerNotSetup("Can't install operating system: " + imageServerErrorMsg);
        }
        Workflow workflow = workflowService.getNewWorkflow(this, OS_INSTALL_WF, true, task._opId);
        String waitFor = null;
        waitFor = workflow.createStep(OS_INSTALL_IMAGE_SERVER_CHECK_STEP, "image server check pre os install", waitFor, img.getId(), img.getImageType(), this.getClass(), new Workflow.Method("preOsInstallImageServerCheck", job.getId()), new Workflow.Method(ROLLBACK_NOTHING_METHOD), null);
        waitFor = workflow.createStep(OS_INSTALL_PREPARE_PXE_STEP, "prepare pxe boot", waitFor, img.getId(), img.getImageType(), this.getClass(), new Workflow.Method("preparePxeBootMethod", job.getId()), new Workflow.Method(ROLLBACK_NOTHING_METHOD), null);
        String prepStepId = workflow.createStepId();
        waitFor = computeDeviceController.addStepsPreOsInstall(workflow, waitFor, cs.getId(), host.getId(), prepStepId);
        waitFor = workflow.createStep(OS_INSTALL_WAIT_FOR_FINISH_STEP, "wait for os install to finish", waitFor, img.getId(), img.getImageType(), this.getClass(), new Workflow.Method("waitForFinishMethod", job.getId(), host.getHostName()), new Workflow.Method(ROLLBACK_NOTHING_METHOD), null);
        waitFor = computeDeviceController.addStepsPostOsInstall(workflow, waitFor, cs.getId(), ce.getId(), host.getId(), prepStepId, job.getVolumeId());
        workflow.executePlan(completer, SUCCESS);
    } catch (Exception e) {
        log.error("installOperatingSystem caught an exception.", e);
        ServiceError serviceError = DeviceControllerException.errors.jobFailed(e);
        completer.error(dbClient, serviceError);
    }
}
Also used : ServiceError(com.emc.storageos.svcs.errorhandling.model.ServiceError) ComputeImageServer(com.emc.storageos.db.client.model.ComputeImageServer) Workflow(com.emc.storageos.workflow.Workflow) Host(com.emc.storageos.db.client.model.Host) InternalException(com.emc.storageos.svcs.errorhandling.resources.InternalException) DeviceControllerException(com.emc.storageos.exceptions.DeviceControllerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) ImageServerControllerException(com.emc.storageos.imageservercontroller.exceptions.ImageServerControllerException) ComputeImage(com.emc.storageos.db.client.model.ComputeImage) OsInstallCompleter(com.emc.storageos.imageservercontroller.OsInstallCompleter) ComputeElement(com.emc.storageos.db.client.model.ComputeElement) ComputeImageJob(com.emc.storageos.db.client.model.ComputeImageJob) TaskCompleter(com.emc.storageos.volumecontroller.TaskCompleter) ComputeSystem(com.emc.storageos.db.client.model.ComputeSystem)

Example 3 with ComputeImage

use of com.emc.storageos.db.client.model.ComputeImage in project coprhd-controller by CoprHD.

the class ImageServerControllerImpl method importImage.

/**
 * Utility method to import an image to the given computeimage server
 *
 * @param imageServer {@link ComputeImageServer} instance.
 * @param ci {@link ComputeImage} instance
 * @param imageserverDialog {@link ImageServerDialog} instance
 */
private void importImage(ComputeImageServer imageServer, ComputeImage ci, ImageServerDialog imageserverDialog) {
    log.info("Importing image {} on to {} imageServer", ci.getLabel(), imageServer.getLabel());
    String deCrpytedURL = decryptImageURLPassword(ci.getImageUrl());
    deCrpytedURL = sanitizeUrl(deCrpytedURL);
    String ts = String.valueOf(System.currentTimeMillis());
    String[] tokens = ci.getImageUrl().split("/");
    String imageName = tokens[tokens.length - 1];
    String imagePath = TMP + "/" + imageName;
    String tempDir = TMP + "/os" + ts + "/";
    imageserverDialog.init();
    log.info("connected to image server {}", imageServer.getLabel());
    log.info("cd to {}", TMP);
    imageserverDialog.cd(TMP);
    log.info("download image");
    // CTRL-12030: special characters in URL's password cause issues on
    // Image Server. Adding quotes.
    boolean res = imageserverDialog.wget("'" + deCrpytedURL + "'", imageName, imageServer.getImageImportTimeoutMs());
    if (res) {
        log.info("downloaded image successfully on to {}  imageServer", imageServer.getLabel());
    } else {
        throw ImageServerControllerException.exceptions.fileDownloadFailed(maskImageURLPassword(ci.getImageUrl()));
    }
    log.info("create temp dir {}", tempDir);
    imageserverDialog.mkdir(tempDir);
    log.info("mount image onto temp dir of {}", imageServer.getLabel());
    imageserverDialog.mount(imageName, tempDir);
    log.info("Analyze metadata");
    ComputeImage osMetadata = getOsMetadata(imageserverDialog, imagePath, tempDir);
    isSupportedImage(osMetadata);
    // make sure it is not already loaded
    List<URI> ids = dbClient.queryByType(ComputeImage.class, true);
    Iterator<ComputeImage> iter = dbClient.queryIterativeObjects(ComputeImage.class, ids);
    while (iter.hasNext()) {
        ComputeImage existingImage = iter.next();
        if (osMetadata.fullName().equals(existingImage.getImageName()) && imageServer.getComputeImages() != null && imageServer.getComputeImages().contains(existingImage.getId().toString())) {
            log.error("This image is already imported, id: {}", existingImage.getId());
            cleanupTemp(imageserverDialog, tempDir, imagePath);
            throw ImageServerControllerException.exceptions.duplicateImage(osMetadata.fullName());
        }
    }
    log.info("Compute image '" + osMetadata.fullName() + "' will be loaded.");
    // copy OS into TFTP boot directory
    String targetDir = imageServer.getTftpBootDir() + imageServer.getImageDir() + osMetadata.fullName();
    imageserverDialog.rm(targetDir);
    log.info("Saving image into target directory " + targetDir);
    imageserverDialog.cpDir(tempDir, targetDir);
    log.info("Saved");
    log.info("Change target directory permissions to 755");
    imageserverDialog.chmodDir("755", targetDir);
    // save in DB
    ci.setOsName(osMetadata.getOsName());
    ci.setOsVersion(osMetadata.getOsVersion());
    ci.setOsUpdate(osMetadata.getOsUpdate());
    ci.setOsBuild(osMetadata.getOsBuild());
    ci.setOsArchitecture(osMetadata.getOsArchitecture());
    ci.setCustomName(osMetadata.getCustomName());
    ci.setPathToDirectory(imageServer.getImageDir() + osMetadata.fullName() + "/");
    ci.setImageName(osMetadata.fullName());
    ci.setImageType(osMetadata.getImageType());
    ci.setComputeImageStatus(ComputeImageStatus.AVAILABLE.toString());
    dbClient.updateObject(ci);
    String ciURIString = ci.getId().toString();
    // update the imageServer with the successfully updated image.
    if (imageServer.getComputeImages() == null) {
        imageServer.setComputeImages(new StringSet());
    }
    imageServer.getComputeImages().add(ciURIString);
    // check if this image was previously failed, if so remove from fail list
    if (imageServer.getFailedComputeImages() != null && imageServer.getFailedComputeImages().contains(ciURIString)) {
        imageServer.getFailedComputeImages().remove(ciURIString);
    }
    log.info("Successfully imported image {} on to {} imageServer", ci.getLabel(), imageServer.getLabel());
    dbClient.updateObject(imageServer);
    // clean up
    cleanupTemp(imageserverDialog, tempDir, imagePath);
}
Also used : StringSet(com.emc.storageos.db.client.model.StringSet) URI(java.net.URI) ComputeImage(com.emc.storageos.db.client.model.ComputeImage)

Example 4 with ComputeImage

use of com.emc.storageos.db.client.model.ComputeImage in project coprhd-controller by CoprHD.

the class ImageServerControllerImpl method preOsInstallImageServerCheck.

/**
 * Performs preOs install check on the imageServer
 * @param jobId {@link URI} job id
 * @param stepId {@link String} step id
 */
public void preOsInstallImageServerCheck(URI jobId, String stepId) {
    log.info("preOsInstallImageServerCheck {} ", jobId);
    ImageServerDialog d = null;
    try {
        WorkflowStepCompleter.stepExecuting(stepId);
        ComputeImageJob job = dbClient.queryObject(ComputeImageJob.class, jobId);
        ComputeImage img = dbClient.queryObject(ComputeImage.class, job.getComputeImageId());
        ComputeImageServer imageServer = dbClient.queryObject(ComputeImageServer.class, job.getComputeImageServerId());
        SSHSession session = new SSHSession();
        session.connect(imageServer.getImageServerIp(), imageServer.getSshPort(), imageServer.getImageServerUser(), imageServer.getImageServerPassword());
        d = new ImageServerDialog(session, imageServer.getSshTimeoutMs());
        d.init();
        log.info("connected to image server");
        log.info("verify the image is still there");
        if (!d.directoryExists(imageServer.getTftpBootDir() + img.getPathToDirectory())) {
            log.error("the image is missing");
            throw ImageServerControllerException.exceptions.computeImageIsMissing(img.getPathToDirectory());
        }
        String pid = d.getServerPid("67");
        if (pid == null) {
            // dhcp down
            throw ImageServerControllerException.exceptions.dhcpServerNotRunning();
        }
        pid = d.getServerPid("69");
        if (pid == null) {
            // tftp down
            throw ImageServerControllerException.exceptions.tftpServerNotRunning();
        }
        log.info("make sure the python server is running");
        pid = d.getServerPid(imageServer.getImageServerHttpPort());
        if (pid == null) {
            log.warn("python server is not running, attempt to start it");
            d.cd(imageServer.getTftpBootDir() + HTTP_DIR);
            d.nohup(String.format("python %s", SERVER_PY_FILE));
            pid = d.getServerPid(imageServer.getImageServerHttpPort());
            if (pid == null) {
                throw ImageServerControllerException.exceptions.httpPythonServerNotRunning();
            }
        }
        WorkflowStepCompleter.stepSucceded(stepId);
    } catch (InternalException e) {
        log.error("Exception during image server check pre os install: " + e.getMessage(), e);
        WorkflowStepCompleter.stepFailed(stepId, e);
    } catch (Exception e) {
        log.error("Unexpected exception during image server check pre os install: " + e.getMessage(), e);
        String opName = ResourceOperationTypeEnum.INSTALL_OPERATING_SYSTEM.getName();
        WorkflowStepCompleter.stepFailed(stepId, ImageServerControllerException.exceptions.unexpectedException(opName, e));
    } finally {
        try {
            if (d != null && d.isConnected()) {
                d.close();
            }
        } catch (Exception e) {
            log.error(FAILED_TO_CLOSE_STR, e);
        }
    }
}
Also used : SSHSession(com.emc.storageos.networkcontroller.SSHSession) ComputeImageServer(com.emc.storageos.db.client.model.ComputeImageServer) ComputeImageJob(com.emc.storageos.db.client.model.ComputeImageJob) InternalException(com.emc.storageos.svcs.errorhandling.resources.InternalException) DeviceControllerException(com.emc.storageos.exceptions.DeviceControllerException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) ImageServerControllerException(com.emc.storageos.imageservercontroller.exceptions.ImageServerControllerException) ComputeImage(com.emc.storageos.db.client.model.ComputeImage) InternalException(com.emc.storageos.svcs.errorhandling.resources.InternalException)

Example 5 with ComputeImage

use of com.emc.storageos.db.client.model.ComputeImage in project coprhd-controller by CoprHD.

the class ImageServerControllerImpl method getOsMetadata.

private ComputeImage getOsMetadata(ImageServerDialog d, String isoPath, String isoMountDir) throws InternalException {
    ComputeImage metadata = new ComputeImage();
    // is it ESXi 5x
    if (d.fileExists(isoMountDir + "upgrade/metadata.xml") && d.fileExists(isoMountDir + "upgrade/profile.xml")) {
        metadata = new ComputeImage();
        d.cd(isoMountDir);
        String cmd = String.format(ImageServerDialogProperties.getString("cmd.grepXmlValue"), "esxVersion", "esxVersion", "upgrade/metadata.xml");
        String esxVersion = d.execCommand(cmd);
        cmd = String.format(ImageServerDialogProperties.getString("cmd.grepXmlValue"), "build", "build", "upgrade/metadata.xml");
        String build = d.execCommand(cmd);
        metadata.setOsVersion(esxVersion);
        metadata.setOsBuild(build);
        metadata.setOsName("esxi");
        metadata.setOsArchitecture("x86_64");
        metadata.setOsUpdate(extractUpdateFromFilePath(isoPath));
        metadata.setImageType(ComputeImage.ImageType.esx.name());
        // figure out custom
        cmd = String.format(ImageServerDialogProperties.getString("cmd.grepXmlValue"), "name", "name", "upgrade/profile.xml");
        String profileName = d.execCommand(cmd);
        if (!profileName.endsWith("-standard")) {
            metadata.setCustomName(profileName.replaceAll(" ", "_"));
        }
        d.cd(TMP);
    } else {
        throw ImageServerControllerException.exceptions.unknownOperatingSystem();
    }
    return metadata;
}
Also used : ComputeImage(com.emc.storageos.db.client.model.ComputeImage)

Aggregations

ComputeImage (com.emc.storageos.db.client.model.ComputeImage)22 URI (java.net.URI)10 ComputeImageServer (com.emc.storageos.db.client.model.ComputeImageServer)8 InternalException (com.emc.storageos.svcs.errorhandling.resources.InternalException)7 Produces (javax.ws.rs.Produces)7 ComputeImageJob (com.emc.storageos.db.client.model.ComputeImageJob)6 DeviceControllerException (com.emc.storageos.exceptions.DeviceControllerException)6 ImageServerControllerException (com.emc.storageos.imageservercontroller.exceptions.ImageServerControllerException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 MalformedURLException (java.net.MalformedURLException)6 Consumes (javax.ws.rs.Consumes)5 Path (javax.ws.rs.Path)5 SSHSession (com.emc.storageos.networkcontroller.SSHSession)4 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)4 URIQueryResultList (com.emc.storageos.db.client.constraint.URIQueryResultList)3 Host (com.emc.storageos.db.client.model.Host)3 POST (javax.ws.rs.POST)3 DbClient (com.emc.storageos.db.client.DbClient)2 ComputeElement (com.emc.storageos.db.client.model.ComputeElement)2 ComputeSystem (com.emc.storageos.db.client.model.ComputeSystem)2