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