feat(metadata): Add --from-url flag

This commit is contained in:
Brian Waldon 2014-03-04 17:06:52 -08:00
parent 20df7b6a19
commit 85f151011c
2 changed files with 18 additions and 6 deletions

View File

@ -6,15 +6,16 @@ import (
) )
type metadataService struct { type metadataService struct {
url string
client http.Client client http.Client
} }
func NewMetadataService() *metadataService { func NewMetadataService(url string) *metadataService {
return &metadataService{http.Client{}} return &metadataService{url, http.Client{}}
} }
func (ms *metadataService) UserData() ([]byte, error) { 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 { if err != nil {
return []byte{}, err return []byte{}, err
} }

View File

@ -20,7 +20,10 @@ func main() {
flag.BoolVar(&printVersion, "version", false, "Print the version and exit") flag.BoolVar(&printVersion, "version", false, "Print the version and exit")
var file string 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 var workspace string
flag.StringVar(&workspace, "workspace", "/var/lib/coreos-cloudinit", "Base directory coreos-cloudinit should use to store data") 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) os.Exit(0)
} }
if file != "" && url != "" {
fmt.Println("Provide one of --from-file or --from-url")
os.Exit(1)
}
if file != "" { if file != "" {
log.Printf("Reading user-data from file: %s", file) log.Printf("Reading user-data from file: %s", file)
userdata, err = ioutil.ReadFile(file) userdata, err = ioutil.ReadFile(file)
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
} else { } else if url != "" {
log.Printf("Reading user-data from metadata service") log.Printf("Reading user-data from metadata service")
svc := cloudinit.NewMetadataService() svc := cloudinit.NewMetadataService(url)
userdata, err = svc.UserData() userdata, err = svc.UserData()
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
} else {
fmt.Println("Provide one of --from-file or --from-url")
os.Exit(1)
} }
parsed, err := cloudinit.ParseUserData(userdata) parsed, err := cloudinit.ParseUserData(userdata)