2014-03-18 20:00:41 +04:00
|
|
|
package initialize
|
2014-03-05 04:36:05 +04:00
|
|
|
|
|
|
|
import (
|
2014-03-18 21:57:10 +04:00
|
|
|
"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
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
func (ee EtcdEnvironment) String() (out string) {
|
|
|
|
norm := normalizeSvcEnv(ee)
|
2014-03-19 00:01:10 +04:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2014-03-19 00:01:10 +04:00
|
|
|
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 20:36:31 +04:00
|
|
|
|
2014-03-18 02:09:59 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
// Unit creates a Unit file drop-in for etcd, using any configured
|
|
|
|
// options and adding a default MachineID if unset.
|
|
|
|
func (ee EtcdEnvironment) Unit(root string) (*system.Unit, error) {
|
2014-05-17 07:27:50 +04:00
|
|
|
if ee == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
if _, ok := ee["name"]; !ok {
|
2014-03-18 21:57:10 +04:00
|
|
|
if machineID := system.MachineID(root); machineID != "" {
|
2014-05-10 07:33:34 +04:00
|
|
|
ee["name"] = machineID
|
2014-03-18 21:57:10 +04:00
|
|
|
} else if hostname, err := system.Hostname(); err == nil {
|
2014-05-10 07:33:34 +04:00
|
|
|
ee["name"] = hostname
|
2014-03-18 21:57:10 +04:00
|
|
|
} else {
|
2014-05-10 07:33:34 +04:00
|
|
|
return nil, errors.New("Unable to determine default etcd name")
|
2014-03-18 20:36:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-10 07:33:34 +04:00
|
|
|
return &system.Unit{
|
|
|
|
Name: "etcd.service",
|
|
|
|
Runtime: true,
|
|
|
|
DropIn: true,
|
|
|
|
Content: ee.String(),
|
|
|
|
}, nil
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|