1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.generator.code;
25
26 import java.io.*;
27 import javax.swing.JTextArea;
28 import zeus.generator.GeneratorModel;
29 import zeus.util.SystemProps;
30
31 /***
32 * ScriptWriter is responsible for generating the files that are used to
33 * invoke a zeus agency from a command prompt
34 * This class was altered somewhat for v1.2.2 so that zsh is supported.
35 */
36 public class ScriptWriter extends Writer {
37
38 protected String language = SystemProps.getProperty("agent.language");
39 protected int ZSHUNIX = 2;
40 protected int ZSHWINDOWS = 3;
41
42
43 protected static final String[][] ScriptName = {
44 { "run1", "run3", "run2" },
45 { "run1.bat", "run3.bat", "run2.bat" },
46 { "all.scr","runAll"},
47 { "all.scr","runAll.bat" }
48 };
49
50
51 protected static final String[][] Preamble = {
52 { "#!/usr/bin/csh\n#This script runs the root servers and should be run first",
53 "#!/usr/bin/csh\n#This script runs the non-root servers and other utility agents",
54 "#!/usr/bin/csh\n#This script runs the task agents"
55 },
56 { "REM This script runs the root servers and should be started first",
57 "REM This script runs the non-root servers and other utility agents",
58 "REM This script runs the task agents"
59 }
60 ,
61 {""}
62
63 };
64
65 public ScriptWriter(GenerationPlan genplan, GeneratorModel genmodel,
66 String directory, JTextArea textArea) {
67 super(genplan,genmodel,directory,textArea);
68 }
69
70 public void write() {
71 String platform = genplan.getPlatform();
72 int mode = UNIX;
73
74 if ( platform.equals(GenerationPlan.UNIX) )
75 mode = UNIX;
76 else if ( platform.equals(GenerationPlan.WINDOWS) )
77 mode = WINDOWS;
78 else {
79 textArea.append("Error: Unknown platform " + platform +
80 "-- cannot write scripts");
81 return;
82 }
83 String shell = genplan.getShell();
84 if (shell.equals(GenerationPlan.NONE))
85 normalScripts (mode);
86 else if (shell.equals (GenerationPlan.ZSH))
87 zshScripts (mode);
88 }
89
90 /***
91 * generate scripts for the Zsh shell - one script that runs the nameserver, then runs
92 * a instance of zsh and invokes the task agents in it, and the facilitator.
93 * A runVis script is also written so that visualisation agents can be run easily
94 * This is quite a simple implementation, so it doesn't handle all the issues like
95 * remote deployment that zeus normally does.
96 *@author Simon Thompson
97 *@since 1.2.2
98 *@param mode - unix(0) or widows (1)
99 *@returns nothing.
100 */
101 public void zshScripts (int mode) {
102 try {
103 NameserverInfo[] server = genplan.getNameservers();
104 FacilitatorInfo[] facilitator = genplan.getFacilitators();
105 VisualiserInfo[] visualiser = genplan.getVisualisers();
106 DbProxyInfo[] proxy = genplan.getDbProxys();
107 AgentInfo[] agent = genplan.getAgents();
108
109
110 textArea.append("Writing zsh.zsh script " +
111 ScriptName[ZSHUNIX][0] + "\n");
112
113 PrintWriter out = createFile(ScriptName[mode+ZSHUNIX][0]);
114 out.println("ns");
115
116 for(int i = 0; i < agent.length; i++ ) {
117 String cmd = writeForScript(mode,agent[i],out);
118 textArea.append(cmd + "\n");
119 }
120
121 out.println("yp");
122 out.println("vis");
123 out.flush();
124 out.close();
125
126 textArea.append ("Writing batch file: "+ ScriptName[mode+ZSHUNIX][1]) ;
127 out = createFile (ScriptName[mode + ZSHUNIX][1]);
128 String ontology = agent[0].ontology_file;
129 if (mode == UNIX) {
130 out.println("java zsh.zsh " + ontology + " 0.2 all.scr &");
131 }
132 else
133 {
134 out.println("start /min java zsh.zsh " + ontology + " 0.2 all.scr ");
135 }
136
137 out.flush();
138 out.close();
139
140 }
141 catch(IOException e) {
142 textArea.append("I/O Error while attempting to create/write script");
143 }
144
145 }
146
147
148 public void normalScripts(int mode) {
149 try {
150 NameserverInfo[] server = genplan.getNameservers();
151 FacilitatorInfo[] facilitator = genplan.getFacilitators();
152 VisualiserInfo[] visualiser = genplan.getVisualisers();
153 DbProxyInfo[] proxy = genplan.getDbProxys();
154 AgentInfo[] agent = genplan.getAgents();
155
156
157 textArea.append("Attempting to write script: " +
158 ScriptName[mode][0] + "\n");
159
160 PrintWriter out = createFile(ScriptName[mode][0]);
161 out.println(Preamble[mode][0]);
162
163 for(int i = 0; i < server.length; i++ ) {
164 if ( server[i].is_root )
165 writeCommand(mode,server[i],out);
166 }
167 out.flush();
168 out.close();
169 textArea.append("Script: " + ScriptName[mode][0] +
170 " written successfully\n");
171
172
173
174 textArea.append("Attempting to write script: " +
175 ScriptName[mode][1] + "\n");
176
177 out = createFile(ScriptName[mode][1]);
178 out.println(Preamble[mode][1]);
179
180 for(int i = 0; i < server.length; i++ )
181 if ( !server[i].is_root )
182 writeCommand(mode,server[i],out);
183
184 for(int i = 0; i < facilitator.length; i++ )
185 writeCommand(mode,facilitator[i],out);
186
187 for(int i = 0; i < visualiser.length; i++ )
188 writeCommand(mode,visualiser[i],out);
189
190 for(int i = 0; i < proxy.length; i++ )
191 writeCommand(mode,proxy[i],out);
192
193 out.flush();
194 out.close();
195 textArea.append("Script: " + ScriptName[mode][1] +
196 " written successfully\n");
197
198
199 textArea.append("Attempting to write script: " +
200 ScriptName[mode][2] + "\n");
201
202 out = createFile(ScriptName[mode][2]);
203 out.println(Preamble[mode][2]);
204
205 for(int i = 0; i < agent.length; i++ )
206 writeCommand(mode,agent[i],out);
207
208 out.flush();
209 out.close();
210 textArea.append("Script: " + ScriptName[mode][2] +
211 " written successfully\n");
212 switch(mode) {
213 case UNIX:
214 textArea.append("Setting script permissions\n");
215 Runtime runtime = Runtime.getRuntime();
216 Process proc = runtime.exec("chmod 777 " +
217 ScriptName[mode][0] + " " +
218 ScriptName[mode][1] + " " +
219 ScriptName[mode][2]);
220
221
222
223
224
225
226
227
228
229 break;
230 }
231
232 }
233 catch(IOException e) {
234 textArea.append("I/O Error while attempting to create/write script");
235 }
236 }
237
238
239
240 /***
241 very simple at the moment, could extend it to allow people
242 to specify the number of these to be run
243 */
244 protected String writeForScript (int mode, GenerationInfo info,
245 PrintWriter out ) {
246
247 String[] summary = info.summarize();
248 String cmd = summary[GenerationInfo.COMMAND];
249 out.println(cmd);
250 return cmd;
251 }
252
253
254
255 protected void writeCommand(int mode, GenerationInfo info,
256 PrintWriter out) {
257 String error;
258
259 if ( (error = info.isValid()) != null ) {
260 textArea.append(error);
261 return;
262 }
263
264 String[] summary = info.summarize();
265 String cmd = language + " " + summary[GenerationInfo.COMMAND];
266 String dir;
267
268 switch(mode) {
269 case UNIX:
270 cmd += " & ";
271 dir = updateFilename(directory,UNIX);
272 if ( info.host == null || info.host.equals("127.0.0.1") ||
273 info.host.equals(GenerationInfo.LOCALHOST) )
274 out.println(cmd);
275 else
276 out.println("rsh " + info.host + " \'cd " + dir +
277 "; " + cmd + "\' & ");
278 break;
279
280 case WINDOWS:
281 cmd = "start /min " + cmd;
282 dir = updateFilename(directory,WINDOWS);
283 if ( info.host == null || info.host.equals("127.0.0.1") ||
284 info.host.equals(GenerationInfo.LOCALHOST) )
285 out.println(cmd);
286 else
287 out.println("rsh " + info.host + " cd " + dir +
288 "; " + cmd);
289 break;
290 }
291 }
292 }