From f3fc7bb91ef04da43002151bc598b239da4bc2f9 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sat, 4 Mar 2017 04:10:25 -0500 Subject: [PATCH] resources: svc: Add basic support for user services These are user specific services and are available on the session bus. This doesn't use the private user API because https://github.com/coreos/go-systemd/pull/225 was NACKed. --- examples/svc2.yaml | 8 ++++++++ resources/svc.go | 21 +++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 examples/svc2.yaml diff --git a/examples/svc2.yaml b/examples/svc2.yaml new file mode 100644 index 00000000..a49c45d0 --- /dev/null +++ b/examples/svc2.yaml @@ -0,0 +1,8 @@ +--- +graph: mygraph +resources: + svc: + - name: purpleidea + state: running + session: true +edges: [] diff --git a/resources/svc.go b/resources/svc.go index abc45d17..7041b0b4 100644 --- a/resources/svc.go +++ b/resources/svc.go @@ -41,6 +41,7 @@ type SvcRes struct { BaseRes `yaml:",inline"` State string `yaml:"state"` // state: running, stopped, undefined Startup string `yaml:"startup"` // enabled, disabled, undefined + Session bool `yaml:"session"` // user session (true) or system? } // Default returns some sensible defaults for this resource. @@ -76,7 +77,14 @@ func (obj *SvcRes) Watch() error { return fmt.Errorf("systemd is not running") } - conn, err := systemd.NewSystemdConnection() // needs root access + var conn *systemd.Conn + var err error + if obj.Session { + conn, err = systemd.NewUserConnection() // user session + } else { + // we want NewSystemConnection but New falls back to this + conn, err = systemd.New() // needs root access + } if err != nil { return errwrap.Wrapf(err, "failed to connect to systemd") } @@ -210,7 +218,13 @@ func (obj *SvcRes) CheckApply(apply bool) (checkOK bool, err error) { return false, fmt.Errorf("systemd is not running") } - conn, err := systemd.NewSystemdConnection() // needs root access + var conn *systemd.Conn + if obj.Session { + conn, err = systemd.NewUserConnection() // user session + } else { + // we want NewSystemConnection but New falls back to this + conn, err = systemd.New() // needs root access + } if err != nil { return false, errwrap.Wrapf(err, "failed to connect to systemd") } @@ -429,6 +443,9 @@ func (obj *SvcRes) Compare(res Res) bool { if obj.Startup != res.Startup { return false } + if obj.Session != res.Session { + return false + } default: return false }