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-07-11 02:44:52 +04:00
|
|
|
|
|
|
|
"github.com/coreos/coreos-cloudinit/system"
|
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-25 04:46:06 +04:00
|
|
|
// TODO(jonboulle): this is getting unwieldy, should be able to simplify the interface somehow
|
|
|
|
func NewEnvironment(root, configRoot, workspace, netconfType, sshKeyName string, substitutions map[string]string) *Environment {
|
2014-06-25 23:07:48 +04:00
|
|
|
if substitutions == nil {
|
|
|
|
substitutions = make(map[string]string)
|
|
|
|
}
|
2014-06-25 05:49:49 +04:00
|
|
|
// If certain values are not in the supplied substitution, fall back to retrieving them from the environment
|
|
|
|
for k, v := range map[string]string{
|
2014-03-21 21:35:18 +04:00
|
|
|
"$public_ipv4": os.Getenv("COREOS_PUBLIC_IPV4"),
|
|
|
|
"$private_ipv4": os.Getenv("COREOS_PRIVATE_IPV4"),
|
2014-06-25 05:49:49 +04:00
|
|
|
} {
|
|
|
|
if _, ok := substitutions[k]; !ok {
|
|
|
|
substitutions[k] = v
|
|
|
|
}
|
2014-03-21 21:35:18 +04:00
|
|
|
}
|
2014-06-18 22:36:06 +04:00
|
|
|
return &Environment{root, configRoot, workspace, netconfType, sshKeyName, substitutions}
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) Workspace() string {
|
|
|
|
return path.Join(e.root, e.workspace)
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) Root() string {
|
|
|
|
return e.root
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) ConfigRoot() string {
|
|
|
|
return e.configRoot
|
2014-06-18 22:36:06 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) NetconfType() string {
|
|
|
|
return e.netconfType
|
2014-06-18 22:36:06 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) SSHKeyName() string {
|
|
|
|
return e.sshKeyName
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) SetSSHKeyName(name string) {
|
|
|
|
e.sshKeyName = name
|
2014-03-18 20:00:41 +04:00
|
|
|
}
|
2014-03-21 21:35:18 +04:00
|
|
|
|
2014-06-21 08:11:57 +04:00
|
|
|
func (e *Environment) Apply(data string) string {
|
|
|
|
for key, val := range e.substitutions {
|
2014-03-21 21:35:18 +04:00
|
|
|
data = strings.Replace(data, key, val, -1)
|
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|
2014-05-10 07:33:34 +04:00
|
|
|
|
2014-07-11 02:44:52 +04:00
|
|
|
func (e *Environment) DefaultEnvironmentFile() *system.EnvFile {
|
|
|
|
ef := system.EnvFile{
|
|
|
|
File: &system.File{
|
|
|
|
Path: "/etc/environment",
|
|
|
|
},
|
|
|
|
Vars: map[string]string{},
|
|
|
|
}
|
|
|
|
if ip, ok := e.substitutions["$public_ipv4"]; ok && len(ip) > 0 {
|
|
|
|
ef.Vars["COREOS_PUBLIC_IPV4"] = ip
|
|
|
|
}
|
|
|
|
if ip, ok := e.substitutions["$private_ipv4"]; ok && len(ip) > 0 {
|
|
|
|
ef.Vars["COREOS_PRIVATE_IPV4"] = ip
|
|
|
|
}
|
|
|
|
if len(ef.Vars) == 0 {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return &ef
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|