2014-03-04 16:36:05 -08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-03-04 17:01:58 -08:00
|
|
|
"fmt"
|
2014-03-04 16:36:05 -08:00
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
2014-03-04 17:01:58 -08:00
|
|
|
"os"
|
2014-03-04 16:36:05 -08:00
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/coreos/coreos-cloudinit/cloudinit"
|
|
|
|
)
|
|
|
|
|
2014-03-05 14:53:02 -08:00
|
|
|
const version = "0.1.1+git"
|
2014-03-04 17:01:58 -08:00
|
|
|
|
2014-03-04 16:36:05 -08:00
|
|
|
func main() {
|
|
|
|
var userdata []byte
|
|
|
|
var err error
|
|
|
|
|
2014-03-04 17:01:58 -08:00
|
|
|
var printVersion bool
|
|
|
|
flag.BoolVar(&printVersion, "version", false, "Print the version and exit")
|
|
|
|
|
2014-03-04 16:36:05 -08:00
|
|
|
var file string
|
2014-03-04 17:06:52 -08:00
|
|
|
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")
|
2014-03-04 16:36:05 -08:00
|
|
|
|
|
|
|
var workspace string
|
|
|
|
flag.StringVar(&workspace, "workspace", "/var/lib/coreos-cloudinit", "Base directory coreos-cloudinit should use to store data")
|
|
|
|
|
2014-03-05 14:30:38 -08:00
|
|
|
var sshKeyName string
|
|
|
|
flag.StringVar(&sshKeyName, "ssh-key-name", cloudinit.DefaultSSHKeyName, "Add SSH keys to the system with the given name")
|
|
|
|
|
2014-03-04 16:36:05 -08:00
|
|
|
flag.Parse()
|
|
|
|
|
2014-03-04 17:01:58 -08:00
|
|
|
if printVersion == true {
|
|
|
|
fmt.Printf("coreos-cloudinit version %s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2014-03-04 17:06:52 -08:00
|
|
|
if file != "" && url != "" {
|
|
|
|
fmt.Println("Provide one of --from-file or --from-url")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2014-03-04 16:36:05 -08:00
|
|
|
if file != "" {
|
|
|
|
log.Printf("Reading user-data from file: %s", file)
|
|
|
|
userdata, err = ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
2014-03-04 17:06:52 -08:00
|
|
|
} else if url != "" {
|
2014-03-04 16:36:05 -08:00
|
|
|
log.Printf("Reading user-data from metadata service")
|
2014-03-04 17:06:52 -08:00
|
|
|
svc := cloudinit.NewMetadataService(url)
|
2014-03-04 16:36:05 -08:00
|
|
|
userdata, err = svc.UserData()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
2014-03-04 17:06:52 -08:00
|
|
|
} else {
|
|
|
|
fmt.Println("Provide one of --from-file or --from-url")
|
|
|
|
os.Exit(1)
|
2014-03-04 16:36:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
parsed, err := cloudinit.ParseUserData(userdata)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed parsing user-data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cloudinit.PrepWorkspace(workspace)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed preparing workspace: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t := parsed.(type) {
|
|
|
|
case cloudinit.CloudConfig:
|
2014-03-05 14:50:20 -08:00
|
|
|
err = cloudinit.ApplyCloudConfig(t, sshKeyName)
|
2014-03-04 16:36:05 -08:00
|
|
|
case cloudinit.Script:
|
|
|
|
var path string
|
|
|
|
path, err = cloudinit.PersistScriptInWorkspace(t, workspace)
|
|
|
|
if err == nil {
|
2014-03-04 21:02:20 -08:00
|
|
|
var name string
|
|
|
|
name, err = cloudinit.ExecuteScript(path)
|
|
|
|
cloudinit.PersistScriptUnitNameInWorkspace(name, workspace)
|
2014-03-04 16:36:05 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed resolving user-data: %v", err)
|
|
|
|
}
|
|
|
|
}
|