2014-03-18 09:00:41 -07:00
|
|
|
package initialize
|
2014-03-04 16:36:05 -08:00
|
|
|
|
|
|
|
import (
|
2014-03-18 10:57:10 -07:00
|
|
|
"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
|
|
|
|
|
2014-05-09 20:33:34 -07:00
|
|
|
func (ee EtcdEnvironment) String() (out string) {
|
|
|
|
norm := normalizeSvcEnv(ee)
|
2014-03-18 13:01:10 -07:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
2014-03-18 13:01:10 -07:00
|
|
|
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-18 09:36:31 -07:00
|
|
|
|
2014-03-17 15:09:59 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-09 20:33:34 -07: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-16 20:27:50 -07:00
|
|
|
if ee == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-05-09 20:33:34 -07:00
|
|
|
if _, ok := ee["name"]; !ok {
|
2014-03-18 10:57:10 -07:00
|
|
|
if machineID := system.MachineID(root); machineID != "" {
|
2014-05-09 20:33:34 -07:00
|
|
|
ee["name"] = machineID
|
2014-03-18 10:57:10 -07:00
|
|
|
} else if hostname, err := system.Hostname(); err == nil {
|
2014-05-09 20:33:34 -07:00
|
|
|
ee["name"] = hostname
|
2014-03-18 10:57:10 -07:00
|
|
|
} else {
|
2014-05-09 20:33:34 -07:00
|
|
|
return nil, errors.New("Unable to determine default etcd name")
|
2014-03-18 09:36:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 20:33:34 -07:00
|
|
|
return &system.Unit{
|
|
|
|
Name: "etcd.service",
|
|
|
|
Runtime: true,
|
|
|
|
DropIn: true,
|
|
|
|
Content: ee.String(),
|
|
|
|
}, nil
|
2014-03-04 16:36:05 -08:00
|
|
|
}
|