Skip to content

Commit

Permalink
Merge pull request #358 from joergsteffens/dev/joergs/master/runscrip…
Browse files Browse the repository at this point in the history
…t-fix

dird: fix RunScript parsing
  • Loading branch information
joergsteffens committed Dec 12, 2019
2 parents 9b0d9aa + 720b0a3 commit b2f656e
Show file tree
Hide file tree
Showing 13 changed files with 353 additions and 12 deletions.
13 changes: 9 additions & 4 deletions core/src/dird/dird_conf.cc
Expand Up @@ -591,7 +591,7 @@ static ResourceItem runscript_items[] = {
{ "FailJobOnError", CFG_TYPE_RUNSCRIPT_BOOL, ITEM(res_runscript,fail_on_error), 0, 0, NULL, NULL, NULL },
{ "AbortJobOnError", CFG_TYPE_RUNSCRIPT_BOOL, ITEM(res_runscript,fail_on_error), 0, 0, NULL, NULL, NULL },
{ "RunsWhen", CFG_TYPE_RUNSCRIPT_WHEN, ITEM(res_runscript,when), 0, 0, NULL, NULL, NULL },
{ "RunsOnClient", CFG_TYPE_RUNSCRIPT_TARGET, ITEMC(res_runscript), 0, 0, NULL, NULL, NULL }, /* TODO */
{ "RunsOnClient", CFG_TYPE_RUNSCRIPT_TARGET, ITEMC(res_runscript), 0, 0, NULL, NULL, NULL },
{nullptr, 0, 0, nullptr, 0, 0, nullptr, nullptr, nullptr}
};

Expand Down Expand Up @@ -3220,6 +3220,14 @@ static void StoreRunscript(LEX* lc, ResourceItem* item, int index, int pass)

res_runscript = new RunScript();

/*
* Run on client by default.
* Set this here, instead of in the class constructor,
* as the class is also used by other daemon,
* where the default differs.
*/
if (res_runscript->target.empty()) { res_runscript->SetTarget("%c"); }

while ((token = LexGetToken(lc, BCT_SKIP_EOL)) != BCT_EOF) {
if (token == BCT_EOB) { break; }

Expand Down Expand Up @@ -3261,9 +3269,6 @@ static void StoreRunscript(LEX* lc, ResourceItem* item, int index, int pass)
}

