-
Notifications
You must be signed in to change notification settings - Fork 20
/
vm.go
160 lines (132 loc) · 3.38 KB
/
vm.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
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
package cmd
import (
"bytes"
"compress/gzip"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
const (
defaultServiceOffering = "medium"
maxUserDataLength = 32768
)
var vmCmd = &cobra.Command{
Use: "vm",
Short: "Compute instances management",
PersistentPreRun: func(_ *cobra.Command, _ []string) {
fmt.Fprintln(os.Stderr,
`**********************************************************************
The "exo vm" commands are deprecated and will be removed in a future
version, please use "exo compute instance" replacement commands.
**********************************************************************`)
},
}
func getVirtualMachineByNameOrID(name string) (*egoscale.VirtualMachine, error) {
vmQuery := egoscale.VirtualMachine{}
id, err := egoscale.ParseUUID(name)
if err != nil {
vmQuery.Name = name
} else {
vmQuery.ID = id
}
vms, err := cs.ListWithContext(gContext, vmQuery)
if err != nil {
return nil, err
}
var vm *egoscale.VirtualMachine
switch len(vms) {
case 0:
return nil, fmt.Errorf("Compute instance %q not found", name) // nolint
case 1:
vm = vms[0].(*egoscale.VirtualMachine)
default:
names := []string{}
for _, i := range vms {
v := i.(*egoscale.VirtualMachine)
if v.Name != vmQuery.Name {
continue
}
vm = v
names = append(names, fmt.Sprintf("\t%s\t%s\t%s", v.ID.String(), v.ZoneName, v.IP()))
}
if len(names) == 1 {
break
}
return nil, errors.New("multiple Compute instances found, specify an ID instead")
}
return vm, nil
}
func getKeyPairPath(vmID string) string {
return path.Join(gConfigFolder, "instances", vmID, "id_rsa")
}
func saveKeyPair(keyPairs *egoscale.SSHKeyPair, vmID egoscale.UUID) {
filePath := getKeyPairPath(vmID.String())
folder := path.Dir(filePath)
if _, err := os.Stat(folder); os.IsNotExist(err) {
if err := os.MkdirAll(folder, os.ModePerm); err != nil {
log.Fatal(err)
}
}
if _, err := os.Stat(filePath); os.IsNotExist(err) {
if err := ioutil.WriteFile(filePath, []byte(keyPairs.PrivateKey), 0o600); err != nil {
log.Fatalf("SSH private key could not be written: %s", err)
}
}
}
func getUserDataFromFile(path string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", err
}
userData, err := encodeUserData(data)
if err != nil {
return "", err
}
if len(userData) >= maxUserDataLength {
return "", fmt.Errorf("user-data maximum allowed length is %d bytes", maxUserDataLength)
}
return userData, nil
}
func encodeUserData(data []byte) (string, error) {
b := new(bytes.Buffer)
gz := gzip.NewWriter(b)
if _, err := gz.Write(data); err != nil {
return "", err
}
if err := gz.Flush(); err != nil {
return "", err
}
if err := gz.Close(); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b.Bytes()), nil
}
func decodeUserData(data string) (string, error) {
base64Decoded, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return "", err
}
gz, err := gzip.NewReader(bytes.NewReader(base64Decoded))
if err != nil {
// User data are not compressed, returning as-is.
if errors.Is(err, gzip.ErrHeader) {
return string(base64Decoded), nil
}
return "", err
}
defer gz.Close()
userData, err := ioutil.ReadAll(gz)
if err != nil {
return "", err
}
return string(userData), nil
}
func init() {
RootCmd.AddCommand(vmCmd)
}