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.
This commit is contained in:
James Shubin
2017-03-04 04:10:25 -05:00
parent 028ef14cc0
commit f3fc7bb91e
2 changed files with 27 additions and 2 deletions

8
examples/svc2.yaml Normal file
View File

@@ -0,0 +1,8 @@
---
graph: mygraph
resources:
svc:
- name: purpleidea
state: running
session: true
edges: []

View File

@@ -41,6 +41,7 @@ type SvcRes struct {
BaseRes `yaml:",inline"` BaseRes `yaml:",inline"`
State string `yaml:"state"` // state: running, stopped, undefined State string `yaml:"state"` // state: running, stopped, undefined
Startup string `yaml:"startup"` // enabled, disabled, 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. // Default returns some sensible defaults for this resource.
@@ -76,7 +77,14 @@ func (obj *SvcRes) Watch() error {
return fmt.Errorf("systemd is not running") 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 { if err != nil {
return errwrap.Wrapf(err, "failed to connect to systemd") 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") 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 { if err != nil {
return false, errwrap.Wrapf(err, "failed to connect to systemd") 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 { if obj.Startup != res.Startup {
return false return false
} }
if obj.Session != res.Session {
return false
}
default: default:
return false return false
} }