aboutsummaryrefslogtreecommitdiff
path: root/bbot/machine-manifest.cxx
blob: 8e5100c36b29cbd8956d18ffea8d42c49af82997 (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
// file      : bbot/machine-manifest.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : TBC; see accompanying LICENSE file

#include <bbot/machine-manifest>

#include <butl/manifest-parser>
#include <butl/manifest-serializer>

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)
  {
    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 machine manifest expected");

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

    optional<machine_type> type;

    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 ("machine id redefinition");

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

        id = move (v);
      }
      else if (n == "name")
      {
        if (!name.empty ())
          bad_name ("machine name redefinition");

        if (v.empty ())
          bad_value ("empty machine name");

        name = move (v);
      }
      else if (n == "summary")
      {
        if (!summary.empty ())
          bad_name ("machine summary redefinition");

        if (v.empty ())
          bad_value ("empty machine summary");

        summary = move (v);
      }
      else 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");

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

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

    if (name.empty ())
      bad_value ("no machine name specified");

    if (summary.empty ())
      bad_value ("no machine summary 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?
    //
    s.next ("", "1"); // Start of manifest.
    s.next ("id", id);
    s.next ("name", name);
    s.next ("summary", summary);
    s.next ("type", to_string (type));

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

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

}