From a4035cffeae497b72f2b51e0d794e2ff0ffaf2e5 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Thu, 22 May 2014 13:48:26 -0700 Subject: [PATCH] feat(config-drive): Add support for reading user-data from config-drive The -config-drive flag tells cloudinit to read the user-data from within the config-drive (./openstack/latest/user-data). --- coreos-cloudinit.go | 7 ++++++- datasource/configdrive.go | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 datasource/configdrive.go diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index 2c75e75..a86630e 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -28,6 +28,9 @@ func main() { var file string flag.StringVar(&file, "from-file", "", "Read user-data from provided file") + var configdrive string + flag.StringVar(&configdrive, "from-configdrive", "", "Read user-data from provided cloud-drive directory") + var url string flag.StringVar(&url, "from-url", "", "Download user-data from provided url") @@ -52,10 +55,12 @@ func main() { ds = datasource.NewLocalFile(file) } else if url != "" { ds = datasource.NewMetadataService(url) + } else if configdrive != "" { + ds = datasource.NewConfigDrive(configdrive) } else if useProcCmdline { ds = datasource.NewProcCmdline() } else { - fmt.Println("Provide one of --from-file, --from-url or --from-proc-cmdline") + fmt.Println("Provide one of --from-file, --from-configdrive, --from-url or --from-proc-cmdline") os.Exit(1) } diff --git a/datasource/configdrive.go b/datasource/configdrive.go new file mode 100644 index 0000000..7459471 --- /dev/null +++ b/datasource/configdrive.go @@ -0,0 +1,27 @@ +package datasource + +import ( + "io/ioutil" + "os" + "path" +) + +type configDrive struct { + path string +} + +func NewConfigDrive(path string) *configDrive { + return &configDrive{path} +} + +func (self *configDrive) Fetch() ([]byte, error) { + data, err := ioutil.ReadFile(path.Join(self.path, "openstack", "latest", "user_data")) + if os.IsNotExist(err) { + err = nil + } + return data, err +} + +func (self *configDrive) Type() string { + return "cloud-drive" +}