2014-03-05 04:36:05 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-03-18 20:00:41 +04:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2014-03-05 04:36:05 +04:00
|
|
|
"flag"
|
2014-03-18 20:00:41 +04:00
|
|
|
"fmt"
|
2014-03-05 04:36:05 +04:00
|
|
|
"log"
|
2014-03-18 20:00:41 +04:00
|
|
|
"os"
|
|
|
|
"strings"
|
2014-03-05 04:36:05 +04:00
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource"
|
|
|
|
"github.com/coreos/coreos-cloudinit/initialize"
|
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
2014-03-05 04:36:05 +04:00
|
|
|
)
|
|
|
|
|
2014-03-14 21:37:16 +04:00
|
|
|
const version = "0.1.2+git"
|
2014-03-05 05:01:58 +04:00
|
|
|
|
2014-03-05 04:36:05 +04:00
|
|
|
func main() {
|
2014-03-05 05:01:58 +04:00
|
|
|
var printVersion bool
|
|
|
|
flag.BoolVar(&printVersion, "version", false, "Print the version and exit")
|
|
|
|
|
2014-03-05 04:36:05 +04:00
|
|
|
var file string
|
2014-03-05 05:06:52 +04: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-05 04:36:05 +04:00
|
|
|
|
|
|
|
var workspace string
|
|
|
|
flag.StringVar(&workspace, "workspace", "/var/lib/coreos-cloudinit", "Base directory coreos-cloudinit should use to store data")
|
|
|
|
|
2014-03-06 02:30:38 +04:00
|
|
|
var sshKeyName string
|
2014-03-18 20:00:41 +04:00
|
|
|
flag.StringVar(&sshKeyName, "ssh-key-name", initialize.DefaultSSHKeyName, "Add SSH keys to the system with the given name")
|
2014-03-06 02:30:38 +04:00
|
|
|
|
2014-03-05 04:36:05 +04:00
|
|
|
flag.Parse()
|
|
|
|
|
2014-03-05 05:01:58 +04:00
|
|
|
if printVersion == true {
|
|
|
|
fmt.Printf("coreos-cloudinit version %s\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2014-03-05 05:06:52 +04:00
|
|
|
if file != "" && url != "" {
|
|
|
|
fmt.Println("Provide one of --from-file or --from-url")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
var ds datasource.Datasource
|
2014-03-05 04:36:05 +04:00
|
|
|
if file != "" {
|
2014-03-18 20:00:41 +04:00
|
|
|
ds = datasource.NewLocalFile(file)
|
2014-03-05 05:06:52 +04:00
|
|
|
} else if url != "" {
|
2014-03-18 20:00:41 +04:00
|
|
|
ds = datasource.NewMetadataService(url)
|
2014-03-05 05:06:52 +04:00
|
|
|
} else {
|
|
|
|
fmt.Println("Provide one of --from-file or --from-url")
|
|
|
|
os.Exit(1)
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
log.Printf("Fetching user-data from datasource of type %q", ds.Type())
|
|
|
|
userdata, err := ds.Fetch()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed fetching user-data from datasource: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-03-14 08:47:59 +04:00
|
|
|
if len(userdata) == 0 {
|
2014-03-14 08:12:14 +04:00
|
|
|
log.Printf("No user data to handle, exiting.")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
parsed, err := ParseUserData(userdata)
|
2014-03-05 04:36:05 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed parsing user-data: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
env := initialize.NewEnvironment("/", workspace)
|
|
|
|
err = initialize.PrepWorkspace(env.Workspace())
|
2014-03-05 04:36:05 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed preparing workspace: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t := parsed.(type) {
|
2014-03-18 20:00:41 +04:00
|
|
|
case initialize.CloudConfig:
|
|
|
|
err = initialize.Apply(t, env)
|
|
|
|
case system.Script:
|
2014-03-05 04:36:05 +04:00
|
|
|
var path string
|
2014-03-18 20:00:41 +04:00
|
|
|
path, err = initialize.PersistScriptInWorkspace(t, env.Workspace())
|
2014-03-05 04:36:05 +04:00
|
|
|
if err == nil {
|
2014-03-05 09:02:20 +04:00
|
|
|
var name string
|
2014-03-18 20:00:41 +04:00
|
|
|
name, err = system.ExecuteScript(path)
|
|
|
|
initialize.PersistUnitNameInWorkspace(name, workspace)
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed resolving user-data: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2014-03-18 20:00:41 +04:00
|
|
|
|
|
|
|
func ParseUserData(contents []byte) (interface{}, error) {
|
|
|
|
bytereader := bytes.NewReader(contents)
|
|
|
|
bufreader := bufio.NewReader(bytereader)
|
|
|
|
header, _ := bufreader.ReadString('\n')
|
|
|
|
|
|
|
|
if strings.HasPrefix(header, "#!") {
|
|
|
|
log.Printf("Parsing user-data as script")
|
|
|
|
return system.Script(contents), nil
|
|
|
|
|
|
|
|
} else if header == "#cloud-config\n" {
|
|
|
|
log.Printf("Parsing user-data as cloud-config")
|
|
|
|
cfg, err := initialize.NewCloudConfig(contents)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err.Error())
|
|
|
|
}
|
|
|
|
return *cfg, nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Unrecognized user-data header: %s", header)
|
|
|
|
}
|
|
|
|
}
|