This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
/
digitalocean.go
460 lines (434 loc) · 10.3 KB
/
digitalocean.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
package digitalocean
import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strconv"
"time"
"context"
"github.com/digitalocean/godo"
"github.com/nuveo/gofn/iaas"
gofnssh "github.com/nuveo/gofn/ssh"
"github.com/nuveo/log"
uuid "github.com/satori/go.uuid"
"golang.org/x/crypto/ssh"
"golang.org/x/oauth2"
)
const (
defaultRegion = "nyc3"
defaultSize = "512mb"
defaultImageSlug = "debian-8-x64"
defaultSnapshotName = "GOFN"
)
var (
// ErrSnapshotNotFound is the error returned if
// ErrorIfSnapshotNotExist is true and there is no snapshot
// with name defined in SnapshotName
ErrSnapshotNotFound = errors.New("snapshot not found")
)
// Digitalocean definition, represents a concrete implementation of an iaas
type Digitalocean struct {
client *godo.Client
Region string
Size string
ImageSlug string
KeyID int
Ctx context.Context
// SnapshotName if not defined GOFN will be used.
SnapshotName string
// ErrorIfSnapshotNotExist if true CreateMachine
// returns error if a snapshot does not exist,
// if false the system will try to create a snapshot,
// defalt false.
ErrorIfSnapshotNotExist bool
sshPublicKeyPath string
sshPrivateKeyPath string
}
// SetSSHPublicKeyPath adjust the system path for the ssh key
// but if the environment variable GOFN_SSH_PUBLICKEY_PATH exists
// the system will use the value contained in the variable instead
// of the one entered in SetSSHPublicKeyPath
func (do *Digitalocean) SetSSHPublicKeyPath(path string) {
do.sshPublicKeyPath = path
}
// SetSSHPrivateKeyPath adjust the system path for the ssh key
// but if the environment variable GOFM_SSH_PRIVATEKEY_PATH exists
// the system will use the value contained in the variable instead
// of the one entered in SetSSHPrivateKeyPath
func (do *Digitalocean) SetSSHPrivateKeyPath(path string) {
do.sshPrivateKeyPath = path
}
// GetSSHPublicKeyPath the path may change according to the
// environment variable GOFN_SSH_PUBLICKEY_PATH or by using
// the SetSSHPublicKeyPath
func (do *Digitalocean) GetSSHPublicKeyPath() (path string) {
path = os.Getenv("GOFN_SSH_PUBLICKEY_PATH")
if path != "" {
return
}
path = do.sshPublicKeyPath
if path != "" {
return
}
do.sshPublicKeyPath = filepath.Join(gofnssh.KeysDir, gofnssh.PublicKeyName)
path = do.sshPublicKeyPath
return
}
// GetSSHPrivateKeyPath the path may change according to the
// environment variable GOFM_SSH_PRIVATEKEY_PATH or by using
// the SetSSHPrivateKeyPath
func (do *Digitalocean) GetSSHPrivateKeyPath() (path string) {
path = os.Getenv("GOFN_SSH_PRIVATEKEY_PATH")
if path != "" {
return
}
path = do.sshPrivateKeyPath
if path != "" {
return
}
do.sshPrivateKeyPath = filepath.Join(gofnssh.KeysDir, gofnssh.PrivateKeyName)
path = do.sshPrivateKeyPath
return
}
// GetSnapshotName returns snapshot name or default if empty
func (do Digitalocean) GetSnapshotName() string {
if do.SnapshotName == "" {
return defaultSnapshotName
}
return do.SnapshotName
}
// GetRegion returns region or default if empty
func (do Digitalocean) GetRegion() string {
if do.Region == "" {
return defaultRegion
}
return do.Region
}
// GetSize returns size or default if empty
func (do Digitalocean) GetSize() string {
if do.Size == "" {
return defaultSize
}
return do.Size
}
// GetImageSlug returns image slug or default if empty
func (do Digitalocean) GetImageSlug() string {
if do.ImageSlug == "" {
return defaultImageSlug
}
return do.ImageSlug
}
// Auth in Digitalocean API
func (do *Digitalocean) Auth() (err error) {
apiURL := os.Getenv("DIGITALOCEAN_API_URL")
key := os.Getenv("DIGITALOCEAN_API_KEY")
if key == "" {
err = errors.New("You must provide a Digital Ocean API Key")
return
}
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: key,
})
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
do.client = godo.NewClient(oauthClient)
if apiURL != "" {
do.client.BaseURL, err = url.Parse(apiURL)
if err != nil {
return
}
}
return
}
// CreateMachine on digitalocean
func (do *Digitalocean) CreateMachine() (machine *iaas.Machine, err error) {
err = do.Auth()
if err != nil {
return
}
lo := godo.ListOptions{
Page: 1,
PerPage: 999999,
}
snapshots, _, err := do.client.Snapshots.List(do.Ctx, &lo)
if err != nil {
return
}
snapshot := godo.Snapshot{}
for _, s := range snapshots {
if s.Name == do.GetSnapshotName() {
snapshot = s
break
}
}
if snapshot.Name == "" && do.ErrorIfSnapshotNotExist {
err = ErrSnapshotNotFound
return
}
image := godo.DropletCreateImage{
Slug: do.GetImageSlug(),
}
if snapshot.Name != "" {
id, _ := strconv.Atoi(snapshot.ID)
image = godo.DropletCreateImage{
ID: id,
}
}
sshKey, err := do.getSSHKeyForDroplet()
if err != nil {
return
}
createRequest := &godo.DropletCreateRequest{
Name: fmt.Sprintf("gofn-%s", uuid.NewV4().String()),
Region: do.GetRegion(),
Size: do.GetSize(),
Image: image,
SSHKeys: []godo.DropletCreateSSHKey{
{
ID: sshKey.ID,
Fingerprint: sshKey.Fingerprint,
},
},
}
newDroplet, _, err := do.client.Droplets.Create(do.Ctx, createRequest)
if err != nil {
return
}
newDroplet, err = do.waitNetworkCreated(newDroplet)
if err != nil {
return
}
ipv4, err := newDroplet.PublicIPv4()
if err != nil {
return
}
machine = &iaas.Machine{
ID: strconv.Itoa(newDroplet.ID),
IP: ipv4,
Image: newDroplet.Image.Slug,
Kind: "digitalocean",
Name: newDroplet.Name,
SSHKeysID: []int{sshKey.ID},
}
cmd := fmt.Sprintf(iaas.RequiredDeps, machine.IP)
if snapshot.Name == "" {
cmd = iaas.OptionalDeps + cmd
}
_, err = do.ExecCommand(machine, cmd)
if err != nil {
return
}
if snapshot.Name == "" {
err = do.CreateSnapshot(machine)
if err != nil {
return
}
}
return
}
func (do *Digitalocean) getSSHKeyForDroplet() (sshKey *godo.Key, err error) {
// Use a key that is already in DO if exist KeyID
if do.KeyID != 0 {
sshKey, _, err = do.client.Keys.GetByID(do.Ctx, do.KeyID)
if err != nil {
return
}
return
}
sshFilePath := do.GetSSHPublicKeyPath()
if sshFilePath == "" {
path := filepath.Join(gofnssh.KeysDir, gofnssh.PublicKeyName)
if !existsKey(path) {
if err = gofnssh.GenerateFNSSHKey(4096); err != nil {
return
}
}
sshFilePath = path
}
content, err := ioutil.ReadFile(sshFilePath)
if err != nil {
return
}
fingerPrint, err := gofnssh.GenerateFingerPrint(string(content))
if err != nil {
return
}
sshKey, _, err = do.client.Keys.GetByFingerprint(do.Ctx, fingerPrint)
if err != nil {
sshKeyRequestCreate := &godo.KeyCreateRequest{
Name: "GOFN",
PublicKey: string(content),
}
sshKey, _, err = do.client.Keys.Create(do.Ctx, sshKeyRequestCreate)
if err != nil {
return
}
}
return
}
// DeleteMachine Shutdown and Delete a droplet
func (do *Digitalocean) DeleteMachine(machine *iaas.Machine) (err error) {
id, _ := strconv.Atoi(machine.ID)
err = do.Auth()
if err != nil {
return
}
action, _, err := do.client.DropletActions.Shutdown(do.Ctx, id)
if err != nil {
log.Println(err)
// Power off force Shutdown
action, _, err = do.client.DropletActions.PowerOff(do.Ctx, id)
if err != nil {
return
}
}
timeout := 120
quit := make(chan struct{})
errs := make(chan error, 1)
ac := make(chan *godo.Action, 1)
go func() {
for {
//running shutdown...
select {
case <-quit:
return
default:
var d *godo.Action
d, _, err = do.client.DropletActions.Get(do.Ctx, id, action.ID)
if err != nil {
errs <- err
return
}
if d.Status == "completed" {
ac <- d
return
}
}
}
}()
select {
case action = <-ac:
_, err = do.client.Droplets.Delete(do.Ctx, id)
return
case err = <-errs:
return
case <-time.After(time.Duration(timeout) * time.Second):
err = errors.New("timed out waiting for Snhutdown")
return
}
}
// CreateSnapshot Create a snapshot from the machine
func (do *Digitalocean) CreateSnapshot(machine *iaas.Machine) (err error) {
id, _ := strconv.Atoi(machine.ID)
err = do.Auth()
if err != nil {
return
}
action, _, err := do.client.DropletActions.Snapshot(do.Ctx, id, do.GetSnapshotName())
if err != nil {
return
}
timeout := 600
quit := make(chan struct{})
errs := make(chan error, 1)
ac := make(chan *godo.Action, 1)
go func() {
for {
//"running snapshot..."
select {
case <-quit:
return
default:
var d *godo.Action
d, _, err = do.client.DropletActions.Get(do.Ctx, id, action.ID)
if err != nil {
errs <- err
return
}
if d.Status == "completed" {
ac <- d
return
}
}
}
}()
select {
case action = <-ac:
return
case err = <-errs:
return
case <-time.After(time.Duration(timeout) * time.Second):
err = errors.New("timed out waiting for Snapshot")
return
}
}
// ExecCommand on droplet
func (do *Digitalocean) ExecCommand(machine *iaas.Machine, cmd string) (output []byte, err error) {
pkPath := do.GetSSHPrivateKeyPath()
// TODO: dynamic user
sshConfig := &ssh.ClientConfig{
User: "root",
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Auth: []ssh.AuthMethod{
gofnssh.PublicKeyFile(pkPath),
},
Timeout: time.Duration(10) * time.Second,
}
err = gofnssh.ProbeConnection(machine.IP, iaas.MediumRetry)
if err != nil {
return
}
connection, err := ssh.Dial("tcp", machine.IP+gofnssh.Port, sshConfig)
if err != nil {
return
}
session, err := connection.NewSession()
if err != nil {
return
}
output, err = session.CombinedOutput(cmd)
if err != nil {
fmt.Println(string(output))
return
}
return
}
func (do *Digitalocean) waitNetworkCreated(droplet *godo.Droplet) (upDroplet *godo.Droplet, err error) {
timeout := 120
quit := make(chan struct{})
errs := make(chan error, 1)
droplets := make(chan *godo.Droplet, 1)
go func() {
for {
//wait for network
select {
case <-quit:
return
default:
d, _, err := do.client.Droplets.Get(do.Ctx, droplet.ID)
if err != nil {
errs <- err
return
}
if len(d.Networks.V4) > 0 && !d.Locked {
droplets <- d
return
}
}
}
}()
select {
case upDroplet = <-droplets:
return upDroplet, nil
case err := <-errs:
return nil, err
case <-time.After(time.Duration(timeout) * time.Second):
return nil, errors.New("timed out waiting for machine network")
}
}
func existsKey(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}