diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index 8c55202..99ceaac 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -89,7 +89,7 @@ func main() { } } - env := initialize.NewEnvironment("/", workspace, sshKeyName) + env := initialize.NewEnvironment("/", ds.ConfigRoot(), workspace, convertNetconf, sshKeyName) if len(userdataBytes) > 0 { if err := processUserdata(string(userdataBytes), env); err != nil { fmt.Printf("Failed resolving user-data: %v\n", err) diff --git a/datasource/configdrive.go b/datasource/configdrive.go index 7459471..7a6f6de 100644 --- a/datasource/configdrive.go +++ b/datasource/configdrive.go @@ -7,21 +7,29 @@ import ( ) type configDrive struct { - path string + root string } -func NewConfigDrive(path string) *configDrive { - return &configDrive{path} +func NewConfigDrive(root string) *configDrive { + return &configDrive{path.Join(root, "openstack")} +} + +func (self *configDrive) ConfigRoot() string { + return self.root } func (self *configDrive) Fetch() ([]byte, error) { - data, err := ioutil.ReadFile(path.Join(self.path, "openstack", "latest", "user_data")) - if os.IsNotExist(err) { - err = nil - } - return data, err + return self.readFile("user_data") } func (self *configDrive) Type() string { return "cloud-drive" } + +func (self *configDrive) readFile(filename string) ([]byte, error) { + data, err := ioutil.ReadFile(path.Join(self.root, "latest", filename)) + if os.IsNotExist(err) { + err = nil + } + return data, err +} diff --git a/datasource/datasource.go b/datasource/datasource.go index e3d2834..38d65fc 100644 --- a/datasource/datasource.go +++ b/datasource/datasource.go @@ -1,6 +1,7 @@ package datasource type Datasource interface { + ConfigRoot() string Fetch() ([]byte, error) Type() string } diff --git a/datasource/file.go b/datasource/file.go index c5cb1ae..24b57ef 100644 --- a/datasource/file.go +++ b/datasource/file.go @@ -12,6 +12,10 @@ func NewLocalFile(path string) *localFile { return &localFile{path} } +func (self *localFile) ConfigRoot() string { + return "" +} + func (self *localFile) Fetch() ([]byte, error) { return ioutil.ReadFile(self.path) } diff --git a/datasource/metadata_service.go b/datasource/metadata_service.go index 4ed036a..72b2665 100644 --- a/datasource/metadata_service.go +++ b/datasource/metadata_service.go @@ -10,6 +10,10 @@ func NewMetadataService(url string) *metadataService { return &metadataService{url} } +func (self *metadataService) ConfigRoot() string { + return "" +} + func (ms *metadataService) Fetch() ([]byte, error) { client := pkg.NewHttpClient() return client.Get(ms.url) diff --git a/datasource/proc_cmdline.go b/datasource/proc_cmdline.go index c389da3..3f861d0 100644 --- a/datasource/proc_cmdline.go +++ b/datasource/proc_cmdline.go @@ -14,7 +14,7 @@ const ( ProcCmdlineCloudConfigFlag = "cloud-config-url" ) -type procCmdline struct{ +type procCmdline struct { Location string } @@ -22,6 +22,10 @@ func NewProcCmdline() *procCmdline { return &procCmdline{Location: ProcCmdlineLocation} } +func (self *procCmdline) ConfigRoot() string { + return "" +} + func (self *procCmdline) Fetch() ([]byte, error) { contents, err := ioutil.ReadFile(self.Location) if err != nil { diff --git a/initialize/env.go b/initialize/env.go index b0ab3c1..948e51d 100644 --- a/initialize/env.go +++ b/initialize/env.go @@ -10,17 +10,19 @@ const DefaultSSHKeyName = "coreos-cloudinit" type Environment struct { root string + configRoot string workspace string + netconfType string sshKeyName string substitutions map[string]string } -func NewEnvironment(root, workspace, sshKeyName string) *Environment { +func NewEnvironment(root, configRoot, workspace, netconfType, sshKeyName string) *Environment { substitutions := map[string]string{ "$public_ipv4": os.Getenv("COREOS_PUBLIC_IPV4"), "$private_ipv4": os.Getenv("COREOS_PRIVATE_IPV4"), } - return &Environment{root, workspace, sshKeyName, substitutions} + return &Environment{root, configRoot, workspace, netconfType, sshKeyName, substitutions} } func (self *Environment) Workspace() string { @@ -31,6 +33,14 @@ func (self *Environment) Root() string { return self.root } +func (self *Environment) ConfigRoot() string { + return self.configRoot +} + +func (self *Environment) NetconfType() string { + return self.netconfType +} + func (self *Environment) SSHKeyName() string { return self.sshKeyName } diff --git a/initialize/env_test.go b/initialize/env_test.go index f10a054..33ff933 100644 --- a/initialize/env_test.go +++ b/initialize/env_test.go @@ -8,7 +8,7 @@ import ( func TestEnvironmentApply(t *testing.T) { os.Setenv("COREOS_PUBLIC_IPV4", "192.0.2.3") os.Setenv("COREOS_PRIVATE_IPV4", "192.0.2.203") - env := NewEnvironment("./", "./", "") + env := NewEnvironment("./", "./", "./", "", "") input := `[Service] ExecStart=/usr/bin/echo "$public_ipv4" ExecStop=/usr/bin/echo $private_ipv4