aboutsummaryrefslogtreecommitdiff
path: root/bbot/machine-manifest.cxx
blob: bddf4d5db7d00461a6b57f7f703a820ee28b5a95 (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
// file      : bbot/machine-manifest.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <bbot/machine-manifest.hxx>

#include <sstream>

#include <libbutl/tab-parser.hxx>
#include <libbutl/string-parser.hxx>
#include <libbutl/manifest-parser.hxx>
#include <libbutl/manifest-serializer.hxx>

using namespace std;
using namespace butl;

namespace bbot
{
  using parser = manifest_parser;
  using parsing = manifest_parsing;
  using serializer = manifest_serializer;
  using serialization = manifest_serialization;
  using name_value = manifest_name_value;

  // machine_type
  //
  string
  to_string (machine_type t)
  {
    switch (t)
    {
    case machine_type::kvm:    return "kvm";
    case machine_type::nspawn: return "nspawn";
    }

    assert (false);
    return string ();
  }

  machine_type
  to_machine_type (const string& t)
  {
         if (t == "kvm")    return machine_type::kvm;
    else if (t == "nspawn") return machine_type::nspawn;
    else throw invalid_argument ("invalid machine type '" + t + '\'');
  }

  // machine_manifest
  //
  machine_manifest::
  machine_manifest (parser& p, bool iu)
      : machine_manifest (p, p.next (), iu)
  {
    // Make sure this is the end.
    //
    name_value nv (p.next ());
    if (!nv.empty ())
      throw parsing (p.name (), nv.name_line, nv.name_column,
                     "single machine manifest expected");
  }

  machine_manifest::
  machine_manifest (parser& p, name_value nv, bool iu)
      : machine_header_manifest (p,
                                 move (nv),
                                 manifest_unknown_mode::stop,
                                 &nv)
  {
    auto bad_name = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.name_line, nv.name_column, d);
    };

    // Offsets are used to tie an error to the specific position inside a
    // manifest value (possibly a multiline one).
    //
    auto bad_value = [&p, &nv] (
      const string& d, uint64_t column_offset = 0, uint64_t line_offset = 0)
    {
      throw parsing (p.name (),
                     nv.value_line + line_offset,
                     (line_offset == 0 ? nv.value_column : 1) + column_offset,
                     d);
    };

    optional<machine_type> type;

    for (; !nv.empty (); nv = p.next ())
    {
      string& n (nv.name);
      string& v (nv.value);

      if (n == "type")
      {
        if (type)
          bad_name ("machine type redefinition");

        try
        {
          type = to_machine_type (v);
        }
        catch (const invalid_argument&)
        {
          bad_value ("invalid machine type");
        }
      }
      else if (n == "mac")
      {
        if (mac)
          bad_name ("machine mac redefinition");

        // @@ Should we check that the value is a valid mac?
        //
        mac = move (v);
      }
      else if (n == "options")
      {
        if (options)
          bad_name ("machine options redefinition");

        strings op;

        // Note that when reporting errors we combine the manifest value
        // position with the respective error position.
        //
        try
        {
          istringstream is (v);
          tab_parser parser (is, "");

          tab_fields tl;
          while (!(tl = parser.next ()).empty ())
          {
            for (auto& tf: tl)
              op.emplace_back (move (tf.value));
          }
        }
        catch (const tab_parsing& e)
        {
          bad_value ("invalid machine options: " + e.description,
                     e.column - 1,
                     e.line - 1);
        }

        if (op.empty ())
          bad_value ("empty machine options");

        options = move (op);
      }
      else if (n == "changes")
      {
        if (v.empty ())
          bad_value ("empty machine changes");

        changes.emplace_back (move (v));
      }
      else if (!iu)
        bad_name ("unknown name '" + n + "' in machine manifest");
    }

    // Verify all non-optional values were specified.
    //
    if (!type)
      bad_value ("no machine type specified");

    this->type = *type;
  }

  void machine_manifest::
  serialize (serializer& s) const
  {
    // @@ Should we check that all non-optional values are specified and all
    //    values are valid?
    //

    machine_header_manifest::serialize (s, false);

    s.next ("type", to_string (type));

    if (mac)
      s.next ("mac", *mac);

    // Recompose options string as a space-separated option list,
    //
    if (options)
    {
      string v;
      for (auto b (options->cbegin ()), i (b), e (options->cend ()); i != e;
           ++i)
      {
        if (i != b)
          v += ' ';

        v += *i;
      }

      s.next ("options", v);
    }

    for (const string& c: changes)
      s.next ("changes", c);

    s.next ("", ""); // End of manifest.
  }

  strings machine_manifest::
  unquoted_options () const
  {
    return options
      ? string_parser::unquote (*options)
      : strings ();
  }

  // toolchain_manifest
  //
  toolchain_manifest::
  toolchain_manifest (parser& p, bool iu)
      : toolchain_manifest (p, p.next (), iu)
  {
    // Make sure this is the end.
    //
    name_value nv (p.next ());
    if (!nv.empty ())
      throw parsing (p.name (), nv.name_line, nv.name_column,
                     "single toolchain manifest expected");
  }

  toolchain_manifest::
  toolchain_manifest (parser& p, name_value nv, bool iu)
  {
    auto bad_name = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.name_line, nv.name_column, d);
    };

    auto bad_value = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.value_line, nv.value_column, d);
    };

    // Make sure this is the start and we support the version.
    //
    if (!nv.name.empty ())
      bad_name ("start of toolchain manifest expected");

    if (nv.value != "1")
      bad_value ("unsupported format version");

    // Parse the toolchain manifest.
    //
    for (nv = p.next (); !nv.empty (); nv = p.next ())
    {
      string& n (nv.name);
      string& v (nv.value);

      if (n == "id")
      {
        if (!id.empty ())
          bad_name ("toolchain id redefinition");

        if (v.empty ())
          bad_value ("empty toolchain id");

        id = move (v);
      }
      else if (!iu)
        bad_name ("unknown name '" + n + "' in toolchain manifest");
    }

    // Verify all non-optional values were specified.
    //
    if (id.empty ())
      bad_value ("no toolchain id specified");
  }

  void toolchain_manifest::
  serialize (serializer& s) const
  {
    // @@ Should we check that all non-optional values are specified?
    //
    s.next ("", "1"); // Start of manifest.
    s.next ("id", id);
    s.next ("", ""); // End of manifest.
  }

  // bootstrapped_machine_manifest
  //
  bootstrapped_machine_manifest::
  bootstrapped_machine_manifest (parser& p, bool iu)
  {
    name_value nv (p.next ());

    auto bad_name = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.name_line, nv.name_column, d);
    };

    auto bad_value = [&p, &nv] (const string& d)
    {
      throw parsing (p.name (), nv.value_line, nv.value_column, d);
    };

    // Make sure this is the start and we support the version.
    //
    if (!nv.name.empty ())
      bad_name ("start of bootstrapped machine manifest expected");

    if (nv.value != "1")
      bad_value ("unsupported format version");

    // Parse the bootstrapped machine manifest. Currently there is no values
    // expected.
    //
    for (nv = p.next (); !nv.empty (); nv = p.next ())
    {
      if (!iu)
        bad_name ("unknown name '" + nv.name +
                  "' in bootstrapped machine manifest");
    }

    nv = p.next ();
    if (nv.empty ())
      bad_value ("machine manifest expected");

    machine = machine_manifest (p, nv, iu);

    if (!machine.mac)
      bad_name ("mac address must be present in machine manifest");

    if (machine.effective_role () == machine_role::build)
    {
      nv = p.next ();
      if (nv.empty ())
        bad_value ("toolchain manifest expected");

      toolchain = toolchain_manifest (p, nv, iu);

      nv = p.next ();
      if (nv.empty ())
        bad_value ("bootstrap manifest expected");

      bootstrap = bootstrap_manifest (p, nv, iu);

      // Make sure this is the end.
      //
      nv = p.next ();
      if (!nv.empty ())
        throw parsing (p.name (), nv.name_line, nv.name_column,
                       "single bootstrapped machine manifest expected");
    }
    else
    {
      // Make sure this is the end.
      //
      nv = p.next ();
      if (!nv.empty ())
        throw parsing (p.name (), nv.name_line, nv.name_column,
                       "single machine manifest expected");
    }
  }

  void bootstrapped_machine_manifest::
  serialize (serializer& s) const
  {
    // @@ Should we check that all non-optional values are specified?
    //
    s.next ("", "1"); // Start of manifest.
    s.next ("", "");  // End of manifest.

    if (!machine.mac)
      throw serialization (s.name (),
                           "mac address must be present in machine manifest");

    machine.serialize (s);

    if (machine.effective_role () == machine_role::build)
    {
      toolchain.serialize (s);
      bootstrap.serialize (s);
    }

    s.next ("", ""); // End of stream.
  }
}