Skip to content

Commit

Permalink
Fix runscript printing.
Browse files Browse the repository at this point in the history
Keep track if a runscript is entered as a short runscript using the
short_form boolean so when printing it we also know if we need to print
it either in its short form or in the new long form. Also keep track
using the from_jobdef boolean if a runscript is inherited from a JobDef
as then we also don't have to print it in the dumping of the current
config.
  • Loading branch information
Marco van Wieringen committed Oct 6, 2014
1 parent f0ac859 commit 7473c51
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
59 changes: 37 additions & 22 deletions src/dird/dird_conf.c
Expand Up @@ -689,7 +689,6 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str)
if (list != NULL) {
foreach_alist(runscript, list) {
int len;
bool shortrunscript = false;
POOLMEM *cmdbuf;

len = strlen(runscript->command);
Expand All @@ -698,46 +697,43 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str)
escape_string(cmdbuf, runscript->command, len);

/*
* Check if runscript can be written as short runscript
* Don't print runscript when its inherited from a JobDef.
*/
if (runscript->cmd_type == '|') { /* short runscripts only support shell command */
if (runscript->from_jobdef) {
continue;
}

/*
* Check if runscript must be written as short runscript
*/
if (runscript->short_form) {
if (runscript->when == SCRIPT_Before && /* runbeforejob */
(bstrcmp(runscript->target, ""))) {
Mmsg(temp, "run before job = \"%s\"\n", cmdbuf);
shortrunscript = true;
} else if (runscript->when == SCRIPT_After && /* runafterjob */
runscript->on_success &&
!runscript->on_failure &&
!runscript->fail_on_error &&
bstrcmp(runscript->target, "")) {
Mmsg(temp, "run after job = \"%s\"\n", cmdbuf);
shortrunscript = true;
} else if (runscript->when == SCRIPT_After && /* client run after job */
runscript->on_success &&
!runscript->on_failure &&
!runscript->fail_on_error &&
!bstrcmp(runscript->target, "")) {
Mmsg(temp, "client run after job = \"%s\"\n", cmdbuf);
shortrunscript = true;
} else if (runscript->when == SCRIPT_Before && /* client run before job */
!bstrcmp(runscript->target, "")) {
Mmsg(temp, "before job = \"%s\"\n", cmdbuf);
shortrunscript = true;
} else if (runscript->when == SCRIPT_After && /* run after failed job */
runscript->on_failure &&
!runscript->on_success &&
!runscript->fail_on_error &&
bstrcmp(runscript->target, "")) {
Mmsg(temp, "run after failed job = \"%s\"\n", cmdbuf);
shortrunscript = true;
}
indent_config_item(cfg_str, 1, temp.c_str());
}

/*
* If we cannot write the runscript as short runscript...
*/
if (!shortrunscript) {
} else {
Mmsg(temp, "runscript {\n");
indent_config_item(cfg_str, 1, temp.c_str());

Expand All @@ -750,7 +746,7 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str)
indent_config_item(cfg_str, 2, temp.c_str());

/*
* default: never
* Default: never
*/
char *when = (char *)"never";
switch (runscript->when) {
Expand All @@ -773,23 +769,29 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str)
indent_config_item(cfg_str, 2, temp.c_str());
}

/* default: fail_on_error = true */
/*
* Default: fail_on_error = true
*/
char *fail_on_error = (char *)"Yes";
if (! runscript->fail_on_error){
if (!runscript->fail_on_error){
fail_on_error = (char *)"No";
Mmsg(temp, "failonerror = %s\n", fail_on_error);
indent_config_item(cfg_str, 2, temp.c_str());
}

/* default: on_success = true */
/*
* Default: on_success = true
*/
char *run_on_success = (char *)"Yes";
if (! runscript->on_success){
if (!runscript->on_success){
run_on_success = (char *)"No";
Mmsg(temp, "runsonsuccess = %s\n", run_on_success);
indent_config_item(cfg_str, 2, temp.c_str());
}

/* default: on_failure = false */
/*
* Default: on_failure = false
*/
char *run_on_failure = (char *)"No";
if (runscript->on_failure) {
run_on_failure = (char *)"Yes";
Expand All @@ -801,7 +803,9 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str)
Dmsg1(200, " level = %d\n", runscript->level);
*/

/* default: runsonclient = yes */
/*
* Default: runsonclient = yes
*/
char *runsonclient = (char *)"Yes";
if (bstrcmp(runscript->target, "")) {
runsonclient = (char *)"No";
Expand All @@ -810,7 +814,7 @@ static inline void print_config_runscript(RES_ITEM *item, POOL_MEM &cfg_str)
}

indent_config_item(cfg_str, 1, "}\n");
} /* not a short runscript */
}

free_pool_memory(cmdbuf);
}
Expand Down Expand Up @@ -2262,6 +2266,7 @@ bool populate_jobdefs()