if (pass == 2) {
// Run on client by default
if (res_runscript->target.empty()) { res_runscript->SetTarget("%c"); }

alist** runscripts = GetItemVariablePointer<alist**>(*item);
if (!*runscripts) { *runscripts = new alist(10, not_owned_by_alist); }

Expand Down
10 changes: 6 additions & 4 deletions core/src/lib/runscript.cc
Expand Up @@ -2,6 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2006-2011 Free Software Foundation Europe e.V.
Copyright (C) 2019-2019 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -130,8 +131,11 @@ int RunScripts(JobControlRecord* jcr,
}

foreach_alist (script, runscripts) {
Dmsg2(200, "runscript: try to run %s:%s\n", NSTDPRNT(script->target),
NSTDPRNT(script->command));
Dmsg5(200,
"runscript: try to run (Target=%s, OnSuccess=%i, OnFailure=%i, "
"CurrentJobStatus=%c, command=%s)\n",
NSTDPRNT(script->target), script->on_success, script->on_failure,
jcr->JobStatus, NSTDPRNT(script->command));
runit = false;

if (!script->IsLocal()) {
Expand Down Expand Up @@ -216,8 +220,6 @@ void RunScript::SetTarget(const std::string& client_name)
{
Dmsg1(500, "runscript: setting target = %s\n", NSTDPRNT(client_name));

if (client_name.empty()) { return; }

target = client_name;
}

Expand Down
7 changes: 4 additions & 3 deletions core/src/lib/runscript.h
Expand Up @@ -37,15 +37,14 @@ class alist;

/* Usage:
*
* #define USE_RUNSCRIPT
* #include "lib/runscript.h"
*
* RunScript *script = new RunScript;
* script->SetCommand("/bin/sleep 20");
* script->on_failure = true;
* script->when = SCRIPT_After;
*
* script->run("LabelBefore"); // the label must contain "Before" or "After"
* script->Run("LabelBefore"); // the label must contain "Before" or "After"
* special keyword FreeRunscript(script);
*/

Expand Down Expand Up @@ -81,7 +80,9 @@ class RunScript : public BareosResource {
RunScript(const RunScript& other) = default;

std::string command; /* Command string */
std::string target; /* Host target */
std::string target; /* Host target. Values:
Empty string: run locally.
"%c": (Client’s name). Run on client. */
int when = SCRIPT_Never; /* SCRIPT_Before|Script_After BEFORE/AFTER JOB*/
int cmd_type = 0; /* Command type -- Shell, Console */
char level = 0; /* Base|Full|Incr...|All (NYI) */
Expand Down
@@ -0,0 +1,27 @@
Job {
Name = "admin-runscript-client-server"
JobDefs = "DefaultJob"
Type = Admin
RunScript {
RunsWhen = Before
Runs On Failure = Yes
#Runs On Client = Yes
FailJobOnError = Yes
# %d Daemon’s name (Such as host-dir or host-fd)
# %n Job name
# %t Job type (Backup, …)
# %i Job Id
Command = "@PROJECT_BINARY_DIR@/tests/@TEST_NAME@/write.sh @working_dir@/jobid-%i-runscript.log 'daemon=%d' 'jobname=%n' 'jobtype=%t' 'jobid=%i'"
}
RunScript {
RunsWhen = Before
Runs On Failure = Yes
Runs On Client = No
FailJobOnError = Yes
# %d Daemon’s name (Such as host-dir or host-fd)
# %n Job name
# %t Job type (Backup, …)
# %i Job Id
Command = "@PROJECT_BINARY_DIR@/tests/@TEST_NAME@/write.sh @working_dir@/jobid-%i-runscript.log 'daemon=%d' 'jobname=%n' 'jobtype=%t' 'jobid=%i'"
}
}
@@ -0,0 +1,16 @@
Job {
Name = "admin-runscript-client"
JobDefs = "DefaultJob"
Type = Admin
RunScript {
RunsWhen = Before
Runs On Failure = Yes
#Runs On Client = Yes
FailJobOnError = Yes
# %d Daemon’s name (Such as host-dir or host-fd)
# %n Job name
# %t Job type (Backup, …)
# %i Job Id
Command = "@PROJECT_BINARY_DIR@/tests/@TEST_NAME@/write.sh @working_dir@/jobid-%i-runscript.log 'daemon=%d' 'jobname=%n' 'jobtype=%t' 'jobid=%i'"
}
}
@@ -0,0 +1,16 @@
Job {
Name = "admin-runscript-server"
JobDefs = "DefaultJob"
Type = Admin
RunScript {
RunsWhen = Before
Runs On Failure = Yes
Runs On Client = No
FailJobOnError = Yes
# %d Daemon’s name (Such as host-dir or host-fd)
# %n Job name
# %t Job type (Backup, …)
# %i Job Id
Command = "@PROJECT_BINARY_DIR@/tests/@TEST_NAME@/write.sh @working_dir@/jobid-%i-runscript.log 'daemon=%d' 'jobname=%n' 'jobtype=%t' 'jobid=%i'"
}
}
@@ -0,0 +1,16 @@
Job {
Name = "backup-bareos-fd-runscript-client-defaults"
JobDefs = "DefaultJob"
Type = Backup
RunScript {
RunsWhen = Before
#Runs On Failure = Yes
#Runs On Client = Yes
#FailJobOnError = Yes
# %d Daemon’s name (Such as host-dir or host-fd)
# %n Job name
# %t Job type (Backup, …)
# %i Job Id
Command = "@PROJECT_BINARY_DIR@/tests/@TEST_NAME@/write.sh @working_dir@/jobid-%i-runscript.log 'daemon=%d' 'jobname=%n' 'jobtype=%t' 'jobid=%i'"
}
}
@@ -0,0 +1,16 @@
Job {
Name = "backup-bareos-fd-runscript-client"
JobDefs = "DefaultJob"
Type = Backup
RunScript {
RunsWhen = Before
Runs On Failure = Yes
Runs On Client = Yes
FailJobOnError = Yes
# %d Daemon’s name (Such as host-dir or host-fd)
# %n Job name
# %t Job type (Backup, …)
# %i Job Id
Command = "@PROJECT_BINARY_DIR@/tests/@TEST_NAME@/write.sh @working_dir@/jobid-%i-runscript.log 'daemon=%d' 'jobname=%n' 'jobtype=%t' 'jobid=%i'"
}
}
@@ -0,0 +1,16 @@
Job {
Name = "backup-bareos-fd-runscript-server"
JobDefs = "DefaultJob"
Type = Backup
RunScript {
RunsWhen = Before
Runs On Failure = Yes
Runs On Client = No
FailJobOnError = Yes
# %d Daemon’s name (Such as host-dir or host-fd)
# %n Job name
# %t Job type (Backup, …)
# %i Job Id
Command = "@PROJECT_BINARY_DIR@/tests/@TEST_NAME@/write.sh @working_dir@/jobid-%i-runscript.log 'daemon=%d' 'jobname=%n' 'jobtype=%t' 'jobid=%i'"
}
}
@@ -0,0 +1,17 @@
JobDefs {
Name = "jobdefs-runscript1"
Type = Admin
Messages = Standard
RunScript {
RunsWhen = Before
Runs On Client = No
FailJobOnError = Yes
# %d Daemon’s name (Such as host-dir or host-fd)
# %n Job name
# %t Job type (Backup, …)
# %i Job Id
Command = "@PROJECT_BINARY_DIR@/tests/@TEST_NAME@/write.sh @working_dir@/jobid-%i-runscript.log 'jobdefs=jobdefs-runscript1' 'daemon=%d' 'jobname=%n' 'jobtype=%t' 'jobid=%i'"
}
Jobdefs = "DefaultJob"
}

@@ -0,0 +1,17 @@
JobDefs {
Name = "jobdefs-runscript2"
Type = Admin
Messages = Standard
RunScript {
RunsWhen = Before
Runs On Client = No
FailJobOnError = Yes
# %d Daemon’s name (Such as host-dir or host-fd)
# %n Job name
# %t Job type (Backup, …)
# %i Job Id
Command = "@PROJECT_BINARY_DIR@/tests/@TEST_NAME@/write.sh @working_dir@/jobid-%i-runscript.log 'jobdefs=jobdefs-runscript2' 'daemon=%d' 'jobname=%n' 'jobtype=%t' 'jobid=%i'"
}
JobDefs = "jobdefs-runscript1"
}

0 comments on commit b2f656e

Please sign in to comment.