refactor(env): Add the config root and netconf type to datasource and env

This commit is contained in:
Alex Crawford 2014-06-18 11:36:06 -07:00
parent 259c7e1fe2
commit 29ed6b38bd
8 changed files with 44 additions and 13 deletions

View File

@ -89,7 +89,7 @@ func main() {
} }
} }
env := initialize.NewEnvironment("/", workspace, sshKeyName) env := initialize.NewEnvironment("/", ds.ConfigRoot(), workspace, convertNetconf, sshKeyName)
if len(userdataBytes) > 0 { if len(userdataBytes) > 0 {
if err := processUserdata(string(userdataBytes), env); err != nil { if err := processUserdata(string(userdataBytes), env); err != nil {
fmt.Printf("Failed resolving user-data: %v\n", err) fmt.Printf("Failed resolving user-data: %v\n", err)

View File

@ -7,21 +7,29 @@ import (
) )
type configDrive struct { type configDrive struct {
path string root string
} }
func NewConfigDrive(path string) *configDrive { func NewConfigDrive(root string) *configDrive {
return &configDrive{path} return &configDrive{path.Join(root, "openstack")}
}
func (self *configDrive) ConfigRoot() string {
return self.root
} }
func (self *configDrive) Fetch() ([]byte, error) { func (self *configDrive) Fetch() ([]byte, error) {
data, err := ioutil.ReadFile(path.Join(self.path, "openstack", "latest", "user_data")) return self.readFile("user_data")
if os.IsNotExist(err) {
err = nil
}
return data, err
} }
func (self *configDrive) Type() string { func (self *configDrive) Type() string {
return "cloud-drive" 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
}

View File

@ -1,6 +1,7 @@
package datasource package datasource
type Datasource interface { type Datasource interface {
ConfigRoot() string
Fetch() ([]byte, error) Fetch() ([]byte, error)
Type() string Type() string
} }

View File

@ -12,6 +12,10 @@ func NewLocalFile(path string) *localFile {
return &localFile{path} return &localFile{path}
} }
func (self *localFile) ConfigRoot() string {
return ""
}
func (self *localFile) Fetch() ([]byte, error) { func (self *localFile) Fetch() ([]byte, error) {
return ioutil.ReadFile(self.path) return ioutil.ReadFile(self.path)
} }

View File

@ -10,6 +10,10 @@ func NewMetadataService(url string) *metadataService {
return &metadataService{url} return &metadataService{url}
} }
func (self *metadataService) ConfigRoot() string {
return ""
}
func (ms *metadataService) Fetch() ([]byte, error) { func (ms *metadataService) Fetch() ([]byte, error) {
client := pkg.NewHttpClient() client := pkg.NewHttpClient()
return client.Get(ms.url) return client.Get(ms.url)

View File

@ -14,7 +14,7 @@ const (
ProcCmdlineCloudConfigFlag = "cloud-config-url" ProcCmdlineCloudConfigFlag = "cloud-config-url"
) )
type procCmdline struct{ type procCmdline struct {
Location string Location string
} }
@ -22,6 +22,10 @@ func NewProcCmdline() *procCmdline {
return &procCmdline{Location: ProcCmdlineLocation} return &procCmdline{Location: ProcCmdlineLocation}
} }
func (self *procCmdline) ConfigRoot() string {
return ""
}
func (self *procCmdline) Fetch() ([]byte, error) { func (self *procCmdline) Fetch() ([]byte, error) {
contents, err := ioutil.ReadFile(self.Location) contents, err := ioutil.ReadFile(self.Location)
if err != nil { if err != nil {

View File

@ -10,17 +10,19 @@ const DefaultSSHKeyName = "coreos-cloudinit"
type Environment struct { type Environment struct {
root string root string
configRoot string
workspace string workspace string
netconfType string
sshKeyName string sshKeyName string
substitutions map[string]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{ substitutions := map[string]string{
"$public_ipv4": os.Getenv("COREOS_PUBLIC_IPV4"), "$public_ipv4": os.Getenv("COREOS_PUBLIC_IPV4"),
"$private_ipv4": os.Getenv("COREOS_PRIVATE_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 { func (self *Environment) Workspace() string {
@ -31,6 +33,14 @@ func (self *Environment) Root() string {
return self.root return self.root
} }
func (self *Environment) ConfigRoot() string {
return self.configRoot
}
func (self *Environment) NetconfType() string {
return self.netconfType
}
func (self *Environment) SSHKeyName() string { func (self *Environment) SSHKeyName() string {
return self.sshKeyName return self.sshKeyName
} }

View File

@ -8,7 +8,7 @@ import (
func TestEnvironmentApply(t *testing.T) { func TestEnvironmentApply(t *testing.T) {
os.Setenv("COREOS_PUBLIC_IPV4", "192.0.2.3") os.Setenv("COREOS_PUBLIC_IPV4", "192.0.2.3")
os.Setenv("COREOS_PRIVATE_IPV4", "192.0.2.203") os.Setenv("COREOS_PRIVATE_IPV4", "192.0.2.203")
env := NewEnvironment("./", "./", "") env := NewEnvironment("./", "./", "./", "", "")
input := `[Service] input := `[Service]
ExecStart=/usr/bin/echo "$public_ipv4" ExecStart=/usr/bin/echo "$public_ipv4"
ExecStop=/usr/bin/echo $private_ipv4 ExecStop=/usr/bin/echo $private_ipv4