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