about summary refs log tree commit diff
path: root/src/build-remote/build-remote.cc
blob: fd710c2b6b7135bed093a110c4b6b9f7bb71bbb0 (plain) (blame)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <set>
#include <memory>
#include <tuple>
#include <iomanip>
#if __APPLE__
#include <sys/time.h>
#endif

#include "shared.hh"
#include "pathlocks.hh"
#include "globals.hh"
#include "serialise.hh"
#include "store-api.hh"
#include "derivations.hh"

using namespace nix;
using std::cin;

static void handleAlarm(int sig) {
}

class Machine {
    const std::set<string> supportedFeatures;
    const std::set<string> mandatoryFeatures;

public:
    const string hostName;
    const std::vector<string> systemTypes;
    const string sshKey;
    const unsigned int maxJobs;
    const unsigned int speedFactor;
    bool enabled;

    bool 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 mandatoryMet(const std::set<string> & features) const {
        return std::all_of(mandatoryFeatures.begin(), mandatoryFeatures.end(),
            [&](const string & feature) {
                return features.count(feature);
            });
    }

    Machine(decltype(hostName) hostName,
        decltype(systemTypes) systemTypes,
        decltype(sshKey) sshKey,
        decltype(maxJobs) maxJobs,
        decltype(speedFactor) speedFactor,
        decltype(supportedFeatures) supportedFeatures,
        decltype(mandatoryFeatures) mandatoryFeatures) :
        supportedFeatures(supportedFeatures),
        mandatoryFeatures(mandatoryFeatures),
        hostName(hostName),
        systemTypes(systemTypes),
        sshKey(sshKey),
        maxJobs(maxJobs),
        speedFactor(std::max(1U, speedFactor)),
        enabled(true)
    {};
};;

static std::vector<Machine> readConf()
{
    auto conf = getEnv("NIX_REMOTE_SYSTEMS", SYSCONFDIR "/nix/machines");

    auto machines = std::vector<Machine>{};
    auto lines = std::vector<string>{};
    try {
        lines = tokenizeString<std::vector<string>>(readFile(conf), "\n");
    } catch (const SysError & e) {
        if (e.errNo != ENOENT)
            throw;
    }
    for (auto line : lines) {
        chomp(line);
        line.erase(std::find(line.begin(), line.end(), '#'), line.end());
        if (line.empty()) {
            continue;
        }
        auto tokens = tokenizeString<std::vector<string>>(line);
        auto sz = tokens.size();
        if (sz < 4)
            throw FormatError("bad machines.conf file ‘%1%’", conf);
        machines.emplace_back(tokens[0],
            tokenizeString<std::vector<string>>(tokens[1], ","),
            tokens[2],
            stoull(tokens[3]),
            sz >= 5 ? stoull(tokens[4]) : 1LL,
            sz >= 6 ?
            tokenizeString<std::set<string>>(tokens[5], ",") :
            std::set<string>{},
            sz >= 7 ?
            tokenizeString<std::set<string>>(tokens[6], ",") :
            std::set<string>{});
    }
    return machines;
}

static string currentLoad;

static AutoCloseFD openSlotLock(const Machine & m, unsigned long long slot)
{
    std::ostringstream fn_stream(currentLoad, std::ios_base::ate | std::ios_base::out);
    fn_stream << "/";
    for (auto t : m.systemTypes) {
        fn_stream << t << "-";
    }
    fn_stream << m.hostName << "-" << slot;
    return openLockFile(fn_stream.str(), true);
}

static char display_env[] = "DISPLAY=";
static char ssh_env[] = "SSH_ASKPASS=";

