Example #1
0
  /**
   * Loads a new application.
   *
   * @param application
   * @return the managed application that was created
   * @throws AlreadyExistingException if the application already exists
   * @throws InvalidApplicationException if the application contains critical errors
   * @throws IOException if the messaging could;not be initialized
   */
  public ManagedApplication loadNewApplication(File applicationFilesDirectory)
      throws AlreadyExistingException, InvalidApplicationException, IOException {

    LoadResult lr = RuntimeModelIo.loadApplication(applicationFilesDirectory);
    if (RoboconfErrorHelpers.containsCriticalErrors(lr.getLoadErrors()))
      throw new InvalidApplicationException(lr.getLoadErrors());

    Application application = lr.getApplication();
    if (null != findApplicationByName(application.getName()))
      throw new AlreadyExistingException(application.getName());

    final IMessageServerClient client = this.messagingClientFactory.create();
    client.setApplicationName(application.getName());
    client.setMessageServerIp(this.messageServerIp);
    client.setSourceName(MessagingUtils.SOURCE_DM);
    client.openConnection(new DmMessageProcessor(application));
    client.bind(MessagingUtils.buildRoutingKeyToDm());

    ManagedApplication ma = new ManagedApplication(application, applicationFilesDirectory, client);
    this.appNameToManagedApplication.put(application.getName(), ma);
    ma.getLogger()
        .fine("Application " + application.getName() + " was successfully loaded and added.");

    return ma;
  }
Example #2
0
  private void deploy(ManagedApplication ma, List<Instance> instances) throws BulkActionException {

    BulkActionException bulkException = new BulkActionException(true);
    for (Instance instance : instances) {
      if (instance.getParent() == null) {
        try {
          IaasInterface iaasInterface = this.iaasResolver.findIaasInterface(ma, instance);
          String machineId =
              iaasInterface.createVM(
                  null, this.messageServerIp, instance.getName(), ma.getApplication().getName());

          // FIXME: the channel name is skipped here
          // As soon as we know what it is useful for, re-add it (it is in the instance)
          instance.getData().put(Instance.MACHINE_ID, machineId);

        } catch (IaasException e) {
          instance.setStatus(InstanceStatus.PROBLEM);
          bulkException.getInstancesToException().put(instance, e);

        } catch (CommunicationToIaasException e) {
          instance.setStatus(InstanceStatus.PROBLEM);
          bulkException.getInstancesToException().put(instance, e);
        }

      } else {
        try {
          // FIXME: we may have to add the instance on the agent too, just like for root instances
          Map<String, byte[]> instanceResources =
              ResourceUtils.storeInstanceResources(ma.getApplicationFilesDirectory(), instance);
          MsgCmdInstanceDeploy message =
              new MsgCmdInstanceDeploy(
                  InstanceHelpers.computeInstancePath(instance), instanceResources);
          ma.getMessagingClient()
              .publish(false, MessagingUtils.buildRoutingKeyToAgent(instance), message);

        } catch (IOException e) {
          // The instance does not have any problem, just keep trace of the exception
          bulkException.getInstancesToException().put(instance, e);
        }
      }
    }

    if (!bulkException.getInstancesToException().isEmpty()) {
      ma.getLogger().severe(bulkException.getLogMessage(false));
      ma.getLogger().finest(bulkException.getLogMessage(true));
      throw bulkException;
    }
  }
Example #3
0
  private void remove(ManagedApplication ma, List<Instance> instances)
      throws UnauthorizedActionException, BulkActionException {

    for (Instance instance : instances) {
      if (instance.getStatus() != InstanceStatus.NOT_DEPLOYED)
        throw new UnauthorizedActionException(
            "Instances are still deployed or running. They cannot be removed.");
    }

    BulkActionException bulkException = new BulkActionException(false);
    for (Instance instance : instances) {

      if (instance.getParent() != null) {
        try {
          MsgCmdInstanceRemove message =
              new MsgCmdInstanceRemove(InstanceHelpers.computeInstancePath(instance));
          ma.getMessagingClient()
              .publish(false, MessagingUtils.buildRoutingKeyToAgent(instance), message);

          // The instance will be removed once the agent has indicated it was removed.
          // See DmMessageProcessor.

        } catch (IOException e) {
          // The instance does not have any problem, just keep trace of the exception
          bulkException.getInstancesToException().put(instance, e);
        }

      } else {
        ma.getApplication().getRootInstances().remove(instance);
      }
    }

    if (!bulkException.getInstancesToException().isEmpty()) {
      ma.getLogger().severe(bulkException.getLogMessage(false));
      ma.getLogger().finest(bulkException.getLogMessage(true));
      throw bulkException;
    }
  }
Example #4
0
  private void undeploy(ManagedApplication ma, List<Instance> instances)
      throws BulkActionException {

    BulkActionException bulkException = new BulkActionException(false);
    for (Instance instance : instances) {
      try {
        MsgCmdInstanceUndeploy message =
            new MsgCmdInstanceUndeploy(InstanceHelpers.computeInstancePath(instance));
        ma.getMessagingClient()
            .publish(false, MessagingUtils.buildRoutingKeyToAgent(instance), message);

      } catch (IOException e) {
        // The instance does not have any problem, just keep trace of the exception
        bulkException.getInstancesToException().put(instance, e);
      }
    }

    if (!bulkException.getInstancesToException().isEmpty()) {
      ma.getLogger().severe(bulkException.getLogMessage(false));
      ma.getLogger().finest(bulkException.getLogMessage(true));
      throw bulkException;
    }
  }