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

230 lines
8.3 KiB
C++
Raw Normal View History

2015-09-13 20:25:26 +00:00
///
/// Copyright 2015-2018 Oliver Giles
2015-09-13 20:25:26 +00:00
///
/// 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 <http://www.gnu.org/licenses/>
///
#include "laminar.capnp.h"
#include <capnp/ez-rpc.h>
#include <kj/vector.h>
#include <stdio.h>
#include <stdlib.h>
2019-04-11 18:00:00 +00:00
#define EXIT_BAD_ARGUMENT 1
#define EXIT_OPERATION_FAILED 2
#define EXIT_RUN_FAILED 3
2015-09-13 20:25:26 +00:00
2019-07-04 09:28:33 +00:00
// Definition needed for musl
typedef unsigned int uint;
2015-09-13 20:25:26 +00:00
template<typename T>
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++;
}
char* job = getenv("JOB");
char* num = getenv("RUN");
2015-09-13 20:25:26 +00:00
char* reason = getenv("LAMINAR_REASON");
auto params = request.initParams(n + (job&&num?2:0) + (reason?1:0));
2015-09-13 20:25:26 +00:00
for(int i = 0; i < n; ++i) {
2015-09-13 20:25:26 +00:00
char* name = argv[i];
char* val = strchr(name, '=');
*val++ = '\0';
params[i].setName(name);
params[i].setValue(val);
}
int argsConsumed = n;
2015-09-13 20:25:26 +00:00
if(job && num) {
params[n].setName("=parentJob");
params[n++].setValue(job);
params[n].setName("=parentBuild");
params[n++].setValue(num);
}
if(reason) {
params[n].setName("=reason");
params[n].setValue(reason);
2015-09-13 20:25:26 +00:00
}
return argsConsumed;
}
static void printTriggerLink(const char* job, uint run) {
// use a private ANSI CSI sequence to mark the JOB:NUM so the
// frontend can recognise it and generate a hyperlink.
printf("\033[{%s:%d\033\\\n", job, run);
}
2015-09-13 20:25:26 +00:00
int main(int argc, char** argv) {
if(argc < 2) {
fprintf(stderr, "Usage: %s <command> [parameters...]\n", argv[0]);
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
2015-09-13 20:25:26 +00:00
}
int ret = 0;
2017-08-15 05:41:53 +00:00
const char* address = getenv("LAMINAR_HOST") ?: getenv("LAMINAR_BIND_RPC") ?: "unix-abstract:laminar";
2015-09-13 20:25:26 +00:00
capnp::EzRpcClient client(address);
LaminarCi::Client laminar = client.getMain<LaminarCi>();
auto& waitScope = client.getWaitScope();
2018-05-12 14:56:56 +00:00
if(strcmp(argv[1], "queue") == 0) {
2015-09-13 20:25:26 +00:00
if(argc < 3) {
2018-05-12 14:56:56 +00:00
fprintf(stderr, "Usage %s queue <jobName>\n", argv[0]);
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
2015-09-13 20:25:26 +00:00
}
2018-05-12 14:56:56 +00:00
kj::Vector<capnp::RemotePromise<LaminarCi::QueueResults>> promises;
2015-09-13 20:25:26 +00:00
int jobNameIndex = 2;
// make a request for each job specified on the commandline
do {
2018-05-12 14:56:56 +00:00
auto req = laminar.queueRequest();
2015-09-13 20:25:26 +00:00
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]);
2019-04-11 18:00:00 +00:00
return EXIT_OPERATION_FAILED;
2015-09-13 20:25:26 +00:00
}
}
2018-05-12 14:56:56 +00:00
} else if(strcmp(argv[1], "start") == 0 || strcmp(argv[1], "trigger") == 0) {
if(strcmp(argv[1], "trigger") == 0)
fprintf(stderr, "Warning: 'trigger' is deprecated, use 'queue' for the old behavior\n");
2015-09-13 20:25:26 +00:00
if(argc < 3) {
2018-05-12 14:56:56 +00:00
fprintf(stderr, "Usage %s queue <jobName>\n", argv[0]);
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
2015-09-13 20:25:26 +00:00
}
2018-05-12 14:56:56 +00:00
kj::Vector<capnp::RemotePromise<LaminarCi::StartResults>> promises;
struct: public kj::TaskSet::ErrorHandler {
void taskFailed(kj::Exception&&) override {}
} ignoreFailed;
kj::TaskSet ts(ignoreFailed);
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);
ts.add(req.send().then([&ret,argv,jobNameIndex](capnp::Response<LaminarCi::StartResults> resp){
if(resp.getResult() != LaminarCi::MethodResult::SUCCESS) {
fprintf(stderr, "Failed to start job '%s'\n", argv[2]);
2019-04-11 18:00:00 +00:00
ret = EXIT_OPERATION_FAILED;
2018-05-12 14:56:56 +00:00
}
printTriggerLink(argv[jobNameIndex], resp.getBuildNum());
2018-05-12 14:56:56 +00:00
}));
jobNameIndex += n + 1;
} while(jobNameIndex < argc);
ts.onEmpty().wait(waitScope);
} else if(strcmp(argv[1], "run") == 0) {
if(argc < 3) {
fprintf(stderr, "Usage %s run <jobName>\n", argv[0]);
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
2018-04-06 10:43:38 +00:00
}
struct: public kj::TaskSet::ErrorHandler {
void taskFailed(kj::Exception&&) override {}
} ignoreFailed;
kj::TaskSet ts(ignoreFailed);
2015-09-13 20:25:26 +00:00
int jobNameIndex = 2;
// make a request for each job specified on the commandline
do {
2018-04-06 10:43:38 +00:00
auto req = laminar.runRequest();
2015-09-13 20:25:26 +00:00
req.setJobName(argv[jobNameIndex]);
int n = setParams(argc - jobNameIndex - 1, &argv[jobNameIndex + 1], req);
2018-04-06 10:43:38 +00:00
ts.add(req.send().then([&ret,argv,jobNameIndex](capnp::Response<LaminarCi::RunResults> resp){
printTriggerLink(argv[jobNameIndex], resp.getBuildNum());
if(resp.getResult() != LaminarCi::JobResult::SUCCESS) {
2019-04-11 18:00:00 +00:00
ret = EXIT_RUN_FAILED;
}
}));
2015-09-13 20:25:26 +00:00
jobNameIndex += n + 1;
} while(jobNameIndex < argc);
ts.onEmpty().wait(waitScope);
2015-09-13 20:25:26 +00:00
} else if(strcmp(argv[1], "set") == 0) {
if(argc < 3) {
fprintf(stderr, "Usage %s set param=value\n", argv[0]);
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
2015-09-13 20:25:26 +00:00
}
auto req = laminar.setRequest();
char* eq = strchr(argv[2], '=');
char* job = getenv("JOB");
char* num = getenv("RUN");
2015-09-13 20:25:26 +00:00
if(job && num && eq) {
char* name = argv[2];
*eq++ = '\0';
char* val = eq;
req.getRun().setJob(job);
req.getRun().setBuildNum(atoi(num));
2015-09-13 20:25:26 +00:00
req.getParam().setName(name);
req.getParam().setValue(val);
req.send().wait(waitScope);
} else {
fprintf(stderr, "Missing $JOB or $RUN or param is not in the format key=value\n");
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
2015-09-13 20:25:26 +00:00
}
} else if(strcmp(argv[1], "abort") == 0) {
if(argc != 4) {
fprintf(stderr, "Usage %s abort <jobName> <jobNumber>\n", argv[0]);
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
}
auto req = laminar.abortRequest();
req.getRun().setJob(argv[2]);
req.getRun().setBuildNum(atoi(argv[3]));
if(req.send().wait(waitScope).getResult() != LaminarCi::MethodResult::SUCCESS)
2019-04-11 18:00:00 +00:00
ret = EXIT_OPERATION_FAILED;
} else if(strcmp(argv[1], "show-jobs") == 0) {
if(argc != 2) {
fprintf(stderr, "Usage: %s show-jobs\n", argv[0]);
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
}
for(auto it : laminar.listKnownRequest().send().wait(waitScope).getResult()) {
printf("%s\n", it.cStr());
}
} else if(strcmp(argv[1], "show-queued") == 0) {
if(argc != 2) {
fprintf(stderr, "Usage: %s show-queued\n", argv[0]);
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
}
for(auto it : laminar.listQueuedRequest().send().wait(waitScope).getResult()) {
printf("%s\n", it.cStr());
}
} else if(strcmp(argv[1], "show-running") == 0) {
if(argc != 2) {
fprintf(stderr, "Usage: %s show-running\n", argv[0]);
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
}
for(auto it : laminar.listRunningRequest().send().wait(waitScope).getResult()) {
printf("%s:%d\n", it.getJob().cStr(), it.getBuildNum());
}
2015-09-13 20:25:26 +00:00
} else {
2017-11-07 00:37:21 +00:00
fprintf(stderr, "Unknown command %s\n", argv[1]);
2019-04-11 18:00:00 +00:00
return EXIT_BAD_ARGUMENT;
2015-09-13 20:25:26 +00:00
}
return ret;
2015-09-13 20:25:26 +00:00
}