2014-03-05 04:36:05 +04:00
package main
import (
"flag"
2014-03-18 20:00:41 +04:00
"fmt"
"os"
2014-06-27 02:17:53 +04:00
"sync"
"time"
2014-03-05 04:36:05 +04:00
2014-03-18 20:00:41 +04:00
"github.com/coreos/coreos-cloudinit/datasource"
2014-07-31 00:56:36 +04:00
"github.com/coreos/coreos-cloudinit/datasource/configdrive"
"github.com/coreos/coreos-cloudinit/datasource/file"
2014-08-05 13:50:21 +04:00
"github.com/coreos/coreos-cloudinit/datasource/metadata/cloudsigma"
2014-07-31 01:23:10 +04:00
"github.com/coreos/coreos-cloudinit/datasource/metadata/ec2"
2014-07-31 00:56:36 +04:00
"github.com/coreos/coreos-cloudinit/datasource/proc_cmdline"
"github.com/coreos/coreos-cloudinit/datasource/url"
2014-03-18 20:00:41 +04:00
"github.com/coreos/coreos-cloudinit/initialize"
2014-06-27 02:17:53 +04:00
"github.com/coreos/coreos-cloudinit/pkg"
2014-03-18 20:00:41 +04:00
"github.com/coreos/coreos-cloudinit/system"
2014-03-05 04:36:05 +04:00
)
2014-06-27 02:17:53 +04:00
const (
2014-08-25 05:40:53 +04:00
version = "0.9.4"
2014-06-27 02:17:53 +04:00
datasourceInterval = 100 * time . Millisecond
datasourceMaxInterval = 30 * time . Second
datasourceTimeout = 5 * time . Minute
)
2014-03-05 05:01:58 +04:00
2014-06-25 00:18:35 +04:00
var (
printVersion bool
ignoreFailure bool
sources struct {
2014-08-05 13:50:21 +04:00
file string
configDrive string
metadataService bool
ec2MetadataService string
cloudSigmaMetadataService bool
url string
procCmdLine bool
2014-06-25 00:18:35 +04:00
}
convertNetconf string
workspace string
sshKeyName string
)
2014-03-05 05:01:58 +04:00
2014-06-25 00:18:35 +04:00
func init ( ) {
flag . BoolVar ( & printVersion , "version" , false , "Print the version and exit" )
2014-03-19 07:47:20 +04:00
flag . BoolVar ( & ignoreFailure , "ignore-failure" , false , "Exits with 0 status in the event of malformed input from user-data" )
2014-06-25 00:18:35 +04:00
flag . StringVar ( & sources . file , "from-file" , "" , "Read user-data from provided file" )
2014-06-21 08:11:57 +04:00
flag . StringVar ( & sources . configDrive , "from-configdrive" , "" , "Read data from provided cloud-drive directory" )
2014-07-31 01:23:10 +04:00
flag . BoolVar ( & sources . metadataService , "from-metadata-service" , false , "[DEPRECATED - Use -from-ec2-metadata] Download data from metadata service" )
flag . StringVar ( & sources . ec2MetadataService , "from-ec2-metadata" , "" , "Download data from the provided metadata service" )
2014-08-05 13:50:21 +04:00
flag . BoolVar ( & sources . cloudSigmaMetadataService , "from-cloudsigma-metadata" , false , "Download data from CloudSigma server context" )
2014-06-25 00:18:35 +04:00
flag . StringVar ( & sources . url , "from-url" , "" , "Download user-data from provided url" )
2014-07-31 00:56:36 +04:00
flag . BoolVar ( & sources . procCmdLine , "from-proc-cmdline" , false , fmt . Sprintf ( "Parse %s for '%s=<url>', using the cloud-config served by an HTTP GET to <url>" , proc_cmdline . ProcCmdlineLocation , proc_cmdline . ProcCmdlineCloudConfigFlag ) )
2014-05-23 02:00:41 +04:00
flag . StringVar ( & convertNetconf , "convert-netconf" , "" , "Read the network config provided in cloud-drive and translate it from the specified format into networkd unit files (requires the -from-configdrive flag)" )
2014-03-05 04:36:05 +04:00
flag . StringVar ( & workspace , "workspace" , "/var/lib/coreos-cloudinit" , "Base directory coreos-cloudinit should use to store data" )
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-06-25 00:18:35 +04:00
}
2014-03-06 02:30:38 +04:00
2014-06-25 00:18:35 +04:00
func main ( ) {
2014-03-05 04:36:05 +04:00
flag . Parse ( )
2014-06-25 00:18:35 +04:00
die := func ( ) {
if ignoreFailure {
os . Exit ( 0 )
}
os . Exit ( 1 )
}
2014-03-05 05:01:58 +04:00
if printVersion == true {
fmt . Printf ( "coreos-cloudinit version %s\n" , version )
os . Exit ( 0 )
}
2014-06-26 01:28:11 +04:00
if convertNetconf != "" && sources . configDrive == "" {
fmt . Println ( "-convert-netconf flag requires -from-configdrive" )
2014-05-23 02:00:41 +04:00
os . Exit ( 1 )
}
switch convertNetconf {
case "" :
case "debian" :
default :
fmt . Printf ( "Invalid option to -convert-netconf: '%s'. Supported options: 'debian'\n" , convertNetconf )
2014-03-05 05:06:52 +04:00
os . Exit ( 1 )
2014-03-05 04:36:05 +04:00
}
2014-06-27 02:17:53 +04:00
dss := getDatasources ( )
if len ( dss ) == 0 {
2014-08-05 13:50:21 +04:00
fmt . Println ( "Provide at least one of --from-file, --from-configdrive, --from-ec2-metadata, --from-cloudsigma-metadata, --from-url or --from-proc-cmdline" )
2014-06-25 00:18:35 +04:00
os . Exit ( 1 )
}
2014-06-27 02:17:53 +04:00
ds := selectDatasource ( dss )
if ds == nil {
fmt . Println ( "No datasources available in time" )
die ( )
}
2014-05-23 02:00:41 +04:00
fmt . Printf ( "Fetching user-data from datasource of type %q\n" , ds . Type ( ) )
2014-06-18 22:58:18 +04:00
userdataBytes , err := ds . FetchUserdata ( )
2014-03-18 20:00:41 +04:00
if err != nil {
2014-05-23 02:00:41 +04:00
fmt . Printf ( "Failed fetching user-data from datasource: %v\n" , err )
2014-06-25 00:18:35 +04:00
die ( )
2014-03-18 20:00:41 +04:00
}
2014-06-18 23:08:10 +04:00
fmt . Printf ( "Fetching meta-data from datasource of type %q\n" , ds . Type ( ) )
metadataBytes , err := ds . FetchMetadata ( )
if err != nil {
fmt . Printf ( "Failed fetching meta-data from datasource: %v\n" , err )
2014-06-25 00:18:35 +04:00
die ( )
2014-06-18 23:08:10 +04:00
}
2014-06-27 04:17:58 +04:00
// Extract IPv4 addresses from metadata if possible
2014-06-25 04:46:06 +04:00
var subs map [ string ] string
if len ( metadataBytes ) > 0 {
subs , err = initialize . ExtractIPsFromMetadata ( metadataBytes )
if err != nil {
fmt . Printf ( "Failed extracting IPs from meta-data: %v\n" , err )
die ( )
}
}
2014-06-27 04:17:58 +04:00
2014-07-01 23:08:42 +04:00
// Apply environment to user-data
2014-06-25 04:46:06 +04:00
env := initialize . NewEnvironment ( "/" , ds . ConfigRoot ( ) , workspace , convertNetconf , sshKeyName , subs )
2014-07-01 23:08:42 +04:00
userdata := env . Apply ( string ( userdataBytes ) )
2014-06-25 04:46:06 +04:00
2014-06-27 04:17:58 +04:00
var ccm , ccu * initialize . CloudConfig
2014-06-28 10:58:27 +04:00
var script * system . Script
2014-06-27 04:17:58 +04:00
if ccm , err = initialize . ParseMetaData ( string ( metadataBytes ) ) ; err != nil {
fmt . Printf ( "Failed to parse meta-data: %v\n" , err )
die ( )
}
2014-07-01 23:08:42 +04:00
if ud , err := initialize . ParseUserData ( userdata ) ; err != nil {
2014-06-27 04:17:58 +04:00
fmt . Printf ( "Failed to parse user-data: %v\n" , err )
die ( )
} else {
switch t := ud . ( type ) {
case * initialize . CloudConfig :
ccu = t
case system . Script :
2014-06-28 10:58:27 +04:00
script = & t
2014-06-25 04:39:48 +04:00
}
2014-06-27 04:17:58 +04:00
}
var cc * initialize . CloudConfig
if ccm != nil && ccu != nil {
fmt . Println ( "Merging cloud-config from meta-data and user-data" )
2014-07-01 09:02:41 +04:00
merged := mergeCloudConfig ( * ccm , * ccu )
2014-06-27 04:17:58 +04:00
cc = & merged
} else if ccm != nil && ccu == nil {
fmt . Println ( "Processing cloud-config from meta-data" )
cc = ccm
} else if ccm == nil && ccu != nil {
fmt . Println ( "Processing cloud-config from user-data" )
cc = ccu
2014-06-25 04:39:48 +04:00
} else {
2014-06-27 04:17:58 +04:00
fmt . Println ( "No cloud-config data to handle." )
2014-06-25 04:39:48 +04:00
}
2014-06-27 04:17:58 +04:00
if cc != nil {
if err = initialize . Apply ( * cc , env ) ; err != nil {
fmt . Printf ( "Failed to apply cloud-config: %v\n" , err )
die ( )
2014-05-23 01:02:10 +04:00
}
2014-03-14 08:12:14 +04:00
}
2014-06-27 04:17:58 +04:00
if script != nil {
2014-06-28 10:58:27 +04:00
if err = runScript ( * script , env ) ; err != nil {
2014-06-27 04:17:58 +04:00
fmt . Printf ( "Failed to run script: %v\n" , err )
die ( )
}
}
}
// mergeCloudConfig merges certain options from mdcc (a CloudConfig derived from
// meta-data) onto udcc (a CloudConfig derived from user-data), if they are
// not already set on udcc (i.e. user-data always takes precedence)
// NB: This needs to be kept in sync with ParseMetadata so that it tracks all
// elements of a CloudConfig which that function can populate.
func mergeCloudConfig ( mdcc , udcc initialize . CloudConfig ) ( cc initialize . CloudConfig ) {
if mdcc . Hostname != "" {
if udcc . Hostname != "" {
2014-07-31 00:56:36 +04:00
fmt . Printf ( "Warning: user-data hostname (%s) overrides metadata hostname (%s)\n" , udcc . Hostname , mdcc . Hostname )
2014-06-27 04:17:58 +04:00
} else {
udcc . Hostname = mdcc . Hostname
}
}
for _ , key := range mdcc . SSHAuthorizedKeys {
udcc . SSHAuthorizedKeys = append ( udcc . SSHAuthorizedKeys , key )
}
if mdcc . NetworkConfigPath != "" {
if udcc . NetworkConfigPath != "" {
2014-07-31 00:56:36 +04:00
fmt . Printf ( "Warning: user-data NetworkConfigPath %s overrides metadata NetworkConfigPath %s\n" , udcc . NetworkConfigPath , mdcc . NetworkConfigPath )
2014-06-27 04:17:58 +04:00
} else {
udcc . NetworkConfigPath = mdcc . NetworkConfigPath
}
}
return udcc
2014-05-23 01:02:10 +04:00
}
2014-03-21 21:35:18 +04:00
2014-06-27 04:17:58 +04:00
// getDatasources creates a slice of possible Datasources for cloudinit based
// on the different source command-line flags.
2014-06-27 02:17:53 +04:00
func getDatasources ( ) [ ] datasource . Datasource {
dss := make ( [ ] datasource . Datasource , 0 , 5 )
2014-06-25 00:18:35 +04:00
if sources . file != "" {
2014-07-31 00:56:36 +04:00
dss = append ( dss , file . NewDatasource ( sources . file ) )
2014-06-25 00:18:35 +04:00
}
if sources . url != "" {
2014-07-31 00:56:36 +04:00
dss = append ( dss , url . NewDatasource ( sources . url ) )
2014-06-25 00:18:35 +04:00
}
if sources . configDrive != "" {
2014-07-31 00:56:36 +04:00
dss = append ( dss , configdrive . NewDatasource ( sources . configDrive ) )
2014-06-25 00:18:35 +04:00
}
2014-06-25 04:56:05 +04:00
if sources . metadataService {
2014-07-31 01:23:10 +04:00
dss = append ( dss , ec2 . NewDatasource ( ec2 . DefaultAddress ) )
}
if sources . ec2MetadataService != "" {
dss = append ( dss , ec2 . NewDatasource ( sources . ec2MetadataService ) )
2014-06-21 08:11:57 +04:00
}
2014-08-05 13:50:21 +04:00
if sources . cloudSigmaMetadataService {
dss = append ( dss , cloudsigma . NewServerContextService ( ) )
}
2014-06-25 00:18:35 +04:00
if sources . procCmdLine {
2014-07-31 00:56:36 +04:00
dss = append ( dss , proc_cmdline . NewDatasource ( ) )
2014-06-25 00:18:35 +04:00
}
2014-06-27 02:17:53 +04:00
return dss
}
2014-06-27 04:17:58 +04:00
// selectDatasource attempts to choose a valid Datasource to use based on its
// current availability. The first Datasource to report to be available is
// returned. Datasources will be retried if possible if they are not
// immediately available. If all Datasources are permanently unavailable or
// datasourceTimeout is reached before one becomes available, nil is returned.
2014-06-27 02:17:53 +04:00
func selectDatasource ( sources [ ] datasource . Datasource ) datasource . Datasource {
ds := make ( chan datasource . Datasource )
stop := make ( chan struct { } )
var wg sync . WaitGroup
for _ , s := range sources {
wg . Add ( 1 )
go func ( s datasource . Datasource ) {
defer wg . Done ( )
duration := datasourceInterval
for {
fmt . Printf ( "Checking availability of %q\n" , s . Type ( ) )
if s . IsAvailable ( ) {
ds <- s
return
} else if ! s . AvailabilityChanges ( ) {
return
}
select {
case <- stop :
return
2014-07-03 05:25:47 +04:00
case <- time . After ( duration ) :
2014-06-27 02:17:53 +04:00
duration = pkg . ExpBackoff ( duration , datasourceMaxInterval )
}
}
} ( s )
}
done := make ( chan struct { } )
go func ( ) {
wg . Wait ( )
close ( done )
} ( )
var s datasource . Datasource
select {
case s = <- ds :
case <- done :
2014-07-03 05:25:47 +04:00
case <- time . After ( datasourceTimeout ) :
2014-06-25 00:18:35 +04:00
}
2014-06-27 02:17:53 +04:00
close ( stop )
return s
2014-06-25 00:18:35 +04:00
}
2014-06-27 04:17:58 +04:00
// TODO(jonboulle): this should probably be refactored and moved into a different module
func runScript ( script system . Script , env * initialize . Environment ) error {
err := initialize . PrepWorkspace ( env . Workspace ( ) )
2014-03-05 04:36:05 +04:00
if err != nil {
2014-05-23 02:00:41 +04:00
fmt . Printf ( "Failed preparing workspace: %v\n" , err )
return err
2014-03-05 04:36:05 +04:00
}
2014-06-27 04:17:58 +04:00
path , err := initialize . PersistScriptInWorkspace ( script , env . Workspace ( ) )
if err == nil {
var name string
name , err = system . ExecuteScript ( path )
initialize . PersistUnitNameInWorkspace ( name , env . Workspace ( ) )
2014-03-05 04:36:05 +04:00
}
2014-05-23 01:02:10 +04:00
return err
2014-03-05 04:36:05 +04:00
}