2014-03-18 20:00:41 +04:00
|
|
|
package initialize
|
|
|
|
|
|
|
|
import (
|
2014-03-21 21:35:18 +04:00
|
|
|
"os"
|
2014-03-18 20:00:41 +04:00
|
|
|
"path"
|
2014-03-21 21:35:18 +04:00
|
|
|
"strings"
|
2014-03-18 20:00:41 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
const DefaultSSHKeyName = "coreos-cloudinit"
|
|
|
|
|
|
|
|
type Environment struct {
|
2014-03-21 21:35:18 +04:00
|
|
|
root string
|
2014-06-18 22:36:06 +04:00
|
|
|
configRoot string
|
2014-03-21 21:35:18 +04:00
|
|
|
workspace string
|
2014-06-18 22:36:06 +04:00
|
|
|
netconfType string
|
2014-03-21 21:35:18 +04:00
|
|
|
sshKeyName string
|
|
|
|
substitutions map[string]string
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
2014-06-18 22:36:06 +04:00
|
|
|
func NewEnvironment(root, configRoot, workspace, netconfType, sshKeyName string) *Environment {
|
2014-03-21 21:35:18 +04:00
|
|
|
substitutions := map[string]string{
|
|
|
|
"$public_ipv4": os.Getenv("COREOS_PUBLIC_IPV4"),
|
|
|
|
"$private_ipv4": os.Getenv("COREOS_PRIVATE_IPV4"),
|
|
|
|
}
|
2014-06-18 22:36:06 +04:00
|
|
|
return &Environment{root, configRoot, workspace, netconfType, sshKeyName, substitutions}
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Environment) Workspace() string {
|
|
|
|
return path.Join(self.root, self.workspace)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Environment) Root() string {
|
|
|
|
return self.root
|
|
|
|
}
|
|
|
|
|
2014-06-18 22:36:06 +04:00
|
|
|
func (self *Environment) ConfigRoot() string {
|
|
|
|
return self.configRoot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Environment) NetconfType() string {
|
|
|
|
return self.netconfType
|
|
|
|
}
|
|
|
|
|
2014-03-18 20:00:41 +04:00
|
|
|
func (self *Environment) SSHKeyName() string {
|
|
|
|
return self.sshKeyName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *Environment) SetSSHKeyName(name string) {
|
|
|
|
self.sshKeyName = name
|
|
|
|
}
|
2014-03-21 21:35:18 +04:00
|
|
|
|
|
|
|
func (self *Environment) Apply(data string) string {
|
|
|
|
for key, val := range self.substitutions {
|
|
|
|
data = strings.Replace(data, key, val, -1)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
|
|
|
|
// normalizeSvcEnv standardizes the keys of the map (environment variables for a service)
|
|
|
|
// by replacing any dashes with underscores and ensuring they are entirely upper case.
|
|
|
|
// For example, "some-env" --> "SOME_ENV"
|
|
|
|
func normalizeSvcEnv(m map[string]string) map[string]string {
|
|
|
|
out := make(map[string]string, len(m))
|
|
|
|
for key, val := range m {
|
|
|
|
key = strings.ToUpper(key)
|
|
|
|
key = strings.Replace(key, "-", "_", -1)
|
|
|
|
out[key] = val
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|