///
/// Copyright 2016 Oliver Giles
///
/// This file is part of Laminar
///
/// Laminar is free software: you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation, either version 3 of the License, or
/// (at your option) any later version.
///
/// Laminar is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with Laminar. If not, see
///
#include "laminar.capnp.h"
#include
#include
#include
#include
#include
#define EFAILED 55
template
static int setParams(int argc, char** argv, T& request) {
int n = 0;
for(int i = 0; i < argc; ++i) {
if(strchr(argv[i], '=') == NULL)
break;
n++;
}
int argsConsumed = n;
char* job = getenv("lJobName");
char* num = getenv("lBuildNum");
char* reason = getenv("LAMINAR_REASON");
if(job && num) n+=2;
else if(reason) n++;
if(n == 0) return argsConsumed;
auto params = request.initParams(n);
for(int i = 0; i < argsConsumed; ++i) {
char* name = argv[i];
char* val = strchr(name, '=');
*val++ = '\0';
params[i].setName(name);
params[i].setValue(val);
}
if(job && num) {
params[argsConsumed].setName("=parentJob");
params[argsConsumed].setValue(job);
params[argsConsumed+1].setName("=parentBuild");
params[argsConsumed+1].setValue(num);
} else if(reason) {
params[argsConsumed].setName("=reason");
params[argsConsumed].setValue(reason);
}
return argsConsumed;
}
int main(int argc, char** argv) {
// TODO: pass this through an enviroment variable set by laminard
const char* address = "unix:\0laminar";
if(argc < 2) {
fprintf(stderr, "Usage: %s [parameters...]\n", argv[0]);
return EINVAL;
}
int ret = 0;
capnp::EzRpcClient client(address);
LaminarCi::Client laminar = client.getMain();
auto& waitScope = client.getWaitScope();
if(strcmp(argv[1], "trigger") == 0) {
if(argc < 3) {
fprintf(stderr, "Usage %s trigger \n", argv[0]);
return EINVAL;
}
kj::Vector> promises;
int jobNameIndex = 2;
// make a request for each job specified on the commandline
do {
auto req = laminar.triggerRequest();
req.setJobName(argv[jobNameIndex]);
int n = setParams(argc - jobNameIndex - 1, &argv[jobNameIndex + 1], req);
promises.add(req.send());
jobNameIndex += n + 1;
} while(jobNameIndex < argc);
// pend on the promises
for(auto& p : promises) {
if(p.wait(waitScope).getResult() != LaminarCi::MethodResult::SUCCESS) {
fprintf(stderr, "Failed to queue job '%s'\n", argv[2]);
return ENOENT;
}
}
} else if(strcmp(argv[1], "start") == 0) {
if(argc < 3) {
fprintf(stderr, "Usage %s start \n", argv[0]);
return EINVAL;
}
kj::Vector> promises;
int jobNameIndex = 2;
// make a request for each job specified on the commandline
do {
auto req = laminar.startRequest();
req.setJobName(argv[jobNameIndex]);
int n = setParams(argc - jobNameIndex - 1, &argv[jobNameIndex + 1], req);
promises.add(req.send());
jobNameIndex += n + 1;
} while(jobNameIndex < argc);
// pend on the promises
for(auto& p : promises) {
if(p.wait(waitScope).getResult() != LaminarCi::JobResult::SUCCESS) {
ret = EFAILED;
}
}
} else if(strcmp(argv[1], "set") == 0) {
if(argc < 3) {
fprintf(stderr, "Usage %s set param=value\n", argv[0]);
return EINVAL;
}
auto req = laminar.setRequest();
char* eq = strchr(argv[2], '=');
char* job = getenv("lJobName");
char* num = getenv("lBuildNum");
if(job && num && eq) {
char* name = argv[2];
*eq++ = '\0';
char* val = eq;
req.setJobName(job);
req.setBuildNum(atoi(num));
req.getParam().setName(name);
req.getParam().setValue(val);
req.send().wait(waitScope);
} else {
fprintf(stderr, "Missing lJobName and lBuildNum or param is not in the format key=value\n");
return EINVAL;
}
} else if(strcmp(argv[1], "wait") == 0) {
auto req = laminar.pendRequest();
req.setJobName(argv[2]);
req.setBuildNum(atoi(argv[3]));
auto response = req.send().wait(waitScope);
if(response.getResult() != LaminarCi::JobResult::SUCCESS)
return EFAILED;
} else {
fprintf(stderr, "Unknown comand %s\n", argv[1]);
return EINVAL;
}
return ret;
}