This repository has been archived by the owner on May 7, 2021. It is now read-only.
/
api.go
635 lines (561 loc) · 16.4 KB
/
api.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Copyright 2017 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package packet
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"strings"
"time"
ignition "github.com/coreos/ignition/config/v2_0/types"
"github.com/coreos/pkg/capnslog"
"github.com/packethost/packngo"
"golang.org/x/crypto/ssh"
"golang.org/x/net/context"
gs "google.golang.org/api/storage/v1"
"github.com/coreos/mantle/auth"
"github.com/coreos/mantle/platform"
"github.com/coreos/mantle/platform/api/gcloud"
"github.com/coreos/mantle/platform/conf"
"github.com/coreos/mantle/storage"
"github.com/coreos/mantle/util"
)
const (
// Provisioning a VM is supposed to take < 8 minutes, but in practice can take longer.
launchTimeout = 10 * time.Minute
launchPollInterval = 30 * time.Second
installTimeout = 15 * time.Minute
installPollInterval = 5 * time.Second
apiRetries = 3
apiRetryInterval = 5 * time.Second
)
var (
plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "platform/api/packet")
defaultInstallerImageBaseURL = map[string]string{
// HTTPS causes iPXE to fail on a "permission denied" error
"amd64-usr": "http://stable.release.core-os.net/amd64-usr/current",
"arm64-usr": "http://beta.release.core-os.net/arm64-usr/current",
}
defaultImageURL = map[string]string{
"amd64-usr": "https://alpha.release.core-os.net/amd64-usr/current/coreos_production_packet_image.bin.bz2",
"arm64-usr": "https://alpha.release.core-os.net/arm64-usr/current/coreos_production_packet_image.bin.bz2",
}
defaultPlan = map[string]string{
"amd64-usr": "baremetal_0",
"arm64-usr": "baremetal_2a",
}
linuxConsole = map[string]string{
"amd64-usr": "ttyS1,115200",
"arm64-usr": "ttyAMA0,115200",
}
)
type Options struct {
*platform.Options
// Config file. Defaults to $HOME/.config/packet.json.
ConfigPath string
// Profile name
Profile string
// API key (overrides config profile)
ApiKey string
// Project UUID (overrides config profile)
Project string
// Packet location code
Facility string
// Slug of the device type (e.g. "baremetal_0")
Plan string
// The Container Linux board name
Board string
// e.g. http://alpha.release.core-os.net/amd64-usr/current
InstallerImageBaseURL string
// e.g. https://alpha.release.core-os.net/amd64-usr/current/coreos_production_packet_image.bin.bz2
ImageURL string
// Options for Google Storage
GSOptions *gcloud.Options
// Google Storage base URL for temporary uploads
// e.g. gs://users.developer.core-os.net/bovik/mantle
StorageURL string
}
type API struct {
c *packngo.Client
bucket *storage.Bucket
opts *Options
}
type Console interface {
io.WriteCloser
SSHClient(ip, user string) (*ssh.Client, error)
}
func New(opts *Options) (*API, error) {
if opts.ApiKey == "" || opts.Project == "" {
profiles, err := auth.ReadPacketConfig(opts.ConfigPath)
if err != nil {
return nil, fmt.Errorf("couldn't read Packet config: %v", err)
}
if opts.Profile == "" {
opts.Profile = "default"
}
profile, ok := profiles[opts.Profile]
if !ok {
return nil, fmt.Errorf("no such profile %q", opts.Profile)
}
if opts.ApiKey == "" {
opts.ApiKey = profile.ApiKey
}
if opts.Project == "" {
opts.Project = profile.Project
}
}
_, ok := linuxConsole[opts.Board]
if !ok {
return nil, fmt.Errorf("unknown board %q", opts.Board)
}
if opts.Plan == "" {
opts.Plan = defaultPlan[opts.Board]
}
if opts.InstallerImageBaseURL == "" {
opts.InstallerImageBaseURL = defaultInstallerImageBaseURL[opts.Board]
}
if opts.ImageURL == "" {
opts.ImageURL = defaultImageURL[opts.Board]
}
gapi, err := gcloud.New(opts.GSOptions)
if err != nil {
return nil, fmt.Errorf("connecting to Google Storage: %v", err)
}
bucket, err := storage.NewBucket(gapi.Client(), opts.StorageURL)
if err != nil {
return nil, fmt.Errorf("connecting to Google Storage bucket: %v", err)
}
client := packngo.NewClient("github.com/coreos/mantle", opts.ApiKey, nil)
return &API{
c: client,
bucket: bucket,
opts: opts,
}, nil
}
func (a *API) PreflightCheck() error {
_, _, err := a.c.Projects.Get(a.opts.Project)
if err != nil {
return fmt.Errorf("querying project %v: %v", a.opts.Project, err)
}
return nil
}
// console is optional, and is closed on error or when the device is deleted.
func (a *API) CreateDevice(hostname string, conf *conf.Conf, console Console) (*packngo.Device, error) {
consoleStarted := false
defer func() {
if console != nil && !consoleStarted {
console.Close()
}
}()
userdata, err := a.wrapUserData(conf)
if err != nil {
return nil, err
}
// The Ignition config can't go in userdata via coreos.config.url=https://metadata.packet.net/userdata because Ignition supplies an Accept header that metadata.packet.net finds 406 Not Acceptable.
// It can't go in userdata via coreos.oem.id=packet because the Packet OEM expects unit files in /usr/share/oem which the PXE image doesn't have.
userdataName, userdataURL, err := a.uploadObject(hostname, "application/vnd.coreos.ignition+json", []byte(userdata))
if err != nil {
return nil, err
}
defer a.bucket.Delete(context.TODO(), userdataName)
// This can't go in userdata because the installed coreos-cloudinit will try to execute it.
ipxeScriptName, ipxeScriptURL, err := a.uploadObject(hostname, "application/octet-stream", []byte(a.ipxeScript(userdataURL)))
if err != nil {
return nil, err
}
defer a.bucket.Delete(context.TODO(), ipxeScriptName)
device, err := a.createDevice(hostname, ipxeScriptURL)
if err != nil {
return nil, fmt.Errorf("couldn't create device: %v", err)
}
deviceID := device.ID
if console != nil {
err := a.startConsole(deviceID, console)
consoleStarted = true
if err != nil {
a.DeleteDevice(deviceID)
return nil, err
}
}
device, err = a.waitForActive(deviceID)
if err != nil {
a.DeleteDevice(deviceID)
return nil, err
}
ipAddress := a.GetDeviceAddress(device, 4, true)
if ipAddress == "" {
a.DeleteDevice(deviceID)
return nil, fmt.Errorf("no public IP address found for %v", deviceID)
}
err = waitForInstall(ipAddress)
if err != nil {
a.DeleteDevice(deviceID)
return nil, fmt.Errorf("timed out waiting for coreos-install: %v", err)
}
return device, nil
}
func (a *API) DeleteDevice(deviceID string) error {
_, err := a.c.Devices.Delete(deviceID)
if err != nil {
return fmt.Errorf("deleting device %q: %v", deviceID, err)
}
return nil
}
func (a *API) GetDeviceAddress(device *packngo.Device, family int, public bool) string {
for _, address := range device.Network {
if address.AddressFamily == family && address.Public == public {
return address.Address
}
}
return ""
}
func (a *API) AddKey(name, key string) (string, error) {
sshKey, _, err := a.c.SSHKeys.Create(&packngo.SSHKeyCreateRequest{
Label: name,
Key: key,
})
if err != nil {
return "", fmt.Errorf("couldn't create SSH key: %v", err)
}
return sshKey.ID, nil
}
func (a *API) DeleteKey(keyID string) error {
_, err := a.c.SSHKeys.Delete(keyID)
if err != nil {
return fmt.Errorf("couldn't delete SSH key: %v", err)
}
return nil
}
func (a *API) ListKeys() ([]packngo.SSHKey, error) {
keys, _, err := a.c.SSHKeys.List()
if err != nil {
return nil, fmt.Errorf("couldn't list SSH keys: %v", err)
}
return keys, nil
}
func (a *API) wrapUserData(conf *conf.Conf) (string, error) {
userDataOption := "-i"
if !conf.IsIgnition() && conf.String() != "" {
// By providing a no-op Ignition config, we prevent Ignition
// from enabling oem-cloudinit.service, which is unordered
// with respect to the cloud-config installed by the -c
// option. Otherwise it might override settings in the
// cloud-config with defaults obtained from the Packet
// metadata endpoint.
userDataOption = "-i /noop.ign -c"
}
escapedImageURL := strings.Replace(a.opts.ImageURL, "%", "%%", -1)
// make systemd units
discardSocketUnit := `
[Unit]
Description=Discard Socket
[Socket]
ListenStream=0.0.0.0:9
Accept=true
[Install]
WantedBy=multi-user.target
`
discardServiceUnit := `
[Unit]
Description=Discard Service
Requires=discard.socket
[Service]
ExecStart=/usr/bin/cat
StandardInput=socket
StandardOutput=null
`
installUnit := fmt.Sprintf(`
[Unit]
Description=Install Container Linux
Requires=network-online.target
After=network-online.target
Requires=dev-sda.device
After=dev-sda.device
[Service]
Type=oneshot
# Prevent coreos-install from validating cloud-config
Environment=PATH=/root/bin:/usr/sbin:/usr/bin
ExecStart=/usr/bin/curl -fo image.bin.bz2 "%v"
# We don't verify signatures because the iPXE script isn't verified either
# (and, in fact, is transferred over HTTP)
ExecStart=/usr/bin/coreos-install -d /dev/sda -f image.bin.bz2 %v /userdata
ExecStart=/usr/bin/systemctl --no-block isolate reboot.target
StandardOutput=journal+console
StandardError=journal+console
[Install]
RequiredBy=multi-user.target
`, escapedImageURL, userDataOption)
// make workarounds
noopIgnitionConfig := base64.StdEncoding.EncodeToString([]byte(`{"ignition": {"version": "2.1.0"}}`))
coreosCloudInit := base64.StdEncoding.EncodeToString([]byte("#!/bin/sh\nexit 0"))
// make Ignition config
b64UserData := base64.StdEncoding.EncodeToString(conf.Bytes())
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(ignition.Config{
Ignition: ignition.Ignition{
Version: ignition.IgnitionVersion{Major: 2},
},
Storage: ignition.Storage{
Files: []ignition.File{
ignition.File{
Filesystem: "root",
Path: "/userdata",
Contents: ignition.FileContents{
Source: ignition.Url{
Scheme: "data",
Opaque: ";base64," + b64UserData,
},
},
Mode: 0644,
},
ignition.File{
Filesystem: "root",
Path: "/noop.ign",
Contents: ignition.FileContents{
Source: ignition.Url{
Scheme: "data",
Opaque: ";base64," + noopIgnitionConfig,
},
},
Mode: 0644,
},
ignition.File{
Filesystem: "root",
Path: "/root/bin/coreos-cloudinit",
Contents: ignition.FileContents{
Source: ignition.Url{
Scheme: "data",
Opaque: ";base64," + coreosCloudInit,
},
},
Mode: 0755,
},
},
},
Systemd: ignition.Systemd{
Units: []ignition.SystemdUnit{
ignition.SystemdUnit{
// don't appear to be running while install is in progress
Name: "sshd.socket",
Mask: true,
},
ignition.SystemdUnit{
// future-proofing
Name: "sshd.service",
Mask: true,
},
ignition.SystemdUnit{
// allow remote detection of install in progress
Name: "discard.socket",
Enable: true,
Contents: discardSocketUnit,
},
ignition.SystemdUnit{
Name: "discard@.service",
Contents: discardServiceUnit,
},
ignition.SystemdUnit{
Name: "coreos-install.service",
Enable: true,
Contents: installUnit,
},
},
},
})
if err != nil {
return "", fmt.Errorf("encoding Ignition config: %v", err)
}
return buf.String(), nil
}
func (a *API) uploadObject(hostname, contentType string, data []byte) (string, string, error) {
if hostname == "" {
hostname = "mantle"
}
b := make([]byte, 5)
rand.Read(b)
name := fmt.Sprintf("%s-%x", hostname, b)
obj := gs.Object{
Name: a.bucket.Prefix() + name,
ContentType: contentType,
}
err := a.bucket.Upload(context.TODO(), &obj, bytes.NewReader(data))
if err != nil {
return "", "", fmt.Errorf("uploading object: %v", err)
}
// HTTPS causes iPXE to fail on a "permission denied" error
url := fmt.Sprintf("http://storage-download.googleapis.com/%v/%v", a.bucket.Name(), obj.Name)
return obj.Name, url, nil
}
func (a *API) ipxeScript(userdataURL string) string {
return fmt.Sprintf(`#!ipxe
set base-url %s
kernel ${base-url}/coreos_production_pxe.vmlinuz initrd=coreos_production_pxe_image.cpio.gz coreos.first_boot=1 coreos.config.url=%s console=%s
initrd ${base-url}/coreos_production_pxe_image.cpio.gz
boot`, strings.TrimRight(a.opts.InstallerImageBaseURL, "/"), userdataURL, linuxConsole[a.opts.Board])
}
// device creation seems a bit flaky, so try a few times
func (a *API) createDevice(hostname, ipxeScriptURL string) (device *packngo.Device, err error) {
for tries := apiRetries; tries >= 0; tries-- {
var response *packngo.Response
device, response, err = a.c.Devices.Create(&packngo.DeviceCreateRequest{
ProjectID: a.opts.Project,
Facility: a.opts.Facility,
Plan: a.opts.Plan,
BillingCycle: "hourly",
Hostname: hostname,
OS: "custom_ipxe",
IPXEScriptURL: ipxeScriptURL,
Tags: []string{"mantle"},
})
if err == nil || response.StatusCode != 500 {
return
}
if tries > 0 {
time.Sleep(apiRetryInterval)
}
}
return
}
func (a *API) startConsole(deviceID string, console Console) error {
ready := make(chan error)
runner := func() error {
defer console.Close()
client, err := console.SSHClient("sos."+a.opts.Facility+".packet.net", deviceID)
if err != nil {
return fmt.Errorf("couldn't create SSH client for %s console: %v", deviceID, err)
}
defer client.Close()
session, err := client.NewSession()
if err != nil {
return fmt.Errorf("couldn't create SSH session for %s console: %v", deviceID, err)
}
defer session.Close()
reader, writer := io.Pipe()
defer writer.Close()
session.Stdin = reader
session.Stdout = console
if err := session.Shell(); err != nil {
return fmt.Errorf("couldn't start shell for %s console: %v", deviceID, err)
}
// cause startConsole to return
ready <- nil
err = session.Wait()
_, ok := err.(*ssh.ExitMissingError)
if err != nil && !ok {
plog.Errorf("%s console session failed: %v", deviceID, err)
}
return nil
}
go func() {
err := runner()
if err != nil {
ready <- err
}
}()
return <-ready
}
func (a *API) waitForActive(deviceID string) (*packngo.Device, error) {
var device *packngo.Device
err := util.WaitUntilReady(launchTimeout, launchPollInterval, func() (bool, error) {
var err error
device, _, err = a.c.Devices.Get(deviceID)
if err != nil {
return false, fmt.Errorf("querying device: %v", err)
}
return device.State == "active", nil
})
if err != nil {
return nil, err
}
return device, nil
}
// Connect to the discard port and wait for the connection to close,
// indicating that install is complete.
func waitForInstall(address string) (err error) {
deadline := time.Now().Add(installTimeout)
dialer := net.Dialer{
Timeout: installPollInterval,
}
for tries := installTimeout / installPollInterval; tries >= 0; tries-- {
var conn net.Conn
start := time.Now()
conn, err = dialer.Dial("tcp", address+":9")
if err == nil {
defer conn.Close()
conn.SetDeadline(deadline)
_, err = conn.Read([]byte{0})
if err == io.EOF {
err = nil
}
return
}
if tries > 0 {
// If Dial returned an error before the timeout,
// e.g. because the device returned ECONNREFUSED,
// wait out the rest of the interval.
time.Sleep(installPollInterval - time.Now().Sub(start))
}
}
return
}
func (a *API) GC(gracePeriod time.Duration) error {
threshold := time.Now().Add(-gracePeriod)
page := packngo.ListOptions{
Page: 1,
PerPage: 1000,
}
for {
devices, _, err := a.c.Devices.List(a.opts.Project, &page)
if err != nil {
return fmt.Errorf("listing devices: %v", err)
}
for _, device := range devices {
tagged := false
for _, tag := range device.Tags {
if tag == "mantle" {
tagged = true
break
}
}
if !tagged {
continue
}
switch device.State {
case "queued", "provisioning":
continue
}
if device.Locked {
continue
}
created, err := time.Parse(time.RFC3339, device.Created)
if err != nil {
return fmt.Errorf("couldn't parse %q: %v", device.Created, err)
}
if created.After(threshold) {
continue
}
if err := a.DeleteDevice(device.ID); err != nil {
return fmt.Errorf("couldn't delete device %v: %v", device.ID, err)
}
}
if len(devices) < page.PerPage {
return nil
}
page.Page += 1
}
}