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-06-24 02:13:11 +04:00
|
|
|
"sort"
|
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-06-24 02:13:11 +04:00
|
|
|
var sorted sort.StringSlice
|
|
|
|
for k, _ := range norm {
|
|
|
|
sorted = append(sorted, k)
|
|
|
|
}
|
|
|
|
sorted.Sort()
|
|
|
|
|
2014-03-18 02:09:59 +04:00
|
|
|
out += "[Service]\n"
|
|
|
|
|
2014-06-24 02:13:11 +04:00
|
|
|
for _, key := range sorted {
|
|
|
|
val := norm[key]
|
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-06-06 03:12:40 +04:00
|
|
|
// Units creates a Unit file drop-in for etcd, using any configured
|
2014-05-10 07:33:34 +04:00
|
|
|
// options and adding a default MachineID if unset.
|
2014-06-06 03:12:40 +04:00
|
|
|
func (ee EtcdEnvironment) Units(root string) ([]system.Unit, error) {
|
2014-08-12 02:34:35 +04:00
|
|
|
if len(ee) < 1 {
|
2014-05-17 07:27:50 +04:00
|
|
|
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-06-06 03:12:40 +04:00
|
|
|
etcd := system.Unit{
|
2014-05-10 07:33:34 +04:00
|
|
|
Name: "etcd.service",
|
|
|
|
Runtime: true,
|
|
|
|
DropIn: true,
|
|
|
|
Content: ee.String(),
|
2014-06-06 03:12:40 +04:00
|
|
|
}
|
|
|
|
return []system.Unit{etcd}, nil
|
2014-03-05 04:36:05 +04:00
|
|
|
}
|