/**
   * loads the provisioning driver class and sets it up.
   *
   * @throws CLIException Indicates the configured could not be found and instantiated
   */
  private void createProvisioningDriver() throws CLIException {
    try {
      provisioning =
          (ProvisioningDriver) Class.forName(cloud.getConfiguration().getClassName()).newInstance();
    } catch (final ClassNotFoundException e) {
      throw new CLIException(
          "Failed to load provisioning class for cloud: "
              + cloud.getName()
              + ". Class not found: "
              + cloud.getConfiguration().getClassName(),
          e);
    } catch (final Exception e) {
      throw new CLIException("Failed to load provisioning class for cloud: " + cloud.getName(), e);
    }
    if (provisioning instanceof ProvisioningDriverClassContextAware) {
      final ProvisioningDriverClassContextAware contextAware =
          (ProvisioningDriverClassContextAware) provisioning;
      contextAware.setProvisioningDriverClassContext(new DefaultProvisioningDriverClassContext());
    }

    provisioning.addListener(new CliProvisioningDriverListener());
    final String serviceName = null;
    provisioning.setConfig(
        cloud, cloud.getConfiguration().getManagementMachineTemplate(), true, serviceName);
  }
Пример #2
0
  private boolean isNotHAManagementSpace() throws IOException, DSLException {
    if (cloudFileName != null && !cloudFileName.trim().isEmpty()) {
      File cloudFile = new File(cloudFileName);

      final Cloud cloud = ServiceReader.readCloud(cloudFile);
      if (cloud != null) {
        if (cloud.getProvider() != null) {
          final int numberOfManagementMachines =
              cloud.getProvider().getNumberOfManagementMachines();
          return numberOfManagementMachines < 2;
        }
      }
    }
    return true;
  }
  private InstallationDetails[] createInstallationDetails(
      final int numOfManagementMachines,
      final MachineDetails[] machineDetails,
      final ComputeTemplate template,
      final String securityProfile,
      final String keystorePassword)
      throws FileNotFoundException {
    final InstallationDetails[] details = new InstallationDetails[numOfManagementMachines];

    final GSAReservationId reservationId = null;
    final String managementAuthGroups = null;

    for (int i = 0; i < details.length; i++) {
      final ExactZonesConfig zones =
          new ExactZonesConfigurer().addZone(MANAGEMENT_GSA_ZONE).create();
      details[i] =
          Utils.createInstallationDetails(
              machineDetails[i],
              cloud,
              template,
              zones,
              null,
              null,
              true,
              this.cloudFile,
              reservationId,
              cloud.getConfiguration().getManagementMachineTemplate(),
              securityProfile,
              keystorePassword,
              managementAuthGroups);
    }

    return details;
  }
  private MachineDetails[] locateManagementMachines() throws CLIStatusException {
    if (provisioning instanceof ManagementLocator) {
      final ManagementLocator locator = (ManagementLocator) provisioning;
      MachineDetails[] mds;
      try {
        mds = locator.getExistingManagementServers();
      } catch (final CloudProvisioningException e) {
        throw new CLIStatusException(
            e, CloudifyErrorMessages.MANAGEMENT_SERVERS_FAILED_TO_READ.getName(), e.getMessage());
      }
      if (mds.length == 0) {
        throw new CLIStatusException(
            CloudifyErrorMessages.MANAGEMENT_SERVERS_NOT_LOCATED.getName());
      }
      if (mds.length != this.cloud.getProvider().getNumberOfManagementMachines()) {
        throw new CLIStatusException(
            CloudifyErrorMessages.MANAGEMENT_SERVERS_NUMBER_NOT_MATCH.getName(),
            cloud.getProvider().getNumberOfManagementMachines(),
            mds.length);
      }

      return mds;
    } else {
      throw new CLIStatusException(
          CloudifyErrorMessages.MANAGEMENT_LOCATOR_NOT_SUPPORTED.getName(), this.cloud.getName());
    }
  }
  private String createLocatorsString(final InstallationDetails[] installations) {

    final Integer port = cloud.getConfiguration().getComponents().getDiscovery().getDiscoveryPort();
    final StringBuilder lookupSb = new StringBuilder();
    for (final InstallationDetails detail : installations) {
      final String ip =
          cloud.getConfiguration().isConnectToPrivateIp()
              ? detail.getPrivateIp()
              : detail.getPublicIp();

      lookupSb.append(ip).append(":").append(port).append(',');
    }

    lookupSb.setLength(lookupSb.length() - 1);

    return lookupSb.toString();
  }
  private MachineDetails[] startManagememntProcesses(
      final MachineDetails[] machines,
      final String securityProfile,
      final String keystorePassword,
      final long endTime)
      throws InterruptedException, TimeoutException, InstallerException, IOException {

    final AgentlessInstaller installer = new AgentlessInstaller();
    installer.addListener(new CliAgentlessInstallerListener(this.verbose));

    // Update the logging level of jsch used by the AgentlessInstaller
    Logger.getLogger(AgentlessInstaller.SSH_LOGGER_NAME)
        .setLevel(Level.parse(cloud.getProvider().getSshLoggingLevel()));

    final ComputeTemplate template =
        cloud
            .getCloudCompute()
            .getTemplates()
            .get(cloud.getConfiguration().getManagementMachineTemplate());

    // fixConfigRelativePaths(cloud, template);

    final int numOfManagementMachines = machines.length;

    final InstallationDetails[] installations =
        createInstallationDetails(
            numOfManagementMachines, machines, template, securityProfile, keystorePassword);
    // only one machine should try and deploy the WebUI and Rest Admin unless
    // noWebServices is true
    int i = isNoWebServices() ? 0 : 1;
    for (; i < installations.length; i++) {
      installations[i].setNoWebServices(true);
    }

    final String lookup = createLocatorsString(installations);
    for (final InstallationDetails detail : installations) {
      detail.setLocator(lookup);
    }

    // executes the agentless installer on all of the machines,
    // asynchronously
    installOnMachines(endTime, installer, numOfManagementMachines, installations);

    return machines;
  }
  private void waitForManagementWebServices(
      final boolean isSecureConnection,
      final String username,
      final String password,
      final Integer restPort,
      final Integer webuiPort,
      final long end,
      final MachineDetails[] servers)
      throws MalformedURLException, URISyntaxException, InterruptedException, TimeoutException,
          CLIException {
    // Wait for rest to become available
    // When the rest gateway is up and running, the cloud is ready to go
    for (final MachineDetails server : servers) {
      String ipAddress = null;
      if (cloud.getConfiguration().isBootstrapManagementOnPublicIp()) {
        ipAddress = server.getPublicAddress();
      } else {
        ipAddress = server.getPrivateAddress();
      }

      final URL restAdminUrl =
          new URI(
                  ShellUtils.getRestProtocol(isSecureConnection),
                  null,
                  ipAddress,
                  restPort,
                  null,
                  null,
                  null)
              .toURL();
      final URL webUIUrl =
          new URI(
                  ShellUtils.getRestProtocol(isSecureConnection),
                  null,
                  ipAddress,
                  webuiPort,
                  null,
                  null,
                  null)
              .toURL();

      // We are relying on start-management command to be run on the
      // new machine, so everything should be up if the rest admin is up
      waitForConnection(
          username,
          password,
          restAdminUrl,
          isSecureConnection,
          CalcUtils.millisUntil(end),
          TimeUnit.MILLISECONDS);

      logger.info("Rest service is available at: " + restAdminUrl + '.');
      logger.info("Webui service is available at: " + webUIUrl + '.');
    }
  }
  private MachineDetails[] locateManagementMachinesFromFile() throws CLIStatusException {
    if (provisioning instanceof ManagementLocator) {
      ObjectMapper mapper = new ObjectMapper();
      ControllerDetails[] controllers = null;
      try {
        controllers =
            mapper.readValue(
                this.existingManagersFile, TypeFactory.arrayType(ControllerDetails.class));
      } catch (IOException e) {
        throw new IllegalArgumentException(
            "Failed to read managers file: "
                + this.existingManagersFile.getAbsolutePath()
                + ". Error was: "
                + e.getMessage(),
            e);
      }
      final ManagementLocator locator = (ManagementLocator) provisioning;
      MachineDetails[] mds;
      try {
        mds = locator.getExistingManagementServers(controllers);
      } catch (final CloudProvisioningException e) {
        throw new CLIStatusException(
            e, CloudifyErrorMessages.MANAGEMENT_SERVERS_FAILED_TO_READ.getName(), e.getMessage());
      }
      if (mds.length == 0) {
        throw new CLIStatusException(
            CloudifyErrorMessages.MANAGEMENT_SERVERS_NOT_LOCATED.getName());
      }
      if (mds.length != this.cloud.getProvider().getNumberOfManagementMachines()) {
        throw new CLIStatusException(
            CloudifyErrorMessages.MANAGEMENT_SERVERS_NUMBER_NOT_MATCH.getName(),
            cloud.getProvider().getNumberOfManagementMachines(),
            mds.length);
      }

      return mds;
    } else {
      throw new CLIStatusException(
          CloudifyErrorMessages.MANAGEMENT_LOCATOR_NOT_SUPPORTED.getName(), this.cloud.getName());
    }
  }
