private static VmInstance maybeFindVm(
     final String instanceId, final String publicIp, final String privateIp) {
   VmInstance vm = null;
   if (instanceId != null) {
     try {
       vm = VmInstances.lookup(instanceId);
     } catch (NoSuchElementException ex) {
       Logs.extreme().error(ex);
     }
   }
   if (vm == null && privateIp != null) {
     try {
       vm = VmInstances.lookupByPrivateIp(privateIp);
     } catch (NoSuchElementException ex) {
       Logs.extreme().error(ex);
     }
   }
   if (vm == null && publicIp != null) {
     try {
       vm = VmInstances.lookupByPublicIp(publicIp);
     } catch (NoSuchElementException ex) {
       Logs.extreme().error(ex);
     }
   }
   if (vm != null
       && VmState.RUNNING.equals(vm.getState())
       && publicIp.equals(vm.getPublicAddress())) {
     Logs.extreme()
         .debug(
             "Candidate vm which claims this address: "
                 + vm.getInstanceId()
                 + " "
                 + vm.getState()
                 + " "
                 + publicIp);
     if (publicIp.equals(vm.getPublicAddress())) {
       Logs.extreme()
           .debug(
               "Found vm which claims this address: "
                   + vm.getInstanceId()
                   + " "
                   + vm.getState()
                   + " "
                   + publicIp);
     }
     return vm;
   } else {
     return null;
   }
 }
  private void removeTags(final String imageId) throws Exception {
    final ImageInfo image = Images.lookupImage(imageId);
    final String imageOwnerId = image.getOwnerUserId();

    DeleteTagsTask task =
        new DeleteTagsTask(
            imageOwnerId,
            Lists.newArrayList(image.getDisplayName()),
            Lists.newArrayList(TAG_KEY_STATE, TAG_KEY_MESSAGE));
    CheckedListenableFuture<Boolean> result = task.dispatch();
    if (result.get()) {;
    }
    final List<VmInstance> instances = this.lookupInstances(imageId);
    for (final VmInstance instance : instances) {
      final String instanceId = instance.getInstanceId();
      final String instanceOwnerId = instance.getOwnerUserId();
      try {
        task =
            new DeleteTagsTask(
                instanceOwnerId,
                Lists.newArrayList(instanceId),
                Lists.newArrayList(TAG_KEY_STATE, TAG_KEY_MESSAGE));
        result = task.dispatch();
        if (result.get()) {;
        }
      } catch (final Exception ex) {;
      }
    }
  }
  private void tagResources(final String imageId, final String state, String statusMessage)
      throws Exception {
    final ImageInfo image = Images.lookupImage(imageId);
    final String imageOwnerId = image.getOwnerUserId();

    final List<VmInstance> instances = this.lookupInstances(imageId);
    if (tagState.containsKey(imageId) && state.equals(tagState.get(imageId))) {;
    } else {
      resetTag(imageOwnerId, imageId, TAG_KEY_STATE, state);
      tagState.put(imageId, state);
    }
    for (final VmInstance instance : instances) {
      final String instanceId = instance.getInstanceId();
      final String instanceOwnerId = instance.getOwnerUserId();
      if (tagState.containsKey(instanceId) && state.equals(tagState.get(instanceId))) {;
      } else {
        resetTag(instanceOwnerId, instanceId, TAG_KEY_STATE, state);
        tagState.put(instanceId, state);
      }
    }

    if (statusMessage == null) statusMessage = "";

    if (tagMessage.containsKey(imageId) && statusMessage.equals(tagMessage.get(imageId))) {;
    } else {
      resetTag(imageOwnerId, imageId, TAG_KEY_MESSAGE, statusMessage);
      tagMessage.put(imageId, statusMessage);
    }
    for (final VmInstance instance : instances) {
      final String instanceId = instance.getInstanceId();
      final String instanceOwnerId = instance.getOwnerUserId();
      if (tagMessage.containsKey(instanceId) && statusMessage.equals(tagMessage.get(instanceId))) {;
      } else {
        resetTag(instanceOwnerId, instanceId, TAG_KEY_MESSAGE, statusMessage);
        tagMessage.put(instanceId, statusMessage);
      }
    }
  }
 @Override
 public void fireException(Throwable e) {
   LOG.debug(e);
   Logs.extreme().error(e, e);
   LOG.debug(
       "Trying to remove invalid volume attachment "
           + this.getRequest().getVolumeId()
           + " from instance "
           + this.getRequest().getInstanceId());
   try {
     VmInstance vm = VmInstances.lookup(this.getRequest().getInstanceId());
     Partition partition = vm.lookupPartition();
     ServiceConfiguration sc = Topology.lookup(Storage.class, partition);
     /** clean up SC session state * */
     try {
       LOG.debug(
           "Sending detach after async failure in attach volume: "
               + this.getRequest().getVolumeId()
               + " sc="
               + sc);
       AsyncRequests.sendSync(sc, new DetachStorageVolumeType(this.getRequest().getVolumeId()));
     } catch (Exception ex) {
       LOG.error(ex);
       Logs.extreme().error(ex, ex);
     }
     /** clean up internal attachment state * */
     final Function<String, VmInstance> removeVolAttachment =
         new Function<String, VmInstance>() {
           public VmInstance apply(final String input) {
             VmInstance vm = VmInstances.lookup(input);
             vm.removeVolumeAttachment(VolumeAttachCallback.this.getRequest().getVolumeId());
             return vm;
           }
         };
     Entities.asTransaction(VmInstance.class, removeVolAttachment)
         .apply(this.getRequest().getInstanceId());
     LOG.debug(
         "Removed failed attachment: "
             + this.getRequest().getVolumeId()
             + " -> "
             + vm.getInstanceId());
   } catch (Exception e1) {
     LOG.error(e1);
     Logs.extreme().error(e1, e1);
   }
 }
 protected static Address lookupOrCreate(
     final Cluster cluster, final ClusterAddressInfo addrInfo) {
   Address addr = null;
   VmInstance vm = null;
   try {
     addr = Addresses.getInstance().lookupDisabled(addrInfo.getAddress());
     LOG.trace("Found address in the inactive set cache: " + addr);
   } catch (final NoSuchElementException e1) {
     try {
       addr = Addresses.getInstance().lookup(addrInfo.getAddress());
       LOG.trace("Found address in the active set cache: " + addr);
     } catch (final NoSuchElementException e) {
     }
   }
   if (addrInfo.hasMapping()) {
     vm =
         Helper.maybeFindVm(
             addr != null ? addr.getInstanceId() : null,
             addrInfo.getAddress(),
             addrInfo.getInstanceIp());
     if ((addr != null) && (vm != null)) {
       Helper.ensureAllocated(addr, vm);
       clearOrphan(addrInfo);
     } else if (addr != null && !addr.isPending() && vm != null && VmStateSet.DONE.apply(vm)) {
       handleOrphan(cluster, addrInfo);
     } else if ((addr != null && addr.isAssigned() && !addr.isPending()) && (vm == null)) {
       handleOrphan(cluster, addrInfo);
     } else if ((addr == null) && (vm != null)) {
       addr =
           new Address(
               Principals.systemFullName(),
               addrInfo.getAddress(),
               vm.getInstanceUuid(),
               vm.getInstanceId(),
               vm.getPrivateAddress());
       clearOrphan(addrInfo);
     } else if ((addr == null) && (vm == null)) {
       addr = new Address(addrInfo.getAddress(), cluster.getPartition());
       handleOrphan(cluster, addrInfo);
     }
   } else {
     if ((addr != null) && addr.isAssigned() && !addr.isPending()) {
       handleOrphan(cluster, addrInfo);
     } else if ((addr != null)
         && !addr.isAssigned()
         && !addr.isPending()
         && addr.isSystemOwned()) {
       try {
         addr.release();
       } catch (final Exception ex) {
         LOG.error(ex);
       }
     } else if ((addr != null) && Address.Transition.system.equals(addr.getTransition())) {
       handleOrphan(cluster, addrInfo);
     } else if (addr == null) {
       addr = new Address(addrInfo.getAddress(), cluster.getPartition());
       Helper.clearVmState(addrInfo);
     }
   }
   return addr;
 }
