Merge pull request #225 from crawford/exit

userdata: change handling of bad userdata
This commit is contained in:
Alex Crawford 2014-09-10 19:16:12 -07:00
commit 24a6f7c49c

View File

@ -62,14 +62,9 @@ func init() {
} }
func main() { func main() {
flag.Parse() failure := false
die := func() { flag.Parse()
if ignoreFailure {
os.Exit(0)
}
os.Exit(1)
}
if printVersion == true { if printVersion == true {
fmt.Printf("coreos-cloudinit version %s\n", version) fmt.Printf("coreos-cloudinit version %s\n", version)
@ -82,33 +77,33 @@ func main() {
case "digitalocean": case "digitalocean":
default: default:
fmt.Printf("Invalid option to -convert-netconf: '%s'. Supported options: 'debian, digitalocean'\n", convertNetconf) fmt.Printf("Invalid option to -convert-netconf: '%s'. Supported options: 'debian, digitalocean'\n", convertNetconf)
os.Exit(1) os.Exit(2)
} }
dss := getDatasources() dss := getDatasources()
if len(dss) == 0 { if len(dss) == 0 {
fmt.Println("Provide at least one of --from-file, --from-configdrive, --from-ec2-metadata, --from-cloudsigma-metadata, --from-url or --from-proc-cmdline") fmt.Println("Provide at least one of --from-file, --from-configdrive, --from-ec2-metadata, --from-cloudsigma-metadata, --from-url or --from-proc-cmdline")
os.Exit(1) os.Exit(2)
} }
ds := selectDatasource(dss) ds := selectDatasource(dss)
if ds == nil { if ds == nil {
fmt.Println("No datasources available in time") fmt.Println("No datasources available in time")
die() os.Exit(1)
} }
fmt.Printf("Fetching user-data from datasource of type %q\n", ds.Type()) fmt.Printf("Fetching user-data from datasource of type %q\n", ds.Type())
userdataBytes, err := ds.FetchUserdata() userdataBytes, err := ds.FetchUserdata()
if err != nil { if err != nil {
fmt.Printf("Failed fetching user-data from datasource: %v\n", err) fmt.Printf("Failed fetching user-data from datasource: %v\nContinuing...\n", err)
die() failure = true
} }
fmt.Printf("Fetching meta-data from datasource of type %q\n", ds.Type()) fmt.Printf("Fetching meta-data from datasource of type %q\n", ds.Type())
metadataBytes, err := ds.FetchMetadata() metadataBytes, err := ds.FetchMetadata()
if err != nil { if err != nil {
fmt.Printf("Failed fetching meta-data from datasource: %v\n", err) fmt.Printf("Failed fetching meta-data from datasource: %v\n", err)
die() os.Exit(1)
} }
// Extract IPv4 addresses from metadata if possible // Extract IPv4 addresses from metadata if possible
@ -117,7 +112,7 @@ func main() {
subs, err = initialize.ExtractIPsFromMetadata(metadataBytes) subs, err = initialize.ExtractIPsFromMetadata(metadataBytes)
if err != nil { if err != nil {
fmt.Printf("Failed extracting IPs from meta-data: %v\n", err) fmt.Printf("Failed extracting IPs from meta-data: %v\n", err)
die() os.Exit(1)
} }
} }
@ -129,7 +124,7 @@ func main() {
var script *system.Script var script *system.Script
if ccm, err = initialize.ParseMetaData(string(metadataBytes)); err != nil { if ccm, err = initialize.ParseMetaData(string(metadataBytes)); err != nil {
fmt.Printf("Failed to parse meta-data: %v\n", err) fmt.Printf("Failed to parse meta-data: %v\n", err)
die() os.Exit(1)
} }
if ccm != nil { if ccm != nil {
@ -137,14 +132,14 @@ func main() {
netconfBytes, err := ds.FetchNetworkConfig(ccm.NetworkConfigPath) netconfBytes, err := ds.FetchNetworkConfig(ccm.NetworkConfigPath)
if err != nil { if err != nil {
fmt.Printf("Failed fetching network config from datasource: %v\n", err) fmt.Printf("Failed fetching network config from datasource: %v\n", err)
die() os.Exit(1)
} }
ccm.NetworkConfig = string(netconfBytes) ccm.NetworkConfig = string(netconfBytes)
} }
if ud, err := initialize.ParseUserData(userdata); err != nil { if ud, err := initialize.ParseUserData(userdata); err != nil {
fmt.Printf("Failed to parse user-data: %v\n", err) fmt.Printf("Failed to parse user-data: %v\nContinuing...\n", err)
die() failure = true
} else { } else {
switch t := ud.(type) { switch t := ud.(type) {
case *initialize.CloudConfig: case *initialize.CloudConfig:
@ -172,16 +167,20 @@ func main() {
if cc != nil { if cc != nil {
if err = initialize.Apply(*cc, env); err != nil { if err = initialize.Apply(*cc, env); err != nil {
fmt.Printf("Failed to apply cloud-config: %v\n", err) fmt.Printf("Failed to apply cloud-config: %v\n", err)
die() os.Exit(1)
} }
} }
if script != nil { if script != nil {
if err = runScript(*script, env); err != nil { if err = runScript(*script, env); err != nil {
fmt.Printf("Failed to run script: %v\n", err) fmt.Printf("Failed to run script: %v\n", err)
die() os.Exit(1)
} }
} }
if failure && !ignoreFailure {
os.Exit(1)
}
} }
// mergeCloudConfig merges certain options from mdcc (a CloudConfig derived from // mergeCloudConfig merges certain options from mdcc (a CloudConfig derived from