diff --git a/config/config.go b/config/config.go index f22f699..bb1d771 100644 --- a/config/config.go +++ b/config/config.go @@ -27,13 +27,12 @@ import ( // directly to YAML. Fields that cannot be set in the cloud-config (fields // used for internal use) have the YAML tag '-' so that they aren't marshalled. type CloudConfig struct { - SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"` - CoreOS CoreOS `yaml:"coreos"` - WriteFiles []File `yaml:"write_files"` - Hostname string `yaml:"hostname"` - Users []User `yaml:"users"` - ManageEtcHosts EtcHosts `yaml:"manage_etc_hosts"` - Internal Internals `yaml:"-"` + SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"` + CoreOS CoreOS `yaml:"coreos"` + WriteFiles []File `yaml:"write_files"` + Hostname string `yaml:"hostname"` + Users []User `yaml:"users"` + ManageEtcHosts EtcHosts `yaml:"manage_etc_hosts"` } type CoreOS struct { @@ -46,10 +45,6 @@ type CoreOS struct { Units []Unit `yaml:"units"` } -type Internals struct { - NetworkConfig []byte -} - func IsCloudConfig(userdata string) bool { header := strings.SplitN(userdata, "\n", 2)[0] diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index 963cb0a..7e0eec4 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -33,6 +33,7 @@ import ( "github.com/coreos/coreos-cloudinit/datasource/url" "github.com/coreos/coreos-cloudinit/datasource/waagent" "github.com/coreos/coreos-cloudinit/initialize" + "github.com/coreos/coreos-cloudinit/network" "github.com/coreos/coreos-cloudinit/pkg" "github.com/coreos/coreos-cloudinit/system" ) @@ -183,7 +184,7 @@ func main() { } // Apply environment to user-data - env := initialize.NewEnvironment("/", ds.ConfigRoot(), flags.workspace, flags.convertNetconf, flags.sshKeyName, metadata) + env := initialize.NewEnvironment("/", ds.ConfigRoot(), flags.workspace, flags.sshKeyName, metadata) userdata := env.Apply(string(userdataBytes)) var ccu *config.CloudConfig @@ -203,7 +204,24 @@ func main() { fmt.Println("Merging cloud-config from meta-data and user-data") cc := mergeConfigs(ccu, metadata) - if err = initialize.Apply(cc, env); err != nil { + var ifaces []network.InterfaceGenerator + if flags.convertNetconf != "" { + var err error + switch flags.convertNetconf { + case "debian": + ifaces, err = network.ProcessDebianNetconf(metadata.NetworkConfig) + case "digitalocean": + ifaces, err = network.ProcessDigitalOceanNetconf(metadata.NetworkConfig) + default: + err = fmt.Errorf("Unsupported network config format %q", flags.convertNetconf) + } + if err != nil { + fmt.Printf("Failed to generate interfaces: %v\n", err) + os.Exit(1) + } + } + + if err = initialize.Apply(cc, ifaces, env); err != nil { fmt.Printf("Failed to apply cloud-config: %v\n", err) os.Exit(1) } @@ -238,7 +256,6 @@ func mergeConfigs(cc *config.CloudConfig, md datasource.Metadata) (out config.Cl for _, key := range md.SSHPublicKeys { out.SSHAuthorizedKeys = append(out.SSHAuthorizedKeys, key) } - out.Internal.NetworkConfig = md.NetworkConfig return } diff --git a/initialize/config.go b/initialize/config.go index f0fc264..3bf7f54 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -42,7 +42,7 @@ type CloudConfigUnit interface { // Apply renders a CloudConfig to an Environment. This can involve things like // configuring the hostname, adding new users, writing various configuration // files to disk, and manipulating systemd services. -func Apply(cfg config.CloudConfig, env *Environment) error { +func Apply(cfg config.CloudConfig, ifaces []network.InterfaceGenerator, env *Environment) error { if cfg.Hostname != "" { if err := system.SetHostname(cfg.Hostname); err != nil { return err @@ -165,23 +165,9 @@ func Apply(cfg config.CloudConfig, env *Environment) error { } } - if env.NetconfType() != "" { - var interfaces []network.InterfaceGenerator - var err error - switch env.NetconfType() { - case "debian": - interfaces, err = network.ProcessDebianNetconf(cfg.Internal.NetworkConfig) - case "digitalocean": - interfaces, err = network.ProcessDigitalOceanNetconf(cfg.Internal.NetworkConfig) - default: - err = fmt.Errorf("Unsupported network config format %q", env.NetconfType()) - } - if err != nil { - return err - } - - units = append(units, createNetworkingUnits(interfaces)...) - if err := system.RestartNetwork(interfaces); err != nil { + if len(ifaces) > 0 { + units = append(units, createNetworkingUnits(ifaces)...) + if err := system.RestartNetwork(ifaces); err != nil { return err } } diff --git a/initialize/env.go b/initialize/env.go index fc9411f..f90cc93 100644 --- a/initialize/env.go +++ b/initialize/env.go @@ -32,13 +32,12 @@ type Environment struct { root string configRoot string workspace string - netconfType string sshKeyName string substitutions map[string]string } // TODO(jonboulle): this is getting unwieldy, should be able to simplify the interface somehow -func NewEnvironment(root, configRoot, workspace, netconfType, sshKeyName string, metadata datasource.Metadata) *Environment { +func NewEnvironment(root, configRoot, workspace, sshKeyName string, metadata datasource.Metadata) *Environment { firstNonNull := func(ip net.IP, env string) string { if ip == nil { return env @@ -51,7 +50,7 @@ func NewEnvironment(root, configRoot, workspace, netconfType, sshKeyName string, "$public_ipv6": firstNonNull(metadata.PublicIPv6, os.Getenv("COREOS_PUBLIC_IPV6")), "$private_ipv6": firstNonNull(metadata.PrivateIPv6, os.Getenv("COREOS_PRIVATE_IPV6")), } - return &Environment{root, configRoot, workspace, netconfType, sshKeyName, substitutions} + return &Environment{root, configRoot, workspace, sshKeyName, substitutions} } func (e *Environment) Workspace() string { @@ -66,10 +65,6 @@ func (e *Environment) ConfigRoot() string { return e.configRoot } -func (e *Environment) NetconfType() string { - return e.netconfType -} - func (e *Environment) SSHKeyName() string { return e.sshKeyName } diff --git a/initialize/env_test.go b/initialize/env_test.go index d56916a..abb770c 100644 --- a/initialize/env_test.go +++ b/initialize/env_test.go @@ -95,7 +95,7 @@ addr: $private_ipv4 }, } { - env := NewEnvironment("./", "./", "./", "", "", tt.metadata) + env := NewEnvironment("./", "./", "./", "", tt.metadata) got := env.Apply(tt.input) if got != tt.out { t.Fatalf("Environment incorrectly applied.\ngot:\n%s\nwant:\n%s", got, tt.out) @@ -118,7 +118,7 @@ func TestEnvironmentFile(t *testing.T) { } defer os.RemoveAll(dir) - env := NewEnvironment("./", "./", "./", "", "", metadata) + env := NewEnvironment("./", "./", "./", "", metadata) ef := env.DefaultEnvironmentFile() err = system.WriteEnvFile(ef, dir) if err != nil { @@ -140,7 +140,7 @@ func TestEnvironmentFileNil(t *testing.T) { os.Clearenv() metadata := datasource.Metadata{} - env := NewEnvironment("./", "./", "./", "", "", metadata) + env := NewEnvironment("./", "./", "./", "", metadata) ef := env.DefaultEnvironmentFile() if ef != nil { t.Fatalf("Environment file not nil: %v", ef)