1 Index: src/dird/fd_cmds.c
2 ===================================================================
3 --- src/dird/fd_cmds.c (révision 6169)
4 +++ src/dird/fd_cmds.c (copie de travail)
7 bool launch_before_cmd = false;
8 POOLMEM *ehost = get_pool_memory(PM_FNAME);
10 + int result, cmd_type;
13 Dmsg0(120, "bdird: sending runscripts to fd\n");
16 Dmsg2(200, "bdird: runscript %s -> %s\n", cmd->target, ehost);
18 if (strcmp(ehost, jcr->client->name()) == 0) {
19 - pm_strcpy(msg, cmd->command);
21 + foreach_alist(c, cmd->commands) {
22 + cmd->get_command(c, &cmd_type, &command);
23 + if (cmd_type == SHELL_CMD) {
24 + pm_strcpy(msg, command);
27 - Dmsg1(120, "bdird: sending runscripts to fd '%s'\n", cmd->command);
28 + Dmsg1(120, "bdird: sending runscripts to fd '%s'\n", command);
30 - /* TODO: remove this with bacula 1.42 */
31 - if (cmd->old_proto) {
32 - result = send_runscript_with_old_proto(jcr, cmd->when, msg);
33 + /* TODO: remove this with bacula 1.42 */
34 + if (cmd->old_proto) {
35 + result = send_runscript_with_old_proto(jcr, cmd->when, msg);
38 + fd->fsend(runscript, cmd->on_success,
45 - fd->fsend(runscript, cmd->on_success,
51 - result = response(jcr, fd, OKRunScript, "RunScript", DISPLAY_ERROR);
52 - launch_before_cmd = true;
53 + result = response(jcr, fd, OKRunScript, "RunScript", DISPLAY_ERROR);
54 + launch_before_cmd = true;
66 /* TODO : we have to play with other client */
69 send command to an other client
76 /* Tell the FD to execute the ClientRunBeforeJob */
77 Index: src/dird/dird_conf.c
78 ===================================================================
79 --- src/dird/dird_conf.c (révision 6169)
80 +++ src/dird/dird_conf.c (copie de travail)
84 if (res->res_job.RunScripts) {
86 - foreach_alist(script, res->res_job.RunScripts) {
87 - sendit(sock, _(" --> RunScript\n"));
88 - sendit(sock, _(" --> Command=%s\n"), NPRT(script->command));
89 - sendit(sock, _(" --> Target=%s\n"), NPRT(script->target));
90 - sendit(sock, _(" --> RunOnSuccess=%u\n"), script->on_success);
91 - sendit(sock, _(" --> RunOnFailure=%u\n"), script->on_failure);
92 - sendit(sock, _(" --> FailJobOnError=%u\n"), script->fail_on_error);
93 - sendit(sock, _(" --> RunWhen=%u\n"), script->when);
97 + foreach_alist(script, res->res_job.RunScripts) {
98 + sendit(sock, _(" --> RunScript\n"));
99 + foreach_alist(c, script->commands) {
100 + sendit(sock, _(" --> Command=%s\n"), NPRT(c));
102 + sendit(sock, _(" --> Target=%s\n"), NPRT(script->target));
103 + sendit(sock, _(" --> RunOnSuccess=%u\n"), script->on_success);
104 + sendit(sock, _(" --> RunOnFailure=%u\n"), script->on_failure);
105 + sendit(sock, _(" --> FailJobOnError=%u\n"), script->fail_on_error);
106 + sendit(sock, _(" --> RunWhen=%u\n"), script->when);
109 if (res->res_job.pool) {
110 sendit(sock, _(" --> "));
111 @@ -1724,14 +1727,14 @@
115 - * Store a runscript->command as a string
116 + * Store a runscript->commands as a alist(char + string)
118 static void store_runscript_cmd(LEX *lc, RES_ITEM *item, int index, int pass)
120 lex_get_token(lc, T_STRING);
123 - ((RUNSCRIPT*)item->value)->set_command(lc->str, item->code);
124 + ((RUNSCRIPT*)item->value)->add_command(lc->str, item->code);
128 @@ -1745,7 +1748,7 @@
129 RUNSCRIPT *script = new_runscript();
130 script->set_job_code_callback(job_code_callback_filesetname);
132 - script->set_command(lc->str);
133 + script->add_command(lc->str);
135 /* TODO: remove all script->old_proto with bacula 1.42 */
137 @@ -1873,7 +1876,7 @@
141 - if (res_runscript.command == NULL) {
142 + if (res_runscript.commands == NULL) {
143 scan_err2(lc, _("%s item is required in %s resource, but not found.\n"),
144 "command", "runscript");
146 @@ -1886,7 +1889,7 @@
147 RUNSCRIPT *script = new_runscript();
148 memcpy(script, &res_runscript, sizeof(RUNSCRIPT));
149 script->set_job_code_callback(job_code_callback_filesetname);
152 if (*runscripts == NULL) {
153 *runscripts = New(alist(10, not_owned_by_alist));
155 Index: src/filed/job.c
156 ===================================================================
157 --- src/filed/job.c (révision 6169)
158 +++ src/filed/job.c (copie de travail)
161 /* Run the command now */
162 script = new_runscript();
163 - script->set_command(cmd);
164 + script->add_command(cmd);
165 script->when = SCRIPT_Before;
166 ok = script->run(jcr, "ClientRunBeforeJob");
167 free_runscript(script);
171 cmd = new_runscript();
172 - cmd->set_command(msg);
173 + cmd->add_command(msg);
174 cmd->on_success = true;
175 cmd->on_failure = false;
176 cmd->when = SCRIPT_After;
178 cmd->fail_on_error = fail_on_error;
181 - cmd->set_command(msg);
182 + cmd->add_command(msg);
184 jcr->RunScripts->append(cmd);
186 Index: src/lib/runscript.h
187 ===================================================================
188 --- src/lib/runscript.h (révision 6169)
189 +++ src/lib/runscript.h (copie de travail)
205 - POOLMEM *command; /* command string */
206 + alist *commands; /* list of command/console string */
207 POOLMEM *target; /* host target */
208 int when; /* SCRIPT_Before|Script_After BEFORE/AFTER JOB*/
209 - int cmd_type; /* Command type -- Shell, Console */
210 char level; /* Base|Full|Incr...|All (NYI) */
211 bool on_success; /* execute command on job success (After) */
212 bool on_failure; /* execute command on job failure (After) */
215 bool run(JCR *job, const char *name=""); /* name must contain "Before" or "After" keyword */
216 bool can_run_at_level(int JobLevel) { return true;}; /* TODO */
217 - void set_command(const POOLMEM *cmd, int cmd_type = SHELL_CMD);
218 + void add_command(const POOLMEM *cmd, char cmd_type = SHELL_CMD);
219 + void get_command(const char *cmd, int *cmd_type, char **cmd);
220 void set_target(const POOLMEM *client_name);
221 void reset_default(bool free_string = false);
222 - bool is_local(); /* true if running on local host */
223 + bool is_local(const char *cmd); /* true if running on local host */
225 + void set_job_code_callback(job_code_callback_t job_code_callback);
227 - void set_job_code_callback(job_code_callback_t job_code_callback);
229 + bool run_command(const char *cmd, JCR *job, const char *name="");
232 /* create new RUNSCRIPT (set all value to 0) */
233 RUNSCRIPT *new_runscript();
235 -/* create new RUNSCRIPT from an other */
236 +/* create new RUNSCRIPT from an other (deep copy) */
237 RUNSCRIPT *copy_runscript(RUNSCRIPT *src);
239 /* launch each script from runscripts*/
240 Index: src/lib/runscript.c
241 ===================================================================
242 --- src/lib/runscript.c (révision 6169)
243 +++ src/lib/runscript.c (copie de travail)
246 void RUNSCRIPT::reset_default(bool free_strings)
248 - if (free_strings && command) {
249 - free_pool_memory(command);
251 + if (free_strings && commands) {
252 + foreach_alist(c, commands) {
253 + free_pool_memory(c);
257 if (free_strings && target) {
258 free_pool_memory(target);
266 fail_on_error = true;
268 RUNSCRIPT *dst = (RUNSCRIPT *)malloc(sizeof(RUNSCRIPT));
269 memcpy(dst, src, sizeof(RUNSCRIPT));
271 - dst->command = NULL;
272 + dst->commands = New(alist(5, not_owned_by_alist));
275 + foreach_alist(c, src->commands) {
276 + m = get_pool_memory(PM_FNAME);
278 + dst->commands->append(m);
283 - dst->set_command(src->command, src->cmd_type);
284 dst->set_target(src->target);
288 void free_runscript(RUNSCRIPT *script)
290 Dmsg0(500, "runscript: freeing RUNSCRIPT object\n");
292 - if (script->command) {
293 - free_pool_memory(script->command);
295 + if (script->commands) {
296 + foreach_alist(c, script->commands) {
297 + free_pool_memory(c);
299 + delete script->commands;
301 if (script->target) {
302 free_pool_memory(script->target);
303 @@ -108,66 +120,25 @@
304 int run_scripts(JCR *jcr, alist *runscripts, const char *label)
306 Dmsg2(200, "runscript: running all RUNSCRIPT object (%s) JobStatus=%c\n", label, jcr->JobStatus);
313 - if (strstr(label, NT_("Before"))) {
314 - when = SCRIPT_Before;
316 - when = SCRIPT_After;
319 if (runscripts == NULL) {
320 Dmsg0(100, "runscript: WARNING RUNSCRIPTS list is NULL\n");
326 foreach_alist(script, runscripts) {
327 - Dmsg2(200, "runscript: try to run %s:%s\n", NPRT(script->target), NPRT(script->command));
330 - if ((script->when & SCRIPT_Before) && (when & SCRIPT_Before)) {
331 - if ((script->on_success
332 - && (jcr->JobStatus == JS_Running || jcr->JobStatus == JS_Created))
333 - || (script->on_failure && job_canceled(jcr))
336 - Dmsg4(200, "runscript: Run it because SCRIPT_Before (%s,%i,%i,%c)\n",
337 - script->command, script->on_success, script->on_failure,
343 - if ((script->when & SCRIPT_After) && (when & SCRIPT_After)) {
344 - if ((script->on_success && (jcr->JobStatus == JS_Terminated))
345 - || (script->on_failure && job_canceled(jcr))
348 - Dmsg4(200, "runscript: Run it because SCRIPT_After (%s,%i,%i,%c)\n",
349 - script->command, script->on_success, script->on_failure,
355 - if (!script->is_local()) {
359 - /* we execute it */
361 - script->run(jcr, label);
363 + script->run(jcr, label);
368 -bool RUNSCRIPT::is_local()
369 +bool RUNSCRIPT::is_local(const char *c)
371 + if (c[0] == CONSOLE_CMD) {
375 if (!target || (strcmp(target, "") == 0)) {
378 @@ -175,23 +146,31 @@
382 -/* set this->command to cmd */
383 -void RUNSCRIPT::set_command(const POOLMEM *cmd, int acmd_type)
384 +/* set add cmd to this->commands alist */
385 +void RUNSCRIPT::add_command(const POOLMEM *cmd, char acmd_type)
387 - Dmsg1(500, "runscript: setting command = %s\n", NPRT(cmd));
388 + Dmsg2(500, "runscript: setting command = %s type=%i\n", NPRT(cmd), acmd_type);
395 - command = get_pool_memory(PM_FNAME);
397 + commands = New(alist(5, not_owned_by_alist));
400 - pm_strcpy(command, cmd);
401 - cmd_type = acmd_type;
402 + POOLMEM *c = get_pool_memory(PM_FNAME);
403 + Mmsg(c, "%c%s", acmd_type, cmd);
404 + commands->append(c);
407 +/* command = ( SHELL_CMD | CONSOLE_CMD ) command */
408 +void RUNSCRIPT::get_command(const char* command, int *acmd_type, char **cmd)
410 + *acmd_type = (int) command[0];
411 + *cmd = (char *)command + 1;
414 /* set this->target to client_name */
415 void RUNSCRIPT::set_target(const POOLMEM *client_name)
417 @@ -210,14 +189,70 @@
419 bool RUNSCRIPT::run(JCR *jcr, const char *name)
421 - Dmsg1(100, "runscript: running a RUNSCRIPT object type=%d\n", cmd_type);
427 + if (strstr(name, NT_("Before"))) {
428 + when = SCRIPT_Before;
430 + when = SCRIPT_After;
433 + foreach_alist(c, this->commands) {
434 + Dmsg2(200, "runscript: try to run %s:%s\n", NPRT(this->target), NPRT(c));
437 + if ((this->when & SCRIPT_Before) && (when & SCRIPT_Before)) {
438 + if ((this->on_success
439 + && (jcr->JobStatus == JS_Running || jcr->JobStatus == JS_Created))
440 + || (this->on_failure && job_canceled(jcr))
443 + Dmsg4(200, "runscript: Run it because SCRIPT_Before (%s,%i,%i,%c)\n",
444 + c, this->on_success, this->on_failure,
450 + if ((this->when & SCRIPT_After) && (when & SCRIPT_After)) {
451 + if ((this->on_success && (jcr->JobStatus == JS_Terminated))
452 + || (this->on_failure && job_canceled(jcr))
455 + Dmsg4(200, "runscript: Run it because SCRIPT_After (%s,%i,%i,%c)\n",
456 + c, this->on_success, this->on_failure,
462 + if (!this->is_local(c)) {
466 + /* we execute it */
468 + this->run_command(c, jcr, name);
474 +/* run a command from the list */
475 +bool RUNSCRIPT::run_command(const char *command, JCR *jcr, const char *name)
477 POOLMEM *ecmd = get_pool_memory(PM_FNAME);
479 + int status, cmd_type;
481 - char line[MAXSTRING];
482 + char line[MAXSTRING], *cmd;
484 - ecmd = edit_job_codes(jcr, ecmd, this->command, "", this->job_code_callback);
485 - Dmsg1(100, "runscript: running '%s'...\n", ecmd);
486 + this->get_command(command, &cmd_type, &cmd);
487 + ecmd = edit_job_codes(jcr, ecmd, cmd, "", this->job_code_callback);
489 + Dmsg2(100, "runscript: running '%s' object type=%c...\n", ecmd, cmd_type);
490 Jmsg(jcr, M_INFO, 0, _("%s: run %s \"%s\"\n"),
491 cmd_type==SHELL_CMD?"shell command":"console command", name, ecmd);
495 void RUNSCRIPT::debug()
498 Dmsg0(200, "runscript: debug\n");
499 Dmsg0(200, _(" --> RunScript\n"));
500 - Dmsg1(200, _(" --> Command=%s\n"), NPRT(command));
501 + foreach_alist(c, commands) {
502 + Dmsg1(200, _(" --> Command=%s\n"), NPRT(c));
504 Dmsg1(200, _(" --> Target=%s\n"), NPRT(target));
505 Dmsg1(200, _(" --> RunOnSuccess=%u\n"), on_success);
506 Dmsg1(200, _(" --> RunOnFailure=%u\n"), on_failure);