Пример #9
0
  @Test
  public void testCloudWithOverridesFile()
      throws IllegalAccessException, InvocationTargetException, NoSuchMethodException,
          DSLException {

    Cloud cloud = ServiceReader.readCloudFromDirectory(EC2_CLOUD_WITH_FILE_PATH);
    // overriden props
    Assert.assertEquals("OverridesTestUser", cloud.getUser().getUser());
    Assert.assertEquals("OverridesTestApiKey", cloud.getUser().getApiKey());
    Assert.assertEquals(
        "OverridesTestKeyPair",
        (String)
            cloud.getCloudCompute().getTemplates().get("SMALL_LINUX").getOptions().get("keyPair"));
    Assert.assertEquals(
        "OverridesTestImageId",
        cloud.getCloudCompute().getTemplates().get("SMALL_LINUX").getImageId());

    // not overrides, taken from .properties file
    Assert.assertEquals(
        "TestKeyFile.pem", cloud.getCloudCompute().getTemplates().get("SMALL_LINUX").getKeyFile());
  }
Пример #10
0
  @Test
  public void testCloudWithOverridesScript() throws DSLException, IOException {

    Cloud cloud =
        ServiceReader.readCloudFromDirectory(
            EC2_CLOUD_WITH_SCRIPT_PATH,
            FileUtils.readFileToString(new File(EC2_CLOUD_WITH_FILE_OVERRIDES_PATH)));
    // overriden props
    Assert.assertEquals("OverridesTestUser", cloud.getUser().getUser());
    Assert.assertEquals("OverridesTestApiKey", cloud.getUser().getApiKey());
    Assert.assertEquals(
        "OverridesTestKeyPair",
        (String)
            cloud.getCloudCompute().getTemplates().get("SMALL_LINUX").getOptions().get("keyPair"));
    Assert.assertEquals(
        "OverridesTestImageId",
        cloud.getCloudCompute().getTemplates().get("SMALL_LINUX").getImageId());

    // not overrides, taken from .properties file
    Assert.assertEquals(
        "TestKeyFile.pem", cloud.getCloudCompute().getTemplates().get("SMALL_LINUX").getKeyFile());
  }
  /**
   * Bootstraps and waits until the management machines are running, or until the timeout is
   * reached.
   *
   * @param securityProfile set security profile (nonsecure/secure/ssl)
   * @param username The username for a secure connection to the server
   * @param password The password for a secure connection to the server
   * @param keystorePassword The password to the keystore to set on the rest server
   * @param timeout The number of {@link TimeUnit}s to wait before timing out
   * @param timeoutUnit The time unit to use (seconds, minutes etc.)
   * @throws InstallerException Indicates the provisioning driver failed to start management
   *     machines or that the management processes failed to start
   * @throws CLIException Indicates a basic failure or a time out. a detailed message is included
   * @throws InterruptedException Indicates a thread was interrupted while waiting
   */
  public void bootstrapCloudAndWait(
      final String securityProfile,
      final String username,
      final String password,
      final String keystorePassword,
      final long timeout,
      final TimeUnit timeoutUnit)
      throws InstallerException, CLIException, InterruptedException {

    final long end = System.currentTimeMillis() + timeoutUnit.toMillis(timeout);

    createProvisioningDriver();

    // Start the cloud machines!!!
    MachineDetails[] servers;
    try {
      servers = provisioning.startManagementMachines(timeout, timeoutUnit);
    } catch (final CloudProvisioningException e) {
      final CLIStatusException cliStatusException =
          new CLIStatusException(
              e, CloudifyErrorMessages.CLOUD_API_ERROR.getName(), e.getMessage());
      throw cliStatusException;
    } catch (final TimeoutException e) {
      throw new CLIException(
          "Cloudify bootstrap on provider "
              + this.cloud.getProvider().getProvider()
              + " timed-out. "
              + "Please try to run again using the –timeout option.",
          e);
    }

    // from this point on - close machines if an exception is thrown (to
    // avoid leaks).
    try {

      // log details in FINE
      if (logger.isLoggable(Level.FINE)) {
        for (final MachineDetails server : servers) {
          logServerDetails(server);
        }
      }

      validateServers(servers);

      // Start the management agents and other processes
      if (servers[0].isAgentRunning()) {
        // must be using existing machines.
        throw new IllegalStateException(
            "Cloud bootstrapper found existing management machines with the same name. "
                + "Please shut them down before continuing");
      }

      startManagememntProcesses(servers, securityProfile, keystorePassword, end);

      if (!isNoWebServices()) {
        final Integer restPort =
            getRestPort(
                cloud.getConfiguration().getComponents().getRest().getPort(),
                ShellUtils.isSecureConnection(securityProfile));
        final Integer webuiPort =
            getWebuiPort(
                cloud.getConfiguration().getComponents().getWebui().getPort(),
                ShellUtils.isSecureConnection(securityProfile));
        waitForManagementWebServices(
            ShellUtils.isSecureConnection(securityProfile),
            username,
            password,
            restPort,
            webuiPort,
            end,
            servers);
      }

    } catch (final IOException e) {
      stopManagementMachines();
      throw new CLIException(
          "Cloudify bootstrap on provider "
              + this.cloud.getProvider().getProvider()
              + " failed. Reason: "
              + e.getMessage(),
          e);
    } catch (final URISyntaxException e) {
      stopManagementMachines();
      throw new CLIException("Bootstrap-cloud failed. Reason: " + e.getMessage(), e);
    } catch (final TimeoutException e) {
      stopManagementMachines();
      throw new CLIException(
          "Cloudify bootstrap on provider "
              + this.cloud.getProvider().getProvider()
              + " timed-out. "
              + "Please try to run again using the –timeout option.",
          e);
    } catch (final CLIException e) {
      stopManagementMachines();
      throw e;
    } catch (final InstallerException e) {
      stopManagementMachines();
      throw e;
    } catch (final InterruptedException e) {
      stopManagementMachines();
      throw e;
    }
  }