diff --git a/initialize/config.go b/initialize/config.go index 8731237..3fdc318 100644 --- a/initialize/config.go +++ b/initialize/config.go @@ -32,6 +32,7 @@ type CloudConfig struct { SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"` Coreos struct { Etcd EtcdEnvironment + Fleet FleetEnvironment OEM OEMRelease Update UpdateConfig Units []system.Unit @@ -133,7 +134,7 @@ func Apply(cfg CloudConfig, env *Environment) error { } } - for _, ccu := range []CloudConfigUnit{cfg.Coreos.Etcd, cfg.Coreos.Update} { + for _, ccu := range []CloudConfigUnit{cfg.Coreos.Etcd, cfg.Coreos.Fleet, cfg.Coreos.Update} { u, err := ccu.Unit(env.Root()) if err != nil { return err diff --git a/initialize/fleet.go b/initialize/fleet.go new file mode 100644 index 0000000..7b3e011 --- /dev/null +++ b/initialize/fleet.go @@ -0,0 +1,34 @@ +package initialize + +import ( + "fmt" + + "github.com/coreos/coreos-cloudinit/system" +) + +type FleetEnvironment map[string]string + +func (fe FleetEnvironment) String() (out string) { + norm := normalizeSvcEnv(fe) + out += "[Service]\n" + + for key, val := range norm { + out += fmt.Sprintf("Environment=\"FLEET_%s=%s\"\n", key, val) + } + + return +} + +// Unit generates a Unit file drop-in for fleet, if any fleet options were +// configured in cloud-config +func (fe FleetEnvironment) Unit(root string) (*system.Unit, error) { + if len(fe) < 1 { + return nil, nil + } + return &system.Unit{ + Name: "fleet.service", + Runtime: true, + DropIn: true, + Content: fe.String(), + }, nil +} diff --git a/initialize/fleet_test.go b/initialize/fleet_test.go new file mode 100644 index 0000000..52616cf --- /dev/null +++ b/initialize/fleet_test.go @@ -0,0 +1,42 @@ +package initialize + +import "testing" + +func TestFleetEnvironment(t *testing.T) { + cfg := make(FleetEnvironment, 0) + cfg["public-ip"] = "12.34.56.78" + + env := cfg.String() + + expect := `[Service] +Environment="FLEET_PUBLIC_IP=12.34.56.78" +` + + if env != expect { + t.Errorf("Generated environment:\n%s\nExpected environment:\n%s", env, expect) + } +} + +func TestFleetUnit(t *testing.T) { + cfg := make(FleetEnvironment, 0) + u, err := cfg.Unit("/") + if u != nil { + t.Errorf("unexpectedly generated unit with empty FleetEnvironment") + } + + cfg["public-ip"] = "12.34.56.78" + + u, err = cfg.Unit("/") + if err != nil { + t.Errorf("error generating fleet unit: %v", err) + } + if u == nil { + t.Fatalf("unexpectedly got nil unit generating fleet unit!") + } + if !u.Runtime { + t.Errorf("bad Runtime for generated fleet unit!") + } + if !u.DropIn { + t.Errorf("bad DropIn for generated fleet unit!") + } +}