2014-03-04 16:36:05 -08:00
package main
import (
"flag"
2014-03-18 09:00:41 -07:00
"fmt"
2014-03-04 16:36:05 -08:00
"log"
2014-03-18 09:00:41 -07:00
"os"
2014-03-04 16:36:05 -08:00
2014-03-18 09:00:41 -07:00
"github.com/coreos/coreos-cloudinit/datasource"
"github.com/coreos/coreos-cloudinit/initialize"
"github.com/coreos/coreos-cloudinit/system"
2014-03-04 16:36:05 -08:00
)
2014-05-16 21:23:34 -07:00
const version = "0.7.1+git"
2014-03-04 17:01:58 -08:00
2014-05-16 17:35:31 -04:00
func init ( ) {
//Removes timestamp since it is displayed already during booting
log . SetFlags ( 0 )
}
2014-03-04 16:36:05 -08:00
func main ( ) {
2014-03-04 17:01:58 -08:00
var printVersion bool
flag . BoolVar ( & printVersion , "version" , false , "Print the version and exit" )
2014-03-18 20:47:20 -07:00
var ignoreFailure bool
flag . BoolVar ( & ignoreFailure , "ignore-failure" , false , "Exits with 0 status in the event of malformed input from user-data" )
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
2014-04-22 15:36:07 -07:00
var useProcCmdline bool
flag . BoolVar ( & useProcCmdline , "from-proc-cmdline" , false , fmt . Sprintf ( "Parse %s for '%s=<url>', using the cloud-config served by an HTTP GET to <url>" , datasource . ProcCmdlineLocation , datasource . ProcCmdlineCloudConfigFlag ) )
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
2014-03-18 09:00:41 -07:00
flag . StringVar ( & sshKeyName , "ssh-key-name" , initialize . DefaultSSHKeyName , "Add SSH keys to the system with the given name" )
2014-03-05 14:30:38 -08:00
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-18 09:00:41 -07:00
var ds datasource . Datasource
2014-03-04 16:36:05 -08:00
if file != "" {
2014-03-18 09:00:41 -07:00
ds = datasource . NewLocalFile ( file )
2014-03-04 17:06:52 -08:00
} else if url != "" {
2014-03-18 09:00:41 -07:00
ds = datasource . NewMetadataService ( url )
2014-04-22 15:36:07 -07:00
} else if useProcCmdline {
ds = datasource . NewProcCmdline ( )
2014-03-04 17:06:52 -08:00
} else {
2014-04-22 15:36:07 -07:00
fmt . Println ( "Provide one of --from-file, --from-url or --from-proc-cmdline" )
2014-03-04 17:06:52 -08:00
os . Exit ( 1 )
2014-03-04 16:36:05 -08:00
}
2014-03-18 09:00:41 -07:00
log . Printf ( "Fetching user-data from datasource of type %q" , ds . Type ( ) )
2014-03-21 10:35:18 -07:00
userdataBytes , err := ds . Fetch ( )
2014-03-18 09:00:41 -07:00
if err != nil {
2014-03-18 20:47:20 -07:00
log . Printf ( "Failed fetching user-data from datasource: %v" , err )
if ignoreFailure {
os . Exit ( 0 )
} else {
os . Exit ( 1 )
}
2014-03-18 09:00:41 -07:00
}
2014-03-21 10:35:18 -07:00
if len ( userdataBytes ) == 0 {
2014-03-13 21:12:14 -07:00
log . Printf ( "No user data to handle, exiting." )
os . Exit ( 0 )
}
2014-03-21 10:35:18 -07:00
env := initialize . NewEnvironment ( "/" , workspace )
userdata := string ( userdataBytes )
userdata = env . Apply ( userdata )
2014-04-21 13:31:23 -07:00
parsed , err := initialize . ParseUserData ( userdata )
2014-03-04 16:36:05 -08:00
if err != nil {
2014-03-18 20:47:20 -07:00
log . Printf ( "Failed parsing user-data: %v" , err )
if ignoreFailure {
os . Exit ( 0 )
} else {
os . Exit ( 1 )
}
2014-03-04 16:36:05 -08:00
}
2014-03-18 09:00:41 -07:00
err = initialize . PrepWorkspace ( env . Workspace ( ) )
2014-03-04 16:36:05 -08:00
if err != nil {
log . Fatalf ( "Failed preparing workspace: %v" , err )
}
switch t := parsed . ( type ) {
2014-03-18 09:00:41 -07:00
case initialize . CloudConfig :
err = initialize . Apply ( t , env )
case system . Script :
2014-03-04 16:36:05 -08:00
var path string
2014-03-18 09:00:41 -07:00
path , err = initialize . PersistScriptInWorkspace ( t , env . Workspace ( ) )
2014-03-04 16:36:05 -08:00
if err == nil {
2014-03-04 21:02:20 -08:00
var name string
2014-03-18 09:00:41 -07:00
name , err = system . ExecuteScript ( path )
initialize . PersistUnitNameInWorkspace ( name , workspace )
2014-03-04 16:36:05 -08:00
}
}
if err != nil {
log . Fatalf ( "Failed resolving user-data: %v" , err )
}
}