forked from RedHatInsights/rhc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activate.go
77 lines (63 loc) · 1.7 KB
/
activate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"context"
"fmt"
systemd "github.com/coreos/go-systemd/v22/dbus"
)
// activateService tries to enable and start yggdrasil service.
// The service can be branded to rhcd on RHEL
func activateService() error {
ctx := context.Background()
conn, err := systemd.NewSystemConnectionContext(ctx)
if err != nil {
return err
}
defer conn.Close()
unitName := ServiceName + ".service"
if _, _, err := conn.EnableUnitFilesContext(ctx, []string{unitName}, false, true); err != nil {
return err
}
done := make(chan string)
if _, err := conn.StartUnitContext(ctx, unitName, "replace", done); err != nil {
return err
}
<-done
properties, err := conn.GetUnitPropertiesContext(ctx, unitName)
if err != nil {
return err
}
activeState := properties["ActiveState"]
if activeState.(string) != "active" {
return fmt.Errorf("error: The unit %v failed to start. Run 'systemctl status %v' for more information", unitName, unitName)
}
err = conn.ReloadContext(ctx)
if err != nil {
return err
}
return nil
}
// deactivateService tries to stop and disable yggdrasil service.
// The service can be branded to rhcd on RHEL
func deactivateService() error {
// Use simple background context without anything extra
ctx := context.Background()
conn, err := systemd.NewSystemConnectionContext(ctx)
if err != nil {
return err
}
defer conn.Close()
unitName := ServiceName + ".service"
done := make(chan string)
if _, err := conn.StopUnitContext(ctx, unitName, "replace", done); err != nil {
return err
}
<-done
if _, err := conn.DisableUnitFilesContext(ctx, []string{unitName}, false); err != nil {
return err
}
err = conn.ReloadContext(ctx)
if err != nil {
return err
}
return nil
}