From cda037f9a528b1a1df451b6b6aec28ac20bfd65e Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Wed, 10 Sep 2014 18:46:38 -0700 Subject: [PATCH 1/2] flags: add oem flag The oem flag will allow each of the OEMs to specify one flag only, acting as a shortcut to their specific configuration. This will allow us to update which options each OEM uses when running cloudinit. --- coreos-cloudinit.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index 19642c0..fdddec1 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -43,6 +43,7 @@ var ( convertNetconf string workspace string sshKeyName string + oem string ) func init() { @@ -56,16 +57,49 @@ func init() { flag.StringVar(&sources.digitalOceanMetadataService, "from-digitalocean-metadata", "", "Download DigitalOcean data from the provided url") flag.StringVar(&sources.url, "from-url", "", "Download user-data from provided url") flag.BoolVar(&sources.procCmdLine, "from-proc-cmdline", false, fmt.Sprintf("Parse %s for '%s=', using the cloud-config served by an HTTP GET to ", proc_cmdline.ProcCmdlineLocation, proc_cmdline.ProcCmdlineCloudConfigFlag)) + flag.StringVar(&oem, "oem", "", "Use the settings specific to the provided OEM") flag.StringVar(&convertNetconf, "convert-netconf", "", "Read the network config provided in cloud-drive and translate it from the specified format into networkd unit files") flag.StringVar(&workspace, "workspace", "/var/lib/coreos-cloudinit", "Base directory coreos-cloudinit should use to store data") flag.StringVar(&sshKeyName, "ssh-key-name", initialize.DefaultSSHKeyName, "Add SSH keys to the system with the given name") } +type oemConfig map[string]string + +var ( + oemConfigs = map[string]oemConfig{ + "digitalocean": oemConfig{ + "from-digitalocean-metadata": "http://169.254.169.254/", + "convert-netconf": "digitalocean", + }, + "ec2-compat": oemConfig{ + "from-ec2-metadata": "http://169.254.169.254/", + "from-configdrive": "/media/configdrive", + }, + "rackspace-onmetal": oemConfig{ + "from-configdrive": "/media/configdrive", + "convert-netconf": "debian", + }, + } +) + func main() { failure := false flag.Parse() + if c, ok := oemConfigs[oem]; ok { + for k, v := range c { + flag.Set(k, v) + } + } else if flags.oem != "" { + oems := make([]string, 0, len(oemConfigs)) + for k := range oemConfigs { + oems = append(oems, k) + } + fmt.Printf("Invalid option to --oem: %q. Supported options: %q\n", flags.oem, oems) + os.Exit(2) + } + if printVersion == true { fmt.Printf("coreos-cloudinit version %s\n", version) os.Exit(0) From ee2416af6463c6225db9f47551767ec40fd1b7e3 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Wed, 10 Sep 2014 18:53:46 -0700 Subject: [PATCH 2/2] flags: move the flags into their own namespace --- coreos-cloudinit.go | 100 ++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 49 deletions(-) diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index fdddec1..2294335 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -28,39 +28,41 @@ const ( ) var ( - printVersion bool - ignoreFailure bool - sources struct { - file string - configDrive string - metadataService bool - ec2MetadataService string - cloudSigmaMetadataService bool - digitalOceanMetadataService string - url string - procCmdLine bool - } - convertNetconf string - workspace string - sshKeyName string - oem string + flags = struct { + printVersion bool + ignoreFailure bool + sources struct { + file string + configDrive string + metadataService bool + ec2MetadataService string + cloudSigmaMetadataService bool + digitalOceanMetadataService string + url string + procCmdLine bool + } + convertNetconf string + workspace string + sshKeyName string + oem string + }{} ) func init() { - flag.BoolVar(&printVersion, "version", false, "Print the version and exit") - flag.BoolVar(&ignoreFailure, "ignore-failure", false, "Exits with 0 status in the event of malformed input from user-data") - flag.StringVar(&sources.file, "from-file", "", "Read user-data from provided file") - flag.StringVar(&sources.configDrive, "from-configdrive", "", "Read data from provided cloud-drive directory") - flag.BoolVar(&sources.metadataService, "from-metadata-service", false, "[DEPRECATED - Use -from-ec2-metadata] Download data from metadata service") - flag.StringVar(&sources.ec2MetadataService, "from-ec2-metadata", "", "Download EC2 data from the provided url") - flag.BoolVar(&sources.cloudSigmaMetadataService, "from-cloudsigma-metadata", false, "Download data from CloudSigma server context") - flag.StringVar(&sources.digitalOceanMetadataService, "from-digitalocean-metadata", "", "Download DigitalOcean data from the provided url") - flag.StringVar(&sources.url, "from-url", "", "Download user-data from provided url") - flag.BoolVar(&sources.procCmdLine, "from-proc-cmdline", false, fmt.Sprintf("Parse %s for '%s=', using the cloud-config served by an HTTP GET to ", proc_cmdline.ProcCmdlineLocation, proc_cmdline.ProcCmdlineCloudConfigFlag)) - flag.StringVar(&oem, "oem", "", "Use the settings specific to the provided OEM") - flag.StringVar(&convertNetconf, "convert-netconf", "", "Read the network config provided in cloud-drive and translate it from the specified format into networkd unit files") - flag.StringVar(&workspace, "workspace", "/var/lib/coreos-cloudinit", "Base directory coreos-cloudinit should use to store data") - flag.StringVar(&sshKeyName, "ssh-key-name", initialize.DefaultSSHKeyName, "Add SSH keys to the system with the given name") + flag.BoolVar(&flags.printVersion, "version", false, "Print the version and exit") + flag.BoolVar(&flags.ignoreFailure, "ignore-failure", false, "Exits with 0 status in the event of malformed input from user-data") + flag.StringVar(&flags.sources.file, "from-file", "", "Read user-data from provided file") + flag.StringVar(&flags.sources.configDrive, "from-configdrive", "", "Read data from provided cloud-drive directory") + flag.BoolVar(&flags.sources.metadataService, "from-metadata-service", false, "[DEPRECATED - Use -from-ec2-metadata] Download data from metadata service") + flag.StringVar(&flags.sources.ec2MetadataService, "from-ec2-metadata", "", "Download EC2 data from the provided url") + flag.BoolVar(&flags.sources.cloudSigmaMetadataService, "from-cloudsigma-metadata", false, "Download data from CloudSigma server context") + flag.StringVar(&flags.sources.digitalOceanMetadataService, "from-digitalocean-metadata", "", "Download DigitalOcean data from the provided url") + flag.StringVar(&flags.sources.url, "from-url", "", "Download user-data from provided url") + flag.BoolVar(&flags.sources.procCmdLine, "from-proc-cmdline", false, fmt.Sprintf("Parse %s for '%s=', using the cloud-config served by an HTTP GET to ", proc_cmdline.ProcCmdlineLocation, proc_cmdline.ProcCmdlineCloudConfigFlag)) + flag.StringVar(&flags.oem, "oem", "", "Use the settings specific to the provided OEM") + flag.StringVar(&flags.convertNetconf, "convert-netconf", "", "Read the network config provided in cloud-drive and translate it from the specified format into networkd unit files") + flag.StringVar(&flags.workspace, "workspace", "/var/lib/coreos-cloudinit", "Base directory coreos-cloudinit should use to store data") + flag.StringVar(&flags.sshKeyName, "ssh-key-name", initialize.DefaultSSHKeyName, "Add SSH keys to the system with the given name") } type oemConfig map[string]string @@ -87,7 +89,7 @@ func main() { flag.Parse() - if c, ok := oemConfigs[oem]; ok { + if c, ok := oemConfigs[flags.oem]; ok { for k, v := range c { flag.Set(k, v) } @@ -100,17 +102,17 @@ func main() { os.Exit(2) } - if printVersion == true { + if flags.printVersion == true { fmt.Printf("coreos-cloudinit version %s\n", version) os.Exit(0) } - switch convertNetconf { + switch flags.convertNetconf { case "": case "debian": case "digitalocean": default: - fmt.Printf("Invalid option to -convert-netconf: '%s'. Supported options: 'debian, digitalocean'\n", convertNetconf) + fmt.Printf("Invalid option to -convert-netconf: '%s'. Supported options: 'debian, digitalocean'\n", flags.convertNetconf) os.Exit(2) } @@ -151,7 +153,7 @@ func main() { } // Apply environment to user-data - env := initialize.NewEnvironment("/", ds.ConfigRoot(), workspace, convertNetconf, sshKeyName, subs) + env := initialize.NewEnvironment("/", ds.ConfigRoot(), flags.workspace, flags.convertNetconf, flags.sshKeyName, subs) userdata := env.Apply(string(userdataBytes)) var ccm, ccu *initialize.CloudConfig @@ -212,7 +214,7 @@ func main() { } } - if failure && !ignoreFailure { + if failure && !flags.ignoreFailure { os.Exit(1) } } @@ -255,28 +257,28 @@ func mergeCloudConfig(mdcc, udcc initialize.CloudConfig) (cc initialize.CloudCon // on the different source command-line flags. func getDatasources() []datasource.Datasource { dss := make([]datasource.Datasource, 0, 5) - if sources.file != "" { - dss = append(dss, file.NewDatasource(sources.file)) + if flags.sources.file != "" { + dss = append(dss, file.NewDatasource(flags.sources.file)) } - if sources.url != "" { - dss = append(dss, url.NewDatasource(sources.url)) + if flags.sources.url != "" { + dss = append(dss, url.NewDatasource(flags.sources.url)) } - if sources.configDrive != "" { - dss = append(dss, configdrive.NewDatasource(sources.configDrive)) + if flags.sources.configDrive != "" { + dss = append(dss, configdrive.NewDatasource(flags.sources.configDrive)) } - if sources.metadataService { + if flags.sources.metadataService { dss = append(dss, ec2.NewDatasource(ec2.DefaultAddress)) } - if sources.ec2MetadataService != "" { - dss = append(dss, ec2.NewDatasource(sources.ec2MetadataService)) + if flags.sources.ec2MetadataService != "" { + dss = append(dss, ec2.NewDatasource(flags.sources.ec2MetadataService)) } - if sources.cloudSigmaMetadataService { + if flags.sources.cloudSigmaMetadataService { dss = append(dss, cloudsigma.NewServerContextService()) } - if sources.digitalOceanMetadataService != "" { - dss = append(dss, digitalocean.NewDatasource(sources.digitalOceanMetadataService)) + if flags.sources.digitalOceanMetadataService != "" { + dss = append(dss, digitalocean.NewDatasource(flags.sources.digitalOceanMetadataService)) } - if sources.procCmdLine { + if flags.sources.procCmdLine { dss = append(dss, proc_cmdline.NewDatasource()) } return dss