Merge pull request #3061 from justincormack/gcp-userdata

Add support for userdata on GCP plus custom name
This commit is contained in:
Rolf Neugebauer 2018-06-07 11:29:08 +01:00 committed by GitHub
commit 8a7ba7197e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 7 deletions

View File

@ -182,7 +182,7 @@ func (g GCPClient) DeleteImage(name string) error {
}
// CreateInstance creates and starts an instance on GCP
func (g GCPClient) CreateInstance(name, image, zone, machineType string, disks Disks, nested, replace bool) error {
func (g GCPClient) CreateInstance(name, image, zone, machineType string, disks Disks, data *string, nested, replace bool) error {
if replace {
if err := g.DeleteInstance(name, zone, true); err != nil {
return err
@ -262,6 +262,10 @@ func (g GCPClient) CreateInstance(name, image, zone, machineType string, disks D
Key: "ssh-keys",
Value: sshKey,
},
{
Key: "userdata",
Value: data,
},
},
},
}

View File

@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
@ -28,13 +29,14 @@ func runGcp(args []string) {
flags := flag.NewFlagSet("gcp", flag.ExitOnError)
invoked := filepath.Base(os.Args[0])
flags.Usage = func() {
fmt.Printf("USAGE: %s run gcp [options] [name]\n\n", invoked)
fmt.Printf("'name' specifies either the name of an already uploaded\n")
fmt.Printf("USAGE: %s run gcp [options] [image]\n\n", invoked)
fmt.Printf("'image' specifies either the name of an already uploaded\n")
fmt.Printf("GCP image or the full path to a image file which will be\n")
fmt.Printf("uploaded before it is run.\n\n")
fmt.Printf("Options:\n\n")
flags.PrintDefaults()
}
name := flags.String("name", "", "Machine name")
zoneFlag := flags.String("zone", defaultZone, "GCP Zone")
machineFlag := flags.String("machine", defaultMachine, "GCP Machine Type")
keysFlag := flags.String("keys", "", "Path to Service Account JSON key file")
@ -45,6 +47,13 @@ func runGcp(args []string) {
skipCleanup := flags.Bool("skip-cleanup", false, "Don't remove images or VMs")
nestedVirt := flags.Bool("nested-virt", false, "Enabled nested virtualization")
data := flags.String("data", "", "String of metadata to pass to VM; error to specify both -data and -data-file")
dataPath := flags.String("data-file", "", "Path to file containing metadata to pass to VM; error to specify both -data and -data-file")
if *data != "" && *dataPath != "" {
log.Fatal("Cannot specify both -data and -data-file")
}
if err := flags.Parse(args); err != nil {
log.Fatal("Unable to parse args")
}
@ -55,7 +64,18 @@ func runGcp(args []string) {
flags.Usage()
os.Exit(1)
}
name := remArgs[0]
image := remArgs[0]
if *name == "" {
*name = image
}
if *dataPath != "" {
dataB, err := ioutil.ReadFile(*dataPath)
if err != nil {
log.Fatalf("Unable to read metadata file: %v", err)
}
*data = string(dataB)
}
zone := getStringValue(zoneVar, *zoneFlag, defaultZone)
machine := getStringValue(machineVar, *machineFlag, defaultMachine)
@ -67,16 +87,16 @@ func runGcp(args []string) {
log.Fatalf("Unable to connect to GCP")
}
if err = client.CreateInstance(name, name, zone, machine, disks, *nestedVirt, true); err != nil {
if err = client.CreateInstance(*name, image, zone, machine, disks, data, *nestedVirt, true); err != nil {
log.Fatal(err)
}
if err = client.ConnectToInstanceSerialPort(name, zone); err != nil {
if err = client.ConnectToInstanceSerialPort(*name, zone); err != nil {
log.Fatal(err)
}
if !*skipCleanup {
if err = client.DeleteInstance(name, zone, true); err != nil {
if err = client.DeleteInstance(*name, zone, true); err != nil {
log.Fatal(err)
}
}