foreach_alist(rs, jobdefs->RunScripts) {
elt = copy_runscript(rs);
elt->from_jobdef = true;
job->RunScripts->append(elt); /* we have to free it */
}
}
Expand Down Expand Up @@ -2932,6 +2937,11 @@ static void store_short_runscript(LEX *lc, RES_ITEM *item, int index, int pass)
script->set_target("");
}

/*
* Remember that the entry was configured in the short runscript form.
*/
script->short_form = true;

if (*runscripts == NULL) {
*runscripts = New(alist(10, not_owned_by_alist));
}
Expand Down Expand Up @@ -3063,6 +3073,11 @@ static void store_runscript(LEX *lc, RES_ITEM *item, int index, int pass)
script->target = NULL;
script->set_target(res_runscript.target);

/*
* Remember that the entry was configured in the short runscript form.
*/
script->short_form = false;

(*runscripts)->append(script);
script->debug();
}
Expand Down
20 changes: 11 additions & 9 deletions src/lib/runscript.h
Expand Up @@ -47,11 +47,11 @@
* RUNSCRIPT->when can take following bit values:
*/
enum {
SCRIPT_Never = 0,
SCRIPT_After = (1<<0), /* AfterJob */
SCRIPT_Never = 0,
SCRIPT_After = (1<<0), /* AfterJob */
SCRIPT_Before = (1<<1), /* BeforeJob */
SCRIPT_AfterVSS = (1<<2), /* BeforeJob and After VSS */
SCRIPT_Any = SCRIPT_Before | SCRIPT_After
SCRIPT_Any = SCRIPT_Before | SCRIPT_After
};

enum {
Expand All @@ -64,14 +64,16 @@ enum {
*/
class RUNSCRIPT {
public:
POOLMEM *command; /* command string */
POOLMEM *target; /* host target */
POOLMEM *command; /* Command string */
POOLMEM *target; /* Host target */
int when; /* SCRIPT_Before|Script_After BEFORE/AFTER JOB*/
int cmd_type; /* Command type -- Shell, Console */
char level; /* Base|Full|Incr...|All (NYI) */
bool on_success; /* execute command on job success (After) */
bool on_failure; /* execute command on job failure (After) */
bool fail_on_error; /* abort job on error (Before) */
bool short_form; /* Run Script in short form */
bool from_jobdef; /* This RUN script comes from JobDef */
bool on_success; /* Execute command on job success (After) */
bool on_failure; /* Execute command on job failure (After) */
bool fail_on_error; /* Abort job on error (Before) */
job_code_callback_t job_code_callback;
/* Optional callback function passed to edit_job_code */
alist *commands; /* Use during parsing */
Expand All @@ -80,7 +82,7 @@ class RUNSCRIPT {
void set_command(const char *cmd, int cmd_type = SHELL_CMD);
void set_target(const char *client_name);
void reset_default(bool free_string = false);
bool is_local(); /* true if running on local host */
bool is_local(); /* True if running on local host */
void debug();

void set_job_code_callback(job_code_callback_t job_code_callback);
Expand Down

0 comments on commit 7473c51

Please sign in to comment.