Ejemplo n.º 1
0
  @Override
  public String attachVolume(String instanceId, String volumeId, String deviceName) {
    IaasProvider iaasInfo = getIaasProvider();
    ComputeServiceContext context = iaasInfo.getComputeService().getContext();
    String region = ComputeServiceBuilderUtil.extractRegion(iaasInfo);
    String zone = ComputeServiceBuilderUtil.extractZone(iaasInfo);
    String device = deviceName == null ? "/dev/sdh" : deviceName;

    if (region == null || zone == null) {
      log.fatal(
          "Cannot attach the volume [id]: "
              + volumeId
              + " in the [region] : "
              + region
              + ", [zone] : "
              + zone
              + " of Iaas : "
              + iaasInfo);
      return null;
    }

    ElasticBlockStoreApi blockStoreApi =
        context.unwrapApi(AWSEC2Api.class).getElasticBlockStoreApiForRegion(region).get();
    Volume.Status volumeStatus = this.getVolumeStatus(blockStoreApi, region, volumeId);

    if (log.isDebugEnabled()) {
      log.debug("Volume " + volumeId + " is in state " + volumeStatus);
    }

    while (volumeStatus != Volume.Status.AVAILABLE) {
      try {
        // TODO Use a proper mechanism to wait till volume becomes available.
        Thread.sleep(1000);
        volumeStatus = this.getVolumeStatus(blockStoreApi, region, volumeId);
        if (log.isDebugEnabled()) {
          log.debug(
              "Volume " + volumeId + " is still NOT in AVAILABLE. Current State=" + volumeStatus);
        }
      } catch (InterruptedException e) {
        // Ignoring the exception
      }
    }
    if (log.isDebugEnabled()) {
      log.debug("Volume " + volumeId + " became  AVAILABLE");
    }

    Attachment attachment =
        blockStoreApi.attachVolumeInRegion(region, volumeId, instanceId, device);

    if (attachment == null) {
      log.fatal(
          "Volume [id]: "
              + volumeId
              + " attachment for instance [id]: "
              + instanceId
              + " was unsuccessful. [region] : "
              + region
              + ", [zone] : "
              + zone
              + " of Iaas : "
              + iaasInfo);
      return null;
    }

    log.info(
        "Volume [id]: "
            + volumeId
            + " attachment for instance [id]: "
            + instanceId
            + " was successful [status]: "
            + attachment.getStatus().value()
            + ". [region] : "
            + region
            + ", [zone] : "
            + zone
            + " of Iaas : "
            + iaasInfo);
    return attachment.getStatus().value();
  }