aboutsummaryrefslogtreecommitdiff
path: root/src/libstore/machines.cc
blob: 9843ccf045e751be1eded5a2a0da5b989123feb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "machines.hh"
#include "util.hh"
#include "globals.hh"
#include "store-api.hh"

#include <algorithm>

namespace nix {

Machine::Machine(decltype(storeUri) storeUri,
    decltype(systemTypes) systemTypes,
    decltype(sshKey) sshKey,
    decltype(maxJobs) maxJobs,
    decltype(speedFactor) speedFactor,
    decltype(supportedFeatures) supportedFeatures,
    decltype(mandatoryFeatures) mandatoryFeatures,
    decltype(sshPublicHostKey) sshPublicHostKey) :
    storeUri(
        // Backwards compatibility: if the URI is schemeless, is not a path,
        // and is not one of the special store connection words, prepend
        // ssh://.
        storeUri.find("://") != std::string::npos
        || storeUri.find("/") != std::string::npos
        || storeUri == "auto"
        || storeUri == "daemon"
        || storeUri == "local"
        || hasPrefix(storeUri, "auto?")
        || hasPrefix(storeUri, "daemon?")
        || hasPrefix(storeUri, "local?")
        || hasPrefix(storeUri, "?")
        ? storeUri
        : "ssh://" + storeUri),
    systemTypes(systemTypes),
    sshKey(sshKey),
    maxJobs(maxJobs),
    speedFactor(std::max(1U, speedFactor)),
    supportedFeatures(supportedFeatures),
    mandatoryFeatures(mandatoryFeatures),
    sshPublicHostKey(sshPublicHostKey)
{}

bool Machine::allSupported(const std::set<string> & features) const {
    return std::all_of(features.begin(), features.end(),
        [&](const string & feature) {
            return supportedFeatures.count(feature) ||
                mandatoryFeatures.count(feature);
        });
}

bool Machine::mandatoryMet(const std::set<string> & features) const {
    return std::all_of(mandatoryFeatures.begin(), mandatoryFeatures.end(),
        [&](const string & feature) {
            return features.count(feature);
        });
}

ref<Store> Machine::openStore() const {
    Store::Params storeParams;
    if (hasPrefix(storeUri, "ssh://")) {
        storeParams["max-connections"] = "1";
        storeParams["log-fd"] = "4";
    }

    if (hasPrefix(storeUri, "ssh://") || hasPrefix(storeUri, "ssh-ng://")) {
        if (sshKey != "")
            storeParams["ssh-key"] = sshKey;
        if (sshPublicHostKey != "")
            storeParams["base64-ssh-public-host-key"] = sshPublicHostKey;
    }

    {
        auto & fs = storeParams["system-features"];
        auto append = [&](auto feats) {
            for (auto & f : feats) {
                if (fs.size() > 0) fs += ' ';
                fs += f;
            }
        };
        append(supportedFeatures);
        append(mandatoryFeatures);
    }

    return nix::openStore(storeUri, storeParams);
}

void parseMachines(const std::string & s, Machines & machines)
{
    for (auto line : tokenizeString<std::vector<string>>(s, "\n;")) {
        trim(line);
        line.erase(std::find(line.begin(), line.end(), '#'), line.end());
        if (line.empty()) continue;

        if (line[0] == '@') {
            auto file = trim(std::string(line, 1));
            try {
                parseMachines(readFile(file), machines);
            } catch (const SysError & e) {
                if (e.errNo != ENOENT)
                    throw;
                debug("cannot find machines file '%s'", file);
            }
            continue;
        }

        auto tokens = tokenizeString<std::vector<string>>(line);
        auto sz = tokens.size();
        if (sz < 1)
            throw FormatError("bad machine specification '%s'", line);

        auto isSet = [&](size_t n) {
            return tokens.size() > n && tokens[n] != "" && tokens[n] != "-";
        };

        machines.emplace_back(tokens[0],
            isSet(1) ? tokenizeString<std::vector<string>>(tokens[1], ",") : std::vector<string>{settings.thisSystem},
            isSet(2) ? tokens[2] : "",
            isSet(3) ? std::stoull(tokens[3]) : 1LL,
            isSet(4) ? std::stoull(tokens[4]) : 1LL,
            isSet(5) ? tokenizeString<std::set<string>>(tokens[5], ",") : std::set<string>{},
            isSet(6) ? tokenizeString<std::set<string>>(tokens[6], ",") : std::set<string>{},
            isSet(7) ? tokens[7] : "");
    }
}

Machines getMachines()
{
    static auto machines = [&]() {
        Machines machines;
        parseMachines(settings.builders, machines);
        return machines;
    }();
    return machines;
}

}