]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/runscript_with_multiple_command.patch
ebl Update about STAP
[bacula/bacula] / bacula / patches / testing / runscript_with_multiple_command.patch
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)
5 @@ -527,7 +527,8 @@
6     RUNSCRIPT *cmd;
7     bool launch_before_cmd = false;
8     POOLMEM *ehost = get_pool_memory(PM_FNAME);
9 -   int result;
10 +   int result, cmd_type;
11 +   char *c, *command;
12  
13     Dmsg0(120, "bdird: sending runscripts to fd\n");
14     
15 @@ -537,29 +538,33 @@
16           Dmsg2(200, "bdird: runscript %s -> %s\n", cmd->target, ehost);
17  
18           if (strcmp(ehost, jcr->client->name()) == 0) {
19 -            pm_strcpy(msg, cmd->command);
20 -            bash_spaces(msg);
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);
25 +                  bash_spaces(msg);
26  
27 -            Dmsg1(120, "bdird: sending runscripts to fd '%s'\n", cmd->command);
28 +                  Dmsg1(120, "bdird: sending runscripts to fd '%s'\n", command);
29              
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);
36 +                     
37 +                  } else {
38 +                     fd->fsend(runscript, cmd->on_success, 
39 +                                          cmd->on_failure,
40 +                                          cmd->fail_on_error,
41 +                                          cmd->when,
42 +                                          msg);
43  
44 -            } else {
45 -               fd->fsend(runscript, cmd->on_success, 
46 -                                    cmd->on_failure,
47 -                                    cmd->fail_on_error,
48 -                                    cmd->when,
49 -                                    msg);
50 -
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;
55 +                  }
56 +                  if (!result) {
57 +                     goto bail_out;
58 +                  }
59 +               }
60              }
61 -            
62 -            if (!result) {
63 -               goto bail_out;
64 -            }
65           }
66           /* TODO : we have to play with other client */
67           /*
68 @@ -567,7 +572,7 @@
69             send command to an other client
70             }
71           */
72 -      }        
73 +      }
74     } 
75  
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)
81 @@ -651,16 +651,19 @@
82           }
83        }
84        if (res->res_job.RunScripts) {
85 -        RUNSCRIPT *script;
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);
94 -        }
95 +         char *c;
96 +         RUNSCRIPT *script;
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));
101 +            }
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);
107 +         }
108        }
109        if (res->res_job.pool) {
110           sendit(sock, _("  --> "));
111 @@ -1724,14 +1727,14 @@
112  }
113  
114  /*
115 - * Store a runscript->command as a string
116 + * Store a runscript->commands as a alist(char + string)
117   */
118  static void store_runscript_cmd(LEX *lc, RES_ITEM *item, int index, int pass)
119  {
120     lex_get_token(lc, T_STRING);
121  
122     if (pass == 2) {
123 -      ((RUNSCRIPT*)item->value)->set_command(lc->str, item->code);
124 +      ((RUNSCRIPT*)item->value)->add_command(lc->str, item->code);
125     }
126     scan_to_eol(lc);
127  }
128 @@ -1745,7 +1748,7 @@
129        RUNSCRIPT *script = new_runscript();
130        script->set_job_code_callback(job_code_callback_filesetname);
131  
132 -      script->set_command(lc->str);
133 +      script->add_command(lc->str);
134  
135        /* TODO: remove all script->old_proto with bacula 1.42 */
136  
137 @@ -1873,7 +1876,7 @@
138     }
139  
140     if (pass == 2) {
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");
145        }
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);
150 -      
151 +
152        if (*runscripts == NULL) {
153          *runscripts = New(alist(10, not_owned_by_alist));
154        }
155 Index: src/filed/job.c
156 ===================================================================
157 --- src/filed/job.c     (révision 6169)
158 +++ src/filed/job.c     (copie de travail)
159 @@ -481,7 +481,7 @@
160  
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);
168 @@ -529,7 +529,7 @@
169     unbash_spaces(msg);
170  
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;
177 @@ -567,7 +567,7 @@
178     cmd->fail_on_error = fail_on_error;
179     unbash_spaces(msg);
180  
181 -   cmd->set_command(msg);
182 +   cmd->add_command(msg);
183     cmd->debug();
184     jcr->RunScripts->append(cmd);
185  
186 Index: src/lib/runscript.h
187 ===================================================================
188 --- src/lib/runscript.h (révision 6169)
189 +++ src/lib/runscript.h (copie de travail)
190 @@ -62,8 +62,8 @@
191  };
192  
193  enum {
194 -   SHELL_CMD   = 1,
195 -   CONSOLE_CMD = 2 
196 +   SHELL_CMD   = '|',
197 +   CONSOLE_CMD = '@' 
198  };
199  
200  /*
201 @@ -71,10 +71,9 @@
202   */
203  class RUNSCRIPT {
204  public:
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) */
213 @@ -86,19 +85,22 @@
214  
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 */
224     void debug();
225 +   void set_job_code_callback(job_code_callback_t job_code_callback);
226  
227 -   void set_job_code_callback(job_code_callback_t job_code_callback);
228 +private:
229 +   bool run_command(const char *cmd, JCR *job, const char *name="");
230  };
231  
232  /* create new RUNSCRIPT (set all value to 0) */
233  RUNSCRIPT *new_runscript();           
234  
235 -/* create new RUNSCRIPT from an other */
236 +/* create new RUNSCRIPT from an other (deep copy) */
237  RUNSCRIPT *copy_runscript(RUNSCRIPT *src);
238  
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)
244 @@ -59,15 +59,18 @@
245  
246  void RUNSCRIPT::reset_default(bool free_strings)
247  {
248 -   if (free_strings && command) {
249 -     free_pool_memory(command);
250 +   char *c;
251 +   if (free_strings && commands) {
252 +      foreach_alist(c, commands) {
253 +         free_pool_memory(c);
254 +      }
255 +      delete commands;
256     }
257     if (free_strings && target) {
258       free_pool_memory(target);
259     }
260 -   
261     target = NULL;
262 -   command = NULL;
263 +   commands = NULL;
264     on_success = true;
265     on_failure = false;
266     fail_on_error = true;
267 @@ -83,10 +86,16 @@
268     RUNSCRIPT *dst = (RUNSCRIPT *)malloc(sizeof(RUNSCRIPT));
269     memcpy(dst, src, sizeof(RUNSCRIPT));
270  
271 -   dst->command = NULL;
272 +   dst->commands =  New(alist(5, not_owned_by_alist));
273 +   char *c;
274 +   POOLMEM *m;
275 +   foreach_alist(c, src->commands) {
276 +      m = get_pool_memory(PM_FNAME);
277 +      pm_strcpy(m, c);
278 +      dst->commands->append(m);
279 +   }
280 +
281     dst->target = NULL;
282 -
283 -   dst->set_command(src->command, src->cmd_type);
284     dst->set_target(src->target);
285  
286     return dst;   
287 @@ -95,9 +104,12 @@
288  void free_runscript(RUNSCRIPT *script)
289  {
290     Dmsg0(500, "runscript: freeing RUNSCRIPT object\n");
291 -
292 -   if (script->command) {
293 -      free_pool_memory(script->command);
294 +   POOLMEM *c;
295 +   if (script->commands) {
296 +      foreach_alist(c, script->commands) {
297 +         free_pool_memory(c);
298 +      }
299 +      delete script->commands;
300     }
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)
305  {
306     Dmsg2(200, "runscript: running all RUNSCRIPT object (%s) JobStatus=%c\n", label, jcr->JobStatus);
307 -   
308 -   RUNSCRIPT *script;
309 -   bool runit;
310  
311 -   int when;
312 -
313 -   if (strstr(label, NT_("Before"))) {
314 -      when = SCRIPT_Before;
315 -   } else {
316 -      when = SCRIPT_After;
317 -   }
318 -
319     if (runscripts == NULL) {
320        Dmsg0(100, "runscript: WARNING RUNSCRIPTS list is NULL\n");
321        return 0;
322     }
323 -
324 +   
325 +   RUNSCRIPT *script;
326     foreach_alist(script, runscripts) {
327 -      Dmsg2(200, "runscript: try to run %s:%s\n", NPRT(script->target), NPRT(script->command));
328 -      runit = false;
329 -
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))
334 -            )
335 -         {
336 -            Dmsg4(200, "runscript: Run it because SCRIPT_Before (%s,%i,%i,%c)\n", 
337 -                  script->command, script->on_success, script->on_failure,
338 -                  jcr->JobStatus );
339 -            runit = true;
340 -         }
341 -      }
342 -
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))
346 -            )
347 -         {
348 -            Dmsg4(200, "runscript: Run it because SCRIPT_After (%s,%i,%i,%c)\n", 
349 -                  script->command, script->on_success, script->on_failure,
350 -                  jcr->JobStatus );
351 -            runit = true;
352 -         }
353 -      }
354 -
355 -      if (!script->is_local()) {
356 -         runit = false;
357 -      }
358 -
359 -      /* we execute it */
360 -      if (runit) {
361 -         script->run(jcr, label);
362 -      }
363 +      script->run(jcr, label);
364     }
365     return 1;
366  }
367  
368 -bool RUNSCRIPT::is_local()
369 +bool RUNSCRIPT::is_local(const char *c)
370  {
371 +   if (c[0] == CONSOLE_CMD) {
372 +      return true;
373 +   }
374 +
375     if (!target || (strcmp(target, "") == 0)) {
376        return true;
377     } else {
378 @@ -175,23 +146,31 @@
379     }
380  }
381  
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)
386  {
387 -   Dmsg1(500, "runscript: setting command = %s\n", NPRT(cmd));
388 +   Dmsg2(500, "runscript: setting command = %s type=%i\n", NPRT(cmd), acmd_type);
389  
390     if (!cmd) {
391        return;
392     }
393  
394 -   if (!command) {
395 -      command = get_pool_memory(PM_FNAME);
396 +   if (!commands) {
397 +      commands = New(alist(5, not_owned_by_alist)); 
398     }
399  
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);
405  }
406  
407 +/* command = ( SHELL_CMD | CONSOLE_CMD ) command */
408 +void RUNSCRIPT::get_command(const char* command, int *acmd_type, char **cmd)
409 +{
410 +   *acmd_type = (int) command[0];
411 +   *cmd = (char *)command + 1;
412 +}
413 +
414  /* set this->target to client_name */
415  void RUNSCRIPT::set_target(const POOLMEM *client_name)
416  {
417 @@ -210,14 +189,70 @@
418  
419  bool RUNSCRIPT::run(JCR *jcr, const char *name)
420  {
421 -   Dmsg1(100, "runscript: running a RUNSCRIPT object type=%d\n", cmd_type);
422 +   char *c;
423 +   bool runit;
424 +
425 +   int when;
426 +
427 +   if (strstr(name, NT_("Before"))) {
428 +      when = SCRIPT_Before;
429 +   } else {
430 +      when = SCRIPT_After;
431 +   }
432 +
433 +   foreach_alist(c, this->commands) {
434 +      Dmsg2(200, "runscript: try to run %s:%s\n", NPRT(this->target), NPRT(c));
435 +      runit = false;
436 +
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))
441 +            )
442 +         {
443 +            Dmsg4(200, "runscript: Run it because SCRIPT_Before (%s,%i,%i,%c)\n", 
444 +                  c, this->on_success, this->on_failure,
445 +                  jcr->JobStatus );
446 +            runit = true;
447 +         }
448 +      }
449 +
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))
453 +            )
454 +         {
455 +            Dmsg4(200, "runscript: Run it because SCRIPT_After (%s,%i,%i,%c)\n", 
456 +                  c, this->on_success, this->on_failure,
457 +                  jcr->JobStatus );
458 +            runit = true;
459 +         }
460 +      }
461 +
462 +      if (!this->is_local(c)) {
463 +         runit = false;
464 +      }
465 +
466 +      /* we execute it */
467 +      if (runit) {
468 +         this->run_command(c, jcr, name);
469 +      }
470 +   }
471 +   return 1;
472 +}
473 +
474 +/* run a command from the list */
475 +bool RUNSCRIPT::run_command(const char *command, JCR *jcr, const char *name)
476 +{
477     POOLMEM *ecmd = get_pool_memory(PM_FNAME);
478 -   int status;
479 +   int status, cmd_type;
480     BPIPE *bpipe;
481 -   char line[MAXSTRING];
482 +   char line[MAXSTRING], *cmd;
483  
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);
488 +
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);
492  
493 @@ -278,9 +313,12 @@
494  
495  void RUNSCRIPT::debug()
496  {
497 +   char *c;
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));
503 +   }
504     Dmsg1(200,  _("  --> Target=%s\n"),  NPRT(target));
505     Dmsg1(200,  _("  --> RunOnSuccess=%u\n"),  on_success);
506     Dmsg1(200,  _("  --> RunOnFailure=%u\n"),  on_failure);