1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2006-2008 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2016 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Eric Bollengier May 2006
24  */
25 /**
26  * @file
27  * BAREOS RunScript Structure definition for FileDaemon and Director
28  */
29 
30 #ifndef BAREOS_LIB_RUNSCRIPT_H_
31 #define BAREOS_LIB_RUNSCRIPT_H_ 1
32 
33 #include "jcr.h"
34 
35 /* Usage:
36  *
37  * #define USE_RUNSCRIPT
38  * #include "lib/runscript.h"
39  *
40  * RunScript *script = NewRunscript();
41  * script->SetCommand("/bin/sleep 20");
42  * script->on_failure = true;
43  * script->when = SCRIPT_After;
44  *
45  * script->run("LabelBefore");  // the label must contain "Before" or "After" special keyword
46  * FreeRunscript(script);
47  */
48 
49 /**
50  * RunScript->when can take following bit values:
51  */
52 enum {
53    SCRIPT_Never = 0,
54    SCRIPT_After = (1<<0),       /* AfterJob */
55    SCRIPT_Before = (1<<1),      /* BeforeJob */
56    SCRIPT_AfterVSS = (1<<2),	/* BeforeJob and After VSS */
57    SCRIPT_Any = SCRIPT_Before | SCRIPT_After
58 };
59 
60 enum {
61    SHELL_CMD   = '|',
62    CONSOLE_CMD = '@'
63 };
64 
65 /**
66  * Structure for RunScript ressource
67  */
68 class RunScript {
69 public:
70    POOLMEM *command;            /* Command string */
71    POOLMEM *target;             /* Host target */
72    int  when;                   /* SCRIPT_Before|Script_After BEFORE/AFTER JOB*/
73    int  cmd_type;               /* Command type -- Shell, Console */
74    char level;                  /* Base|Full|Incr...|All (NYI) */
75    bool short_form;             /* Run Script in short form */
76    bool from_jobdef;            /* This RUN script comes from JobDef */
77    bool on_success;             /* Execute command on job success (After) */
78    bool on_failure;             /* Execute command on job failure (After) */
79    bool fail_on_error;          /* Abort job on error (Before) */
80    job_code_callback_t job_code_callback;
81                                 /* Optional callback function passed to edit_job_code */
82    alist *commands;             /* Use during parsing */
83    bool run(JobControlRecord *job, const char *name=""); /* name must contain "Before" or "After" keyword */
CanRunAtLevel(int JobLevel)84    bool CanRunAtLevel(int JobLevel) { return true;}        /* TODO */
85    void SetCommand(const char *cmd, int cmd_type = SHELL_CMD);
86    void SetTarget(const char *client_name);
87    void ResetDefault(bool free_string = false);
88    bool IsLocal();             /* True if running on local host */
89    void debug();
90 
91    void SetJobCodeCallback(job_code_callback_t job_code_callback);
92 };
93 
94 /* create new RunScript (set all value to 0) */
95 RunScript *NewRunscript();
96 
97 /* create new RunScript from another */
98 RunScript *copy_runscript(RunScript *src);
99 
100 /* launch each script from runscripts*/
101 int RunScripts(JobControlRecord *jcr, alist *runscripts, const char *name,
102                 alist *allowed_script_dirs = NULL);
103 
104 /* free RunScript (and all POOLMEM) */
105 void FreeRunscript(RunScript *script);
106 
107 /* foreach_alist free RunScript */
108 void FreeRunscripts(alist *runscripts); /* you have to free alist */
109 
110 extern bool (*console_command)(JobControlRecord *jcr, const char *cmd);
111 
112 #endif /* BAREOS_LIB_RUNSCRIPT_H_ */
113