Esempio n. 1
0
  @Override
  protected Object doExecute() throws Exception {
    MQBrokerConfigDTO dto = createDTO();

    Profile profile = MQManager.createOrUpdateProfile(dto, fabricService);
    String profileId = profile.getId();

    System.out.println("MQ profile " + profileId + " ready");

    // assign profile to existing containers
    if (assign != null) {
      String[] assignContainers = assign.split(",");
      MQManager.assignProfileToContainers(fabricService, profile, assignContainers);
    }

    // create containers
    if (create != null) {
      String[] createContainers = create.split(",");
      List<CreateContainerBasicOptions.Builder> builderList =
          MQManager.createContainerBuilders(
              dto, fabricService, "child", profileId, dto.version(), createContainers);
      for (CreateContainerBasicOptions.Builder builder : builderList) {
        CreateContainerMetadata[] metadatas = null;
        try {
          if (builder instanceof CreateChildContainerOptions.Builder) {
            CreateChildContainerOptions.Builder childBuilder =
                (CreateChildContainerOptions.Builder) builder;
            builder = childBuilder.jmxUser(username).jmxPassword(password);
          }
          metadatas = fabricService.createContainers(builder.build());
          ShellUtils.storeFabricCredentials(session, username, password);
        } catch (FabricAuthenticationException fae) {
          // If authentication fails, prompts for credentials and try again.
          if (builder instanceof CreateChildContainerOptions.Builder) {
            CreateChildContainerOptions.Builder childBuilder =
                (CreateChildContainerOptions.Builder) builder;
            promptForJmxCredentialsIfNeeded();
            metadatas =
                fabricService.createContainers(
                    childBuilder.jmxUser(username).jmxPassword(password).build());
            ShellUtils.storeFabricCredentials(session, username, password);
          }
        }
      }
    }
    return null;
  }
Esempio n. 2
0
  @Override
  public CreateContainerMetadata[] createContainers(
      CreateContainerOptions options, CreationStateListener listener) {
    assertValid();
    try {
      final ContainerProvider provider = getProvider(options.getProviderType());
      if (provider == null) {
        throw new FabricException(
            "Unable to find a container provider supporting '" + options.getProviderType() + "'");
      }

      String originalName = options.getName();
      if (originalName == null || originalName.length() == 0) {
        throw new FabricException("A name must be specified when creating containers");
      }

      if (listener == null) {
        listener = new NullCreationStateListener();
      }

      ObjectMapper mapper = new ObjectMapper();
      mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
      Map optionsMap = mapper.readValue(mapper.writeValueAsString(options), Map.class);
      String versionId =
          options.getVersion() != null ? options.getVersion() : getDataStore().getDefaultVersion();
      Set<String> profileIds = options.getProfiles();
      if (profileIds == null || profileIds.isEmpty()) {
        profileIds = new LinkedHashSet<String>();
        profileIds.add("default");
      }
      optionsMap.put("version", versionId);
      optionsMap.put("profiles", profileIds);
      optionsMap.put("number", 0);

      final List<CreateContainerMetadata> metadatas =
          new CopyOnWriteArrayList<CreateContainerMetadata>();
      int orgNumber = options.getNumber();
      int number = Math.max(orgNumber, 1);
      final CountDownLatch latch = new CountDownLatch(number);
      for (int i = 1; i <= number; i++) {
        String containerName;
        if (orgNumber >= 1) {
          containerName = originalName + i;
        } else {
          containerName = originalName;
        }
        optionsMap.put("name", containerName);

        // Check if datastore configuration has been specified and fallback to current container
        // settings.
        if (!hasValidDataStoreProperties(optionsMap)) {
          optionsMap.put("dataStoreProperties", getDataStore().getDataStoreProperties());
        }
        Class cl =
            options
                .getClass()
                .getClassLoader()
                .loadClass(options.getClass().getName() + "$Builder");
        CreateContainerBasicOptions.Builder builder =
            (CreateContainerBasicOptions.Builder)
                mapper.readValue(mapper.writeValueAsString(optionsMap), cl);
        // We always want to pass the obfuscated version of the password to the container provider.
        builder =
            (CreateContainerBasicOptions.Builder)
                builder.zookeeperPassword(PasswordEncoder.encode(getZookeeperPassword()));
        final CreateContainerOptions containerOptions = builder.build();
        final CreationStateListener containerListener = listener;
        new Thread("Creating container " + containerName) {
          public void run() {
            try {
              getDataStore().createContainerConfig(containerOptions);
              CreateContainerMetadata metadata =
                  provider.create(containerOptions, containerListener);
              if (metadata.isSuccess()) {
                Container parent =
                    containerOptions.getParent() != null
                        ? getContainer(containerOptions.getParent())
                        : null;
                // An ensemble server can be created without an existing ensemble.
                // In this case container config will be created by the newly created container.
                // TODO: We need to make sure that this entries are somehow added even to ensemble
                // servers.
                if (!containerOptions.isEnsembleServer()) {
                  getDataStore().createContainerConfig(metadata);
                }
                ContainerImpl container =
                    new ContainerImpl(parent, metadata.getContainerName(), FabricServiceImpl.this);
                metadata.setContainer(container);
                LOGGER.info(
                    "The container "
                        + metadata.getContainerName()
                        + " has been successfully created");
              } else {
                LOGGER.info(
                    "The creation of the container " + metadata.getContainerName() + " has failed",
                    metadata.getFailure());
              }
              metadatas.add(metadata);
            } catch (Throwable t) {
              CreateContainerBasicMetadata metadata = new CreateContainerBasicMetadata();
              metadata.setCreateOptions(containerOptions);
              metadata.setFailure(t);
              metadatas.add(metadata);
              getDataStore().deleteContainer(containerOptions.getName());
            } finally {
              latch.countDown();
            }
          }
        }.start();
      }
      if (!latch.await(15, TimeUnit.MINUTES)) {
        throw new FabricException("Timeout waiting for container creation");
      }
      return metadatas.toArray(new CreateContainerMetadata[metadatas.size()]);
    } catch (Exception e) {
      LOGGER.error("Failed to create containers " + e, e);
      throw FabricException.launderThrowable(e);
    }
  }