/
cloudlaunch.go
445 lines (401 loc) · 11.7 KB
/
cloudlaunch.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
/*
Copyright 2015 The Camlistore Authors
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 cloudlaunch helps binaries run themselves on The Cloud, copying
// themselves to GCE.
package cloudlaunch
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"time"
"go4.org/cloud/google/gceutil"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
compute "google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
storageapi "google.golang.org/api/storage/v1"
"google.golang.org/cloud"
"google.golang.org/cloud/storage"
)
func readFile(v string) string {
slurp, err := ioutil.ReadFile(v)
if err != nil {
log.Fatalf("Error reading %s: %v", v, err)
}
return strings.TrimSpace(string(slurp))
}
const baseConfig = `#cloud-config
coreos:
update:
group: stable
reboot-strategy: off
units:
- name: $NAME.service
command: start
content: |
[Unit]
Description=$NAME service
After=network.target
[Service]
Type=simple
ExecStartPre=/bin/sh -c 'mkdir -p /opt/bin && /usr/bin/curl --silent -f -o /opt/bin/$NAME $URL?$(date +%s) && chmod +x /opt/bin/$NAME'
ExecStart=/opt/bin/$NAME
RestartSec=10
Restart=always
StartLimitInterval=0
[Install]
WantedBy=network-online.target
`
// RestartPolicy controls whether the binary automatically restarts.
type RestartPolicy int
const (
RestartOnUpdates RestartPolicy = iota
RestartNever
// TODO: more graceful restarts; make systemd own listening on network sockets,
// don't break connections.
)
type Config struct {
// Name is the name of a service to run.
// This is the name of the systemd service (without .service)
// and the name of the GCE instance.
Name string
// RestartPolicy controls whether the binary automatically restarts
// on updates. The zero value means automatic.
RestartPolicy RestartPolicy
// BinaryBucket and BinaryObject are the GCS bucket and object
// within that bucket containing the Linux binary to download
// on boot and occasionally run. This binary must be public
// (at least for now).
BinaryBucket string
BinaryObject string // defaults to Name
GCEProjectID string
Zone string // defaults to us-central1-f
SSD bool
Scopes []string // any additional scopes
MachineType string
InstanceName string
}
// cloudLaunch is a launch of a Config.
type cloudLaunch struct {
*Config
oauthClient *http.Client
computeService *compute.Service
}
func (c *Config) binaryURL() string {
return "https://storage.googleapis.com/" + c.BinaryBucket + "/" + c.binaryObject()
}
func (c *Config) instName() string { return c.Name } // for now
func (c *Config) zone() string { return strDefault(c.Zone, "us-central1-f") }
func (c *Config) machineType() string { return strDefault(c.MachineType, "g1-small") }
func (c *Config) binaryObject() string { return strDefault(c.BinaryObject, c.Name) }
func (c *Config) projectAPIURL() string {
return "https://www.googleapis.com/compute/v1/projects/" + c.GCEProjectID
}
func (c *Config) machineTypeURL() string {
return c.projectAPIURL() + "/zones/" + c.zone() + "/machineTypes/" + c.machineType()
}
func strDefault(a, b string) string {
if a != "" {
return a
}
return b
}
var (
doLaunch = flag.Bool("cloudlaunch", false, "Deploy or update this binary to the cloud. Must be on Linux, for now.")
)
func (c *Config) MaybeDeploy() {
flag.Parse()
if !*doLaunch {
go c.restartLoop()
return
}
defer os.Exit(1) // backup, in case we return without Fatal or os.Exit later
if runtime.GOOS != "linux" || runtime.GOARCH != "amd64" {
log.Fatal("Can only use --cloudlaunch on linux/amd64, for now.")
}
if c.GCEProjectID == "" {
log.Fatal("cloudconfig.GCEProjectID is empty")
}
filename := filepath.Join(os.Getenv("HOME"), "keys", c.GCEProjectID+".key.json")
log.Printf("Using OAuth config from JSON service file: %s", filename)
jwtConf, err := google.JWTConfigFromJSON([]byte(readFile(filename)), append([]string{
storageapi.DevstorageFullControlScope,
compute.ComputeScope,
"https://www.googleapis.com/auth/cloud-platform",
}, c.Scopes...)...)
if err != nil {
log.Fatalf("ConfigFromJSON: %v", err)
}
cl := &cloudLaunch{
Config: c,
oauthClient: jwtConf.Client(oauth2.NoContext),
}
cl.computeService, _ = compute.New(cl.oauthClient)
cl.uploadBinary()
cl.createInstance()
os.Exit(0)
}
func (c *Config) restartLoop() {
if c.RestartPolicy == RestartNever {
return
}
url := "https://storage.googleapis.com/" + c.BinaryBucket + "/" + c.binaryObject()
var lastEtag string
for {
res, err := http.Head(url + "?" + fmt.Sprint(time.Now().Unix()))
if err != nil {
log.Printf("Warning: %v", err)
time.Sleep(15 * time.Second)
continue
}
etag := res.Header.Get("Etag")
if etag == "" {
log.Printf("Warning, no ETag in response: %v", res)
time.Sleep(15 * time.Second)
continue
}
if lastEtag != "" && etag != lastEtag {
log.Printf("Binary updated; restarting.")
// TODO: more graceful restart, letting systemd own the network connections.
// Then we can finish up requests here.
os.Exit(0)
}
lastEtag = etag
time.Sleep(15 * time.Second)
}
}
// uploadBinary uploads the currently-running Linux binary.
// It crashes if it fails.
func (cl *cloudLaunch) uploadBinary() {
ctx := context.Background()
if cl.BinaryBucket == "" {
log.Fatal("cloudlaunch: Config.BinaryBucket is empty")
}
stoClient, err := storage.NewClient(ctx, cloud.WithBaseHTTP(cl.oauthClient))
if err != nil {
log.Fatal(err)
}
w := stoClient.Bucket(cl.BinaryBucket).Object(cl.binaryObject()).NewWriter(ctx)
if err != nil {
log.Fatal(err)
}
w.ACL = []storage.ACLRule{
// If you don't give the owners access, the web UI seems to
// have a bug and doesn't have access to see that it's public, so
// won't render the "Shared Publicly" link. So we do that, even
// though it's dumb and unnecessary otherwise:
{
Entity: storage.ACLEntity("project-owners-" + cl.GCEProjectID),
Role: storage.RoleOwner,
},
// Public, so our systemd unit can get it easily:
{
Entity: storage.AllUsers,
Role: storage.RoleReader,
},
}
w.CacheControl = "no-cache"
selfPath := getSelfPath()
log.Printf("Uploading %q to %v", selfPath, cl.binaryURL())
f, err := os.Open(selfPath)
if err != nil {
log.Fatal(err)
}
defer f.Close()
n, err := io.Copy(w, f)
if err != nil {
log.Fatal(err)
}
if err := w.Close(); err != nil {
log.Fatal(err)
}
log.Printf("Uploaded %d bytes", n)
}
func getSelfPath() string {
if runtime.GOOS != "linux" {
panic("TODO")
}
v, err := os.Readlink("/proc/self/exe")
if err != nil {
log.Fatal(err)
}
return v
}
func zoneInRegion(zone, regionURL string) bool {
if zone == "" {
panic("empty zone")
}
if regionURL == "" {
panic("empty regionURL")
}
// zone is like "us-central1-f"
// regionURL is like "https://www.googleapis.com/compute/v1/projects/camlistore-website/regions/us-central1"
region := path.Base(regionURL) // "us-central1"
if region == "" {
panic("empty region")
}
return strings.HasPrefix(zone, region)
}
// findIP finds an IP address to use, or returns the empty string if none is found.
// It tries to find a reserved one in the same region where the name of the reserved IP
// is "NAME-ip" and the IP is not in use.
func (cl *cloudLaunch) findIP() string {
// Try to find it by name.
aggAddrList, err := cl.computeService.Addresses.AggregatedList(cl.GCEProjectID).Do()
if err != nil {
log.Fatal(err)
}
// https://godoc.org/google.golang.org/api/compute/v1#AddressAggregatedList
var ip string
IPLoop:
for _, asl := range aggAddrList.Items {
for _, addr := range asl.Addresses {
log.Printf(" addr: %#v", addr)
if addr.Name == cl.Name+"-ip" && addr.Status == "RESERVED" && zoneInRegion(cl.zone(), addr.Region) {
ip = addr.Address
break IPLoop
}
}
}
return ip
}
func (cl *cloudLaunch) createInstance() {
inst := cl.lookupInstance()
if inst != nil {
log.Printf("Instance exists; not re-creating.")
return
}
log.Printf("Instance doesn't exist; creating...")
ip := cl.findIP()
log.Printf("Found IP: %v", ip)
cloudConfig := strings.NewReplacer(
"$NAME", cl.Name,
"$URL", cl.binaryURL(),
).Replace(baseConfig)
instance := &compute.Instance{
Name: cl.instName(),
Description: cl.Name,
MachineType: cl.machineTypeURL(),
Disks: []*compute.AttachedDisk{cl.instanceDisk()},
Tags: &compute.Tags{
Items: []string{"http-server", "https-server"},
},
Metadata: &compute.Metadata{
Items: []*compute.MetadataItems{
{
Key: "user-data",
Value: googleapi.String(cloudConfig),
},
},
},
NetworkInterfaces: []*compute.NetworkInterface{
&compute.NetworkInterface{
AccessConfigs: []*compute.AccessConfig{
&compute.AccessConfig{
Type: "ONE_TO_ONE_NAT",
Name: "External NAT",
NatIP: ip,
},
},
Network: cl.projectAPIURL() + "/global/networks/default",
},
},
ServiceAccounts: []*compute.ServiceAccount{
{
Email: "default",
Scopes: cl.Scopes,
},
},
}
log.Printf("Creating instance...")
op, err := cl.computeService.Instances.Insert(cl.GCEProjectID, cl.zone(), instance).Do()
if err != nil {
log.Fatalf("Failed to create instance: %v", err)
}
opName := op.Name
log.Printf("Created. Waiting on operation %v", opName)
OpLoop:
for {
time.Sleep(2 * time.Second)
op, err := cl.computeService.ZoneOperations.Get(cl.GCEProjectID, cl.zone(), opName).Do()
if err != nil {
log.Fatalf("Failed to get op %s: %v", opName, err)
}
switch op.Status {
case "PENDING", "RUNNING":
log.Printf("Waiting on operation %v", opName)
continue
case "DONE":
if op.Error != nil {
for _, operr := range op.Error.Errors {
log.Printf("Error: %+v", operr)
}
log.Fatalf("Failed to start.")
}
log.Printf("Success. %+v", op)
break OpLoop
default:
log.Fatalf("Unknown status %q: %+v", op.Status, op)
}
}
inst, err = cl.computeService.Instances.Get(cl.GCEProjectID, cl.zone(), cl.instName()).Do()
if err != nil {
log.Fatalf("Error getting instance after creation: %v", err)
}
ij, _ := json.MarshalIndent(inst, "", " ")
log.Printf("%s", ij)
log.Printf("Instance created.")
os.Exit(0)
}
// returns nil if instance doesn't exist.
func (cl *cloudLaunch) lookupInstance() *compute.Instance {
inst, err := cl.computeService.Instances.Get(cl.GCEProjectID, cl.zone(), cl.instName()).Do()
if ae, ok := err.(*googleapi.Error); ok && ae.Code == 404 {
return nil
} else if err != nil {
log.Fatalf("Instances.Get: %v", err)
}
return inst
}
func (cl *cloudLaunch) instanceDisk() *compute.AttachedDisk {
imageURL, err := gceutil.CoreOSImageURL(cl.oauthClient)
if err != nil {
log.Fatalf("error looking up latest CoreOS stable image: %v", err)
}
diskName := cl.instName() + "-coreos-stateless-pd"
var diskType string
if cl.SSD {
diskType = cl.projectAPIURL() + "/zones/" + cl.zone() + "/diskTypes/pd-ssd"
}
return &compute.AttachedDisk{
AutoDelete: true,
Boot: true,
Type: "PERSISTENT",
InitializeParams: &compute.AttachedDiskInitializeParams{
DiskName: diskName,
SourceImage: imageURL,
DiskSizeGb: 50,
DiskType: diskType,
},
}
}