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 err := processUserdata(string(userdataBytes), env); err != nil {
fmt.Printf("Failed resolving user-data: %v\n", err)

View File

@ -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
}

View File

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

View File

@ -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)
}

View File

@ -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)

View File

@ -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 {

View File

@ -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
}

View File

@ -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