Skip to content

Commit fa8411d

Browse files
authored
Merge pull request #188 from hosting-de-labs/feature/MaskUnitFiles
dbus: add mask/unmask for unit files
2 parents d6c05a1 + 3b4747c commit fa8411d

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

dbus/methods.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,87 @@ type DisableUnitFileChange struct {
473473
Destination string // Destination of the symlink
474474
}
475475

476+
// MaskUnitFiles masks one or more units in the system
477+
//
478+
// It takes three arguments:
479+
// * list of units to mask (either just file names or full
480+
// absolute paths if the unit files are residing outside
481+
// the usual unit search paths)
482+
// * runtime to specify whether the unit was enabled for runtime
483+
// only (true, /run/systemd/..), or persistently (false, /etc/systemd/..)
484+
// * force flag
485+
func (c *Conn) MaskUnitFiles(files []string, runtime bool, force bool) ([]MaskUnitFileChange, error) {
486+
result := make([][]interface{}, 0)
487+
err := c.sysobj.Call("org.freedesktop.systemd1.Manager.MaskUnitFiles", 0, files, runtime, force).Store(&result)
488+
if err != nil {
489+
return nil, err
490+
}
491+
492+
resultInterface := make([]interface{}, len(result))
493+
for i := range result {
494+
resultInterface[i] = result[i]
495+
}
496+
497+
changes := make([]MaskUnitFileChange, len(result))
498+
changesInterface := make([]interface{}, len(changes))
499+
for i := range changes {
500+
changesInterface[i] = &changes[i]
501+
}
502+
503+
err = dbus.Store(resultInterface, changesInterface...)
504+
if err != nil {
505+
return nil, err
506+
}
507+
508+
return changes, nil
509+
}
510+
511+
type MaskUnitFileChange struct {
512+
Type string // Type of the change (one of symlink or unlink)
513+
Filename string // File name of the symlink
514+
Destination string // Destination of the symlink
515+
}
516+
517+
// UnmaskUnitFiles unmasks one or more units in the system
518+
//
519+
// It takes two arguments:
520+
// * list of unit files to mask (either just file names or full
521+
// absolute paths if the unit files are residing outside
522+
// the usual unit search paths)
523+
// * runtime to specify whether the unit was enabled for runtime
524+
// only (true, /run/systemd/..), or persistently (false, /etc/systemd/..)
525+
func (c *Conn) UnmaskUnitFiles(files []string, runtime bool) ([]UnmaskUnitFileChange, error) {
526+
result := make([][]interface{}, 0)
527+
err := c.sysobj.Call("org.freedesktop.systemd1.Manager.UnmaskUnitFiles", 0, files, runtime).Store(&result)
528+
if err != nil {
529+
return nil, err
530+
}
531+
532+
resultInterface := make([]interface{}, len(result))
533+
for i := range result {
534+
resultInterface[i] = result[i]
535+
}
536+
537+
changes := make([]UnmaskUnitFileChange, len(result))
538+
changesInterface := make([]interface{}, len(changes))
539+
for i := range changes {
540+
changesInterface[i] = &changes[i]
541+
}
542+
543+
err = dbus.Store(resultInterface, changesInterface...)
544+
if err != nil {
545+
return nil, err
546+
}
547+
548+
return changes, nil
549+
}
550+
551+
type UnmaskUnitFileChange struct {
552+
Type string // Type of the change (one of symlink or unlink)
553+
Filename string // File name of the symlink
554+
Destination string // Destination of the symlink
555+
}
556+
476557
// Reload instructs systemd to scan for and reload unit files. This is
477558
// equivalent to a 'systemctl daemon-reload'.
478559
func (c *Conn) Reload() error {

dbus/methods_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,56 @@ func TestConnJobListener(t *testing.T) {
507507
t.Fatal("JobListener jobs leaked")
508508
}
509509
}
510+
511+
// Enables a unit and then masks/unmasks it
512+
func TestMaskUnmask(t *testing.T) {
513+
target := "mask-unmask.service"
514+
conn := setupConn(t)
515+
516+
setupUnit(target, conn, t)
517+
abs := findFixture(target, t)
518+
runPath := filepath.Join("/run/systemd/system/", target)
519+
520+
// 1. Enable the unit
521+
install, changes, err := conn.EnableUnitFiles([]string{abs}, true, true)
522+
if err != nil {
523+
t.Fatal(err)
524+
}
525+
526+
if install != false {
527+
t.Log("Install was true")
528+
}
529+
530+
if len(changes) < 1 {
531+
t.Fatalf("Expected one change, got %v", changes)
532+
}
533+
534+
if changes[0].Filename != runPath {
535+
t.Fatal("Unexpected target filename")
536+
}
537+
538+
// 2. Mask the unit
539+
mChanges, err := conn.MaskUnitFiles([]string{target}, true, true)
540+
if err != nil {
541+
t.Fatal(err)
542+
}
543+
if mChanges[0].Filename != runPath {
544+
t.Fatalf("Change should include correct filename, %+v", mChanges[0])
545+
}
546+
if mChanges[0].Destination != "" {
547+
t.Fatalf("Change destination should be empty, %+v", mChanges[0])
548+
}
549+
550+
// 3. Unmask the unit
551+
uChanges, err := conn.UnmaskUnitFiles([]string{target}, true)
552+
if err != nil {
553+
t.Fatal(err)
554+
}
555+
if uChanges[0].Filename != runPath {
556+
t.Fatalf("Change should include correct filename, %+v", uChanges[0])
557+
}
558+
if uChanges[0].Destination != "" {
559+
t.Fatalf("Change destination should be empty, %+v", uChanges[0])
560+
}
561+
562+
}

fixtures/mask-unmask.service

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

0 commit comments

Comments
 (0)