Skip to content

Commit

Permalink
Automatically generate, store, and subsequently load a VM UUID in the…
Browse files Browse the repository at this point in the history
… VM's run dir if not specified.
  • Loading branch information
bensallen committed Dec 23, 2019
1 parent fba5087 commit c19ca61
Show file tree
Hide file tree
Showing 26 changed files with 1,000 additions and 8 deletions.
8 changes: 4 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

## Features

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

### Config

- Reorganize internal/config to move VM action logic outside of config, eg. vm/vm.go
- Generate HDD if it doesn't already exist
- Generate UUID if not specified, store in .run/vm/<name>/uuid
- Generate MAC for tap interfaces if not specified, store in .run/vm/<name>/<net>_mac
- Generate MAC for tap interfaces if not specified, store in .run/vm/\<name\>/\<net\>_mac
- Check write privs on run_dir, pid, and tty in Validate()
- Check write privs on hdd, read privs on cdrom
- Add template support for kexec cmdline for IP, ssh public
Expand Down Expand Up @@ -77,4 +75,6 @@ cmd: ifconfig bridge1 192.168.99.1 netmask 0xffffff00
- If one of the tap interfaces doesn't come up, still add the ones that do come up to the bridge
- Make relative paths be relative to the config file.
- Make relative paths in a VM config relative to the run_dir
- Add pid in status output
- Add pid in status output
- Add CI, CircleCI, clean code, etc.
- Generate UUID if not specified, store in .run/vm/\<name\>/uuid
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.12

require (
github.com/BurntSushi/toml v0.3.1
github.com/google/uuid v1.1.1
github.com/integrii/flaggy v1.2.1-0.20190517180110-07ea7eb77404
github.com/kr/pretty v0.1.0
github.com/mitchellh/go-ps v0.0.0-20190716172923-621e5597135b
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/integrii/flaggy v1.2.1-0.20190517180110-07ea7eb77404 h1:RlJzIvFdjZXuVrJqvPkzlaVq6q4AAyrjYYy+exCBWjM=
github.com/integrii/flaggy v1.2.1-0.20190517180110-07ea7eb77404/go.mod h1:tnTxHeTJbah0gQ6/K0RW0J7fMUBk9MCF5blhm43LNpI=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
Expand Down
7 changes: 5 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ func (c *Config) UpdateRelativePaths() {
}

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

for name := range c.VM {
c.VM[name].defaults(configDir, name)
if err := c.VM[name].defaults(configDir, name); err != nil {
return err
}
}
return nil
}
40 changes: 39 additions & 1 deletion internal/config/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"syscall"

"github.com/google/uuid"
"github.com/mitchellh/go-ps"
)

Expand Down Expand Up @@ -188,6 +189,23 @@ func pidFile(path string) (int, error) {
return pid, nil
}

func uuidFile(path string) (uuid.UUID, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return [16]byte{}, fmt.Errorf("uuid file not found")
}
uuidTxt, err := ioutil.ReadFile(path)
if err != nil {
return [16]byte{}, fmt.Errorf("uuid file cannot be read")
}

uuid, err := uuid.Parse(string(uuidTxt))
if err != nil {
return [16]byte{}, fmt.Errorf("uuid file does not contain an UUID")
}

return uuid, nil
}

func (v *VMConfig) Cli() []string {

var args []string
Expand Down Expand Up @@ -266,10 +284,30 @@ func (v *VMConfig) Validate() error {
return nil
}

func (v *VMConfig) defaults(configDir string, name string) {
func (v *VMConfig) defaults(configDir string, name string) error {
if v.RunDir == "" {
v.RunDir = filepath.Join(configDir, ".run/vm/", name)
}

if v.UUID == "" {
UUID, err := uuidFile(v.RunDir + "/uuid")

if err != nil {
UUID = uuid.New()
w, err := os.Create(v.RunDir + "/uuid")
if err != nil {
return err
}

defer w.Close()

if _, err := w.WriteString(UUID.String()); err != nil {
return err
}
}
v.UUID = UUID.String()
}
return nil
}

func (v *VMConfig) updateRelativePaths(configDir string, name string) {
Expand Down
4 changes: 3 additions & 1 deletion internal/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ func Run() error {
return err
}

config.Defaults()
if err := config.Defaults(); err != nil {
return err
}
config.UpdateRelativePaths()

if debug {
Expand Down
9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/google/uuid/CONTRIBUTORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions vendor/github.com/google/uuid/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions vendor/github.com/google/uuid/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions vendor/github.com/google/uuid/dce.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/google/uuid/doc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/github.com/google/uuid/go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions vendor/github.com/google/uuid/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c19ca61

Please sign in to comment.