-
Notifications
You must be signed in to change notification settings - Fork 7
/
app.go
156 lines (129 loc) · 3.19 KB
/
app.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
package main
import (
"fmt"
"net/http"
"os"
"path/filepath"
"github.com/flowci/flow-agent-x/config"
"github.com/flowci/flow-agent-x/controller"
"github.com/flowci/flow-agent-x/domain"
"github.com/flowci/flow-agent-x/util"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/urfave/cli"
)
const version = "1.22.38"
func init() {
util.LogInit()
util.EnableDebugLog()
}
func main() {
app := cli.NewApp()
app.Name = "Agent of flow.ci"
app.Usage = ""
app.Action = start
app.Author = "yang.guo"
app.Version = version
cm := config.GetInstance()
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "Enable debug model",
EnvVar: domain.VarAgentDebug,
Destination: &cm.Debug,
},
cli.StringFlag{
Name: "url, u",
Value: "http://127.0.0.1:8080",
Usage: "flow.ci server url",
EnvVar: domain.VarServerUrl,
Destination: &cm.Server,
},
cli.StringFlag{
Name: "token, t",
Usage: "Token for agent",
EnvVar: domain.VarAgentToken,
Destination: &cm.Token,
},
cli.IntFlag{
Name: "port, p",
Value: -1,
Usage: "Port for agent",
EnvVar: domain.VarAgentPort,
Destination: &cm.Port,
},
cli.StringFlag{
Name: "profile",
Usage: "Enable or disable agent profiling",
EnvVar: domain.VarAgentEnableProfile,
Value: "true",
Destination: &cm.ProfileEnabledStr,
},
cli.BoolFlag{
Name: "k8sEnabled",
Usage: "Indicate is run from k8s",
EnvVar: domain.VarK8sEnabled,
Destination: &cm.K8sEnabled,
},
cli.BoolFlag{
Name: "k8sInCluster",
Usage: "Indicate is k8s run in cluster",
EnvVar: domain.VarK8sInCluster,
Destination: &cm.K8sCluster,
},
cli.StringFlag{
Name: "workspace, w",
Value: filepath.Join(util.HomeDir, ".flow.ci.agent"),
Usage: "Agent working directory",
EnvVar: domain.VarAgentWorkspace,
Destination: &cm.Workspace,
},
cli.StringFlag{
Name: "volumes, m",
Usage: "List of volume that will mount to docker from step \n" +
"format: name=xxx,dest=xxx,script=xxx;name=xxx,dest=xxx,script=xxx;...",
EnvVar: domain.VarAgentVolumes,
Destination: &cm.VolumesStr,
},
}
err := app.Run(os.Args)
util.LogIfError(err)
}
func start(c *cli.Context) error {
util.LogInfo("Staring flow.ci agent (v%s)...", version)
defer func() {
if err := recover(); err != nil {
util.LogIfError(err.(error))
}
util.LogInfo("Agent stopped")
}()
cm := config.GetInstance()
cm.Init()
defer cm.Close()
// connect to ci server
startGin(cm)
return nil
}
func startGin(cm *config.Manager) {
router := gin.Default()
controller.NewCmdController(router)
controller.NewHealthController(router)
if cm.Debug {
pprof.Register(router)
}
server := &http.Server{
Addr: fmt.Sprintf(":%d", cm.Port),
Handler: router,
}
go func() {
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
util.FailOnError(err, "Unable to listen")
}
}()
// wait
<-cm.AppCtx.Done()
if err := server.Shutdown(cm.AppCtx); err != nil {
util.FailOnError(err, "Unable to stop the agent")
}
}