int main (int argc, char * * argv)
{
    return handleExceptions(argv[0], [&]() {
        initNix();

        /* Ensure we don't get any SSH passphrase or host key popups. */
        if (putenv(display_env) == -1 ||
            putenv(ssh_env) == -1)
            throw SysError("setting SSH env vars");

        if (argc != 4)
            throw UsageError("called without required arguments");

        auto store = openStore();

        auto localSystem = argv[1];
        settings.maxSilentTime = stoull(string(argv[2]));
        settings.buildTimeout = stoull(string(argv[3]));

        currentLoad = getEnv("NIX_CURRENT_LOAD", "/run/nix/current-load");

        std::shared_ptr<Store> sshStore;
        AutoCloseFD bestSlotLock;

        auto machines = readConf();
        string drvPath;
        string hostName;
        for (string line; getline(cin, line);) {
            auto tokens = tokenizeString<std::vector<string>>(line);
            auto sz = tokens.size();
            if (sz != 3 && sz != 4)
                throw Error("invalid build hook line ‘%1%’", line);
            auto amWilling = tokens[0] == "1";
            auto neededSystem = tokens[1];
            drvPath = tokens[2];
            auto requiredFeatures = sz == 3 ?
                std::set<string>{} :
                tokenizeString<std::set<string>>(tokens[3], ",");
            auto canBuildLocally = amWilling && (neededSystem == localSystem);

            /* Error ignored here, will be caught later */
            mkdir(currentLoad.c_str(), 0777);

            while (true) {
                bestSlotLock = -1;
                AutoCloseFD lock = openLockFile(currentLoad + "/main-lock", true);
                lockFile(lock.get(), ltWrite, true);

                bool rightType = false;

                Machine * bestMachine = nullptr;
                unsigned long long bestLoad = 0;
                for (auto & m : machines) {
                    if (m.enabled && std::find(m.systemTypes.begin(),
                            m.systemTypes.end(),
                            neededSystem) != m.systemTypes.end() &&
                        m.allSupported(requiredFeatures) &&
                        m.mandatoryMet(requiredFeatures)) {
                        rightType = true;
                        AutoCloseFD free;
                        unsigned long long load = 0;
                        for (unsigned long long slot = 0; slot < m.maxJobs; ++slot) {
                            auto slotLock = openSlotLock(m, slot);
                            if (lockFile(slotLock.get(), ltWrite, false)) {
                                if (!free) {
                                    free = std::move(slotLock);
                                }
                            } else {
                                ++load;
                            }
                        }
                        if (!free) {
                            continue;
                        }
                        bool best = false;
                        if (!bestSlotLock) {
                            best = true;
                        } else if (load / m.speedFactor < bestLoad / bestMachine->speedFactor) {
                            best = true;
                        } else if (load / m.speedFactor == bestLoad / bestMachine->speedFactor) {
                            if (m.speedFactor > bestMachine->speedFactor) {
                                best = true;
                            } else if (m.speedFactor == bestMachine->speedFactor) {
                                if (load < bestLoad) {
                                    best = true;
                                }
                            }
                        }
                        if (best) {
                            bestLoad = load;
                            bestSlotLock = std::move(free);
                            bestMachine = &m;
                        }
                    }
                }

                if (!bestSlotLock) {
                    if (rightType && !canBuildLocally)
                        std::cerr << "# postpone\n";
                    else
                        std::cerr << "# decline\n";
                    break;
                }

#if __APPLE__
                futimes(bestSlotLock.get(), NULL);
#else
                futimens(bestSlotLock.get(), NULL);
#endif

                lock = -1;

                try {
                    sshStore = openStore("ssh://" + bestMachine->hostName + "?ssh-key=" + bestMachine->sshKey);
                    hostName = bestMachine->hostName;
                } catch (std::exception & e) {
                    printError("unable to open SSH connection to ‘%s’: %s; trying other available machines...",
                        bestMachine->hostName, e.what());
                    bestMachine->enabled = false;
                    continue;
                }
                goto connected;
            }
        }

connected:
        std::cerr << "# accept\n";
        string line;
        if (!getline(cin, line))
            throw Error("hook caller didn't send inputs");
        auto inputs = tokenizeString<std::list<string>>(line);
        if (!getline(cin, line))
            throw Error("hook caller didn't send outputs");
        auto outputs = tokenizeString<Strings>(line);
        AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + hostName + ".upload-lock", true);
        auto old = signal(SIGALRM, handleAlarm);
        alarm(15 * 60);
        if (!lockFile(uploadLock.get(), ltWrite, true))
            printError("somebody is hogging the upload lock for ‘%s’, continuing...");
        alarm(0);
        signal(SIGALRM, old);
        copyPaths(store, ref<Store>(sshStore), inputs);
        uploadLock = -1;

        printError("building ‘%s’ on ‘%s’", drvPath, hostName);
        sshStore->buildDerivation(drvPath, readDerivation(drvPath));

        std::remove_if(outputs.begin(), outputs.end(), [=](const Path & path) { return store->isValidPath(path); });
        if (!outputs.empty()) {
            setenv("NIX_HELD_LOCKS", concatStringsSep(" ", outputs).c_str(), 1); /* FIXME: ugly */
            copyPaths(ref<Store>(sshStore), store, outputs);
        }
        return;
    });
}