56 lines
1.1 KiB
Go
Raw Normal View History

2014-03-18 09:00:41 -07:00
package initialize
2014-03-04 16:36:05 -08:00
import (
"errors"
2014-03-17 15:09:59 -07:00
"fmt"
2014-03-18 09:00:41 -07:00
"github.com/coreos/coreos-cloudinit/system"
2014-03-04 16:36:05 -08:00
)
2014-03-17 15:09:59 -07: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-17 15:09:59 -07:00
out += "[Service]\n"
for key, val := range norm {
2014-03-17 15:09:59 -07:00
out += fmt.Sprintf("Environment=\"ETCD_%s=%s\"\n", key, val)
}
2014-03-17 15:09:59 -07: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-04 16:36:05 -08:00
}