Search in sources :

Example 6 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 7 with ComputeImage

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

the class OsInstallCompleter method complete.

@Override
protected void complete(DbClient dbClient, Status status, ServiceCoded coded) throws DeviceControllerException {
    log.info("OsInstallCompleter.complete {} {}", status.name(), coded);
    Host host = dbClient.queryObject(Host.class, getId());
    ComputeImageJob job = dbClient.queryObject(ComputeImageJob.class, jobId);
    AuditLogManager auditMgr = AuditLogManagerFactory.getAuditLogManager();
    if (status == Status.ready && job.getJobStatus().equals(JobStatus.SUCCESS.name())) {
        // set host type based on image type
        ComputeImage image = dbClient.queryObject(ComputeImage.class, job.getComputeImageId());
        if (image.getImageType().equals(ComputeImage.ImageType.esx.name())) {
            host.setType(Host.HostType.Esx.name());
            host.setOsVersion(image.getOsVersion());
        } else if (image.getImageType().equals(ComputeImage.ImageType.linux.name())) {
            host.setType(Host.HostType.Linux.name());
            host.setOsVersion(String.format("%s %s", image.getOsName(), image.getOsVersion()));
        }
        /*
             * Create the IpInterface that represents the IpAddress that's
             * supposed to come on the ESX Management Network (for ESX
             * installations)
             */
        IpInterface ipInterface = new IpInterface();
        ipInterface.setHost(host.getId());
        ipInterface.setId(URIUtil.createId(IpInterface.class));
        ipInterface.setIpAddress(job.getHostIp());
        ipInterface.setLabel(job.getHostName());
        ipInterface.setNetmask(job.getNetmask());
        ipInterface.setProtocol(HostInterface.Protocol.IPV4.toString());
        ipInterface.setRegistrationStatus(DiscoveredDataObject.RegistrationStatus.REGISTERED.toString());
        dbClient.createObject(ipInterface);
        /*
             * End create IpInterface. Consider making this a seperate method.
             */
        host.setCompatibilityStatus(CompatibilityStatus.COMPATIBLE.name());
        host.setProvisioningStatus(ProvisioningJobStatus.COMPLETE.toString());
        dbClient.persistObject(host);
        dbClient.ready(Host.class, getId(), getOpId());
        auditMgr.recordAuditLog(null, null, serviceType, OperationTypeEnum.INSTALL_COMPUTE_IMAGE, System.currentTimeMillis(), AuditLogManager.AUDITLOG_SUCCESS, AuditLogManager.AUDITOP_END, host.getId(), job.getId());
    } else {
        host.setProvisioningStatus(ProvisioningJobStatus.ERROR.toString());
        dbClient.persistObject(host);
        job.setJobStatus(JobStatus.FAILED.name());
        dbClient.persistObject(job);
        dbClient.error(Host.class, getId(), getOpId(), coded);
        auditMgr.recordAuditLog(null, null, serviceType, OperationTypeEnum.INSTALL_COMPUTE_IMAGE, System.currentTimeMillis(), AuditLogManager.AUDITLOG_FAILURE, AuditLogManager.AUDITOP_END, host.getId(), job.getId());
    }
}
Also used : IpInterface(com.emc.storageos.db.client.model.IpInterface) AuditLogManager(com.emc.storageos.security.audit.AuditLogManager) Host(com.emc.storageos.db.client.model.Host) ComputeImageJob(com.emc.storageos.db.client.model.ComputeImageJob) ComputeImage(com.emc.storageos.db.client.model.ComputeImage)

Example 8 with ComputeImage

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

the class ComputeImageService method updateComputeImage.

/**
 * Updates an already present compute image.
 *
 * @param id
 *            compute image URN.
 * @param param
 *            The ComputeImageUpdate object with attributes to be updated.
 * @brief Update compute image details
 * @return Update task REST representation.
 */
