diff --git a/src/cmd/linuxkit/gcp.go b/src/cmd/linuxkit/gcp.go index a315de50b..1697a312d 100644 --- a/src/cmd/linuxkit/gcp.go +++ b/src/cmd/linuxkit/gcp.go @@ -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, + }, }, }, } diff --git a/src/cmd/linuxkit/run_gcp.go b/src/cmd/linuxkit/run_gcp.go index 1e7c2a820..0c1161512 100644 --- a/src/cmd/linuxkit/run_gcp.go +++ b/src/cmd/linuxkit/run_gcp.go @@ -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) } }