cloudinit/initialize/etcd.go

63 lines
1.4 KiB
Go
Raw Normal View History

2014-03-18 20:00:41 +04:00
package initialize
2014-03-05 04:36:05 +04:00
import (
"errors"
2014-03-18 02:09:59 +04:00
"fmt"
2014-03-05 04:36:05 +04:00
"path"
2014-03-18 02:09:59 +04:00
"strings"
2014-03-18 20:00:41 +04:00
"github.com/coreos/coreos-cloudinit/system"
2014-03-05 04:36:05 +04:00
)
2014-03-18 02:09:59 +04:00
type EtcdEnvironment map[string]string
func (ec EtcdEnvironment) normalized() map[string]string {
out := make(map[string]string, len(ec))
for key, val := range ec {
key = strings.ToUpper(key)
key = strings.Replace(key, "-", "_", -1)
out[key] = val
}
return out
}
2014-03-18 02:09:59 +04:00
func (ec EtcdEnvironment) String() (out string) {
norm := ec.normalized()
if val, ok := norm["DISCOVERY_URL"]; ok {
delete(norm, "DISCOVERY_URL")
if _, ok := norm["DISCOVERY"]; !ok {
norm["DISCOVERY"] = val
}
}
2014-03-18 02:09:59 +04:00
out += "[Service]\n"
for key, val := range norm {
2014-03-18 02:09:59 +04:00
out += fmt.Sprintf("Environment=\"ETCD_%s=%s\"\n", key, val)
}
2014-03-18 02:09:59 +04:00
return
}
// Write an EtcdEnvironment to the appropriate path on disk for etcd.service
2014-03-18 20:00:41 +04:00
func WriteEtcdEnvironment(env EtcdEnvironment, root string) error {
if _, ok := env["name"]; !ok {
if machineID := system.MachineID(root); machineID != "" {
env["name"] = machineID
} else if hostname, err := system.Hostname(); err == nil {
env["name"] = hostname
} else {
return errors.New("Unable to determine default etcd name")
}
}
2014-03-18 20:00:41 +04:00
file := system.File{
Path: path.Join(root, "run", "systemd", "system", "etcd.service.d", "20-cloudinit.conf"),
2014-03-18 20:00:41 +04:00
RawFilePermissions: "0644",
Content: env.String(),
2014-03-05 04:36:05 +04:00
}
2014-03-18 20:00:41 +04:00
return system.WriteFile(&file)
2014-03-05 04:36:05 +04:00
}