hydra/src/hydra-eval-jobs/hydra-eval-jobs.cc

291 lines
9.5 KiB
C++
Raw Normal View History

#include <map>
#include <iostream>
#include <gc/gc_allocator.h>
#include "shared.hh"
#include "store-api.hh"
#include "eval.hh"
#include "eval-inline.hh"
2009-03-05 15:41:43 +00:00
#include "util.hh"
2016-10-06 15:05:05 +02:00
#include "json.hh"
#include "get-drvs.hh"
#include "common-opts.hh"
2014-09-12 14:30:01 +02:00
#include "globals.hh"
using namespace nix;
2009-03-15 11:56:11 +00:00
static Path gcRootsDir;
2010-11-19 11:01:31 +00:00
typedef std::list<Value *, traceable_allocator<Value *> > ValueList;
typedef std::map<Symbol, ValueList> AutoArgs;
2009-03-05 15:41:43 +00:00
static void findJobs(EvalState & state, JSONObject & top,
const AutoArgs & argsLeft, Value & v, const string & attrPath);
static void tryJobAlts(EvalState & state, JSONObject & top,
const AutoArgs & argsLeft, const string & attrPath, Value & fun,
Formals::Formals_::iterator cur,
Formals::Formals_::iterator last,
2014-09-24 15:55:19 +02:00
Bindings & actualArgs) // FIXME: should be const
{
if (cur == last) {
2010-11-19 11:01:31 +00:00
Value v, * arg = state.allocValue();
state.mkAttrs(*arg, 0);
2014-09-24 15:55:19 +02:00
arg->attrs = &actualArgs;
2010-11-19 11:01:31 +00:00
mkApp(v, fun, *arg);
findJobs(state, top, argsLeft, v, attrPath);
return;
}
AutoArgs::const_iterator a = argsLeft.find(cur->name);
Formals::Formals_::iterator next = cur; ++next;
if (a == argsLeft.end()) {
if (!cur->def)
throw TypeError(format("job `%1%' requires an argument named `%2%'")
% attrPath % cur->name);
tryJobAlts(state, top, argsLeft, attrPath, fun, next, last, actualArgs);
return;
}
int n = 0;
2015-07-31 01:45:16 +02:00
for (auto & i : a->second) {
2014-09-24 15:55:19 +02:00
Bindings & actualArgs2(*state.allocBindings(actualArgs.size() + 1)); // !!! inefficient
2015-07-31 01:45:16 +02:00
for (auto & j : actualArgs)
actualArgs2.push_back(j);
AutoArgs argsLeft2(argsLeft);
2015-07-31 01:45:16 +02:00
actualArgs2.push_back(Attr(cur->name, i));
2010-11-19 11:01:31 +00:00
actualArgs2.sort(); // !!! inefficient
argsLeft2.erase(cur->name);
tryJobAlts(state, top, argsLeft2, attrPath, fun, next, last, actualArgs2);
++n;
}
}
static string queryMetaStrings(EvalState & state, DrvInfo & drv, const string & name)
2009-07-07 13:20:00 +00:00
{
Strings res;
std::function<void(Value & v)> rec;
rec = [&](Value & v) {
state.forceValue(v);
if (v.type == tString)
res.push_back(v.string.s);
else if (v.isList())
for (unsigned int n = 0; n < v.listSize(); ++n)
rec(*v.listElems()[n]);
else if (v.type == tAttrs) {
auto a = v.attrs->find(state.symbols.create("shortName"));
if (a != v.attrs->end())
res.push_back(state.forceString(*a->value));
}
};
Value * v = drv.queryMeta(name);
if (v) rec(*v);
return concatStringsSep(", ", res);
}
static void findJobsWrapped(EvalState & state, JSONObject & top,
const AutoArgs & argsLeft, Value & v, const string & attrPath)
2009-03-05 15:41:43 +00:00
{
debug(format("at path `%1%'") % attrPath);
2010-11-19 11:01:31 +00:00
checkInterrupt();
state.forceValue(v);
2009-03-05 15:41:43 +00:00
if (v.type == tAttrs) {
2009-03-05 15:41:43 +00:00
2017-07-21 13:09:08 +02:00
auto drv = getDerivation(state, v, false);
2013-01-22 14:41:02 +01:00
2017-07-21 13:09:08 +02:00
if (drv) {
2009-03-15 11:56:11 +00:00
Path drvPath;
2009-03-05 15:41:43 +00:00
2017-07-21 13:09:08 +02:00
DrvInfo::Outputs outputs = drv->queryOutputs();
2017-07-21 13:09:08 +02:00
if (drv->querySystem() == "unknown")
throw EvalError("derivation must have a system attribute");
2015-03-18 22:03:55 +01:00
{
2016-10-06 15:05:05 +02:00
auto res = top.object(attrPath);
2017-07-21 13:09:08 +02:00
res.attr("nixName", drv->queryName());
res.attr("system", drv->querySystem());
res.attr("drvPath", drvPath = drv->queryDrvPath());
res.attr("description", drv->queryMetaString("description"));
res.attr("license", queryMetaStrings(state, *drv, "license"));
res.attr("homepage", drv->queryMetaString("homepage"));
res.attr("maintainers", queryMetaStrings(state, *drv, "maintainers"));
res.attr("schedulingPriority", drv->queryMetaInt("schedulingPriority", 100));
res.attr("timeout", drv->queryMetaInt("timeout", 36000));
res.attr("maxSilent", drv->queryMetaInt("maxSilent", 7200));
res.attr("isChannel", drv->queryMetaBool("isHydraChannel", false));
/* If this is an aggregate, then get its constituents. */
Bindings::iterator a = v.attrs->find(state.symbols.create("_hydraAggregate"));
2016-10-06 15:05:05 +02:00
if (a != v.attrs->end() && state.forceBool(*a->value, *a->pos)) {
Bindings::iterator a = v.attrs->find(state.symbols.create("constituents"));
if (a == v.attrs->end())
throw EvalError("derivation must have a constituents attribute");
PathSet context;
2014-04-08 17:08:09 +02:00
state.coerceToString(*a->pos, *a->value, context, true, false);
PathSet drvs;
2015-07-31 01:45:16 +02:00
for (auto & i : context)
if (i.at(0) == '!') {
size_t index = i.find("!", 1);
drvs.insert(string(i, index + 1));
}
res.attr("constituents", concatStringsSep(" ", drvs));
}
2009-03-15 11:56:11 +00:00
/* Register the derivation as a GC root. !!! This
registers roots for jobs that we may have already
done. */
2016-10-06 15:05:05 +02:00
auto localStore = state.store.dynamic_pointer_cast<LocalFSStore>();
if (gcRootsDir != "" && localStore) {
Path root = gcRootsDir + "/" + baseNameOf(drvPath);
2016-10-06 15:05:05 +02:00
if (!pathExists(root)) localStore->addPermRoot(drvPath, root, false);
}
2013-01-22 14:41:02 +01:00
2016-10-06 15:05:05 +02:00
auto res2 = res.object("outputs");
for (auto & j : outputs)
res2.attr(j.first, j.second);
2015-03-18 22:03:55 +01:00
}
2009-03-05 15:41:43 +00:00
}
else {
if (!state.isDerivation(v)) {
2015-07-31 01:45:16 +02:00
for (auto & i : *v.attrs)
findJobs(state, top, argsLeft, *i.value,
(attrPath.empty() ? "" : attrPath + ".") + (string) i.name);
}
2009-03-05 15:41:43 +00:00
}
}
else if (v.type == tLambda && v.lambda.fun->matchAttrs) {
2014-09-22 16:47:02 +02:00
Bindings & tmp(*state.allocBindings(0));
tryJobAlts(state, top, argsLeft, attrPath, v,
v.lambda.fun->formals->formals.begin(),
v.lambda.fun->formals->formals.end(),
2014-09-22 16:47:02 +02:00
tmp);
2009-03-05 15:41:43 +00:00
}
else if (v.type == tNull) {
// allow null values, meaning 'do nothing'
}
else
throw TypeError(format("unsupported value: %1%") % v);
}
static void findJobs(EvalState & state, JSONObject & top,
const AutoArgs & argsLeft, Value & v, const string & attrPath)
{
try {
findJobsWrapped(state, top, argsLeft, v, attrPath);
} catch (EvalError & e) {
2015-03-23 12:59:41 +01:00
{
2016-10-06 15:05:05 +02:00
auto res = top.object(attrPath);
res.attr("error", e.msg());
2015-03-23 12:59:41 +01:00
}
}
2009-03-05 15:41:43 +00:00
}
int main(int argc, char * * argv)
{
2012-05-23 14:44:10 -04:00
/* Prevent undeclared dependencies in the evaluation via
$NIX_PATH. */
unsetenv("NIX_PATH");
2013-01-22 14:41:02 +01:00
return handleExceptions(argv[0], [&]() {
initNix();
2015-03-19 20:16:38 +01:00
initGC();
Strings searchPath;
Path releaseExpr;
std::map<string, Strings> autoArgs_;
parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) {
if (*arg == "--arg" || *arg == "--argstr") {
/* This is like --arg in nix-instantiate, except that it
supports multiple versions for the same argument.
That is, autoArgs is a mapping from variable names to
*lists* of values. */
auto what = *arg;
string name = getArg(what, arg, end);
string value = getArg(what, arg, end);
autoArgs_[name].push_back((what == "--arg" ? 'E' : 'S') + value);
}
else if (parseSearchPathArg(arg, end, searchPath))
;
else if (*arg == "--gc-roots-dir")
gcRootsDir = getArg(*arg, arg, end);
2014-09-12 14:30:01 +02:00
else if (*arg == "--dry-run")
settings.readOnlyMode = true;
else if (*arg != "" && arg->at(0) == '-')
return false;
else
releaseExpr = absPath(*arg);
return true;
});
/* FIXME: The build hook in conjunction with import-from-derivation is causing "unexpected EOF" during eval */
settings.useBuildHook = false;
2015-04-14 15:16:00 +02:00
/* Prevent access to paths outside of the Nix search path and
to the environment. */
settings.set("restrict-eval", "true");
if (releaseExpr == "") throw UsageError("no expression specified");
2013-01-22 14:41:02 +01:00
if (gcRootsDir == "") printMsg(lvlError, "warning: `--gc-roots-dir' not specified");
2013-01-22 14:41:02 +01:00
2016-02-11 15:59:47 +01:00
EvalState state(searchPath, openStore());
AutoArgs autoArgs;
Value * inputsSet = state.allocValue();
state.mkAttrs(*inputsSet, autoArgs_.size());
for (auto & i : autoArgs_) {
Symbol inputName = state.symbols.create(i.first);
bool first = true;
for (auto & j : i.second) {
Value * v = state.allocValue();
if (j[0] == 'E')
state.eval(state.parseExprFromString(string(j, 1), absPath(".")), *v);
else
mkString(*v, string(j, 1));
autoArgs[inputName].push_back(v);
if (first) {
2015-08-04 08:12:24 -04:00
inputsSet->attrs->push_back(Attr(inputName, v));
first = false;
}
}
}
Symbol sInputs = state.symbols.create("inputs");
if (autoArgs.find(sInputs) == autoArgs.end()) {
inputsSet->attrs->sort();
autoArgs[sInputs].push_back(inputsSet);
}
2009-03-05 15:41:43 +00:00
Value v;
state.evalFile(releaseExpr, v);
2016-10-06 15:05:05 +02:00
JSONObject json(std::cout, true);
findJobs(state, json, autoArgs, v, "");
state.printStats();
});
}