예제 #1
0
  @Override
  public Statement apply(String group) {
    checkNotNull(group, "group");
    String validatorClientName = validatorName.get();
    PrivateKey validatorKey = validatorCredential.get();

    JsonBall bootstrapConfig = null;
    try {
      bootstrapConfig = bootstrapConfigForGroup.load(group);
    } catch (Exception e) {
      throw propagate(e);
    }

    Map<String, JsonBall> config =
        json.fromJson(bootstrapConfig.toString(), BootstrapConfigForGroup.BOOTSTRAP_CONFIG_TYPE);
    Optional<JsonBall> environment = Optional.fromNullable(config.get("environment"));

    String chefConfigDir = "{root}etc{fs}chef";
    Statement createChefConfigDir = exec("{md} " + chefConfigDir);
    Statement createClientRb =
        appendFile(
            chefConfigDir + "{fs}client.rb",
            ImmutableList.of(
                "require 'rubygems'",
                "require 'ohai'",
                "o = Ohai::System.new",
                "o.all_plugins",
                String.format("node_name \"%s-\" + o[:ipaddress]", group),
                "log_level :info",
                "log_location STDOUT",
                String.format("validation_client_name \"%s\"", validatorClientName),
                String.format("chef_server_url \"%s\"", endpoint.get())));

    Statement createValidationPem =
        appendFile(
            chefConfigDir + "{fs}validation.pem",
            Splitter.on(newLinePattern).split(Pems.pem(validatorKey)));

    String chefBootFile = chefConfigDir + "{fs}first-boot.json";
    Statement createFirstBoot =
        appendFile(chefBootFile, Collections.singleton(json.toJson(bootstrapConfig)));

    ImmutableMap.Builder<String, String> options = ImmutableMap.builder();
    options.put("-j", chefBootFile);
    if (environment.isPresent()) {
      options.put("-E", environment.get().toString());
    }
    String strOptions = Joiner.on(' ').withKeyValueSeparator(" ").join(options.build());
    Statement runChef = exec("chef-client " + strOptions);

    return newStatementList(
        new ExitInsteadOfReturn(installChef),
        createChefConfigDir,
        createClientRb,
        createValidationPem,
        createFirstBoot,
        runChef);
  }
예제 #2
0
 @Override
 public String apply(JsonBall input) {
   String value = input.toString();
   if (value.length() >= 2
       && value.charAt(0) == '"'
       && value.charAt(input.length() - 1) == '"')
     return value.substring(1, input.length() - 1);
   return value;
 }
예제 #3
0
  protected Object parseResponse(String requestUrl, JsonBall response) {
    if (response == null) {
      return null;
    } else if (requestUrl.contains("import")) {
      Type type = new TypeLiteral<Map<String, Set<ParseDomain.RawDomain>>>() {}.getType();
      Map<String, Set<RawDomain>> domainMap = json.fromJson(response.toString(), type);
      Domain domain = Iterators.getOnlyElement(domainMap.get("domains").iterator()).getDomain();

      return domain;
    } else if (requestUrl.contains("export")) {
      Type type = new TypeLiteral<Map<String, String>>() {}.getType();
      Map<String, String> exportMap = json.fromJson(response.toString(), type);
      String contents = exportMap.get("contents");
      List<String> contentsAsList =
          Lists.newArrayList(Splitter.on("\n").omitEmptyStrings().split(contents));

      return contentsAsList;
    } else if (response.toString().contains("domains")) {
      Type type = new TypeLiteral<Map<String, Set<RawDomain>>>() {}.getType();
      Map<String, Set<RawDomain>> domainMap = json.fromJson(response.toString(), type);
      Set<Domain> domains =
          FluentIterable.from(domainMap.get("domains")).transform(toDomain).toSet();

      return domains;
    } else if (response.toString().contains("records")) {
      Type type = new TypeLiteral<Map<String, Set<RawRecord>>>() {}.getType();
      Map<String, Set<RawRecord>> recordMap = json.fromJson(response.toString(), type);
      Set<RecordDetail> records =
          FluentIterable.from(recordMap.get("records")).transform(toRecordDetails).toSet();

      if (isCreateSingleRecord) {
        return Iterables.getOnlyElement(records);
      } else {
        return records;
      }
    } else {
      throw new IllegalStateException(
          "Job parsing problem. Did not recognize any type in job response.\n"
              + response.toString());
    }
  }