This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
systemd.go
224 lines (206 loc) · 5.77 KB
/
systemd.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2018 Datadog, Inc.
package setup
import (
"fmt"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/coreos/go-systemd/dbus"
unit2 "github.com/coreos/go-systemd/unit"
"github.com/golang/glog"
)
const (
UnitPath = "/run/systemd/system/"
customSystemdSection = "X-p8s"
)
var (
fieldsToCompare = []string{
"ExecStart",
//"RootPath", TODO
}
)
func getUnitOptions(unitABSPath string) ([]*unit2.UnitOption, error) {
f, err := os.OpenFile(unitABSPath, os.O_RDONLY, 0)
if err != nil {
glog.Errorf("Cannot open %v", err)
return nil, err
}
defer f.Close()
opts, err := unit2.Deserialize(f)
if err != nil {
glog.Errorf("Cannot deserialize %s: %v", unitABSPath, err)
return nil, err
}
glog.V(4).Infof("Deserialized %s with %d items", unitABSPath, len(opts))
return opts, nil
}
func pushUnitInMap(opts []*unit2.UnitOption) map[string]string {
m := make(map[string]string, 2)
for _, elt := range opts {
if elt.Section == customSystemdSection && elt.Name == "RootPath" {
m[elt.Name] = elt.Value
} else if elt.Section == "Service" && elt.Name == "ExecStart" {
m[elt.Name] = elt.Value
}
}
return m
}
func (e *Environment) isUnitUpToDate(onDiskOpts, currentOpts []*unit2.UnitOption) bool {
disk := pushUnitInMap(onDiskOpts)
current := pushUnitInMap(currentOpts)
for _, field := range fieldsToCompare {
diskValue, ok := disk[field]
if !ok {
glog.Warningf("%s isn't in the systemd unit on disk", field)
return false
}
// we supposed the current is fine
if diskValue != current[field] {
glog.Warningf("On disk unit is different than the generated one")
dSplited := strings.Split(diskValue, " ")
cSplited := strings.Split(current[field], " ")
if len(dSplited) == len(cSplited) {
for i := 0; i < len(dSplited); i++ {
if dSplited[i] == cSplited[i] {
continue
}
glog.Warningf("Diff disk: %q, current: %q", dSplited[i], cSplited[i])
}
}
return false
}
}
glog.V(4).Infof("Unit on disk matched the current one")
return true
}
func statExecStart(opts []*unit2.UnitOption) error {
for _, elt := range opts {
if elt.Section != "Service" {
continue
}
if elt.Name != "ExecStart" {
continue
}
commandLine := strings.Split(elt.Value, " ")
_, err := os.Stat(commandLine[0])
// TODO maybe check if executable ?
return err
}
return fmt.Errorf("cannot find ExecStart in systemd options")
}
func (e *Environment) linkSystemdUnit(unitOpt []*unit2.UnitOption, manifestUnitName, unitName string) error {
unitABSPath := path.Join(UnitPath, unitName)
_, err := os.Stat(unitABSPath)
if err == nil {
glog.V(2).Infof("Already created systemd unit: %s, untouched", unitName)
// Validate the content
runSystemdSystemUnit, err := getUnitOptions(unitABSPath)
if err != nil {
return err
}
if !e.isUnitUpToDate(runSystemdSystemUnit, unitOpt) {
if e.cleanOptions.Systemd {
err = fmt.Errorf("non uptodate systemd unit %s", unitABSPath)
glog.Errorf("Unexpected error: %v", err)
return err
}
glog.Warningf(`The already created unit %q doesn't match the generated one, used clean options are %q use instead "%s,systemd"`, unitName, e.cleanOptions.StringCLI(), e.cleanOptions.StringCLI())
}
err = statExecStart(runSystemdSystemUnit)
if err != nil {
glog.Errorf("Current ExecStart in %s unit is incorrect: %v", unitABSPath, err)
return err
}
return nil
}
// Link
glog.V(4).Infof("Creating systemd unit %s ...", unitName)
err = os.Symlink(manifestUnitName, unitABSPath)
if err != nil {
glog.Errorf("Fail to link systemd unit %s -> %s: %v", manifestUnitName, unitABSPath, err)
return err
}
glog.V(4).Infof("Successfully linked systemd unit %%s -> %s", manifestUnitName, unitABSPath)
return nil
}
func (e *Environment) createEnd2EndSection() []*unit2.UnitOption {
return []*unit2.UnitOption{
{
Section: customSystemdSection,
Name: "RootPath",
Value: e.rootABSPath,
},
{
Section: customSystemdSection,
Name: "HyperkubeVersion",
Value: e.binaryHyperkube.version,
},
{
Section: customSystemdSection,
Name: "EtcdVersion",
Value: e.binaryEtcd.version,
},
{
Section: customSystemdSection,
Name: "VaultVersion",
Value: e.binaryVault.version,
},
{
Section: customSystemdSection,
Name: "CNIVersion",
Value: e.binaryCNI.version,
},
{
Section: customSystemdSection,
Name: "Timestamp",
Value: strconv.Itoa(int(time.Now().Unix())),
},
}
}
func (e *Environment) createUnitFromTemplate(unitName string) error {
unitNameNoPrefix := strings.TrimPrefix(unitName, e.systemdUnitPrefix)
manifestUnitName := path.Join(e.manifestSystemdUnit, unitNameNoPrefix)
fd, err := os.OpenFile(manifestUnitName, os.O_RDONLY, 0)
if err != nil {
glog.Errorf("Cannot read %s: %v", unitNameNoPrefix, err)
return err
}
defer fd.Close()
unitOptions, err := unit2.Deserialize(fd)
if err != nil {
glog.Errorf("Unexpected error during parsing s: %v", unitNameNoPrefix, err)
return err
}
// TODO see how to insert e.systemdEnd2EndSection
err = e.linkSystemdUnit(unitOptions, manifestUnitName, unitName)
if err != nil {
return err
}
return nil
}
func (e *Environment) setupSystemd() error {
conn, err := dbus.NewSystemdConnection()
if err != nil {
glog.Errorf("Cannot connect to dbus: %v", err)
return err
}
e.dbusClient = conn
for _, u := range e.systemdUnitNames {
glog.V(4).Infof("Creating systemd unit %s ...", u)
err = e.createUnitFromTemplate(u)
if err != nil {
return err
}
}
err = conn.Reload()
if err != nil {
glog.Errorf("Cannot daemon-reload: %v", err)
return err
}
return nil
}