From 604ef7ecb44decd606f68909f3a609c6d741788c Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Mon, 18 Aug 2014 12:20:25 -0700 Subject: [PATCH] datasource: Add FetchNetworkConfig FetchNetworkConfig is currently only used by ConfigDrive to read the network config file from the disk. --- coreos-cloudinit.go | 11 +++++++++++ datasource/configdrive/configdrive.go | 4 ++++ datasource/datasource.go | 1 + datasource/file/file.go | 4 ++++ datasource/metadata/cloudsigma/server_context.go | 4 ++++ datasource/metadata/metadata.go | 4 ++++ datasource/proc_cmdline/proc_cmdline.go | 4 ++++ datasource/url/url.go | 8 +++++++- initialize/config.go | 12 +++--------- 9 files changed, 42 insertions(+), 10 deletions(-) diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index 2296833..0668924 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -132,6 +132,17 @@ func main() { fmt.Printf("Failed to parse meta-data: %v\n", err) die() } + + if ccm != nil { + fmt.Printf("Fetching network config from datasource of type %q\n", ds.Type()) + netconfBytes, err := ds.FetchNetworkConfig(ccm.NetworkConfigPath) + if err != nil { + fmt.Printf("Failed fetching network config from datasource: %v\n", err) + die() + } + ccm.NetworkConfig = string(netconfBytes) + } + if ud, err := initialize.ParseUserData(userdata); err != nil { fmt.Printf("Failed to parse user-data: %v\n", err) die() diff --git a/datasource/configdrive/configdrive.go b/datasource/configdrive/configdrive.go index 1baebf1..81d401e 100644 --- a/datasource/configdrive/configdrive.go +++ b/datasource/configdrive/configdrive.go @@ -41,6 +41,10 @@ func (cd *configDrive) FetchUserdata() ([]byte, error) { return cd.tryReadFile(path.Join(cd.openstackVersionRoot(), "user_data")) } +func (cd *configDrive) FetchNetworkConfig(filename string) ([]byte, error) { + return cd.tryReadFile(path.Join(cd.openstackRoot(), filename)) +} + func (cd *configDrive) Type() string { return "cloud-drive" } diff --git a/datasource/datasource.go b/datasource/datasource.go index 7146925..ec30cca 100644 --- a/datasource/datasource.go +++ b/datasource/datasource.go @@ -6,5 +6,6 @@ type Datasource interface { ConfigRoot() string FetchMetadata() ([]byte, error) FetchUserdata() ([]byte, error) + FetchNetworkConfig(string) ([]byte, error) Type() string } diff --git a/datasource/file/file.go b/datasource/file/file.go index b629641..6af928c 100644 --- a/datasource/file/file.go +++ b/datasource/file/file.go @@ -34,6 +34,10 @@ func (f *localFile) FetchUserdata() ([]byte, error) { return ioutil.ReadFile(f.path) } +func (f *localFile) FetchNetworkConfig(filename string) ([]byte, error) { + return nil, nil +} + func (f *localFile) Type() string { return "local-file" } diff --git a/datasource/metadata/cloudsigma/server_context.go b/datasource/metadata/cloudsigma/server_context.go index 4bd9d0b..005c762 100644 --- a/datasource/metadata/cloudsigma/server_context.go +++ b/datasource/metadata/cloudsigma/server_context.go @@ -126,6 +126,10 @@ func (scs *serverContextService) FetchUserdata() ([]byte, error) { return []byte(userData), nil } +func (scs *serverContextService) FetchNetworkConfig(a string) ([]byte, error) { + return nil, nil +} + func isBase64Encoded(field string, userdata map[string]string) bool { base64Fields, ok := userdata["base64_fields"] if !ok { diff --git a/datasource/metadata/metadata.go b/datasource/metadata/metadata.go index 9f58bdb..9309b73 100644 --- a/datasource/metadata/metadata.go +++ b/datasource/metadata/metadata.go @@ -38,6 +38,10 @@ func (ms MetadataService) FetchUserdata() ([]byte, error) { return ms.FetchData(ms.UserdataUrl()) } +func (ms MetadataService) FetchNetworkConfig(filename string) ([]byte, error) { + return nil, nil +} + func (ms MetadataService) FetchData(url string) ([]byte, error) { if data, err := ms.Client.GetRetry(url); err == nil { return data, err diff --git a/datasource/proc_cmdline/proc_cmdline.go b/datasource/proc_cmdline/proc_cmdline.go index 02205c3..4b113f6 100644 --- a/datasource/proc_cmdline/proc_cmdline.go +++ b/datasource/proc_cmdline/proc_cmdline.go @@ -66,6 +66,10 @@ func (c *procCmdline) FetchUserdata() ([]byte, error) { return cfg, nil } +func (c *procCmdline) FetchNetworkConfig(filename string) ([]byte, error) { + return nil, nil +} + func (c *procCmdline) Type() string { return "proc-cmdline" } diff --git a/datasource/url/url.go b/datasource/url/url.go index ed73ba4..9a31056 100644 --- a/datasource/url/url.go +++ b/datasource/url/url.go @@ -1,6 +1,8 @@ package url -import "github.com/coreos/coreos-cloudinit/pkg" +import ( + "github.com/coreos/coreos-cloudinit/pkg" +) type remoteFile struct { url string @@ -33,6 +35,10 @@ func (f *remoteFile) FetchUserdata() ([]byte, error) { return client.GetRetry(f.url) } +func (f *remoteFile) FetchNetworkConfig(filename string) ([]byte, error) { + return nil, nil +} + func (f *remoteFile) Type() string { return "url" } diff --git a/initialize/config.go b/initialize/config.go index bf7a2da..3d07328 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -3,7 +3,6 @@ package initialize import ( "errors" "fmt" - "io/ioutil" "log" "path" @@ -42,6 +41,7 @@ type CloudConfig struct { Users []system.User ManageEtcHosts EtcHosts `yaml:"manage_etc_hosts"` NetworkConfigPath string + NetworkConfig string } type warner func(format string, v ...interface{}) @@ -258,17 +258,11 @@ func Apply(cfg CloudConfig, env *Environment) error { } if env.NetconfType() != "" { - filename := path.Join(env.ConfigRoot(), cfg.NetworkConfigPath) - log.Printf("Attempting to read config from %q\n", filename) - netconfBytes, err := ioutil.ReadFile(filename) - if err != nil { - return err - } - var interfaces []network.InterfaceGenerator + var err error switch env.NetconfType() { case "debian": - interfaces, err = network.ProcessDebianNetconf(string(netconfBytes)) + interfaces, err = network.ProcessDebianNetconf(cfg.NetworkConfig) default: return fmt.Errorf("Unsupported network config format %q", env.NetconfType()) }