From 91b35a81221efa4f993caecabeacf61bcfbb799d Mon Sep 17 00:00:00 2001
From: Hugues Malphettes <hmalphettes@gmail.com>
Date: Wed, 23 Mar 2016 09:55:06 +0800
Subject: [PATCH] Param -systemd_user=true to use `systemd --user`

---
 config/config.go   |  1 +
 fleetd/fleetd.go   |  2 ++
 server/server.go   |  2 +-
 systemd/manager.go | 11 +++++++++--
 4 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/config/config.go b/config/config.go
index 4d0cafd75..a13cd265b 100644
--- a/config/config.go
+++ b/config/config.go
@@ -35,6 +35,7 @@ type Config struct {
 	DisableWatches          bool
 	VerifyUnits             bool
 	UnitsDirectory          string
+	SystemdUser             bool
 	AuthorizedKeysFile      string
 }
 
diff --git a/fleetd/fleetd.go b/fleetd/fleetd.go
index da2154aaf..32872ef02 100755
--- a/fleetd/fleetd.go
+++ b/fleetd/fleetd.go
@@ -87,6 +87,7 @@ func main() {
 	cfgset.String("metadata", "", "List of key-value metadata to assign to the fleet machine")
 	cfgset.String("agent_ttl", agent.DefaultTTL, "TTL in seconds of fleet machine state in etcd")
 	cfgset.String("units_directory", "/run/fleet/units/", "Path to the fleet units directory")
+	cfgset.Bool("systemd_user", false, "When true use systemd --user)")
 	cfgset.Int("token_limit", 100, "Maximum number of entries per page returned from API requests")
 	cfgset.Bool("disable_engine", false, "Disable the engine entirely, use with care")
 	cfgset.Bool("disable_watches", false, "Disable the use of etcd watches. Increases scheduling latency")
@@ -218,6 +219,7 @@ func getConfig(flagset *flag.FlagSet, userCfgFile string) (*config.Config, error
 		DisableWatches:          (*flagset.Lookup("disable_watches")).Value.(flag.Getter).Get().(bool),
 		VerifyUnits:             (*flagset.Lookup("verify_units")).Value.(flag.Getter).Get().(bool),
 		UnitsDirectory:          (*flagset.Lookup("units_directory")).Value.(flag.Getter).Get().(string),
+		SystemdUser:             (*flagset.Lookup("systemd_user")).Value.(flag.Getter).Get().(bool),
 		TokenLimit:              (*flagset.Lookup("token_limit")).Value.(flag.Getter).Get().(int),
 		AuthorizedKeysFile:      (*flagset.Lookup("authorized_keys_file")).Value.(flag.Getter).Get().(string),
 	}
diff --git a/server/server.go b/server/server.go
index bddb55427..780c252d5 100644
--- a/server/server.go
+++ b/server/server.go
@@ -72,7 +72,7 @@ func New(cfg config.Config) (*Server, error) {
 		return nil, err
 	}
 
-	mgr, err := systemd.NewSystemdUnitManager(cfg.UnitsDirectory)
+	mgr, err := systemd.NewSystemdUnitManager(cfg.UnitsDirectory, cfg.SystemdUser)
 	if err != nil {
 		return nil, err
 	}
diff --git a/systemd/manager.go b/systemd/manager.go
index 785c77647..cac6df969 100644
--- a/systemd/manager.go
+++ b/systemd/manager.go
@@ -36,8 +36,8 @@ type systemdUnitManager struct {
 	mutex  sync.RWMutex
 }
 
-func NewSystemdUnitManager(uDir string) (*systemdUnitManager, error) {
-	systemd, err := dbus.New()
+func NewSystemdUnitManager(uDir string, systemdUser bool) (*systemdUnitManager, error) {
+	systemd, err := createDbusConnection(systemdUser)
 	if err != nil {
 		return nil, err
 	}
@@ -60,6 +60,13 @@ func NewSystemdUnitManager(uDir string) (*systemdUnitManager, error) {
 	return &mgr, nil
 }
 
+func createDbusConnection(systemdUser bool) (*dbus.Conn, error) {
+	if systemdUser {
+		return dbus.NewUserConnection()
+	}
+	return dbus.New()
+}
+
 func hashUnitFiles(dir string) (map[string]unit.Hash, error) {
 	uNames, err := lsUnitsDir(dir)
 	if err != nil {