This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
539 lines (467 loc) · 12.1 KB
/
main.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2018 Intel Corporation
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 main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/intelsdi-x/snap-deploy/runner"
"github.com/urfave/cli"
)
var (
snapS3URL = "snap.ci.snap-telemetry.io"
snapLocation = ""
snapteldName = "snapteld"
osName = runtime.GOOS
taskManifestLoc = "/tmp/task.yml"
hostname = "localhost"
snapteldURL = fmt.Sprintf("http://%s/snap/latest_build/%s/x86_64/snapteld", snapS3URL, osName)
snaptelURL = fmt.Sprintf("http://%s/snap/latest_build/%s/x86_64/snaptel", snapS3URL, osName)
snapteldDirPath = filepath.Join(snapLocation, "bin")
snapteldFilePath = filepath.Join(snapteldDirPath, snapteldName)
pluginsDirPath = filepath.Join(snapLocation, "plugin")
dirLocations = []string{"bin", "plugin"}
execCommand = exec.Command
removeCommand = os.RemoveAll
)
func pluginURL(pluginName string) string {
return fmt.Sprintf("http://snap.ci.snap-telemetry.io/plugins/snap-plugin-%s/latest/%s/x86_64/snap-plugin-%s", pluginName, osName, pluginName)
}
func downloadFromURL(url string, location string) {
tokens := strings.Split(url, "/")
fileName := tokens[len(tokens)-1]
log.Println("Downloading", url, "to", fileName)
fullPath := filepath.Join(location, fileName)
output, err := os.Create(fullPath)
if err != nil {
log.Println("Error while creating", fileName, "-", err)
return
}
defer output.Close()
response, err := http.Get(url)
if err != nil {
log.Println("Error while downloading", url, "-", err)
return
}
defer response.Body.Close()
n, err := io.Copy(output, response.Body)
if err != nil {
log.Println("Error while downloading", url, "-", err)
return
}
log.Println(n, "bytes downloaded.")
err = os.Chmod(fullPath, 0755)
if err != nil {
log.Println(err)
}
}
func runSnapd(snapLocation string, port string) int {
cmd := execCommand("nice", "-10", filepath.Join(snapLocation, "bin", snapteldName), "-t", "0", "-p", port, "-o", "/var/log/")
cmd.Stdout = os.Stdout
err := cmd.Start()
if err != nil {
log.Fatal(err)
}
time.Sleep(time.Second * 7)
return cmd.Process.Pid
}
func killSnapd() {
cmd, err := execCommand("killall", snapteldName).CombinedOutput()
if err != nil {
log.Fatalln(err.Error())
}
log.Println(cmd)
time.Sleep(time.Second * 2)
}
func createDirectories(config ConfigAPI) error {
err := removeCommand(config.snapLocation)
if err != nil {
log.Fatalf("Error removing directory %s\n", err)
}
for _, dir := range dirLocations {
location := filepath.Join(config.snapLocation, dir)
log.Println("Creating dir: ", location)
err := os.MkdirAll(location, 0777)
if err != nil {
log.Fatalf("Error creating directory %s\n", location)
}
}
return nil
}
func main() {
app := cli.NewApp()
config := ConfigAPI{}
app.Email = "marcin.spoczynski@intel.com"
app.Usage = "snap-deploy - Snap controller"
app.Version = "0.0.1"
app.EnableBashCompletion = true
app.Action = func(c *cli.Context) error {
if c.NArg() == 0 {
fmt.Println("Please use --help switch")
}
return nil
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "dbhost, o",
Value: "localhost",
EnvVar: "DB_HOST",
Destination: &config.DbHost,
},
cli.StringFlag{
Name: "dbdatabase, d",
Value: "snap",
EnvVar: "DB_NAME",
Destination: &config.DbDatabase,
},
cli.StringFlag{
Name: "dbuser, u",
Value: "admin",
EnvVar: "DB_USER",
Destination: &config.DbUser,
},
cli.StringFlag{
Name: "dbpassword, p",
Value: "admin",
EnvVar: "DB_PASS",
Destination: &config.DbPassword,
},
cli.StringFlag{
Name: "interval, i",
Value: "1s",
EnvVar: "INTERVAL",
Destination: &config.Interval,
},
cli.StringFlag{
Name: "tags, t",
EnvVar: "TAGS",
Destination: &config.Tags,
},
cli.StringFlag{
Name: "metrics, m",
EnvVar: "METRICS",
Destination: &config.Metrics,
},
cli.StringFlag{
Name: "port, sp",
Value: "8181",
EnvVar: "PORT",
Destination: &config.SnapPort,
},
cli.StringFlag{
Name: "directory, e",
EnvVar: "DIRECTORY",
Usage: "Directory where snap deployed",
Destination: &config.snapLocation,
},
cli.StringFlag{
Name: "plugins, r",
EnvVar: "PLUGINS",
Usage: "Plugins list",
Destination: &config.plugins,
},
}
app.Commands = []cli.Command{
{
Name: "deploy",
Aliases: []string{"d"},
Usage: "deploy",
Action: func(c *cli.Context) error {
deploy(config)
return nil
},
},
{
Name: "redeploy",
Aliases: []string{"rd"},
Usage: "redeploy",
Action: func(c *cli.Context) error {
killSnapd()
deploy(config)
return nil
},
},
{
Name: "download",
Aliases: []string{"f"},
Usage: "download",
Action: func(c *cli.Context) error {
download(config)
return nil
},
},
{
Name: "kill",
Aliases: []string{"k"},
Usage: "kill",
Action: func(c *cli.Context) error {
killSnapd()
return nil
},
},
{
Name: "start",
Aliases: []string{"s"},
Usage: "start",
Action: func(c *cli.Context) error {
runSnapd(config.snapLocation, config.SnapPort)
return nil
},
},
{
Name: "generate_task",
Aliases: []string{"f"},
Usage: "generate_task",
Action: func(c *cli.Context) error {
task, err := generateTask(config)
if err != nil {
log.Fatalf("Error generating manifest file: %s\n", err.Error())
panic(err)
}
err = ioutil.WriteFile(taskManifestLoc, task, 0644)
if err != nil {
log.Fatalf("Error writting manifest file: %s\n", err.Error())
panic(err)
}
log.Fatalf("Manifest file succesfully generated %s", taskManifestLoc)
return nil
},
},
}
app.Run(os.Args)
}
func deploy(config ConfigAPI) {
download(config)
pid := runSnapd(config.snapLocation, config.SnapPort)
log.Printf("Snap is running %d\n", pid)
loadPlugins(config)
task, err := createTaskCli(config)
if err != nil {
log.Fatalf("%s\n", err)
}
log.Println("Task created with ID: ", task)
}
func download(config ConfigAPI) {
err := createDirectories(config)
if err != nil {
panic(err)
}
downloadFromURL(snapteldURL, filepath.Join(config.snapLocation, "bin"))
downloadFromURL(snaptelURL, filepath.Join(config.snapLocation, "bin"))
pluginList := strings.Split(config.plugins, ",")
for _, plugin := range pluginList {
downloadFromURL(pluginURL(plugin), filepath.Join(config.snapLocation, "plugin"))
}
}
func unpackTags(config ConfigAPI) map[string]string {
var tagMap map[string]string
tagMap = make(map[string]string)
pairs := strings.Split(config.Tags, ",")
for _, p := range pairs {
item := strings.Split(p, ":")
if len(item) == 2 {
tagMap[item[0]] = item[1]
}
}
return tagMap
}
func generateTask(config ConfigAPI) ([]byte, error) {
schedule := Schedule{
Type: "simple",
Interval: config.Interval}
publishConfig := PublishConfig{
Host: config.DbHost,
User: config.DbUser,
Password: config.DbPassword,
Database: config.DbDatabase,
Port: 8086,
}
publish := Publish{
{PluginName: "influxdb", PublishConfig: publishConfig},
}
libvirt := Libvirt{
Nova: true,
}
tags := Tags{
Tags: unpackTags(config),
}
configMetric := MetricConfig{
Libvirt: libvirt,
}
collect := Collect{
Publish: publish,
Tags: tags,
Metrics: Metric{},
MetricConfig: configMetric,
}
t := &Task{
Version: 1,
Schedule: schedule,
Workflow: Workflow{
Collect: collect,
},
}
b, err := json.Marshal(t)
if err != nil {
return []byte{}, err
}
return createMetricList(config.Metrics, b), nil
}
func createMetricList(metricList string, taskManifest []byte) []byte {
var metrics []string
metrics = strings.Split(metricList, ",")
var buffer bytes.Buffer
metricLen := len(metrics)
for i, metric := range metrics {
metric = strings.Trim(metric, "")
buffer.WriteString(fmt.Sprintf("\"%s\":{}", metric))
if i < metricLen-1 {
buffer.WriteString(",")
}
}
sortedMetrics := strings.Replace(
string(taskManifest),
"\"Name\":null", string(buffer.String()),
1)
return []byte(sortedMetrics)
}
//Tags info for snap task
type Tags struct {
Tags map[string]string `json:"/intel"`
}
//ConfigAPI config for Snap Deploy
type ConfigAPI struct {
DbHost string
DbDatabase string
DbUser string
DbPassword string
Tags string
Interval string
Metrics string
Name string
SnapCtl string
SnapPort string
snapLocation string
plugins string
}
//Schedule info for snap task
type Schedule struct {
Type string `json:"type"`
Interval string `json:"interval"`
}
//Publish info for snap task
type Publish []struct {
PluginName string `json:"plugin_name"`
PublishConfig PublishConfig `json:"config"`
}
//PublishConfig info for snap task
type PublishConfig struct {
Host string `json:"host"`
Port int `json:"port"`
Database string `json:"database"`
User string `json:"user"`
Password string `json:"password"`
}
//Process info for snap task
type Process []struct {
PluginName string `json:"plugin_name"`
TagsConfig TagsConfig `json:"config"`
ProcessC interface{} `json:"process"`
Publish Publish `json:"publish"`
}
//TagsConfig info for snap task
type TagsConfig struct {
Tags string `json:"tags"`
}
//Collect info for snap task
type Collect struct {
Metrics Metric `json:"metrics"`
Tags Tags `json:"tags"`
Publish Publish `json:"publish"`
MetricConfig MetricConfig `json:"config"`
}
//Task info for snap task
type Task struct {
Version int `json:"version"`
Schedule Schedule `json:"schedule"`
Workflow Workflow `json:"workflow"`
}
//Workflow info for snap task
type Workflow struct {
Collect Collect `json:"collect"`
}
//Metric info for snap task
type Metric struct {
Name []interface{}
}
//MetricConfig info for snap task
type MetricConfig struct {
Libvirt Libvirt `json:"/intel/libvirt"`
}
//Libvirt info for snap task
type Libvirt struct {
Nova bool `json:"nova"`
}
func getSnapURL(config ConfigAPI) string {
return fmt.Sprintf("http://%s:%s", hostname, config.SnapPort)
}
func loadPlugins(config ConfigAPI) error {
pluginList := strings.Split(config.plugins, ",")
snapURL := getSnapURL(config)
log.Println("Loading plugins")
for _, plugin := range pluginList {
log.Println("Loading plugin", fmt.Sprintf("snap-plugin-%s", plugin))
cmd0, err := execCommand(filepath.Join(config.snapLocation, "bin", "snaptel"), "-u", snapURL, "plugin", "load", filepath.Join(config.snapLocation, "plugin", fmt.Sprintf("snap-plugin-%s", plugin))).CombinedOutput()
log.Println(string(cmd0))
if err != nil {
log.Println(string(err.Error()))
}
}
return nil
}
func createTaskCli(config ConfigAPI) (string, error) {
fileName := "/tmp/task.yml"
taskManifest, _ := generateTask(config)
err := ioutil.WriteFile(fileName, taskManifest, 0644)
if err != nil {
log.Fatalf("%s", err)
}
url := getSnapURL(config)
cmdRunner := runner.CmdRunner{}
reader, err := cmdRunner.Run(filepath.Join(config.snapLocation, "bin", "snaptel"), []string{"-u", url, "task", "create", "-t", fileName})
if err != nil {
log.Fatal(err.Error())
return "", err
}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "ID") {
fmt.Println(scanner.Text())
taskID := strings.Split(scanner.Text(), " ")
return taskID[1], nil
}
}
return "", fmt.Errorf("Can't find task ID")
}