1
0
mirror of https://github.com/ohwgiles/laminar.git synced 2024-10-27 20:34:20 +00:00

allow Run::addScript to accept a CWD for scripts

This commit is contained in:
Oliver Giles 2017-08-07 08:06:49 +03:00
parent ae213b4f74
commit 4fc2310ed9
3 changed files with 28 additions and 19 deletions

View File

@ -557,13 +557,13 @@ void Laminar::assignNewJobs() {
if(nodeCanQueue(node, *run)) { if(nodeCanQueue(node, *run)) {
int buildNum = buildNums[run->name] + 1; int buildNum = buildNums[run->name] + 1;
// create a working directory (different to a workspace!) // create the run directory (different to a workspace!)
fs::path wd = fs::path(homeDir)/"run"/run->name/std::to_string(buildNum); fs::path rd = fs::path(homeDir)/"run"/run->name/std::to_string(buildNum);
if(!fs::create_directory(wd)) { if(!fs::create_directory(rd)) {
LLOG(ERROR, "Could not create working directory", wd.string()); LLOG(ERROR, "Could not create working directory", rd.string());
break; break;
} }
run->wd = wd.string(); run->runDir = rd.string();
// create an archive directory // create an archive directory
fs::path archive = fs::path(homeDir)/"archive"/run->name/std::to_string(buildNum); fs::path archive = fs::path(homeDir)/"archive"/run->name/std::to_string(buildNum);
if(fs::is_directory(archive)) { if(fs::is_directory(archive)) {
@ -723,8 +723,8 @@ void Laminar::runFinished(Run * r) {
waiter.release(r->result); waiter.release(r->result);
waiters.erase(r); waiters.erase(r);
// remove the workdir // remove the rundir
fs::remove_all(r->wd); fs::remove_all(r->runDir);
// will delete the job // will delete the job
activeJobs.get<2>().erase(r); activeJobs.get<2>().erase(r);

View File

@ -59,7 +59,7 @@ std::string Run::reason() const {
} }
bool Run::step() { bool Run::step() {
if(!currentScript.empty() && procStatus != 0) if(!currentScript.path.empty() && procStatus != 0)
result = RunState::FAILED; result = RunState::FAILED;
if(scripts.size()) { if(scripts.size()) {
@ -87,7 +87,7 @@ bool Run::step() {
PATH.append(p); PATH.append(p);
} }
chdir(wd.c_str()); chdir(currentScript.cwd.c_str());
for(std::string file : env) { for(std::string file : env) {
StringMap vars = parseConfFile(file.c_str()); StringMap vars = parseConfFile(file.c_str());
@ -108,14 +108,14 @@ bool Run::step() {
for(auto& pair : params) { for(auto& pair : params) {
setenv(pair.first.c_str(), pair.second.c_str(), false); setenv(pair.first.c_str(), pair.second.c_str(), false);
} }
fprintf(stderr, "[laminar] Executing %s\n", currentScript.c_str()); fprintf(stderr, "[laminar] Executing %s\n", currentScript.path.c_str());
execl(currentScript.c_str(), currentScript.c_str(), NULL); execl(currentScript.path.c_str(), currentScript.path.c_str(), NULL);
// cannot use LLOG because stdout/stderr are captured // cannot use LLOG because stdout/stderr are captured
fprintf(stderr, "[laminar] Failed to execute %s\n", currentScript.c_str()); fprintf(stderr, "[laminar] Failed to execute %s\n", currentScript.path.c_str());
_exit(1); _exit(1);
} }
LLOG(INFO, "Forked", currentScript, pid); LLOG(INFO, "Forked", currentScript.path, currentScript.cwd, pid);
close(pfd[1]); close(pfd[1]);
fd = pfd[0]; fd = pfd[0];
this->pid = pid; this->pid = pid;
@ -125,8 +125,8 @@ bool Run::step() {
return true; return true;
} }
} }
void Run::addScript(std::string script) { void Run::addScript(std::string scriptPath, std::string scriptWorkingDir) {
scripts.push(script); scripts.push({scriptPath, scriptWorkingDir});
} }
void Run::addEnv(std::string path) { void Run::addEnv(std::string path) {
env.push_back(path); env.push_back(path);

View File

@ -57,7 +57,10 @@ public:
void complete(); void complete();
// adds a script to the queue of scripts to be executed by this run // adds a script to the queue of scripts to be executed by this run
void addScript(std::string script); void addScript(std::string scriptPath, std::string scriptWorkingDir);
// adds a script to the queue using the runDir as the scripts CWD
void addScript(std::string script) { addScript(script, runDir); }
// adds an environment file that will be sourced before this run // adds an environment file that will be sourced before this run
void addEnv(std::string path); void addEnv(std::string path);
@ -74,7 +77,7 @@ public:
RunState lastResult; RunState lastResult;
std::string laminarHome; std::string laminarHome;
std::string name; std::string name;
std::string wd; std::string runDir;
std::string parentName; std::string parentName;
int parentBuild = 0; int parentBuild = 0;
std::string reasonMsg; std::string reasonMsg;
@ -88,8 +91,14 @@ public:
time_t queuedAt; time_t queuedAt;
time_t startedAt; time_t startedAt;
private: private:
std::queue<std::string> scripts;
std::string currentScript; struct Script {
std::string path;
std::string cwd;
};
std::queue<Script> scripts;
Script currentScript;
std::list<std::string> env; std::list<std::string> env;
}; };