forked from apporbit/libretto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm.go
364 lines (321 loc) · 9.52 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright 2015 Apcera Inc. All rights reserved.
package digitalocean
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"time"
libssh "github.com/apcera/libretto/ssh"
"github.com/apcera/libretto/util"
lvm "github.com/apcera/libretto/virtualmachine"
)
var (
// ErrNoInstanceID is returned when attempting to perform an operation on an instance, but the ID is missing.
ErrNoInstanceID = errors.New("Missing droplet ID")
)
// Base API URL strings
const (
apiBaseURL = "https://api.digitalocean.com"
apiDropletURL = "/v2/droplets"
)
// VM struct represents a full DigitalOcean VM in libretto. It contains the
// droplet itself, along with authentication and SSH credential information. It
// also contains the original creation config.
type VM struct {
APIToken string // required
Credentials libssh.Credentials
Config Config
Droplet *Droplet
}
var _ lvm.VirtualMachine = (*VM)(nil)
// Config is the new droplet payload
type Config struct {
Name string `json:"name,omitempty"` // required
Region string `json:"region,omitempty"` // required
Size string `json:"size,omitempty"` // required
Image string `json:"image,omitempty"` // required
SSHKeys []string `json:"ssh_keys,omitempty"`
Backups bool `json:"backups,omitempty"`
IPv6 bool `json:"ipv6,omitempty"`
PrivateNetworking bool `json:"private_networking,omitempty"`
UserData string `json:"user_data,omitempty"`
}
// DropletsResponse is the API response containing multiple droplets
type DropletsResponse struct {
Droplets []*Droplet `json:"droplets"`
Meta *Meta `json:"meta,omitempty"`
}
// Meta contains metadata from API responses
type Meta struct {
Total int `json:"total,omitempty"`
}
// DropletResponse is the API response containing one droplet
type DropletResponse struct {
Droplet *Droplet `json:"droplet,omitempty"`
}
// Droplet is the base VM type in DigitalOcean
type Droplet struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Region *Region `json:"region,omitempty"`
Image *Image `json:"image,omitempty"`
Size *Size `json:"size,omitempty"`
SizeSlug string `json:"size_slug,omitempty"`
Locked bool `json:"locked,omitempty"`
Status string `json:"status,omitempty"`
Networks *Networks `json:"networks,omitempty"`
Kernel *Kernel `json:"kernel,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
BackupIds []int64 `json:"backup_ids,omitempty"`
SnapshotIds []int64 `json:"snapshot_ids,omitempty"`
ActionIds []int64 `json:"action_ids,omitempty"`
}
// Networks contains information on all droplet networks
type Networks struct {
V4 []*V4Network `json:"v4,omitempty"`
V6 []*V6Network `json:"v6,omitempty"`
}
// Kernel contains droplet kernel information
type Kernel struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Version string `json:"version,omitempty"`
}
// V4Network contains an IPV4 network's values
type V4Network struct {
IPAddress string `json:"ip_address,omitempty"`
Netmask string `json:"netmask,omitempty"`
Gateway string `json:"gateway,omitempty"`
Type string `json:"type,omitempty"`
}
// V6Network contains an IPV6 network's values
type V6Network struct {
IPAddress string `json:"ip_address,omitempty"`
Netmask int64 `json:"netmask,omitempty"`
Gateway string `json:"gateway,omitempty"`
Type string `json:"type,omitempty"`
}
// Region contains droplet region information
type Region struct {
Slug string `json:"slug,omitempty"`
Name string `json:"name,omitempty"`
Sizes []string `json:"sizes,omitempty"`
Available bool `json:"available,omitempty"`
Features []string `json:"features,omitempty"`
}
// Image contains droplet image information
type Image struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Distribution string `json:"distribution,omitempty"`
Slug string `json:"slug,omitempty"`
Public bool `json:"public,omitempty"`
Regions []string `json:"regions,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
}
// Size describes droplet sizing
type Size struct {
Slug string `json:"slug,omitempty"`
Memory int `json:"memory,omitempty"`
VCpus int `json:"v_cpus,omitempty"`
Disk int `json:"disk,omitempty"`
Transfer interface{} `json:"transfer,omitempty"`
PriceMonthley float64 `json:"price_monthley,omitempty"`
PriceHourly float64 `json:"price_hourly,omitempty"`
Regions []string `json:"regions,omitempty"`
}
const (
// StatusOk is the first number of a successful HTTP return code
StatusOk = '2'
// StatusNotFound is the status code for a 404
StatusNotFound = 404
)
// GetName returns the name of the virtual machine
func (vm *VM) GetName() string {
return vm.Config.Name
}
// Provision creates a new VM
func (vm *VM) Provision() error {
b, err := json.Marshal(vm.Config)
if err != nil {
return err
}
client := &http.Client{}
req, err := BuildRequest(vm.APIToken, "POST", apiBaseURL+apiDropletURL, bytes.NewReader(b))
if err != nil {
return err
}
rsp, err := client.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
b, err = ioutil.ReadAll(rsp.Body)
if err != nil {
return err
}
if rsp.Status[0] != StatusOk {
return fmt.Errorf("Error: %s: %s", rsp.Status, string(b))
}
// Fill out vm.Droplet with data on new droplet
r := &DropletResponse{}
err = json.Unmarshal(b, r)
if err != nil {
return err
}
vm.Droplet = r.Droplet
return nil
}
// GetIPs returns a list of ip addresses associated with the VM
func (vm *VM) GetIPs() ([]net.IP, error) {
var ips []net.IP
if err := vm.Update(); err != nil {
return nil, err
}
for _, ip := range vm.Droplet.Networks.V4 {
ips = append(ips, net.ParseIP(ip.IPAddress))
}
for _, ip := range vm.Droplet.Networks.V6 {
ips = append(ips, net.ParseIP(ip.IPAddress))
}
return ips, nil
}
// GetSSH returns an ssh client for the the vm.
func (vm *VM) GetSSH(options libssh.Options) (libssh.Client, error) {
ips, err := util.GetVMIPs(vm, options)
if err != nil {
return nil, err
}
client := libssh.SSHClient{Creds: &vm.Credentials, IP: ips[0], Port: 22, Options: options}
return &client, nil
}
// Destroy powers off the VM and deletes its files from disk
func (vm *VM) Destroy() error {
id := fmt.Sprintf("%v", vm.Droplet.ID)
if id == "" {
return ErrNoInstanceID
}
client := &http.Client{}
req, err := BuildRequest(vm.APIToken, "DELETE", apiBaseURL+apiDropletURL+"/"+id, nil)
if err != nil {
return err
}
rsp, err := client.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return err
}
if rsp.Status[0] != StatusOk {
return fmt.Errorf("Error: %s: %s", rsp.Status, string(b))
}
return nil
}
// GetState gets the running state of the VM through the DigitalOcean API
// Returns droplet state if available and 'not_found' if ID could not be located.
func (vm *VM) GetState() (string, error) {
id := fmt.Sprintf("%v", vm.Droplet.ID)
if id == "" {
return "", ErrNoInstanceID
}
client := &http.Client{}
req, err := BuildRequest(vm.APIToken, "GET", apiBaseURL+apiDropletURL+"/"+id, nil)
if err != nil {
return "", err
}
rsp, err := client.Do(req)
if err != nil {
return "", err
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return "", err
}
if rsp.StatusCode == StatusNotFound {
return "not_found", nil
}
if rsp.Status[0] != StatusOk {
return "", fmt.Errorf("Error: %s: %s", rsp.Status, string(b))
}
// Fill out vm.Droplet with data on droplet
r := &DropletResponse{}
err = json.Unmarshal(b, r)
if err != nil {
return "", err
}
vm.Droplet = r.Droplet
return vm.Droplet.Status, nil
}
// Start powers on the VM
func (vm *VM) Start() error {
id := fmt.Sprintf("%v", vm.Droplet.ID)
if id == "" {
return ErrNoInstanceID
}
client := &http.Client{}
req, err := BuildRequest(vm.APIToken, "POST", apiBaseURL+apiDropletURL+"/"+id+"/actions", strings.NewReader(`{"type": "power_on"}`))
if err != nil {
return err
}
rsp, err := client.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return err
}
if rsp.Status[0] != StatusOk {
return fmt.Errorf("Error: %s: %s", rsp.Status, string(b))
}
return nil
}
// Halt powers off the VM without destroying it
func (vm *VM) Halt() error {
id := fmt.Sprintf("%v", vm.Droplet.ID)
if id == "" {
return ErrNoInstanceID
}
client := &http.Client{}
req, err := BuildRequest(vm.APIToken, "POST", apiBaseURL+apiDropletURL+"/"+id+"/actions", strings.NewReader(`{"type": "power_off"}`))
if err != nil {
return err
}
rsp, err := client.Do(req)
if err != nil {
return err
}
defer rsp.Body.Close()
b, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return err
}
if rsp.Status[0] != StatusOk {
return fmt.Errorf("Error: %s: %s", rsp.Status, string(b))
}
return nil
}
// Suspend always returns an error because this isn't supported by DigitalOcean
func (vm *VM) Suspend() error {
return lvm.ErrSuspendNotSupported
}
// Resume always returns an error because this isn't supported by DigitalOcean
func (vm *VM) Resume() error {
return lvm.ErrResumeNotSupported
}
func (vm *VM) AddDisk() error {
return errors.New("TO-DO")
}
func (vm *VM) RemoveDisk(diskName string) error {
return errors.New("TO-DO")
}