This repository has been archived by the owner on Jul 23, 2019. It is now read-only.
/
main.go
154 lines (134 loc) · 5.2 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
package main
import (
"github.com/docker/docker/client"
"github.com/gaia-docker/tugbot-collect/log"
"github.com/urfave/cli"
"github.com/gaia-docker/tugbot-collect/eventlistener"
"github.com/gaia-docker/tugbot-collect/processor"
"github.com/gaia-docker/tugbot-collect/scanner"
"os"
"os/signal"
"syscall"
"strings"
"errors"
)
var logger = log.GetLogger("main")
var dockerRM bool
var scanOnStartup bool
var skipEvents bool
var outputDir string
var publishTarGzTo string
var publishTestCasesTo string
var matchLabel string
var resultsDirLabel string
func main() {
app := cli.NewApp()
app.Version = "1.0.0"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "publishTarGzTo, g",
Value: "http://result-service:8080/results",
Usage: "send http POST to `URL` with tar.gz payload contains all of the extracted results. To disable - set this flag to 'null'",
Destination: &publishTarGzTo,
},
cli.StringFlag{
Name: "publishTestsTo, c",
Value: "http://result-service-es:8081/results",
Usage: "send http POST to `URL` in json format for any junit test extracted from junit XMLs within the results dir. To disable - set this flag to 'null'",
Destination: &publishTestCasesTo,
},
cli.StringFlag{
Name: "outputDir, o",
Value: "/tmp/tugbot-collect",
Usage: "write results to `DIR_LOCATION`, if you want not to output results - set this flag with the directory '/dev/null'",
Destination: &outputDir,
},
cli.StringFlag{
Name: "resultsDirLabel, r",
Value: "tugbot-results-dir",
Usage: "tugbot-collect will use this docker label `KEY` and fetch the label value from the test container to resolve the results dir location. If the label could not be found on the test container, the default results dir location: '" + processor.ResultsDirDefaultPath + "' will be in use",
Destination: &resultsDirLabel,
},
cli.StringFlag{
Name: "matchLabel, m",
Value: "tugbot-test",
Usage: "tugbot-collect will collect results from test containers matching this label `KEY`",
Destination: &matchLabel,
},
cli.BoolFlag{
Name: "scanOnStartup, e",
Usage: "scan for existed containers on startup and extract their results (default is false)",
Destination: &scanOnStartup,
},
cli.BoolFlag{
Name: "dockerRM, d",
Usage: "remove the container after extracting results (default is false)",
Destination: &dockerRM,
},
cli.BoolFlag{
Name: "skipEvents, s",
Usage: "do not register to docker 'die' events (default is false - by default we register to events and collect results for any stopped or killed test container)",
Destination: &skipEvents,
},
}
app.Name = "tugbot-collect"
app.Usage = "Collects result from test containers (use TC_LOG_LEVEL env var to change the default which is debug"
app.Action = start
if err := app.Run(os.Args); err != nil {
logger.Error("exiting from main: ", err)
}
}
func start(c *cli.Context) error {
if (outputDir == "/dev/null") && (!strings.EqualFold(publishTestCasesTo, "null") || !strings.EqualFold(publishTarGzTo, "null")) {
return errors.New("outputDir cannot be /dev/null when publishTestCasesTo or publishTarGzTo are in use.")
}
logger.Info("tugbot-collect is going to run with this configuration:")
logger.Info("scanOnStartup: ", scanOnStartup)
logger.Info("skipEvents: ", skipEvents)
logger.Info("outputDir: ", outputDir)
logger.Info("dockerRM: ", dockerRM)
logger.Info("matchLabel: ", matchLabel)
logger.Info("resultsDirLabel: ", resultsDirLabel)
logger.Info("publishTarGzTo: ", publishTarGzTo)
logger.Info("publishTestCasesTo: ", publishTestCasesTo)
// Go signal notification works by sending `os.Signal`
// values on a channel. We'll create a channel to
// receive these notifications (we'll also make one to
// notify us when the program can exit).
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
// `signal.Notify` registers the given channel to
// receive notifications of the specified signals.
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// This goroutine executes a blocking receive for
// signals. When it gets one it'll print it out
// and then notify the program that it can finish.
go func() {
var logger = log.GetLogger("signalgoroutine")
sig := <-sigs
logger.Info("got signal: \"", sig, "\", on goroutine. Going to notify main")
done <- true
}()
//Creating docker client
defaultHeaders := map[string]string{"User-Agent": "engine-api-cli-1.0"}
dockerClient, err := client.NewClient("unix:///var/run/docker.sock", "v1.22", nil, defaultHeaders)
if err != nil {
logger.Fatal("Failed to create docker client. why: ", err, ". panic the system.")
panic(err)
}
p := processor.NewProcessor(dockerClient, outputDir, publishTarGzTo, publishTestCasesTo, resultsDirLabel, dockerRM)
p.Run()
if scanOnStartup {
scanner.Scan(dockerClient, matchLabel, p.Tasks)
}
if !skipEvents {
eventlistener.Register(dockerClient, matchLabel, p.Tasks)
}
// The program will wait here until it gets the
// expected signal (as indicated by the goroutine
// above sending a value on `done`) and then exit.
logger.Info("awaiting signal")
<-done
logger.Info("exiting")
return nil
}