Skip to content

Commit c19ca61

Browse files
committed
Automatically generate, store, and subsequently load a VM UUID in the VM's run dir if not specified.
1 parent fba5087 commit c19ca61

File tree

26 files changed

+1000
-8
lines changed

26 files changed

+1000
-8
lines changed

TODO.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
## Features
44

5-
- Add CI, CircleCI, clean code, etc.
65
- Add dependency graph ordering
76

87
### Config
98

109
- Reorganize internal/config to move VM action logic outside of config, eg. vm/vm.go
1110
- Generate HDD if it doesn't already exist
12-
- Generate UUID if not specified, store in .run/vm/<name>/uuid
13-
- Generate MAC for tap interfaces if not specified, store in .run/vm/<name>/<net>_mac
11+
- Generate MAC for tap interfaces if not specified, store in .run/vm/\<name\>/\<net\>_mac
1412
- Check write privs on run_dir, pid, and tty in Validate()
1513
- Check write privs on hdd, read privs on cdrom
1614
- Add template support for kexec cmdline for IP, ssh public
@@ -77,4 +75,6 @@ cmd: ifconfig bridge1 192.168.99.1 netmask 0xffffff00
7775
- If one of the tap interfaces doesn't come up, still add the ones that do come up to the bridge
7876
- Make relative paths be relative to the config file.
7977
- Make relative paths in a VM config relative to the run_dir
80-
- Add pid in status output
78+
- Add pid in status output
79+
- Add CI, CircleCI, clean code, etc.
80+
- Generate UUID if not specified, store in .run/vm/\<name\>/uuid

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.12
44

55
require (
66
github.com/BurntSushi/toml v0.3.1
7+
github.com/google/uuid v1.1.1
78
github.com/integrii/flaggy v1.2.1-0.20190517180110-07ea7eb77404
89
github.com/kr/pretty v0.1.0
910
github.com/mitchellh/go-ps v0.0.0-20190716172923-621e5597135b

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
22
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3+
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
4+
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
35
github.com/integrii/flaggy v1.2.1-0.20190517180110-07ea7eb77404 h1:RlJzIvFdjZXuVrJqvPkzlaVq6q4AAyrjYYy+exCBWjM=
46
github.com/integrii/flaggy v1.2.1-0.20190517180110-07ea7eb77404/go.mod h1:tnTxHeTJbah0gQ6/K0RW0J7fMUBk9MCF5blhm43LNpI=
57
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=

internal/config/config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ func (c *Config) UpdateRelativePaths() {
2222
}
2323

2424
// Defaults sets default values for unset variables in the config.
25-
func (c *Config) Defaults() {
25+
func (c *Config) Defaults() error {
2626
configDir := filepath.Dir(c.Path)
2727

2828
for name := range c.VM {
29-
c.VM[name].defaults(configDir, name)
29+
if err := c.VM[name].defaults(configDir, name); err != nil {
30+
return err
31+
}
3032
}
33+
return nil
3134
}

internal/config/vm.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"syscall"
1313

14+
"github.com/google/uuid"
1415
"github.com/mitchellh/go-ps"
1516
)
1617

@@ -188,6 +189,23 @@ func pidFile(path string) (int, error) {
188189
return pid, nil
189190
}
190191

192+
func uuidFile(path string) (uuid.UUID, error) {
193+
if _, err := os.Stat(path); os.IsNotExist(err) {
194+
return [16]byte{}, fmt.Errorf("uuid file not found")
195+
}
196+
uuidTxt, err := ioutil.ReadFile(path)
197+
if err != nil {
198+
return [16]byte{}, fmt.Errorf("uuid file cannot be read")
199+
}
200+
201+
uuid, err := uuid.Parse(string(uuidTxt))
202+
if err != nil {
203+
return [16]byte{}, fmt.Errorf("uuid file does not contain an UUID")
204+
}
205+
206+
return uuid, nil
207+
}
208+
191209
func (v *VMConfig) Cli() []string {
192210

193211
var args []string
@@ -266,10 +284,30 @@ func (v *VMConfig) Validate() error {
266284
return nil
267285
}
268286

269-
func (v *VMConfig) defaults(configDir string, name string) {
287+
func (v *VMConfig) defaults(configDir string, name string) error {
270288
if v.RunDir == "" {
271289
v.RunDir = filepath.Join(configDir, ".run/vm/", name)
272290
}
291+
292+
if v.UUID == "" {
293+
UUID, err := uuidFile(v.RunDir + "/uuid")
294+
295+
if err != nil {
296+
UUID = uuid.New()
297+
w, err := os.Create(v.RunDir + "/uuid")
298+
if err != nil {
299+
return err
300+
}
301+
302+
defer w.Close()
303+
304+
if _, err := w.WriteString(UUID.String()); err != nil {
305+
return err
306+
}
307+
}
308+
v.UUID = UUID.String()
309+
}
310+
return nil
273311
}
274312

275313
func (v *VMConfig) updateRelativePaths(configDir string, name string) {

internal/root/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ func Run() error {
107107
return err
108108
}
109109

110-
config.Defaults()
110+
if err := config.Defaults(); err != nil {
111+
return err
112+
}
111113
config.UpdateRelativePaths()
112114

113115
if debug {

vendor/github.com/google/uuid/.travis.yml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/uuid/CONTRIBUTING.md

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/uuid/CONTRIBUTORS

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/uuid/LICENSE

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)