Beispiel #6
0
  public DetachVolumeResponseType detach(DetachVolumeType request) throws EucalyptusCloudException {
    DetachVolumeResponseType reply = (DetachVolumeResponseType) request.getReply();
    Context ctx = Contexts.lookup();

    Volume vol;
    try {
      vol = Volumes.lookup(ctx.getUserFullName().asAccountFullName(), request.getVolumeId());
    } catch (Exception ex1) {
      throw new EucalyptusCloudException("Volume does not exist: " + request.getVolumeId());
    }
    if (!RestrictedTypes.filterPrivileged().apply(vol)) {
      throw new EucalyptusCloudException(
          "Not authorized to detach volume "
              + request.getVolumeId()
              + " by "
              + ctx.getUser().getName());
    }

    VmInstance vm = null;
    AttachedVolume volume = null;
    try {
      VmVolumeAttachment vmVolAttach = VmInstances.lookupVolumeAttachment(request.getVolumeId());
      volume = VmVolumeAttachment.asAttachedVolume(vmVolAttach.getVmInstance()).apply(vmVolAttach);
      vm = vmVolAttach.getVmInstance();
    } catch (NoSuchElementException ex) {
      /** no such attachment * */
    }
    if (volume == null) {
      throw new EucalyptusCloudException("Volume is not attached: " + request.getVolumeId());
    }
    if (!RestrictedTypes.filterPrivileged().apply(vm)) {
      throw new EucalyptusCloudException(
          "Not authorized to detach volume from instance "
              + request.getInstanceId()
              + " by "
              + ctx.getUser().getName());
    }
    if (!vm.getInstanceId().equals(request.getInstanceId())
        && request.getInstanceId() != null
        && !request.getInstanceId().equals("")) {
      throw new EucalyptusCloudException(
          "Volume is not attached to instance: " + request.getInstanceId());
    }
    if (request.getDevice() != null
        && !request.getDevice().equals("")
        && !volume.getDevice().equals(request.getDevice())) {
      throw new EucalyptusCloudException(
          "Volume is not attached to device: " + request.getDevice());
    }

    Cluster cluster = null;
    ServiceConfiguration ccConfig = null;
    try {
      ccConfig = Topology.lookup(ClusterController.class, vm.lookupPartition());
      cluster = Clusters.lookup(ccConfig);
    } catch (NoSuchElementException e) {
      LOG.debug(e, e);
      throw new EucalyptusCloudException(
          "Cluster does not exist in partition: " + vm.getPartition());
    }
    ServiceConfiguration scVm;
    try {
      scVm = Topology.lookup(Storage.class, vm.lookupPartition());
    } catch (Exception ex) {
      LOG.error(ex, ex);
      throw new EucalyptusCloudException(
          "Failed to lookup SC for partition: " + vm.getPartition(), ex);
    }
    request.setVolumeId(volume.getVolumeId());
    request.setRemoteDevice(volume.getRemoteDevice());
    request.setDevice(volume.getDevice().replaceAll("unknown,requested:", ""));
    request.setInstanceId(vm.getInstanceId());
    VolumeDetachCallback ncDetach = new VolumeDetachCallback(request);
    try {
      AsyncRequests.sendSync(scVm, new DetachStorageVolumeType(volume.getVolumeId()));
    } catch (Exception e) {
      LOG.debug(e);
      Logs.extreme().debug(e, e);
      // GRZE: attach is idempotent, failure here is ok.      throw new EucalyptusCloudException(
      // e.getMessage( ) );
    }
    AsyncRequests.newRequest(ncDetach).dispatch(cluster.getConfiguration());
    EventRecord.here(VolumeManager.class, EventClass.VOLUME, EventType.VOLUME_DETACH)
        .withDetails(vm.getOwner().toString(), volume.getVolumeId(), "instance", vm.getInstanceId())
        .withDetails("cluster", ccConfig.getFullName().toString())
        .info();
    volume.setStatus("detaching");
    reply.setDetachedVolume(volume);
    return reply;
  }
