Skip to content

Instantly share code, notes, and snippets.

@mdaniel
Created December 6, 2017 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdaniel/235110889c16515e09615dde0ecc2717 to your computer and use it in GitHub Desktop.
Save mdaniel/235110889c16515e09615dde0ecc2717 to your computer and use it in GitHub Desktop.
Issue daemon-reload and install systemd unit files from golang
package: systemd-installer
import:
- package: github.com/coreos/go-systemd
version: v15
- package: github.com/godbus/dbus
version: v4.0.0
package main
import (
"fmt"
"github.com/coreos/go-systemd/dbus"
"log"
"os"
)
func main() {
var err error
var conn *dbus.Conn
conn, err = dbus.NewSystemdConnection()
defer func() {
conn.Close()
}()
var unitFiles []string = os.Args[1:len(os.Args)]
if len(unitFiles) == 0 {
fmt.Fprintf(os.Stderr, "Hey, what's with 0 unitFiles; argv=%v", os.Args)
return
}
fmt.Printf("Issuing daemon-reload to pick up unitFiles=%v\n", unitFiles)
if err = conn.Reload(); err != nil {
log.Panicln("Unable to `systemd daemon-reload`", err)
}
fmt.Println("daemon-reload OK")
didInstallAction, changes, enableErr := conn.EnableUnitFiles(unitFiles, false, false)
if enableErr != nil {
log.Panicf("Unable to enable your Unit Files: files=%v err=%v", unitFiles, enableErr)
}
if didInstallAction {
fmt.Println("Enabling those units successfully Install-ed them")
}
if len(changes) == 0 {
fmt.Fprintf(os.Stderr, "So sad, enabling unitFiles=%v appears to have changed nothing\n", unitFiles)
} else {
for _, ch := range changes {
fmt.Printf("Successfully changed type=%s filename=%s destination=%s\n",
ch.Type, ch.Filename, ch.Destination)
}
}
fmt.Println("OK")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment