|
| 1 | +package lxd |
| 2 | + |
| 3 | +import ( |
| 4 | + _ "embed" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "text/template" |
| 9 | + |
| 10 | + "github.com/roots/trellis-cli/trellis" |
| 11 | +) |
| 12 | + |
| 13 | +//go:embed files/config.yml |
| 14 | +var ConfigTemplate string |
| 15 | + |
| 16 | +//go:embed files/inventory.txt |
| 17 | +var inventoryTemplate string |
| 18 | + |
| 19 | +var ( |
| 20 | + ConfigErr = errors.New("Could not write LXD config file") |
| 21 | + IpErr = errors.New("Could not determine IP address for VM") |
| 22 | +) |
| 23 | + |
| 24 | +type Device struct { |
| 25 | + Source string |
| 26 | + Dest string |
| 27 | +} |
| 28 | + |
| 29 | +type NetworkAddress struct { |
| 30 | + Family string `json:"family"` |
| 31 | + Address string `json:"address"` |
| 32 | +} |
| 33 | + |
| 34 | +type Network struct { |
| 35 | + Addresses []NetworkAddress `json:"addresses"` |
| 36 | +} |
| 37 | + |
| 38 | +type State struct { |
| 39 | + Status string `json:"status"` |
| 40 | + Network map[string]Network `json:"network"` |
| 41 | +} |
| 42 | + |
| 43 | +type Instance struct { |
| 44 | + ConfigFile string |
| 45 | + InventoryFile string |
| 46 | + Sites map[string]*trellis.Site |
| 47 | + Name string `json:"name"` |
| 48 | + State State `json:"state"` |
| 49 | + Username string `json:"username,omitempty"` |
| 50 | + Uid int |
| 51 | + Gid int |
| 52 | + SshPublicKey string |
| 53 | + Devices map[string]Device |
| 54 | +} |
| 55 | + |
| 56 | +func (i *Instance) CreateConfig() error { |
| 57 | + tpl := template.Must(template.New("lxc").Parse(ConfigTemplate)) |
| 58 | + |
| 59 | + file, err := os.Create(i.ConfigFile) |
| 60 | + if err != nil { |
| 61 | + return fmt.Errorf("%v: %w", ConfigErr, err) |
| 62 | + } |
| 63 | + |
| 64 | + err = tpl.Execute(file, i) |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("%v: %w", ConfigErr, err) |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +func (i *Instance) CreateInventoryFile() error { |
| 73 | + tpl := template.Must(template.New("lxd").Parse(inventoryTemplate)) |
| 74 | + |
| 75 | + file, err := os.Create(i.InventoryFile) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("Could not create Ansible inventory file: %v", err) |
| 78 | + } |
| 79 | + |
| 80 | + err = tpl.Execute(file, i) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("Could not template Ansible inventory file: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (i *Instance) IP() (ip string, err error) { |
| 89 | + network, ok := i.State.Network["eth0"] |
| 90 | + if !ok { |
| 91 | + return "", fmt.Errorf("%v: eth0 network not found", IpErr) |
| 92 | + } |
| 93 | + |
| 94 | + for _, address := range network.Addresses { |
| 95 | + if address.Family == "inet" && address.Address != "" { |
| 96 | + return address.Address, nil |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return "", fmt.Errorf("%v: inet address family not found", IpErr) |
| 101 | +} |
| 102 | + |
| 103 | +func (i *Instance) Running() bool { |
| 104 | + return i.State.Status == "Running" |
| 105 | +} |
| 106 | + |
| 107 | +func (i *Instance) Stopped() bool { |
| 108 | + return i.State.Status == "Stopped" |
| 109 | +} |
0 commit comments