Merge pull request #40 from bcwaldon/unit-command

Implement unit.command
This commit is contained in:
Brian Waldon 2014-03-19 16:06:01 -07:00
commit b89ddae983
4 changed files with 85 additions and 85 deletions

View File

@ -46,9 +46,16 @@ Note that hyphens in the coreos.etcd.* keys are mapped to underscores.
Arbitrary systemd units may be provided in the `coreos.units` attribute. Arbitrary systemd units may be provided in the `coreos.units` attribute.
`coreos.units` is a list of objects with the following fields: `coreos.units` is a list of objects with the following fields:
- **name**: string representing unit's name - **name**: String representing unit's name. Required.
- **runtime**: boolean indicating whether or not to persist the unit across reboots. This is analagous to the `--runtime` flag to `systemd enable`. - **runtime**: Boolean indicating whether or not to persist the unit across reboots. This is analagous to the `--runtime` argument to `systemd enable`. Default value is false.
- **content**: plaintext string representing entire unit file - **content**: Plaintext string representing entire unit file. If no value is provided, the unit is assumed to exist already.
- **command**: Command to execute on unit: start, stop, reload, restart, try-restart, reload-or-restart, reload-or-try-restart. Default value is restart.
**NOTE:** The command field is ignored for all network, netdev, and link units. The systemd-networkd.service unit will be restarted in their place.
##### Examples
Write a unit to disk, automatically starting it.
``` ```
#cloud-config #cloud-config
@ -71,6 +78,19 @@ coreos:
WantedBy=local.target WantedBy=local.target
``` ```
Start the builtin `etcd` and `fleet` services:
```
# cloud-config
coreos:
units:
- name: etcd.service
command: start
- name: fleet.service
command: start
## Cloud-Config Parameters ## Cloud-Config Parameters
### ssh_authorized_keys ### ssh_authorized_keys

View File

@ -14,7 +14,6 @@ type CloudConfig struct {
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"` SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"`
Coreos struct { Coreos struct {
Etcd EtcdEnvironment Etcd EtcdEnvironment
Fleet struct{ Autostart bool }
Units []system.Unit Units []system.Unit
} }
WriteFiles []system.File `yaml:"write_files"` WriteFiles []system.File `yaml:"write_files"`
@ -115,8 +114,11 @@ func Apply(cfg CloudConfig, env *Environment) error {
} }
if len(cfg.Coreos.Units) > 0 { if len(cfg.Coreos.Units) > 0 {
commands := make(map[string]string, 0)
for _, unit := range cfg.Coreos.Units { for _, unit := range cfg.Coreos.Units {
log.Printf("Placing unit %s on filesystem", unit.Name) if unit.Content != "" {
log.Printf("Writing unit %s to filesystem", unit.Name)
dst, err := system.PlaceUnit(&unit, env.Root()) dst, err := system.PlaceUnit(&unit, env.Root())
if err != nil { if err != nil {
return err return err
@ -133,17 +135,28 @@ func Apply(cfg CloudConfig, env *Environment) error {
log.Printf("Skipping enable for network-like unit %s", unit.Name) log.Printf("Skipping enable for network-like unit %s", unit.Name)
} }
} }
system.DaemonReload()
system.StartUnits(cfg.Coreos.Units) if unit.Group() != "network" {
command := unit.Command
if command == "" {
command = "restart"
}
commands[unit.Name] = command
} else {
commands["systemd-networkd.service"] = "restart"
}
} }
if cfg.Coreos.Fleet.Autostart { system.DaemonReload()
err := system.StartUnitByName("fleet.service")
if err == nil { for unit, command := range commands {
log.Printf("Started fleet service.") log.Printf("Calling unit command '%s %s'", command, unit)
} else { res, err := system.RunUnitCommand(command, unit)
if err != nil {
return err return err
} }
log.Printf("Result of '%s %s': %s", command, unit, res)
}
} }
return nil return nil

View File

@ -17,10 +17,6 @@ func TestCloudConfigEmpty(t *testing.T) {
t.Error("Parsed incorrect number of SSH keys") t.Error("Parsed incorrect number of SSH keys")
} }
if cfg.Coreos.Fleet.Autostart {
t.Error("Expected AutostartFleet not to be defined")
}
if len(cfg.WriteFiles) != 0 { if len(cfg.WriteFiles) != 0 {
t.Error("Expected zero WriteFiles") t.Error("Expected zero WriteFiles")
} }
@ -36,8 +32,6 @@ func TestCloudConfig(t *testing.T) {
coreos: coreos:
etcd: etcd:
discovery: "https://discovery.etcd.io/827c73219eeb2fa5530027c37bf18877" discovery: "https://discovery.etcd.io/827c73219eeb2fa5530027c37bf18877"
fleet:
autostart: Yes
units: units:
- name: 50-eth0.network - name: 50-eth0.network
runtime: yes runtime: yes
@ -77,10 +71,6 @@ hostname: trontastic
t.Error("Expected first SSH key to be 'foobaz'") t.Error("Expected first SSH key to be 'foobaz'")
} }
if !cfg.Coreos.Fleet.Autostart {
t.Error("Expected AutostartFleet to be true")
}
if len(cfg.WriteFiles) != 1 { if len(cfg.WriteFiles) != 1 {
t.Error("Failed to parse correct number of write_files") t.Error("Failed to parse correct number of write_files")
} else { } else {

View File

@ -21,6 +21,7 @@ type Unit struct {
Name string Name string
Runtime bool Runtime bool
Content string Content string
Command string
} }
func (u *Unit) Type() string { func (u *Unit) Type() string {
@ -80,34 +81,33 @@ func EnableUnitFile(file string, runtime bool) error {
return err return err
} }
func separateNetworkUnits(units []Unit) ([]Unit, []Unit) { func RunUnitCommand(command, unit string) (string, error) {
networkUnits := make([]Unit, 0) conn, err := dbus.New()
nonNetworkUnits := make([]Unit, 0) if err != nil {
for _, unit := range units { return "", err
if unit.Group() == "network" {
networkUnits = append(networkUnits, unit)
} else {
nonNetworkUnits = append(nonNetworkUnits, unit)
}
}
return networkUnits, nonNetworkUnits
}
func StartUnits(units []Unit) error {
networkUnits, nonNetworkUnits := separateNetworkUnits(units)
if len(networkUnits) > 0 {
if err := RestartUnitByName("systemd-networkd.service"); err != nil {
return err
}
} }
for _, unit := range nonNetworkUnits { var fn func(string, string) (string, error)
if err := RestartUnitByName(unit.Name); err != nil { switch command {
return err case "start":
} fn = conn.StartUnit
case "stop":
fn = conn.StopUnit
case "restart":
fn = conn.RestartUnit
case "reload":
fn = conn.ReloadUnit
case "try-restart":
fn = conn.TryRestartUnit
case "reload-or-restart":
fn = conn.ReloadOrRestartUnit
case "reload-or-try-restart":
fn = conn.ReloadOrTryRestartUnit
default:
return "", fmt.Errorf("Unsupported systemd command %q", command)
} }
return nil return fn(unit, "replace")
} }
func DaemonReload() error { func DaemonReload() error {
@ -120,29 +120,6 @@ func DaemonReload() error {
return err return err
} }
func RestartUnitByName(name string) error {
log.Printf("Restarting unit %s", name)
conn, err := dbus.New()
if err != nil {
return err
}
output, err := conn.RestartUnit(name, "replace")
log.Printf("Restart completed with '%s'", output)
return err
}
func StartUnitByName(name string) error {
conn, err := dbus.New()
if err != nil {
return err
}
_, err = conn.StartUnit(name, "replace")
return err
}
func ExecuteScript(scriptPath string) (string, error) { func ExecuteScript(scriptPath string) (string, error) {
props := []dbus.Property{ props := []dbus.Property{
dbus.PropDescription("Unit generated and executed by coreos-cloudinit on behalf of user"), dbus.PropDescription("Unit generated and executed by coreos-cloudinit on behalf of user"),