// Path: agent/src/main/java/org/apache/hms/agent/Agent.java
// public class Agent {
//   private static Log log = LogFactory.getLog(Agent.class);
//   
//   private static Agent instance = null;
//   private Server server = null;
//   private static URL serverConf = null;
// 
//   public static Agent getInstance() {
//     if(instance==null) {
//       instance = new Agent();
//     }
//     return instance;
//   }
// 
//   public void start() {
//     try {
//       System.out.close();
//       System.err.close();
//       instance = this;
//       run();
//     } catch(Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//       System.exit(-1);
//     }
//   }
// 
//   public void run() {
//     server = new Server(4080);
// 
//     XmlConfiguration configuration;
//     try {
//       Context root = new Context(server, "/", Context.SESSIONS);
//       ServletHolder sh = new ServletHolder(ServletContainer.class);
//       sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
//       sh.setInitParameter("com.sun.jersey.config.property.packages", "org.apache.hms.agent.rest");
//       root.addServlet(sh, "/*");
//       server.setStopAtShutdown(true);
//       server.start();
//     } catch (Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//     }
//   }
// 
//   public void stop() throws Exception {
//     try {
//       server.stop();
//     } catch (Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//     }
//   }
// 
//   public static void main(String[] args) {
//     Agent agent = Agent.getInstance();
//     agent.start();
//   }
// 
// }
// 
// Path: common/src/main/java/org/apache/hms/common/util/FileUtil.java
// public class FileUtil {
//   public static boolean deleteDir(File dir) {
//     if (dir.isDirectory()) {
//         String[] children = dir.list();
//         for (int i=0; i<children.length; i++) {
//             boolean success = deleteDir(new File(dir, children[i]));
//             if (!success) {
//                 return false;
//             }
//         }
//     }
//     return dir.delete();
//   }
// }


import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hms.agent.Agent;
import org.apache.hms.common.entity.PackageCommand;
import org.apache.hms.common.entity.PackageInfo;
import org.apache.hms.common.entity.ScriptCommand;
import org.apache.hms.common.entity.agent.DaemonAction;
import org.apache.hms.common.rest.Response;
import org.apache.hms.common.util.FileUtil;
    return r;    
  }
  
  private Response dryRun(PackageCommand dc) {
    Response r = null;
    ScriptCommand cmd = new ScriptCommand();
    cmd.setCmd(dc.getCmd());
    cmd.setScript("yum");
    PackageInfo[] packages = dc.getPackages();
    String[] parms = new String[packages.length+4];
    parms[0] = "install";
    parms[1] = "-y";
    parms[2] = "--downloadonly";
    parms[3] = "--downloaddir=/tmp/system_update";
    for(int i=0;i<packages.length;i++) {
      parms[i+4] = packages[i].getName();
    }
    cmd.setParms(parms);
    ShellRunner shell = new ShellRunner();
    r = shell.run(cmd);
    if(r.getCode()!=1) {
      return r;
    } else {
      cmd.setScript("rpm");
      String[] rpmParms = new String[3];
      rpmParms[0] = "-i";
      rpmParms[1] = "--test";
      rpmParms[2] = "/tmp/system_update/*.rpm";
      cmd.setParms(rpmParms);
      r = shell.run(cmd);
// Path: agent/src/main/java/org/apache/hms/agent/Agent.java
// public class Agent {
//   private static Log log = LogFactory.getLog(Agent.class);
//   
//   private static Agent instance = null;
//   private Server server = null;
//   private static URL serverConf = null;
// 
//   public static Agent getInstance() {
//     if(instance==null) {
//       instance = new Agent();
//     }
//     return instance;
//   }
// 
//   public void start() {
//     try {
//       System.out.close();
//       System.err.close();
//       instance = this;
//       run();
//     } catch(Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//       System.exit(-1);
//     }
//   }
// 
//   public void run() {
//     server = new Server(4080);
// 
//     XmlConfiguration configuration;
//     try {
//       Context root = new Context(server, "/", Context.SESSIONS);
//       ServletHolder sh = new ServletHolder(ServletContainer.class);
//       sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
//       sh.setInitParameter("com.sun.jersey.config.property.packages", "org.apache.hms.agent.rest");
//       root.addServlet(sh, "/*");
//       server.setStopAtShutdown(true);
//       server.start();
//     } catch (Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//     }
//   }
// 
//   public void stop() throws Exception {
//     try {
//       server.stop();
//     } catch (Exception e) {
//       log.error(ExceptionUtil.getStackTrace(e));
//     }
//   }
// 
//   public static void main(String[] args) {
//     Agent agent = Agent.getInstance();
//     agent.start();
//   }
// 
// }
// 
// Path: common/src/main/java/org/apache/hms/common/util/FileUtil.java
// public class FileUtil {
//   public static boolean deleteDir(File dir) {
//     if (dir.isDirectory()) {
//         String[] children = dir.list();
//         for (int i=0; i<children.length; i++) {
//             boolean success = deleteDir(new File(dir, children[i]));
//             if (!success) {
//                 return false;
//             }
//         }
//     }
//     return dir.delete();
//   }
// }


// Path: agent/src/main/java/org/apache/hms/agent/dispatcher/PackageRunner.java
import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hms.agent.Agent;
import org.apache.hms.common.entity.PackageCommand;
import org.apache.hms.common.entity.PackageInfo;
import org.apache.hms.common.entity.ScriptCommand;
import org.apache.hms.common.entity.agent.DaemonAction;
import org.apache.hms.common.rest.Response;
import org.apache.hms.common.util.FileUtil;

    return r;    
  }
  
  private Response dryRun(PackageCommand dc) {
    Response r = null;
    ScriptCommand cmd = new ScriptCommand();
    cmd.setCmd(dc.getCmd());
    cmd.setScript("yum");
    PackageInfo[] packages = dc.getPackages();
    String[] parms = new String[packages.length+4];
    parms[0] = "install";
    parms[1] = "-y";
    parms[2] = "--downloadonly";
    parms[3] = "--downloaddir=/tmp/system_update";
    for(int i=0;i<packages.length;i++) {
      parms[i+4] = packages[i].getName();
    }
    cmd.setParms(parms);
    ShellRunner shell = new ShellRunner();
    r = shell.run(cmd);
    if(r.getCode()!=1) {
      return r;
    } else {
      cmd.setScript("rpm");
      String[] rpmParms = new String[3];
      rpmParms[0] = "-i";
      rpmParms[1] = "--test";
      rpmParms[2] = "/tmp/system_update/*.rpm";
      cmd.setParms(rpmParms);
      r = shell.run(cmd);
      FileUtil.deleteDir(new File("/tmp/system_update"));