forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
361 lines (308 loc) · 7.97 KB
/
container.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package main
import (
"errors"
"fmt"
"math"
"path/filepath"
"time"
)
const influxDataRPMRepo = `[influxdata]
name = InfluxData Repository - Stable
baseurl = https://repos.influxdata.com/stable/x86_64/main
enabled = 1
gpgcheck = 1
gpgkey = https://repos.influxdata.com/influxdata-archive_compat.key
`
type Container struct {
Name string
client IncusClient
packageManager string
}
// create container with given name and image
func (c *Container) Create(image string) error {
if c.Name == "" {
return errors.New("unable to create container: no name given")
}
c.client = IncusClient{}
err := c.client.Connect()
if err != nil {
return fmt.Errorf("failed to connect to incus: %w", err)
}
err = c.client.Create(c.Name, "images", image)
if err != nil {
return fmt.Errorf("failed to create instance: %w", err)
}
// at this point the container is created, so on any error during setup
// we want to delete it as well
err = c.client.Start(c.Name)
if err != nil {
c.Delete()
return fmt.Errorf("failed to start instance: %w", err)
}
if err := c.detectPackageManager(); err != nil {
c.Delete()
return err
}
if err := c.waitForNetwork(); err != nil {
c.Delete()
return err
}
if err := c.setupRepo(); err != nil {
c.Delete()
return err
}
return nil
}
// delete the container
func (c *Container) Delete() {
//nolint:errcheck // cleaning up state so no need to check for error
c.client.Stop(c.Name)
//nolint:errcheck // cleaning up state so no need to check for error
c.client.Delete(c.Name)
}
// installs the package from configured repos
func (c *Container) Install(packageName ...string) error {
var cmd []string
switch c.packageManager {
case "apt":
cmd = append([]string{"apt-get", "install", "--yes"}, packageName...)
case "yum":
cmd = append([]string{"yum", "install", "-y"}, packageName...)
case "dnf":
cmd = append([]string{"dnf", "install", "-y"}, packageName...)
case "zypper":
cmd = append([]string{"zypper", "--gpg-auto-import-keys", "install", "-y"}, packageName...)
}
err := c.client.Exec(c.Name, cmd...)
if err != nil {
return err
}
return nil
}
func (c *Container) CheckStatus(serviceName string) error {
// push a valid config first, then start the service
err := c.client.Exec(
c.Name,
"bash",
"-c",
"--",
"echo '[[inputs.cpu]]\n[[outputs.file]]' | "+
"tee /etc/telegraf/telegraf.conf",
)
if err != nil {
return err
}
err = c.client.Exec(
c.Name,
"bash",
"-c",
"--",
"ls -la /etc/telegraf/",
)
if err != nil {
return err
}
err = c.client.Exec(c.Name, "systemctl", "start", serviceName)
if err != nil {
//nolint:errcheck // cleaning up state so no need to check for error
c.client.Exec(c.Name, "systemctl", "status", serviceName)
//nolint:errcheck // cleaning up state so no need to check for error
c.client.Exec(c.Name, "journalctl", "--no-pager", "--unit", serviceName)
return err
}
err = c.client.Exec(c.Name, "systemctl", "status", serviceName)
if err != nil {
//nolint:errcheck // cleaning up state so no need to check for error
c.client.Exec(c.Name, "journalctl", "--no-pager", "--unit", serviceName)
return err
}
return nil
}
func (c *Container) UploadAndInstall(filename string) error {
basename := filepath.Base(filename)
destination := "/root/" + basename
if err := c.client.Push(c.Name, filename, destination); err != nil {
return err
}
return c.Install(destination)
}
// Push key and config and update
func (c *Container) configureApt() error {
err := c.client.Exec(c.Name, "apt-get", "update")
if err != nil {
return err
}
err = c.Install("ca-certificates", "gpg", "wget")
if err != nil {
return err
}
err = c.client.Exec(c.Name, "wget", "https://repos.influxdata.com/influxdata-archive_compat.key")
if err != nil {
return err
}
err = c.client.Exec(
c.Name,
"bash",
"-c",
"--",
"echo '393e8779c89ac8d958f81f942f9ad7fb82a25e133faddaf92e15b16e6ac9ce4c influxdata-archive_compat.key' | "+
"sha256sum -c && cat influxdata-archive_compat.key | gpg --dearmor | "+
"sudo tee /etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg > /dev/null",
)
if err != nil {
return err
}
err = c.client.Exec(
c.Name,
"bash",
"-c",
"--",
"echo 'deb [signed-by=/etc/apt/trusted.gpg.d/influxdata-archive_compat.gpg] https://repos.influxdata.com/debian stable main' | "+
"tee /etc/apt/sources.list.d/influxdata.list",
)
if err != nil {
return err
}
err = c.client.Exec(
c.Name,
"bash", "-c", "--",
"cat /etc/apt/sources.list.d/influxdata.list",
)
if err != nil {
return err
}
err = c.client.Exec(c.Name, "apt-get", "update")
if err != nil {
return err
}
return nil
}
// Create config and update yum
func (c *Container) configureYum() error {
err := c.client.Exec(
c.Name,
"bash", "-c", "--",
fmt.Sprintf("echo -e %q > /etc/yum.repos.d/influxdata.repo", influxDataRPMRepo),
)
if err != nil {
return err
}
err = c.client.Exec(
c.Name,
"bash", "-c", "--",
"cat /etc/yum.repos.d/influxdata.repo",
)
if err != nil {
return err
}
// will return a non-zero return code if there are packages to update
return c.client.Exec(c.Name, "bash", "-c", "yum check-update || true")
}
// Create config and update dnf
func (c *Container) configureDnf() error {
err := c.client.Exec(
c.Name,
"bash", "-c", "--",
fmt.Sprintf("echo -e %q > /etc/yum.repos.d/influxdata.repo", influxDataRPMRepo),
)
if err != nil {
return err
}
err = c.client.Exec(
c.Name,
"bash", "-c", "--",
"cat /etc/yum.repos.d/influxdata.repo",
)
if err != nil {
return err
}
// will return a non-zero return code if there are packages to update
return c.client.Exec(c.Name, "bash", "-c", "dnf check-update || true")
}
// Create config and update zypper
func (c *Container) configureZypper() error {
err := c.client.Exec(
c.Name,
"bash", "-c", "--",
fmt.Sprintf("echo -e %q > /etc/zypp/repos.d/influxdata.repo", influxDataRPMRepo),
)
if err != nil {
return err
}
err = c.client.Exec(
c.Name,
"bash", "-c", "--",
"cat /etc/zypp/repos.d/influxdata.repo",
)
if err != nil {
return err
}
return c.client.Exec(c.Name, "zypper", "--no-gpg-checks", "refresh")
}
// Determine if the system uses yum or apt for software
func (c *Container) detectPackageManager() error {
// Different options required across the distros as apt returns -1 when
// run with no options. yum is listed last to prefer the newer
// options first.
err := c.client.Exec(c.Name, "which", "apt")
if err == nil {
c.packageManager = "apt"
return nil
}
err = c.client.Exec(c.Name, "dnf")
if err == nil {
c.packageManager = "dnf"
return nil
}
err = c.client.Exec(c.Name, "yum", "version")
if err == nil {
c.packageManager = "yum"
return nil
}
err = c.client.Exec(c.Name, "which", "zypper")
if err == nil {
c.packageManager = "zypper"
return nil
}
return errors.New("unable to determine package manager")
}
// Configure the system with InfluxData repo
func (c *Container) setupRepo() error {
if c.packageManager == "apt" {
if err := c.configureApt(); err != nil {
return err
}
} else if c.packageManager == "yum" {
if err := c.configureYum(); err != nil {
return err
}
} else if c.packageManager == "zypper" {
if err := c.configureZypper(); err != nil {
return err
}
} else if c.packageManager == "dnf" {
if err := c.configureDnf(); err != nil {
return err
}
}
return nil
}
// Wait for the network to come up on a container
func (c *Container) waitForNetwork() error {
var exponentialBackoffCeilingSecs int64 = 128
attempts := 0
for {
if err := c.client.Exec(c.Name, "getent", "hosts", "influxdata.com"); err == nil {
return nil
}
// uses exponetnial backoff to try after 1, 2, 4, 8, 16, etc. seconds
delaySecs := int64(math.Pow(2, float64(attempts)))
if delaySecs > exponentialBackoffCeilingSecs {
break
}
fmt.Printf("waiting for network, sleeping for %d second(s)\n", delaySecs)
time.Sleep(time.Duration(delaySecs) * time.Second)
attempts++
}
return errors.New("timeout reached waiting for network on container")
}