Add support for userdata on GCP

This was missing in the linuxkit CLI, even though we support it in the
metadata package.

Signed-off-by: Justin Cormack <justin.cormack@docker.com>
This commit is contained in:
Justin Cormack 2018-06-06 16:05:51 -07:00
parent 414b4c5275
commit c9db3f0625
No known key found for this signature in database
GPG Key ID: 609102888A2EE3F9
2 changed files with 22 additions and 2 deletions

View File

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

View File

@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -45,6 +46,13 @@ func runGcp(args []string) {
skipCleanup := flags.Bool("skip-cleanup", false, "Don't remove images or VMs") skipCleanup := flags.Bool("skip-cleanup", false, "Don't remove images or VMs")
nestedVirt := flags.Bool("nested-virt", false, "Enabled nested virtualization") 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 { if err := flags.Parse(args); err != nil {
log.Fatal("Unable to parse args") log.Fatal("Unable to parse args")
} }
@ -57,6 +65,14 @@ func runGcp(args []string) {
} }
name := remArgs[0] name := remArgs[0]
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) zone := getStringValue(zoneVar, *zoneFlag, defaultZone)
machine := getStringValue(machineVar, *machineFlag, defaultMachine) machine := getStringValue(machineVar, *machineFlag, defaultMachine)
keys := getStringValue(keysVar, *keysFlag, "") keys := getStringValue(keysVar, *keysFlag, "")
@ -67,7 +83,7 @@ func runGcp(args []string) {
log.Fatalf("Unable to connect to GCP") 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, name, zone, machine, disks, data, *nestedVirt, true); err != nil {
log.Fatal(err) log.Fatal(err)
} }