/
dbus.go
183 lines (154 loc) · 5.92 KB
/
dbus.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2023 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dbus
import (
"context"
"fmt"
"reflect"
"time"
"github.com/coreos/go-systemd/v22/dbus"
"github.com/coreos/go-systemd/v22/login1"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
)
// DBus is an interface for interacting with systemd via dbus.
type DBus interface {
// DaemonReload reload the systemd configuration, same as executing "systemctl daemon-reload".
DaemonReload(ctx context.Context) error
// Enable the given units, same as executing "systemctl enable unit".
Enable(ctx context.Context, unitNames ...string) error
// Disable the given units, same as executing "systemctl disable unit".
Disable(ctx context.Context, unitNames ...string) error
// Start the given unit and record an event to the node object, same as executing "systemctl start unit".
Start(ctx context.Context, recorder record.EventRecorder, node runtime.Object, unitName string) error
// Stop the given unit and record an event to the node object, same as executing "systemctl stop unit".
Stop(ctx context.Context, recorder record.EventRecorder, node runtime.Object, unitName string) error
// Restart the given unit and record an event to the node object, same as executing "systemctl restart unit".
Restart(ctx context.Context, recorder record.EventRecorder, node runtime.Object, unitName string) error
// Reboot this machines, is the same as executing "systemctl reboot".
Reboot() error
}
type db struct {
log logr.Logger
}
// New returns a new working DBus
func New(log logr.Logger) DBus {
return &db{log: log.WithName("dbus")}
}
func (_ *db) Reboot() error {
login, err := login1.New()
if err != nil {
return fmt.Errorf("unable to connect to dbus: %w", err)
}
defer login.Close()
login.Reboot(false)
return nil
}
func (_ *db) Enable(ctx context.Context, unitNames ...string) error {
dbc, err := dbus.NewWithContext(ctx)
if err != nil {
return fmt.Errorf("unable to connect to dbus: %w", err)
}
defer dbc.Close()
_, _, err = dbc.EnableUnitFilesContext(ctx, unitNames, false, true)
return err
}
func (_ *db) Disable(ctx context.Context, unitNames ...string) error {
dbc, err := dbus.NewWithContext(ctx)
if err != nil {
return fmt.Errorf("unable to connect to dbus: %w", err)
}
defer dbc.Close()
_, err = dbc.DisableUnitFilesContext(ctx, unitNames, false)
return err
}
func (d *db) Stop(ctx context.Context, recorder record.EventRecorder, node runtime.Object, unitName string) error {
dbc, err := dbus.NewWithContext(ctx)
if err != nil {
return fmt.Errorf("unable to connect to dbus: %w", err)
}
defer dbc.Close()
return d.runCommand(ctx, recorder, node, unitName, dbc.StopUnitContext, "SystemDUnitStop", "stop")
}
func (d *db) Start(ctx context.Context, recorder record.EventRecorder, node runtime.Object, unitName string) error {
dbc, err := dbus.NewWithContext(ctx)
if err != nil {
return fmt.Errorf("unable to connect to dbus: %w", err)
}
defer dbc.Close()
return d.runCommand(ctx, recorder, node, unitName, dbc.StartUnitContext, "SystemDUnitStart", "start")
}
func (d *db) Restart(ctx context.Context, recorder record.EventRecorder, node runtime.Object, unitName string) error {
dbc, err := dbus.NewWithContext(ctx)
if err != nil {
return fmt.Errorf("unable to connect to dbus: %w", err)
}
defer dbc.Close()
return d.runCommand(ctx, recorder, node, unitName, dbc.RestartUnitContext, "SystemDUnitRestart", "restart")
}
func (_ *db) DaemonReload(ctx context.Context) error {
dbc, err := dbus.NewWithContext(ctx)
if err != nil {
return fmt.Errorf("unable to connect to dbus: %w", err)
}
defer dbc.Close()
if err := dbc.ReloadContext(ctx); err != nil {
return fmt.Errorf("systemd daemon-reload failed: %w", err)
}
return nil
}
func (d *db) runCommand(
ctx context.Context,
recorder record.EventRecorder,
node runtime.Object,
unitName string,
f func(context.Context, string, string, chan<- string) (int, error),
eventReason string,
operation string,
) error {
var (
resultCh = make(chan string)
err error
)
if _, err := f(ctx, unitName, "replace", resultCh); err != nil {
return fmt.Errorf("unable to %s unit %s: %w", operation, unitName, err)
}
select {
case <-ctx.Done(): // context is cancelled
err = ctx.Err()
case result := <-resultCh: // job channel reported back
if result != "done" {
err = fmt.Errorf("%s failed for %s, due %s", operation, unitName, result)
}
case <-time.After(10 * time.Second): // after 10s, we continue even if the result channel did not report back
d.log.Info("Systemd operation is blocking for more than 10s, aborting and continuing anyways", "unitName", unitName, "operation", operation)
}
recordEvent(recorder, node, err, unitName, eventReason, operation)
return err
}
func recordEvent(recorder record.EventRecorder, node runtime.Object, err error, unitName, reason, operation string) {
if recorder != nil && node != nil && !reflect.ValueOf(node).IsNil() { // nil is not nil :(
var (
eventType = corev1.EventTypeNormal
message = fmt.Sprintf("processed %s of unit %s", operation, unitName)
)
if err != nil {
eventType = corev1.EventTypeWarning
message += fmt.Sprintf(" failed with error %+v", err)
}
recorder.Eventf(node, eventType, reason, message)
}
}