Example #1
0
  @Override
  public List<ShellCommand> getCommands() throws IOException {

    Set<JsonElement> paramsToMerge = DagParams.getGlobalParams();
    if (paramsToMerge != null) {
      try {
        // Merge in Global return results into the json file.
        JsonElement obj = new JsonParser().parse(json);
        if (obj.isJsonObject()) {
          JsonObject jsonObj = obj.getAsJsonObject();

          for (JsonElement param : paramsToMerge) {
            if (param.isJsonObject()) {
              JsonObject paramObj = param.getAsJsonObject();
              merge(jsonObj, paramObj);
            }
          }
          GsonBuilder builder = new GsonBuilder();
          builder.disableHtmlEscaping();
          Gson gson = builder.setPrettyPrinting().create();
          json = gson.toJson(jsonObj);
        } else {
          logger.warn(String.format("Invalid json object for chef-solo: \n %s'", json));
        }
      } catch (JsonIOException | JsonSyntaxException ex) {
        logger.warn(
            String.format("Invalid return value as Json object: %s \n %s'", ex.toString(), json));
      }
    }

    if (commands == null) {
      String jsonFileName = recipeCanonicalName.replaceAll(Settings.COOOKBOOK_DELIMITER, "__");
      commands =
          ShellCommandBuilder.fileScript2Commands(
              Settings.SCRIPT_PATH_RUN_RECIPE,
              "chef_json",
              json,
              "json_file_name",
              jsonFileName,
              "log_file_name",
              jsonFileName,
              "sudo_command",
              ClusterService.getInstance().getCommonContext().getSudoCommand());
    }
    return commands;
  }
Example #2
0
 /**
  * It then parses the JSON object and updates a central location for Chef Attributes.
  *
  * @param sshMachine
  * @throws se.kth.karamel.common.exception.KaramelException
  */
 @Override
 public void collectResults(MachineInterface sshMachine) throws KaramelException {
   String remoteFile = Settings.RECIPE_RESULT_REMOTE_PATH(getRecipeCanonicalName());
   String localResultsFile =
       Settings.RECIPE_RESULT_LOCAL_PATH(
           getRecipeCanonicalName(),
           getMachine().getGroup().getCluster().getName(),
           getMachine().getPublicIp());
   try {
     sshMachine.downloadRemoteFile(remoteFile, localResultsFile, true);
   } catch (IOException ex) {
     logger.debug(
         String.format(
             "No return values for %s on %s",
             getRecipeCanonicalName(), getMachine().getPublicIp()));
     return;
   }
   JsonReader reader;
   try {
     reader = new JsonReader(new FileReader(localResultsFile));
   } catch (FileNotFoundException ex) {
     String msg =
         String.format(
             "Cannot find the results file for %s on %s",
             getRecipeCanonicalName(), getMachine().getPublicIp());
     throw new KaramelException(msg, ex);
   }
   JsonParser jsonParser = new JsonParser();
   try {
     JsonElement el = jsonParser.parse(reader);
     DagParams.setGlobalParams(el);
   } catch (JsonIOException | JsonSyntaxException ex) {
     throw new KaramelException(
         String.format(
             "Invalid return value as Json object for %s on %s",
             getRecipeCanonicalName(), getMachine().getPublicIp()),
         ex);
   }
 }