package com.twitter.heron.scheduler.aurora;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import com.twitter.heron.spi.common.ShellUtils;

/**
 * This file defines Utils methods used by Aurora
 */
public class AuroraUtils {
  private static final Logger LOG = Logger.getLogger(AuroraUtils.class.getName());

  // Create an aurora job
  public static boolean createAuroraJob(String jobName, String cluster, String role, String env,
                                        String auroraFilename, Map<String, String> bindings,
                                        boolean isVerbose) {
    List<String> auroraCmd = Arrays.asList("aurora", "job", "create", "--wait-until", "RUNNING");

    for (Map.Entry<String, String> binding : bindings.entrySet()) {
      auroraCmd.add("--bind");
      auroraCmd.add(String.format("%s=%s", binding.getKey(), binding.getValue()));
    }

    String jobSpec = String.format("%s/%s/%s/%s", cluster, role, env, jobName);
    auroraCmd.add(jobSpec);
    auroraCmd.add(auroraFilename);

    if (isVerbose) {
      auroraCmd.add("--verbose");
    }

    return 0 == ShellUtils.runProcess(
        isVerbose, (String[]) auroraCmd.toArray(), new StringBuilder(), new StringBuilder());
  }

  // Kill an aurora job
  public static boolean killAuroraJob(String jobName, String cluster, String role, String env,
                                      boolean isVerbose) {
    List<String> auroraCmd = Arrays.asList("aurora", "job", "killall");
    String jobSpec = String.format("%s/%s/%s/%s", cluster, role, env, jobName);
    auroraCmd.add(jobSpec);

    appendAuroraCommandOptions(auroraCmd, isVerbose);

    return 0 == ShellUtils.runProcess(
        isVerbose, (String[]) auroraCmd.toArray(), new StringBuilder(), new StringBuilder());
  }

  // Restart an aurora job
  public static boolean restartAuroraJob(String jobName, String cluster, String role, String env,
                                         int containerId, boolean isVerbose) {
    List<String> auroraCmd = Arrays.asList("aurora", "job", "restart");
    String jobSpec = String.format("%s/%s/%s/%s", cluster, role, env, jobName);
    if (containerId != -1) {
      jobSpec = String.format("%s/%s", jobSpec, "" + containerId);
    }
    auroraCmd.add(jobSpec);

    appendAuroraCommandOptions(auroraCmd, isVerbose);

    return 0 == ShellUtils.runProcess(
        isVerbose, (String[]) auroraCmd.toArray(), new StringBuilder(), new StringBuilder());
  }

  // Static method to append verbose and batching options if needed
  public static void appendAuroraCommandOptions(List<String> auroraCmd,
                                                boolean isVerbose) {
    if (isVerbose) {
      auroraCmd.add("--verbose");
    }
    auroraCmd.add("--no-batching");

    return;
  }
}

@@ -38,16 +38,93 @@
 urora;%0A%0A
+import com.twitter.heron.spi.common.ShellUtils;%0A%0Aimport java.util.ArrayList;%0A
 import j
@@ -140,16 +140,16 @@
 Arrays;%0A
-
 import j
@@ -223,57 +223,8 @@
 r;%0A%0A
-import com.twitter.heron.spi.common.ShellUtils;%0A%0A
 /**%0A
@@ -686,32 +686,48 @@
 ng%3E auroraCmd = 
+new ArrayList%3C%3E(
 Arrays.asList(%22a
@@ -777,16 +777,17 @@
 UNNING%22)
+)
 ;%0A%0A    f
@@ -1229,35 +1229,24 @@
   isVerbose,
- (String%5B%5D)
  auroraCmd.t
@@ -1244,32 +1244,45 @@
 roraCmd.toArray(
+new String%5B0%5D
 ), new StringBui
@@ -1515,32 +1515,48 @@
 ng%3E auroraCmd = 
+new ArrayList%3C%3E(
 Arrays.asList(%22a
@@ -1580,16 +1580,17 @@
 illall%22)
+)
 ;%0A    St
@@ -1800,35 +1800,24 @@
   isVerbose,
- (String%5B%5D)
  auroraCmd.t
@@ -1815,32 +1815,45 @@
 roraCmd.toArray(
+new String%5B0%5D
 ), new StringBui
@@ -2120,16 +2120,32 @@
 raCmd = 
+new ArrayList%3C%3E(
 Arrays.a
@@ -2177,16 +2177,17 @@
 estart%22)
+)
 ;%0A    St
@@ -2507,19 +2507,8 @@
 ose,
- (String%5B%5D)
  aur
@@ -2522,16 +2522,29 @@
 toArray(
+new String%5B0%5D
 ), new S

Fix UnsupportedOperationException in AuroraUtils