-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
143 lines (130 loc) · 3.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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/EGT-Ukraine/gitlab-trigger-watcher/models"
"github.com/EGT-Ukraine/gitlab-trigger-watcher/trigger"
"github.com/urfave/cli"
)
const (
lifecycleTimeout = 10 * time.Minute
thresholdTimeout = 2 * time.Second
)
func main() {
go shutdownSignalReceiver()
app := cli.App{
Flags: []cli.Flag{
cli.BoolFlag{
Name: "skipVerifyTLS",
},
cli.StringFlag{
Name: "schema",
Usage: "http schema",
Value: "https",
},
cli.StringFlag{
Name: "host",
Value: trigger.DefaultHost,
},
cli.StringFlag{
Name: "ref",
Usage: "for change the branch",
Value: "master",
},
cli.StringFlag{
Name: "urlPrefix",
Value: "/",
},
cli.IntFlag{
Name: "projectID",
Usage: "project ID",
Value: 0,
},
cli.StringFlag{
Name: "privateToken",
Usage: "your personal private token",
Value: "",
},
cli.StringFlag{
Name: "token",
Usage: "project token",
Value: "",
},
cli.StringSliceFlag{
Name: "variables",
Usage: "variable1:value,variable2:value",
Value: nil,
},
},
Commands: []cli.Command{
{
Name: "run",
Action: func(ctx *cli.Context) {
schema, err := schemaConverter(ctx.GlobalString("schema"))
if err != nil {
log.Fatal(err)
return
}
trigger := trigger.New(
ctx.GlobalBool("skipVerifyTLS"),
schema,
ctx.GlobalString("host"),
ctx.GlobalString("urlPrefix"),
ctx.GlobalString("privateToken"),
ctx.GlobalString("token"),
ctx.GlobalString("ref"),
ctx.GlobalInt("projectID"),
ctx.GlobalStringSlice("variables"),
)
triggerRunResp, err := trigger.RunPipeline()
if err != nil {
log.Fatal(err)
}
log.Printf("trigger's job URL: %s", triggerRunResp.WebURL)
go func() {
time.Sleep(lifecycleTimeout)
log.Fatalf("lifecycle time(%v) has been expired", lifecycleTimeout)
}()
var previousPipelineStatus models.PipelineStatus
for {
triggerCompletionResp, err := trigger.PollForCompletion(triggerRunResp.ID)
if err != nil {
log.Fatal(err)
}
switch triggerCompletionResp.Status {
case models.Pending, models.Running:
if previousPipelineStatus != triggerCompletionResp.Status {
log.Printf("pipeline status: %s\n", triggerCompletionResp.Status)
previousPipelineStatus = triggerCompletionResp.Status
}
case models.Failed:
log.Fatalln("pipeline failed!")
case models.Canceled:
log.Fatalln("pipeline canceled!")
case models.Success:
log.Println("pipeline success!")
os.Exit(0)
default:
log.Printf("unknown status: %s", triggerCompletionResp.Status)
os.Exit(1)
}
time.Sleep(thresholdTimeout)
}
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatalf("start application failed: %s", err)
}
}
func shutdownSignalReceiver() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
<-quit
log.Println("Shutting down...")
os.Exit(0)
}