@PUT
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("/{id}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@CheckPermission(roles = { Role.SYSTEM_ADMIN, Role.RESTRICTED_SYSTEM_ADMIN })
public TaskResourceRep updateComputeImage(@PathParam("id") URI id, ComputeImageUpdate param) {
    log.info("updateComputeImage: {}, new name: {}", id, param.getName());
    ArgValidator.checkFieldUriType(id, ComputeImage.class, "id");
    ArgValidator.checkFieldNotEmpty(param.getName(), "name");
    ComputeImage ci = _dbClient.queryObject(ComputeImage.class, id);
    ArgValidator.checkEntity(ci, id, isIdEmbeddedInURL(id));
    if (!ci.getLabel().equals(param.getName())) {
        checkDuplicateLabel(ComputeImage.class, param.getName());
        ci.setLabel(param.getName());
    }
    boolean reImport = false;
    // see if image URL needs updating
    if (!StringUtils.isBlank(param.getImageUrl()) && !param.getImageUrl().equals(ci.getImageUrl())) {
        ArgValidator.checkUrl(param.getImageUrl(), "image_url");
        // URL can only be update if image not successfully loaded
        if (ci.getComputeImageStatus().equals(ComputeImageStatus.NOT_AVAILABLE.name())) {
            String prevImageUrl = ci.getImageUrl();
            boolean isEncrypted = false;
            String oldPassword = ImageServerControllerImpl.extractPasswordFromImageUrl(prevImageUrl);
            String newPassword = ImageServerControllerImpl.extractPasswordFromImageUrl(param.getImageUrl());
            if (StringUtils.isNotBlank(oldPassword) && StringUtils.isNotBlank(newPassword)) {
                // password present in the DB.
                if (ImageServerControllerImpl.MASKED_PASSWORD.equals(newPassword)) {
                    isEncrypted = true;
                }
            }
            // and the constant will be same and we do not update the password but update other parts if changed.
            if (isEncrypted) {
                ci.setImageUrl(StringUtils.replace(param.getImageUrl(), ":" + newPassword + "@", ":" + oldPassword + "@"));
            } else {
                ci.setImageUrl(encryptImageURLPassword(param.getImageUrl(), isEncrypted));
            }
            ci.setComputeImageStatus(ComputeImageStatus.IN_PROGRESS.name());
            reImport = true;
        } else {
            throw APIException.badRequests.invalidParameterCannotUpdateComputeImageUrl();
        }
    }
    _dbClient.updateObject(ci);
    auditOp(OperationTypeEnum.UPDATE_COMPUTE_IMAGE, true, null, ci.getId().toString(), ci.getImageUrl());
    return createUpdateTasks(ci, reImport);
}
Also used : ComputeImage(com.emc.storageos.db.client.model.ComputeImage) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 9 with ComputeImage

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

the class ComputeImageService method createComputeImage.

/**
 * Create compute image from image URL or existing installable image URN.
 *
 * @param param
 *            The ComputeImageCreate object contains all the parameters for
 *            creation.
 * @brief Create compute image
 * @return Creation task REST representation.
 */
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@CheckPermission(roles = { Role.SYSTEM_ADMIN, Role.RESTRICTED_SYSTEM_ADMIN })
public TaskResourceRep createComputeImage(ComputeImageCreate param) {
    log.info("createComputeImage");
    // unique name required
    ArgValidator.checkFieldNotEmpty(param.getName(), "name");
    checkDuplicateLabel(ComputeImage.class, param.getName());
    ArgValidator.checkFieldNotEmpty(param.getImageUrl(), "image_url");
    ArgValidator.checkUrl(param.getImageUrl(), "image_url");
    if (!checkForImageServers()) {
        throw APIException.badRequests.cannotAddImageWithoutImageServer();
    }
    ComputeImage ci = new ComputeImage();
    ci.setId(URIUtil.createId(ComputeImage.class));
    // IN_PROGRESS until successfully loaded by image server controller
    ci.setComputeImageStatus(ComputeImageStatus.IN_PROGRESS.name());
    ci.setLabel(param.getName());
    ci.setImageUrl(encryptImageURLPassword(param.getImageUrl(), false));
    _dbClient.createObject(ci);
    auditOp(OperationTypeEnum.CREATE_COMPUTE_IMAGE, true, AuditLogManager.AUDITOP_BEGIN, ci.getId().toString(), ci.getImageUrl(), ci.getComputeImageStatus());
    try {
        return doImportImage(ci);
    } catch (Exception e) {
        ci.setComputeImageStatus(ComputeImageStatus.NOT_AVAILABLE.name());
        _dbClient.updateObject(ci);
        throw e;
    }
}
Also used : APIException(com.emc.storageos.svcs.errorhandling.resources.APIException) ComputeImage(com.emc.storageos.db.client.model.ComputeImage) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) CheckPermission(com.emc.storageos.security.authorization.CheckPermission)

Example 10 with ComputeImage

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

the class ComputeImageService method getComputeImages.

/**
 * Returns a list of all compute images.
 *
 * @brief Show compute images
 * @return List of all compute images.
 */
@GET
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public ComputeImageList getComputeImages(@QueryParam("imageType") String imageType) {
    log.info("getComputeImages, imageType: {}", imageType);
    // validate query param
    if (imageType != null) {
        ArgValidator.checkFieldValueFromEnum(imageType, "imageType", ComputeImage.ImageType.class);
    }
    List<URI> ids = _dbClient.queryByType(ComputeImage.class, true);
    ComputeImageList list = new ComputeImageList();
    Iterator<ComputeImage> iter = _dbClient.queryIterativeObjects(ComputeImage.class, ids);
    while (iter.hasNext()) {
        ComputeImage img = iter.next();
        if (imageType == null || imageType.equals(img.getImageType())) {
            list.getComputeImages().add(DbObjectMapper.toNamedRelatedResource(img));
        }
    }
    return list;
}
Also used : ComputeImageList(com.emc.storageos.model.compute.ComputeImageList) URI(java.net.URI) ComputeImage(com.emc.storageos.db.client.model.ComputeImage) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

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