-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_linux.go
More file actions
74 lines (61 loc) · 1.71 KB
/
Copy pathmain_linux.go
File metadata and controls
74 lines (61 loc) · 1.71 KB
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
//go:build linux
// +build linux
package main
import (
"context"
"fmt"
"time"
"github.com/coreos/go-systemd/v22/dbus"
)
const (
restartMode = "replace" // or "force-reload"
)
func restartSystemdServices(ctx context.Context, config Service) ([]error) {
var errs []error
for _, targetSystemdUnit := range config.RestartSystemdServices {
// Connect to systemd
// Specifically this will look DBUS_SYSTEM_BUS_ADDRESS environment variable
// For example: `unix:path=/run/dbus/system_bus_socket`
systemdConnection, err := dbus.NewSystemConnectionContext(ctx)
if err != nil {
errs = append(errs, err)
}
defer systemdConnection.Close()
listOfUnits, err := systemdConnection.ListUnitsContext(ctx)
if err != nil {
fmt.Printf("Failed to list units: %v\n", err)
}
found := false
// targetUnit := dbus.UnitStatus{}
for _, unit := range listOfUnits {
if unit.Name == targetSystemdUnit {
fmt.Printf("Found systemd unit %s\n", targetSystemdUnit)
found = true
// targetUnit = unit
break
}
}
if !found {
fmt.Printf("Expected systemd unit %s not found\n", targetSystemdUnit)
}
completedRestartCh := make(chan string)
jobID, err := systemdConnection.RestartUnitContext(
ctx,
targetSystemdUnit,
restartMode,
completedRestartCh,
)
if err != nil {
errs = append(errs, err)
}
fmt.Printf("Restart job id: %d\n", jobID)
// Wait for the restart to complete
select {
case <-completedRestartCh:
fmt.Printf("Restart job completed for unit: %s\n", targetSystemdUnit)
case <-time.After(time.Duration(config.RestartTimeout) * time.Second):
fmt.Printf("Timed out waiting for restart job to complete for unit: %s\n", targetSystemdUnit)
}
}
return errs
}