Skip to content

Commit 88b39e2

Browse files
committed
Merge pull request #35 from smarterclayton/add_disable_unit_files
Add DisableUnitFiles as a method
2 parents e420ef8 + 2356c76 commit 88b39e2

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

dbus/methods.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,50 @@ type EnableUnitFileChange struct {
277277
Destination string // Destination of the symlink
278278
}
279279

280+
// DisableUnitFiles() may be used to disable one or more units in the system (by
281+
// removing symlinks to them from /etc or /run).
282+
//
283+
// It takes a list of unit files to disable (either just file names or full
284+
// absolute paths if the unit files are residing outside the usual unit
285+
// search paths), and one boolean: whether the unit was enabled for runtime
286+
// only (true, /run), or persistently (false, /etc).
287+
//
288+
// This call returns an array with the changes made. The changes list
289+
// consists of structures with three strings: the type of the change (one of
290+
// symlink or unlink), the file name of the symlink and the destination of the
291+
// symlink.
292+
func (c *Conn) DisableUnitFiles(files []string, runtime bool) ([]DisableUnitFileChange, error) {
293+
result := make([][]interface{}, 0)
294+
err := c.sysobj.Call("DisableUnitFiles", 0, files, runtime).Store(&result)
295+
if err != nil {
296+
return nil, err
297+
}
298+
299+
resultInterface := make([]interface{}, len(result))
300+
for i := range result {
301+
resultInterface[i] = result[i]
302+
}
303+
304+
changes := make([]DisableUnitFileChange, len(result))
305+
changesInterface := make([]interface{}, len(changes))
306+
for i := range changes {
307+
changesInterface[i] = &changes[i]
308+
}
309+
310+
err = dbus.Store(resultInterface, changesInterface...)
311+
if err != nil {
312+
return nil, err
313+
}
314+
315+
return changes, nil
316+
}
317+
318+
type DisableUnitFileChange struct {
319+
Type string // Type of the change (one of symlink or unlink)
320+
Filename string // File name of the symlink
321+
Destination string // Destination of the symlink
322+
}
323+
280324
// Reload instructs systemd to scan for and reload unit files. This is
281325
// equivalent to a 'systemctl daemon-reload'.
282326
func (c *Conn) Reload() (string, error) {

dbus/methods_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,37 @@ func TestStartStopUnit(t *testing.T) {
122122
}
123123
}
124124

125+
// Enables a unit and then immediately tears it down
126+
func TestEnableDisableUnit(t *testing.T) {
127+
target := "enable-disable.service"
128+
conn := setupConn(t)
129+
130+
setupUnit(target, conn, t)
131+
132+
abs, err := filepath.Abs("../fixtures/" + target)
133+
if err != nil {
134+
t.Fatal(err)
135+
}
136+
137+
path := filepath.Join("/run/systemd/system/", target)
138+
139+
// 2. Disable the unit
140+
changes, err := conn.DisableUnitFiles([]string{abs}, true)
141+
if err != nil {
142+
t.Fatal(err)
143+
}
144+
145+
if len(changes) != 1 {
146+
t.Fatalf("Changes should include the path, %v", changes)
147+
}
148+
if changes[0].Filename != path {
149+
t.Fatalf("Change should include correct filename, %+v", changes[0])
150+
}
151+
if changes[0].Destination != "" {
152+
t.Fatalf("Change destination should be empty, %+v", changes[0])
153+
}
154+
}
155+
125156
// TestGetUnitProperties reads the `-.mount` which should exist on all systemd
126157
// systems and ensures that one of its properties is valid.
127158
func TestGetUnitProperties(t *testing.T) {

fixtures/enable-disable.service

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[Unit]
2+
Description=enable disable test
3+
4+
[Service]
5+
ExecStart=/bin/sleep 400

0 commit comments

Comments
 (0)