From 85f151011c17ec8f047f08be41594eb697eac73b Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 4 Mar 2014 17:06:52 -0800 Subject: [PATCH] feat(metadata): Add --from-url flag --- cloudinit/metadata_service.go | 7 ++++--- coreos-cloudinit.go | 17 ++++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cloudinit/metadata_service.go b/cloudinit/metadata_service.go index 201bf4a..351dcd7 100644 --- a/cloudinit/metadata_service.go +++ b/cloudinit/metadata_service.go @@ -6,15 +6,16 @@ import ( ) type metadataService struct { + url string client http.Client } -func NewMetadataService() *metadataService { - return &metadataService{http.Client{}} +func NewMetadataService(url string) *metadataService { + return &metadataService{url, http.Client{}} } func (ms *metadataService) UserData() ([]byte, error) { - resp, err := ms.client.Get("http://169.254.169.254/2012-01-12/user-data") + resp, err := ms.client.Get(ms.url) if err != nil { return []byte{}, err } diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index bbaf36e..9c73d30 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -20,7 +20,10 @@ func main() { flag.BoolVar(&printVersion, "version", false, "Print the version and exit") var file string - flag.StringVar(&file, "from-file", "", "Read user-data from file rather than metadata service") + flag.StringVar(&file, "from-file", "", "Read user-data from provided file") + + var url string + flag.StringVar(&url, "from-url", "", "Download user-data from provided url") var workspace string flag.StringVar(&workspace, "workspace", "/var/lib/coreos-cloudinit", "Base directory coreos-cloudinit should use to store data") @@ -32,19 +35,27 @@ func main() { os.Exit(0) } + if file != "" && url != "" { + fmt.Println("Provide one of --from-file or --from-url") + os.Exit(1) + } + if file != "" { log.Printf("Reading user-data from file: %s", file) userdata, err = ioutil.ReadFile(file) if err != nil { log.Fatal(err.Error()) } - } else { + } else if url != "" { log.Printf("Reading user-data from metadata service") - svc := cloudinit.NewMetadataService() + svc := cloudinit.NewMetadataService(url) userdata, err = svc.UserData() if err != nil { log.Fatal(err.Error()) } + } else { + fmt.Println("Provide one of --from-file or --from-url") + os.Exit(1) } parsed, err := cloudinit.ParseUserData(userdata)