-
Notifications
You must be signed in to change notification settings - Fork 2
/
machine.go
113 lines (94 loc) · 2.94 KB
/
machine.go
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
package machina
import "github.com/gentlemanautomaton/machina/summary"
// Machine describes an individual virtual machine in machina. It contains
// identity information, tags, and a definition.
//
// If tags are present, the machine's definition can be merged with its tag
// definitions through use of the Build function.
//
// The Machine structure is intended to be marshaled to and from JSON. It
// defines the format of files in the machina.conf.d directory.
type Machine struct {
Name MachineName `json:"name,omitempty"`
Description string `json:"description,omitempty"`
ID MachineID `json:"id,omitempty"`
Tags []Tag `json:"tags,omitempty"`
Definition
}
// Info returns copy of the machine's entity information by itself.
func (m Machine) Info() MachineInfo {
return MachineInfo{
ID: m.ID,
Description: m.Description,
Name: m.Name,
}
}
// Summary returns a multiline string summarizing the machine configuration.
func (m Machine) Summary() string {
var out summary.Builder
out.Descend()
if m.Name != "" {
out.Add("Name: %s", m.Name)
}
if m.Description != "" {
out.Add("Description: %s", m.Description)
}
if !m.ID.IsZero() {
out.Add("ID: %s", m.ID)
}
if len(m.Tags) > 0 {
out.StartLine()
for i, tag := range m.Tags {
if i == 0 {
out.Printf("Tags: %s", tag)
} else {
out.Printf(",%s", tag)
}
}
}
m.Definition.Config(m.Info(), &out)
return out.String()
}
// MachineInfo holds identifying information for a machine.
type MachineInfo struct {
ID MachineID
Description string
Name MachineName
}
// Seed returns an identity generation seed for the machine info.
func (info MachineInfo) Seed() Seed {
return Seed{info: info}
}
// Vars returns a set of identifying machine variables. These can be used
// as variables for expansion.
func (info MachineInfo) Vars() Vars {
return Vars{
"machine-name": string(info.Name),
"machine-description": info.Description,
"machine-id": info.ID.String(),
}
}
// MachineName is the name of a machina virtual machine.
//
// TODO: Document restrictions on machine names and add validity checks.
// These names are used in various places when generating QEMU arguments.
// Spaces in particular could lead to argument parsing badness.
type MachineName string
// MachineID is a universally unique identifer for a machine.
type MachineID UUID
// IsZero returns true if the machine ID holds a zero value.
func (m MachineID) IsZero() bool {
return m == MachineID{}
}
// String returns a string representation of the machine ID.
func (m MachineID) String() string {
return UUID(m).String()
}
// MarshalText implements the encoding.TextMarshaler interface.
func (m MachineID) MarshalText() (text []byte, err error) {
return UUID(m).MarshalText()
}
// UnmarshalText implements the encoding.TextUnmarshaler interface.
func (m *MachineID) UnmarshalText(text []byte) error {
return (*UUID)(m).UnmarshalText(text)
}