This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
/
main.go
426 lines (351 loc) · 11.3 KB
/
main.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
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
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 main
import (
"flag"
"fmt"
"golang.org/x/crypto/ssh"
"math/rand"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/ghodss/yaml"
"github.com/golang/glog"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
"google.golang.org/api/storage/v1"
"io/ioutil"
"k8s.io/kube-deploy/imagebuilder/pkg/imagebuilder"
"k8s.io/kube-deploy/imagebuilder/pkg/imagebuilder/executor"
"net/url"
"os"
"path"
"strings"
)
var flagConfig = flag.String("config", "", "Config file to load")
//var flagRegion = flag.String("region", "", "Cloud region to use")
//var flagImage = flag.String("image", "", "Image to use as builder")
//var flagSSHKey = flag.String("sshkey", "", "Name of SSH key to use")
//var flagInstanceType = flag.String("instancetype", "m3.medium", "Instance type to launch")
//var flagSubnet = flag.String("subnet", "", "Subnet in which to launch")
//var flagSecurityGroup = flag.String("securitygroup", "", "Security group to use for launch")
//var flagTemplatePath = flag.String("template", "", "Path to image template")
var flagUp = flag.Bool("up", true, "Set to create instance (if not found)")
var flagBuild = flag.Bool("build", true, "Set to build image")
var flagTag = flag.Bool("tag", true, "Set to tag image")
var flagPublish = flag.Bool("publish", true, "Set to publish image")
var flagReplicate = flag.Bool("replicate", true, "Set to copy the image to all regions")
var flagDown = flag.Bool("down", true, "Set to shut down instance (if found)")
var flagAddTags = flag.String("addtags", "", "Comma-separated list of key=value pairs to be added as additional Tags")
var flagLocalhost = flag.Bool("localhost", false, "Set to use local machine for execution")
func loadConfig(dest interface{}, src string) error {
data, err := ioutil.ReadFile(src)
if err != nil {
return fmt.Errorf("error reading file %q: %v", src, err)
}
err = yaml.Unmarshal(data, dest)
if err != nil {
return fmt.Errorf("error parsing file %q: %v", src, err)
}
return nil
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
flag.Set("alsologtostderr", "true")
flag.Parse()
if *flagConfig == "" {
glog.Exitf("--config must be specified")
}
var templateContext interface{}
config := &imagebuilder.Config{}
config.InitDefaults()
err := loadConfig(config, *flagConfig)
if err != nil {
glog.Exitf("Error loading config: %v", err)
}
for key, value := range splitAdditionalTags() {
glog.Infof("Injecting additional tag: %q = %q", key, value)
config.Tags[key] = value
}
var cloud imagebuilder.Cloud
switch config.Cloud {
case "aws":
awsConfig, awsCloud, err := initAWS(*flagLocalhost)
if err != nil {
glog.Exitf("%v", err)
}
awsConfig.Tags = config.Tags
templateContext = awsConfig
cloud = awsCloud
case "gce":
if *flagPublish {
glog.Exitf("Publishing images is not supported on gce (pass --publish=false)")
}
gceConfig, gceCloud, err := initGCE()
if err != nil {
glog.Exitf("%v", err)
}
gceConfig.Tags = config.Tags
templateContext = gceConfig
cloud = gceCloud
case "":
glog.Exitf("Cloud not set")
default:
glog.Exitf("Unknown cloud: %q", config.Cloud)
}
if *flagBuild && config.TemplatePath == "" {
glog.Fatalf("TemplatePath must be provided")
}
var bvzTemplate *imagebuilder.BootstrapVzTemplate
var imageName string
if config.TemplatePath != "" {
templateResolved := path.Join(path.Dir(*flagConfig), config.TemplatePath)
templateRaw, err := imagebuilder.ReadFile(templateResolved)
if err != nil {
glog.Fatalf("error reading template: %v", err)
}
templateString, err := imagebuilder.ExpandTemplate(templateResolved, string(templateRaw), templateContext)
if err != nil {
glog.Fatalf("error executing template: %v", err)
}
bvzTemplate, err = imagebuilder.NewBootstrapVzTemplate(templateString)
if err != nil {
glog.Fatalf("error parsing template: %v", err)
}
imageName, err = bvzTemplate.BuildImageName()
if err != nil {
glog.Fatalf("error inferring image name: %v", err)
}
glog.Infof("Parsed template %q; will build image with name %s", config.TemplatePath, imageName)
}
instance, err := cloud.GetInstance()
if err != nil {
glog.Fatalf("error getting instance: %v", err)
}
if instance == nil && *flagUp {
instance, err = cloud.CreateInstance()
if err != nil {
glog.Fatalf("error creating instance: %v", err)
}
}
image, err := cloud.FindImage(imageName)
if err != nil {
glog.Fatalf("error finding image %q: %v", imageName, err)
}
if image != nil {
glog.Infof("found existing image %q", image)
}
if *flagBuild && image == nil {
if instance == nil {
glog.Fatalf("Instance was not found (specify --up?)")
}
sshConfig := &ssh.ClientConfig{
User: config.SSHUsername,
}
if !*flagLocalhost {
if config.SSHPrivateKey == "" {
glog.Fatalf("SSHPublicKey is required")
// We used to allow the SSH agent, but probably more trouble than it is worth?
//sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
//if err != nil {
// glog.Fatalf("error connecting to SSH agent: %v", err)
//}
//
//sshConfig.Auth = append(sshConfig.Auth, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
} else {
keyBytes, err := imagebuilder.ReadFile(config.SSHPrivateKey)
if err != nil {
glog.Exitf("error loading SSH private key: %v", err)
}
key, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
glog.Exitf("error parsing SSH private key: %v", err)
}
sshConfig.Auth = append(sshConfig.Auth, ssh.PublicKeys(key))
}
}
x, err := instance.DialSSH(sshConfig)
if err != nil {
glog.Fatalf("error SSHing to instance: %v", err)
}
defer x.Close()
sshHelper := executor.NewTarget(x)
builder := imagebuilder.NewBuilder(config, sshHelper)
err = builder.RunSetupCommands()
if err != nil {
glog.Fatalf("error setting up instance: %v", err)
}
extraEnv, err := cloud.GetExtraEnv()
if err != nil {
glog.Fatalf("error building environment: %v", err)
}
err = builder.BuildImage(bvzTemplate.Bytes(), extraEnv)
if err != nil {
glog.Fatalf("error building image: %v", err)
}
image, err = cloud.FindImage(imageName)
if err != nil {
glog.Fatalf("error finding image %q: %v", imageName, err)
}
if image == nil {
glog.Fatalf("image not found after build: %q", imageName)
}
}
if *flagTag {
if image == nil {
glog.Fatalf("image not found: %q", imageName)
}
glog.Infof("Tagging image %q", image)
tags := make(map[string]string)
for k, v := range config.Tags {
tags[k] = v
}
{
t := time.Now().UTC().Format("20060102150405")
tags["k8s.io/build"] = t
}
err = image.AddTags(tags)
if err != nil {
glog.Fatalf("error tagging image %q: %v", imageName, err)
}
glog.Infof("Tagged image %q", image)
}
if *flagPublish {
if image == nil {
glog.Fatalf("image not found: %q", imageName)
}
glog.Infof("Making image public: %v", image)
err = image.EnsurePublic()
if err != nil {
glog.Fatalf("error making image public %q: %v", imageName, err)
}
glog.Infof("Made image public: %v", image)
}
if *flagReplicate {
if image == nil {
glog.Fatalf("image not found: %q", imageName)
}
glog.Infof("Copying image to all regions: %v", image)
images, err := image.ReplicateImage(*flagPublish)
if err != nil {
glog.Fatalf("error replicating image %q: %v", imageName, err)
}
for region, imageID := range images {
glog.Infof("Image in region %q: %q", region, imageID)
}
}
if *flagDown {
if instance == nil {
glog.Infof("Instance not found / already shutdown")
} else {
err := instance.Shutdown()
if err != nil {
glog.Fatalf("error terminating instance: %v", err)
}
}
}
}
func splitAdditionalTags() map[string]string {
tags := make(map[string]string)
if *flagAddTags != "" {
for _, tagpair := range strings.Split(*flagAddTags, ",") {
trimmed := strings.TrimSpace(tagpair)
kv := strings.Split(trimmed, "=")
if len(kv) != 2 {
glog.Fatalf("addtags value malformed, should be key=value: %q", tagpair)
}
tags[kv[0]] = kv[1]
}
}
return tags
}
func initAWS(useLocalhost bool) (*imagebuilder.AWSConfig, *imagebuilder.AWSCloud, error) {
region := os.Getenv("AWS_REGION")
if region == "" {
region = os.Getenv("AWS_DEFAULT_REGION")
}
awsConfig := &imagebuilder.AWSConfig{}
awsConfig.InitDefaults(region)
err := loadConfig(awsConfig, *flagConfig)
if err != nil {
glog.Exitf("Error loading AWS config: %v", err)
}
if awsConfig.Region == "" {
glog.Exitf("Region must be set")
}
ec2Client := ec2.New(session.New(), &aws.Config{Region: &awsConfig.Region})
awsCloud := imagebuilder.NewAWSCloud(ec2Client, awsConfig, useLocalhost)
return awsConfig, awsCloud, nil
}
func initGCE() (*imagebuilder.GCEConfig, *imagebuilder.GCECloud, error) {
config := &imagebuilder.GCEConfig{}
config.InitDefaults()
err := loadConfig(config, *flagConfig)
if err != nil {
return nil, nil, fmt.Errorf("Error loading GCE config: %v", err)
}
if config.Project == "" {
return nil, nil, fmt.Errorf("Project must be set")
}
if config.MachineName == "" {
return nil, nil, fmt.Errorf("Name must be set")
}
if config.Zone == "" {
return nil, nil, fmt.Errorf("Zone must be set")
}
if config.MachineType == "" {
return nil, nil, fmt.Errorf("MachineType must be set")
}
if config.Image == "" {
return nil, nil, fmt.Errorf("Image must be set")
}
if config.GCSDestination == "" {
return nil, nil, fmt.Errorf("GCSDestination must be set")
}
// Avoid common mistake...
if !strings.HasSuffix(config.GCSDestination, "/") {
return nil, nil, fmt.Errorf("GCSDestination should end in /")
}
if !strings.HasPrefix(config.GCSDestination, "gs://") {
return nil, nil, fmt.Errorf("GCSDestination should start with gs://")
}
ctx := context.Background()
client, err := google.DefaultClient(ctx, compute.ComputeScope)
if err != nil {
return nil, nil, fmt.Errorf("error building google API client: %v", err)
}
computeService, err := compute.New(client)
if err != nil {
return nil, nil, fmt.Errorf("error building compute API client: %v", err)
}
storageService, err := storage.New(client)
if err != nil {
return nil, nil, fmt.Errorf("error building compute API client: %v", err)
}
u, err := url.Parse(config.GCSDestination)
if err != nil {
return nil, nil, fmt.Errorf("GCSDestination %q is not a well-formed URL: %v", config.GCSDestination, err)
}
glog.Infof("Checking for bucket %q", u.Host)
_, err = storageService.Buckets.Get(u.Host).Do()
if err != nil {
if imagebuilder.IsGCENotFound(err) {
return nil, nil, fmt.Errorf("GCS bucket does not exist: %v", config.GCSDestination)
}
return nil, nil, fmt.Errorf("Error checking that bucket exists: %v", err)
}
cloud := imagebuilder.NewGCECloud(computeService, config)
return config, cloud, nil
}