cloudinit/initialize/etcd.go

56 lines
1.1 KiB
Go
Raw Permalink 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-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 (ee EtcdEnvironment) String() (out string) {
norm := normalizeSvcEnv(ee)
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
}
// Units creates a Unit file drop-in for etcd, using any configured
// options and adding a default MachineID if unset.
func (ee EtcdEnvironment) Units(root string) ([]system.Unit, error) {
if ee == nil {
return nil, nil
}
if _, ok := ee["name"]; !ok {
if machineID := system.MachineID(root); machineID != "" {
ee["name"] = machineID
} else if hostname, err := system.Hostname(); err == nil {
ee["name"] = hostname
} else {
return nil, errors.New("Unable to determine default etcd name")
}
}
etcd := system.Unit{
Name: "etcd.service",
Runtime: true,
DropIn: true,
Content: ee.String(),
}
return []system.Unit{etcd}, nil
2014-03-05 04:36:05 +04:00
}