Exemplo n.º 1
0
    @Override
    public boolean apply(Allocation allocInfo)
        throws MetadataException, AuthException, VerificationException {
      RunInstancesType msg = allocInfo.getRequest();
      String imageId = msg.getImageId();
      VmType vmType = allocInfo.getVmType();
      try {
        BootableSet bootSet = Emis.newBootableSet(imageId);
        allocInfo.setBootableSet(bootSet);

        // Add (1024L * 1024L * 10) to handle NTFS min requirements.
        if (!bootSet.isBlockStorage()) {
          if (Platform.windows.equals(bootSet.getMachine().getPlatform())
              && bootSet.getMachine().getImageSizeBytes()
                  > ((1024L * 1024L * 1024L * vmType.getDisk()) + (1024L * 1024L * 10))) {
            throw new ImageInstanceTypeVerificationException(
                "Unable to run instance "
                    + bootSet.getMachine().getDisplayName()
                    + " in which the size "
                    + bootSet.getMachine().getImageSizeBytes()
                    + " bytes of the instance is greater than the vmType "
                    + vmType.getDisplayName()
                    + " size "
                    + vmType.getDisk()
                    + " GB.");
          } else if (bootSet.getMachine().getImageSizeBytes()
              > ((1024L * 1024L * 1024L * vmType.getDisk()))) {
            throw new ImageInstanceTypeVerificationException(
                "Unable to run instance "
                    + bootSet.getMachine().getDisplayName()
                    + " in which the size "
                    + bootSet.getMachine().getImageSizeBytes()
                    + " bytes of the instance is greater than the vmType "
                    + vmType.getDisplayName()
                    + " size "
                    + vmType.getDisk()
                    + " GB.");
          }
        }
      } catch (VerificationException e) {
        throw e;
      } catch (MetadataException ex) {
        LOG.error(ex);
        throw ex;
      } catch (RuntimeException ex) {
        LOG.error(ex);
        throw new VerificationException(
            "Failed to verify references for request: "
                + msg.toSimpleString()
                + " because of: "
                + ex.getMessage(),
            ex);
      }
      return true;
    }
Exemplo n.º 2
0
 @Override
 public boolean apply(Allocation allocInfo) throws MetadataException {
   if (allocInfo.getRequest().getKeyName() == null
       || "".equals(allocInfo.getRequest().getKeyName())) {
     allocInfo.setSshKeyPair(KeyPairs.noKey());
     return true;
   }
   UserFullName ownerFullName = allocInfo.getOwnerFullName();
   RunInstancesType request = allocInfo.getRequest();
   String keyName = request.getKeyName();
   SshKeyPair key = KeyPairs.lookup(ownerFullName.asAccountFullName(), keyName);
   if (!RestrictedTypes.filterPrivileged().apply(key)) {
     throw new IllegalMetadataAccessException(
         "Not authorized to use keypair " + keyName + " by " + ownerFullName.getUserName());
   }
   allocInfo.setSshKeyPair(key);
   return true;
 }
Exemplo n.º 3
0
 @Override
 public boolean apply(Allocation allocInfo) throws MetadataException {
   RunInstancesType request = allocInfo.getRequest();
   String zoneName = request.getAvailabilityZone();
   if (Clusters.getInstance().listValues().isEmpty()) {
     LOG.debug("enabled values: " + Joiner.on("\n").join(Clusters.getInstance().listValues()));
     LOG.debug("disabled values: " + Joiner.on("\n").join(Clusters.getInstance().listValues()));
     throw new VerificationException(
         "Not enough resources: no cluster controller is currently available to run instances.");
   } else if (Partitions.exists(zoneName)) {
     Partition partition = Partitions.lookupByName(zoneName);
     allocInfo.setPartition(partition);
   } else if (Partition.DEFAULT_NAME.equals(zoneName)) {
     Partition partition = Partition.DEFAULT;
     allocInfo.setPartition(partition);
   } else {
     throw new VerificationException(
         "Not enough resources: no cluster controller is currently available to run instances.");
   }
   return true;
 }
Exemplo n.º 4
0
    private Allocation(final RunInstancesType request) {
      this.context = Contexts.lookup();
      this.instanceIds = Maps.newHashMap();
      this.instanceUuids = Maps.newHashMap();
      this.request = request;
      this.minCount = request.getMinCount();
      this.maxCount = request.getMaxCount();
      this.usePrivateAddressing = "private".equals(request.getAddressingType());
      this.disableApiTermination =
          Optional.fromNullable(request.getDisableTerminate()).or(Boolean.FALSE);
      this.monitoring = request.getMonitoring() == null ? Boolean.FALSE : request.getMonitoring();
      this.clientToken = Strings.emptyToNull(request.getClientToken());

      this.ownerFullName = this.context.getUserFullName();
      if ((this.request.getInstanceType() == null) || "".equals(this.request.getInstanceType())) {
        this.request.setInstanceType(VmTypes.defaultTypeName());
      }

      this.reservationIndex = UniqueIds.nextIndex(VmInstance.class, (long) request.getMaxCount());
      this.reservationId = IdentityIdFormats.generate(getAuthenticatedArn(), "r");
      this.request.setMonitoring(this.monitoring);
      // GRZE:FIXME: moved all this encode/decode junk into util.UserDatas
      if (this.request.getUserData() != null) {
        try {
          this.userData = Base64.decode(this.request.getUserData());
          this.request.setUserData(new String(Base64.encode(this.userData)));
        } catch (Exception e) {
        }
      } else {
        try {
          this.request.setUserData(new String(Base64.encode(new byte[0])));
        } catch (Exception ex) {
          LOG.error(ex, ex);
        }
      }
      this.credential = null;
    }