]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.agent/src/main/java/org/argeo/slc/cli/SlcMain.java
Integrate Slc Execution notification in the new runtime
[gpl/argeo-slc.git] / org.argeo.slc.agent / src / main / java / org / argeo / slc / cli / SlcMain.java
1 package org.argeo.slc.cli;
2
3 import java.io.File;
4 import java.util.Properties;
5
6 import org.apache.commons.cli.CommandLine;
7 import org.apache.commons.cli.CommandLineParser;
8 import org.apache.commons.cli.GnuParser;
9 import org.apache.commons.cli.HelpFormatter;
10 import org.apache.commons.cli.Option;
11 import org.apache.commons.cli.OptionBuilder;
12 import org.apache.commons.cli.Options;
13 import org.apache.commons.cli.ParseException;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16 import org.argeo.slc.core.SlcException;
17 import org.argeo.slc.logging.Log4jUtils;
18 import org.springframework.core.io.DefaultResourceLoader;
19 import org.springframework.core.io.FileSystemResource;
20 import org.springframework.core.io.Resource;
21
22 public class SlcMain {
23 public enum Mode {
24 single, agent
25 }
26
27 private static Log log = null;
28
29 private final static String BOOTSTRAP_LOG4J_CONFIG = "org/argeo/slc/cli/bootstrapLog4j.properties";
30
31 private final static Option modeOpt = OptionBuilder.withLongOpt("mode")
32 .withArgName("mode").hasArg().isRequired().withDescription(
33 "SLC execution mode, one of: " + listModeValues()).create(
34 'm');
35
36 private final static Option propertyOpt = OptionBuilder.withLongOpt(
37 "property").withArgName("prop1=val1,prop2=val2").hasArgs()
38 .withValueSeparator(',').withDescription(
39 "use value for given property").create('p');
40
41 private final static Option scriptOpt = OptionBuilder.withLongOpt("script")
42 .withArgName("script").hasArg().withDescription(
43 "SLC script to execute").create('s');
44
45 private final static Option targetsOpt = OptionBuilder.withLongOpt(
46 "targets").withArgName("targets").hasArg().withDescription(
47 "Targets to execute").create('t');
48
49 private final static Option runtimeOpt = OptionBuilder.withLongOpt(
50 "runtime").withArgName("runtime").hasArg().withDescription(
51 "Runtime to use, either a full path or relative to slc app conf dir: "
52 + "<conf dir>/runtime/<runtime>/.xml").create('r');
53
54 private final static Options options;
55
56 private final static String commandName = "slc";
57
58 static {
59 options = new Options();
60 options.addOption(modeOpt);
61 options.addOption(scriptOpt);
62 options.addOption(targetsOpt);
63 options.addOption(propertyOpt);
64 options.addOption(runtimeOpt);
65 }
66
67 public static void main(String[] args) {
68 Mode mode = null;
69 Properties properties = new Properties();
70 String script = null;
71 String targets = null;
72 String runtimeStr = null;
73
74 try {
75
76 CommandLineParser clParser = new GnuParser();
77 CommandLine cl = clParser.parse(options, args);
78
79 // Mode
80 String modeStr = cl.getOptionValue(modeOpt.getOpt());
81 try {
82 mode = Mode.valueOf(modeStr);
83 } catch (IllegalArgumentException e) {
84 throw new SlcException("Unrecognized mode '" + modeStr + "'", e);
85 }
86
87 // Script
88 if (mode.equals(Mode.single)) {
89 if (!cl.hasOption(scriptOpt.getOpt()))
90 throw new SlcException("Mode " + Mode.single
91 + " requires option '" + scriptOpt.getLongOpt()
92 + "'");
93 script = cl.getOptionValue(scriptOpt.getOpt());
94
95 // Targets
96 if (cl.hasOption(targetsOpt.getOpt()))
97 targets = cl.getOptionValue(targetsOpt.getOpt());
98 }
99
100 // Properties
101 if (cl.hasOption(propertyOpt.getOpt())) {
102 for (String property : cl.getOptionValues(propertyOpt.getOpt())) {
103 addProperty(properties, property);
104 }
105 }
106
107 // Runtime
108 if (cl.hasOption(runtimeOpt.getOpt())) {
109 runtimeStr = cl.getOptionValue(runtimeOpt.getOpt());
110 } else {
111 runtimeStr = "default";
112 }
113
114 } catch (ParseException e) {
115 System.err.println("Problem with command line arguments. "
116 + e.getMessage());
117 badExit();
118 } catch (SlcException e) {
119 System.err.println(e.getMessage());
120 badExit();
121 } catch (Exception e) {
122 System.err.println("Unexpected exception when bootstrapping.");
123 e.printStackTrace();
124 badExit();
125 }
126
127 // Initializes logging and log arguments
128 initLogging(properties);
129 if (log.isDebugEnabled()) {
130 log.debug("Mode: " + mode);
131 log.debug("Runtime: " + runtimeStr);
132 log.debug("User properties: " + properties);
133 if (script != null)
134 log.debug("Script: " + script);
135 if (targets != null)
136 log.debug("Targets: " + targets);
137 }
138
139 // Execution
140 if (mode.equals(Mode.single)) {
141 Resource scriptRes;
142 if (new File(script).exists()) {
143 scriptRes = new FileSystemResource(script);
144 } else {
145 scriptRes = new DefaultResourceLoader(SlcMain.class
146 .getClassLoader()).getResource(script);
147 }
148
149 DefaultSlcRuntime runtime = new DefaultSlcRuntime();
150 runtime.executeScript(runtimeStr, scriptRes, targets, properties,
151 null);
152 }
153 }
154
155 public static void printUsage() {
156 new HelpFormatter().printHelp(commandName, options, true);
157 }
158
159 private static String listModeValues() {
160 StringBuffer buf = new StringBuffer("");
161 for (Mode mode : Mode.values()) {
162 buf.append(mode).append(", ");
163 }
164 String str = buf.toString();
165 // unsafe, but there will be at least one value in the enum
166 return str.substring(0, str.length() - 2);
167 }
168
169 private static void addProperty(Properties properties, String property) {
170 int eqIndex = property.indexOf('=');
171 if (eqIndex == 0)
172 throw new SlcException("Badly formatted property " + property);
173
174 if (eqIndex > 0) {
175 String key = property.substring(0, eqIndex);
176 String value = property.substring(eqIndex + 1);
177 properties.setProperty(key, value);
178
179 } else {
180 properties.setProperty(property, "true");
181 }
182
183 }
184
185 private static void initLogging(Properties userProperties) {
186 System.setProperty("log4j.defaultInitOverride", "true");
187
188 // Add log4j user properties to System properties
189 for (String key : userProperties.stringPropertyNames()) {
190 if (key.startsWith("log4j.")) {
191 System.setProperty(key, userProperties.getProperty(key));
192 }
193 }
194 Log4jUtils.initLog4j(System.getProperty("log4j.configuration",
195 "classpath:" + BOOTSTRAP_LOG4J_CONFIG));
196 log = LogFactory.getLog(SlcMain.class);
197
198 }
199
200 private static void badExit() {
201 printUsage();
202 System.exit(1);
203 }
204 }