Beispiel #7
0
  public AttachVolumeResponseType AttachVolume(AttachVolumeType request)
      throws EucalyptusCloudException {
    AttachVolumeResponseType reply = (AttachVolumeResponseType) request.getReply();
    final String deviceName = request.getDevice();
    final String volumeId = request.getVolumeId();
    final Context ctx = Contexts.lookup();

    if (request.getDevice() == null
        || request.getDevice().endsWith("sda")
        || request.getDevice().endsWith("sdb")) {
      throw new EucalyptusCloudException("Invalid device name specified: " + request.getDevice());
    }
    VmInstance vm = null;
    try {
      vm = RestrictedTypes.doPrivileged(request.getInstanceId(), VmInstance.class);
    } catch (NoSuchElementException ex) {
      LOG.debug(ex, ex);
      throw new EucalyptusCloudException("Instance does not exist: " + request.getInstanceId(), ex);
    } catch (Exception ex) {
      LOG.debug(ex, ex);
      throw new EucalyptusCloudException(ex.getMessage(), ex);
    }
    AccountFullName ownerFullName = ctx.getUserFullName().asAccountFullName();
    Volume volume = Volumes.lookup(ownerFullName, volumeId);
    if (!RestrictedTypes.filterPrivileged().apply(volume)) {
      throw new EucalyptusCloudException(
          "Not authorized to attach volume "
              + request.getVolumeId()
              + " by "
              + ctx.getUser().getName());
    }
    try {
      vm.lookupVolumeAttachmentByDevice(deviceName);
      throw new EucalyptusCloudException(
          "Already have a device attached to: " + request.getDevice());
    } catch (NoSuchElementException ex1) {
      /** no attachment * */
    }
    try {
      VmInstances.lookupVolumeAttachment(volumeId);
      throw new EucalyptusCloudException("Volume already attached: " + request.getVolumeId());
    } catch (NoSuchElementException ex1) {
      /** no attachment * */
    }

    Partition volPartition = Partitions.lookupByName(volume.getPartition());
    ServiceConfiguration sc = Topology.lookup(Storage.class, volPartition);
    ServiceConfiguration scVm = Topology.lookup(Storage.class, vm.lookupPartition());
    if (!sc.equals(scVm)) {
      throw new EucalyptusCloudException(
          "Can only attach volumes in the same zone: " + request.getVolumeId());
    }
    ServiceConfiguration ccConfig = Topology.lookup(ClusterController.class, vm.lookupPartition());
    AttachStorageVolumeResponseType scAttachResponse;
    try {
      AttachStorageVolumeType req =
          new AttachStorageVolumeType(Nodes.lookupIqn(vm), volume.getDisplayName());
      scAttachResponse = AsyncRequests.sendSync(sc, req);
    } catch (Exception e) {
      LOG.debug(e, e);
      throw new EucalyptusCloudException(e.getMessage(), e);
    }
    request.setRemoteDevice(scAttachResponse.getRemoteDeviceString());

    AttachedVolume attachVol =
        new AttachedVolume(
            volume.getDisplayName(),
            vm.getInstanceId(),
            request.getDevice(),
            request.getRemoteDevice());
    vm.addTransientVolume(deviceName, scAttachResponse.getRemoteDeviceString(), volume);
    AsyncRequests.newRequest(new VolumeAttachCallback(request)).dispatch(ccConfig);

    EventRecord.here(VolumeManager.class, EventClass.VOLUME, EventType.VOLUME_ATTACH)
        .withDetails(
            volume.getOwner().toString(), volume.getDisplayName(), "instance", vm.getInstanceId())
        .withDetails("partition", vm.getPartition().toString())
        .info();
    reply.setAttachedVolume(attachVol);
    return reply;
  }