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:
8
examples/svc2.yaml
Normal file
8
examples/svc2.yaml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
graph: mygraph
|
||||||
|
resources:
|
||||||
|
svc:
|
||||||
|
- name: purpleidea
|
||||||
|
state: running
|
||||||
|
session: true
|
||||||
|
edges: []
|
||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user