aboutsummaryrefslogtreecommitdiff
path: root/bbot/agent.cxx
blob: e608af28bd82bfe0f94fe8fd1ee148c3deb116c1 (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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// file      : bbot/agent.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bbot/agent>

#include <limits.h> // PATH_MAX
#include <signal.h> // signal()
#include <unistd.h> // sleep(), realink()

#include <iostream>

#include <butl/pager>
#include <butl/filesystem> // dir_iterator

#include <bbot/manifest>

#include <bbot/types>
#include <bbot/utility>
#include <bbot/diagnostics>

#include <bbot/machine>
#include <bbot/bootstrap-manifest>

namespace bbot
{
  agent_options ops;

  const string bs_prot ("1"); // Bootstrap protocol version.

  string tc_name; // Toolchain name.
  string tc_num;  // Toolchain number.
  string tc_id;   // Toolchain id.
}

using namespace std;
using namespace butl;
using namespace bbot;

// The btrfs tool likes to print informational messages, like "Created
// snapshot such and such". Luckily, it writes them to stdout while proper
// diagnostics to stderr.
//
template <typename... A>
inline void
btrfs (tracer& t, A&&... a)
{
  if (verb >= 3)
    run_io (t, fdnull (), 2, 2, "btrfs", forward<A> (a)...);
  else
    run_io (t, fdnull (), fdnull (), 2, "btrfs", forward<A> (a)...);
}

template <typename... A>
inline butl::process_exit::code_type
btrfs_exit (tracer& t, A&&... a)
{
  return verb >= 3
    ? run_io_exit (t, fdnull (), 2, 2, "btrfs", forward<A> (a)...)
    : run_io_exit (t, fdnull (), fdnull (), 2, "btrfs", forward<A> (a)...);
}

static bootstrapped_machine_manifest
bootstrap_machine (const dir_path& md,
                   const machine_manifest& mm,
                   optional<bootstrapped_machine_manifest> obmm)
{
  bootstrapped_machine_manifest r {
    mm,
    toolchain_manifest {tc_id},
    bootstrap_manifest {
      bootstrap_manifest::versions_type {
        {"bbot",    BBOT_VERSION},
        {"libbbot", LIBBBOT_VERSION},
        {"libbpkg", LIBBPKG_VERSION},
        {"libbutl", LIBBUTL_VERSION}
      }
    }
  };

  if (ops.fake_bootstrap ())
  {
    r.machine.mac = "de:ad:be:ef:de:ad";
  }
  else
  {
    unique_ptr<machine> m (
      start_machine (md,
                     mm,
                     obmm ? obmm->machine.mac : nullopt));

    r.machine.mac = m->mac;

    sleep (10);

    if (!m->shutdown ())
    {
      error << "forcing machine " << md << " down";
      m->forcedown ();
      throw failed ();
    }
  }

  serialize_manifest (r, md / "manifest", "bootstrapped machine");
  return r;
}

static machine_header_manifests
enumerate_machines (const dir_path& rd)
try
{
  tracer trace ("enumerate_machines");

  machine_header_manifests r;

  // The first level are machine volumes.
  //
  for (const dir_entry& ve: dir_iterator (rd))
  {
    const string vn (ve.path ().string ());

    // Ignore hidden directories.
    //
    if (ve.type () != entry_type::directory || vn[0] == '.')
      continue;

    const dir_path vd (dir_path (rd) /= vn);

    // Inside we have machines.
    //
    try
    {
      for (const dir_entry& me: dir_iterator (vd))
      {
        const string mn (me.path ().string ());

        if (me.type () != entry_type::directory || mn[0] == '.')
          continue;

        const dir_path md (dir_path (vd) /= mn);

        // Our endgoal here is to obtain a bootstrapped snapshot of this
        // machine while watching out for potential race conditions (machines
        // being added/upgraded/removed; see the manual for details).
        //
        // So here is our overall plan:
        //
        // 1. Resolve current subvolume link for our bootstrap protocol.
        //
        // 2. If there is no link, cleanup and ignore this machine.
        //
        // 3. Try to create a snapshot of current subvolume (this operation is
        //    atomic). If failed (e.g., someone changed the link and removed
        //    the subvolume in the meantime), retry from #1.
        //
        // 4. Compare the snapshot to the already bootstrapped version (if
        //    any) and see if we need to re-bootstrap. If so, use the snapshot
        //    as a starting point. Rename to bootstrapped at the end (atomic).
        //
        const dir_path lp (dir_path (md) /= (mn + '-' + bs_prot)); // -<P>
        const dir_path tp (dir_path (md) /= (mn + '-' + tc_name)); // -<too...>
        bool te (dir_exists (tp));

        auto delete_t = [&tp, &trace] ()
        {
          btrfs (trace, "property", "set", "-ts", tp, "ro", "false");
          btrfs (trace, "subvolume", "delete", tp);
        };

        for (size_t retry (0);; ++retry)
        {
          if (retry != 0)
            sleep (1);

          // Resolve the link to subvolume path.
          //
          dir_path sp; // <name>-<P>.<R>
          try
          {
            char b [PATH_MAX + 1];
            ssize_t r (readlink (lp.string ().c_str (), b, sizeof (b)));

            if (r == -1)
            {
              if (errno != ENOENT)
                throw_generic_error (errno);
            }
            else if (static_cast<size_t> (r) >= sizeof (b))
              throw_generic_error (EINVAL);
            else
            {
              b[r] = '\0';
              sp = dir_path (b);
              if (sp.relative ())
                sp = md / sp;
            }
          }
          catch (const system_error& e)
          {
            fail << "unable to read subvolume link " << lp << ": " << e;
          }

          // If the resolution fails, then this means there is no current
          // machine subvolume (for this bootstrap protocol). In this case we
          // clean up our toolchain subvolume (<name>-<toolchain>) and ignore
          // this machine.
          //
          if (sp.empty ())
          {
            if (te)
              delete_t ();

            l2 ([&]{trace << "skipping " << md << ": no subvolume link";});
            break;
          }

          // <name>-<toolchain>-<xxx>
          //
          const dir_path xp (dir_path (md) /=
                             path::traits::temp_name (mn + '-' + tc_name));

          if (btrfs_exit (trace, "subvolume", "snapshot", sp, xp) != 0)
          {
            if (retry >= 10)
              fail << "unable to snapshot subvolume " << sp;

            continue;
          }

          // Load the (original) machine manifest.
          //
          auto mm (
            parse_manifest<machine_manifest> (sp / "manifest", "machine"));

          // If we already have <name>-<toolchain>, see if it needs to be re-
          // bootstrapped. Things that render it obsolete:
          //
          // 1. New machine revision  (compare machine ids).
          // 2. New toolchain         (compare toolchain ids).
          // 3. New bbot/libbbot      (compare versions).
          //
          // The last case has a complication: what should we do if we have
          // bootstrapped a newer version of bbot? This would mean that we are
          // about to be stopped and upgraded (and the upgraded version will
          // probably be able to use the result). So we simply ignore this
          // machine for this run.

          // Return -1 if older, 0 if the same, and +1 if newer.
          //
          auto compare_bbot = [] (const bootstrap_manifest& m) -> int
          {
            auto cmp = [&m] (const string& n, uint64_t v) -> int
            {
              auto i = m.versions.find (n);
              return
                i == m.versions.end () || i->second < v
                ? -1
                : i->second > v ? 1 : 0;
            };

            // Start from the top assuming a new dependency cannot be added
            // without changing the dependent's version.
            //
            int r;
            return
              (r = cmp ("bbot",       BBOT_VERSION)) != 0 ? r :
              (r = cmp ("libbbot", LIBBBOT_VERSION)) != 0 ? r :
              (r = cmp ("libbpkg", LIBBPKG_VERSION)) != 0 ? r :
              (r = cmp ("libbutl", LIBBUTL_VERSION)) != 0 ? r : 0;
          };

          optional<bootstrapped_machine_manifest> obmm;
          if (te)
          {
            obmm = parse_manifest<bootstrapped_machine_manifest> (
              tp / "manifest", "bootstrapped machine");

            if (obmm->machine.id != mm.id)
            {
              l2 ([&]{trace << "re-bootstrapping " << tp << ": new machine";});
              te = false;
            }

            if (obmm->toolchain.id != tc_id)
            {
              l2 ([&]{trace << "re-bootstrapping " << tp << ": new toolchain";});
              te = false;
            }

            if (int i = compare_bbot (obmm->bootstrap))
            {
              if (i < 0)
              {
                l2 ([&]{trace << "re-bootstrapping " << tp << ": new bbot";});
                te = false;
              }
              else
              {
                l2 ([&]{trace << "ignoring " << tp << ": old bbot";});
                btrfs (trace, "subvolume", "delete", xp);
                break;
              }
            }

            if (!te)
              delete_t ();
          }
          else
            l2 ([&]{trace << "bootstrapping " << tp;});

          if (!te)
          {
            // Use the <name>-<toolchain>-<xxx> snapshot that we have made to
            // bootstrap the new machine. Then atomically rename it to
            // <name>-<toolchain>.
            //
            bootstrapped_machine_manifest bmm (
              bootstrap_machine (xp, mm, move (obmm)));

            try
            {
              mvdir (xp, tp);
            }
            catch (const system_error& e)
            {
              fail << "unable to rename " << xp << " to " << tp;
            }

            te = true;

            // Check the boostrapped bbot version as above and ignore this
            // machine if it's newer than us.
            //
            if (int i = compare_bbot (bmm.bootstrap))
            {
              assert (i > 0);
              l2 ([&]{trace << "ignoring " << tp << ": old bbot";});
              break;
            }
          }
          else
            btrfs (trace, "subvolume", "delete", xp);

          // Add the machine to the list.
          //
          r.push_back (
            machine_header_manifest (move (mm.id),
                                     move (mm.name),
                                     move (mm.summary)));

          break;
        }
      }
    }
    catch (const system_error& e)
    {
      fail << "unable to iterate over " << vd << ": " << e << endf;
    }
  }

  return r;
}
catch (const system_error& e)
{
  fail << "unable to iterate over " << rd << ": " << e << endf;
}

extern "C" void
handle_signal (int sig)
{
  switch (sig)
  {
  case SIGHUP:  exit (3); // Unimplemented feature.
  case SIGTERM: exit (0);
  default:      assert (false);
  }
}

// Right arrow followed by newline.
//
const char systemd_indent[] = "\xE2\x86\xB2\n";

int
main (int argc, char* argv[])
try
{
  cli::argv_scanner scan (argc, argv, true);
  ops.parse (scan);

  verb = ops.verbose ();

  if (ops.systemd_daemon ())
  {
    // Map to systemd severity prefixes (see sd-daemon(3) for details). Note
    // that here we assume we will never have location (like file name which
    // would end up being before the prefix).
    //
    trace_indent    =
      fail.indent_  =
      error.indent_ =
      warn.indent_  =
      info.indent_  =
      text.indent_  = systemd_indent;

    fail.type_  = "<3>";
    error.type_ = "<3>";
    warn.type_  = "<4>";
    info.type_  = "<6>";
    trace_type  = "<7>";

    info << "bbot agent for " << tc_name << '/' << tc_num <<
      info << "toolchain id " << tc_id <<
      info << "CPU(s)       " << ops.cpu () <<
      info << "RAM(kB)      " << ops.ram ();
  }

  tracer trace ("main");

  // On POSIX ignore SIGPIPE which is signaled to a pipe-writing process if
  // the pipe reading end is closed. Note that by default this signal
  // terminates a process. Also note that there is no way to disable this
  // behavior on a file descriptor basis or for the write() function call.
  //
  if (signal (SIGPIPE, SIG_IGN) == SIG_ERR)
    fail << "unable to ignore broken pipe (SIGPIPE) signal: "
         << system_error (errno, generic_category ()); // Sanitize.

  // Version.
  //
  if (ops.version ())
  {
    cout << "bbot-agent " << BBOT_VERSION_STR << endl
         << "libbbot " << LIBBBOT_VERSION_STR << endl
         << "libbutl " << LIBBUTL_VERSION_STR << endl
         << "Copyright (c) 2014-2017 Code Synthesis Ltd" << endl
         << "MIT; see accompanying LICENSE file" << endl;

    return 0;
  }

  // Help.
  //
  if (ops.help ())
  {
    pager p ("bbot-agent help", false);
    print_bbot_agent_usage (p.stream ());

    // If the pager failed, assume it has issued some diagnostics.
    //
    return p.wait () ? 0 : 1;
  }

  if (argc != 4)
    fail << "toolchain name/id/num excected" <<
      info << "run " << argv[0] << " --help for details";

  tc_name = argv[1];
  tc_num  = argv[2];
  tc_id   = argv[3];

  // Handle SIGHUP and SIGTERM.
  //
  if (signal (SIGHUP,  &handle_signal) == SIG_ERR ||
      signal (SIGTERM, &handle_signal) == SIG_ERR)
    fail << "unable to set signal handler: "
         << system_error (errno, generic_category ()); // Sanitize.

  // The work loop. The steps we go through are:
  //
  // 1. Enumerate the available machines, (re-)bootstrapping any of necessary.
  //
  // 2. Poll controller(s) for build tasks.
  //
  // 3. If no build tasks are available, go to #1 after sleeping a bit.
  //
  // 4. If a build task is returned, do it, upload the result, and go to #1
  //    immediately.
  //
  for (unsigned int s; (s = 60); sleep (s))
  {
    machine_header_manifests mms (enumerate_machines (ops.machines ()));

    if (ops.dump_machines ())
    {
      for (const machine_header_manifest& mm: mms)
        serialize_manifest (mm, cout, "stdout", "machine manifest");

      return 0;
    }
  }
}
catch (const failed&)
{
  return 1; // Diagnostics has already been issued.
}
catch (const cli::exception& e)
{
  error << e;
  return 1;
}