mirror of
https://github.com/linuxkit/linuxkit.git
synced 2025-07-24 19:28:09 +00:00
Merge pull request #3061 from justincormack/gcp-userdata
Add support for userdata on GCP plus custom name
This commit is contained in:
commit
8a7ba7197e
@ -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,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@ -28,13 +29,14 @@ func runGcp(args []string) {
|
|||||||
flags := flag.NewFlagSet("gcp", flag.ExitOnError)
|
flags := flag.NewFlagSet("gcp", flag.ExitOnError)
|
||||||
invoked := filepath.Base(os.Args[0])
|
invoked := filepath.Base(os.Args[0])
|
||||||
flags.Usage = func() {
|
flags.Usage = func() {
|
||||||
fmt.Printf("USAGE: %s run gcp [options] [name]\n\n", invoked)
|
fmt.Printf("USAGE: %s run gcp [options] [image]\n\n", invoked)
|
||||||
fmt.Printf("'name' specifies either the name of an already uploaded\n")
|
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("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("uploaded before it is run.\n\n")
|
||||||
fmt.Printf("Options:\n\n")
|
fmt.Printf("Options:\n\n")
|
||||||
flags.PrintDefaults()
|
flags.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
name := flags.String("name", "", "Machine name")
|
||||||
zoneFlag := flags.String("zone", defaultZone, "GCP Zone")
|
zoneFlag := flags.String("zone", defaultZone, "GCP Zone")
|
||||||
machineFlag := flags.String("machine", defaultMachine, "GCP Machine Type")
|
machineFlag := flags.String("machine", defaultMachine, "GCP Machine Type")
|
||||||
keysFlag := flags.String("keys", "", "Path to Service Account JSON key file")
|
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")
|
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")
|
||||||
}
|
}
|
||||||
@ -55,7 +64,18 @@ func runGcp(args []string) {
|
|||||||
flags.Usage()
|
flags.Usage()
|
||||||
os.Exit(1)
|
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)
|
zone := getStringValue(zoneVar, *zoneFlag, defaultZone)
|
||||||
machine := getStringValue(machineVar, *machineFlag, defaultMachine)
|
machine := getStringValue(machineVar, *machineFlag, defaultMachine)
|
||||||
@ -67,16 +87,16 @@ 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, image, zone, machine, disks, data, *nestedVirt, true); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = client.ConnectToInstanceSerialPort(name, zone); err != nil {
|
if err = client.ConnectToInstanceSerialPort(*name, zone); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !*skipCleanup {
|
if !*skipCleanup {
|
||||||
if err = client.DeleteInstance(name, zone, true); err != nil {
|
if err = client.DeleteInstance(*name, zone, true); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user