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 }