Example #1
0
func RunUnitCommand(command, unit string) (string, error) {
	conn, err := dbus.New()
	if err != nil {
		return "", err
	}

	var fn func(string, string) (string, error)
	switch command {
	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 fn(unit, "replace")
}
Example #2
0
func DaemonReload() error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}

	return conn.Reload()
}
Example #3
0
func StartUnitByName(name string) error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}

	_, err = conn.StartUnit(name, "replace")
	return err
}
Example #4
0
func EnableUnitFile(unit string, runtime bool) error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}

	units := []string{unit}
	_, _, err = conn.EnableUnitFiles(units, runtime, true)
	return err
}
Example #5
0
func EnableUnitFile(file string, runtime bool) error {
	conn, err := dbus.New()
	if err != nil {
		return err
	}

	files := []string{file}
	_, _, err = conn.EnableUnitFiles(files, runtime, true)
	return err
}
Example #6
0
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
}
Example #7
0
func ExecuteScript(scriptPath string) (string, error) {
	props := []dbus.Property{
		dbus.PropDescription("Unit generated and executed by coreos-cloudinit on behalf of user"),
		dbus.PropExecStart([]string{"/bin/bash", scriptPath}, false),
	}

	base := path.Base(scriptPath)
	name := fmt.Sprintf("coreos-cloudinit-%s.service", base)

	log.Printf("Creating transient systemd unit '%s'", name)

	conn, err := dbus.New()
	if err != nil {
		return "", err
	}

	_, err = conn.StartTransientUnit(name, "replace", props...)
	return name, err
}