-
Notifications
You must be signed in to change notification settings - Fork 46
/
main.go
214 lines (203 loc) · 5.78 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
package main
import (
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"github.com/wrfly/container-web-tty/config"
"github.com/wrfly/container-web-tty/util"
)
func main() {
conf := config.New()
appFlags := []cli.Flag{
&cli.StringFlag{
Name: "addr",
EnvVars: util.EnvVars("address"),
Usage: "server binding address",
Value: "0.0.0.0",
Destination: &conf.Server.Address,
},
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
EnvVars: util.EnvVars("port"),
Usage: "HTTP server port, -1 for disable the HTTP server",
Value: 8080,
Destination: &conf.Server.Port,
},
&cli.StringFlag{
Name: "base",
EnvVars: util.EnvVars("base"),
Usage: "server base path, default is /",
Value: "/",
Destination: &conf.Server.Base,
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Value: false,
EnvVars: util.EnvVars("debug"),
Usage: "debug mode (log-level=debug enable pprof)",
Destination: &conf.Debug,
},
&cli.StringFlag{
Name: "backend",
Aliases: []string{"b"},
EnvVars: util.EnvVars("backend"),
Value: "docker",
Usage: "backend type, 'docker' or 'kube' or 'grpc'(remote)",
Destination: &conf.Backend.Type,
},
&cli.StringFlag{
Name: "docker-host",
EnvVars: append(util.EnvVars("docker-host"), "DOCKER_HOST"),
Value: "/var/run/docker.sock",
Usage: "docker host path",
Destination: &conf.Backend.Docker.DockerHost,
},
&cli.StringFlag{
Name: "docker-ps",
EnvVars: util.EnvVars("docker-ps"),
Usage: "docker ps options",
Destination: &conf.Backend.Docker.PsOptions,
},
&cli.StringFlag{
Name: "kube-config",
EnvVars: util.EnvVars("kube-config"),
Value: util.KubeConfigPath(),
Usage: "kube config path",
Destination: &conf.Backend.Kube.ConfigPath,
},
&cli.IntFlag{
Name: "grpc-port",
EnvVars: util.EnvVars("grpc-port"),
Usage: "grpc server port, -1 for disable the grpc server",
Value: -1,
Destination: &conf.Server.GrpcPort,
},
&cli.StringFlag{
Name: "grpc-servers",
EnvVars: util.EnvVars("grpc-servers"),
Usage: "upstream servers, for proxy mode(grpc address and port), use comma for split",
},
&cli.StringFlag{
Name: "grpc-auth",
EnvVars: util.EnvVars("grpc-auth"),
Usage: "grpc auth token",
Value: "password",
Destination: &conf.Backend.GRPC.Auth,
},
&cli.StringFlag{
Name: "grpc-proxy",
EnvVars: util.EnvVars("grpc-proxy"),
Usage: "grpc proxy address, in the format of http://127.0.0.1:8080 or socks5://127.0.0.1:1080",
Value: "",
Destination: &conf.Backend.GRPC.Proxy,
},
&cli.StringFlag{
Name: "idle-time",
EnvVars: util.EnvVars("idle-time"),
Usage: "time out of an idle connection",
},
&cli.BoolFlag{
Name: "control-all",
Aliases: []string{"ctl-a"},
EnvVars: util.EnvVars("ctl-a"),
Usage: "enable container control",
Destination: &conf.Server.Control.All,
},
&cli.BoolFlag{
Name: "control-start",
Aliases: []string{"ctl-s"},
EnvVars: util.EnvVars("ctl-s"),
Usage: "enable container start ",
Destination: &conf.Server.Control.Start,
},
&cli.BoolFlag{
Name: "control-stop",
Aliases: []string{"ctl-t"},
EnvVars: util.EnvVars("ctl-t"),
Usage: "enable container stop ",
Destination: &conf.Server.Control.Stop,
},
&cli.BoolFlag{
Name: "control-restart",
Aliases: []string{"ctl-r"},
EnvVars: util.EnvVars("ctl-r"),
Usage: "enable container restart",
Destination: &conf.Server.Control.Restart,
},
&cli.BoolFlag{
Name: "enable-collaborate",
Aliases: []string{"clb"},
EnvVars: util.EnvVars("collaborate"),
Usage: "shared terminal can write to the same TTY",
Destination: &conf.Server.Collaborate,
},
&cli.BoolFlag{
Name: "enable-audit",
Aliases: []string{"audit"},
EnvVars: util.EnvVars("audit"),
Usage: "enable audit the container outputs",
Destination: &conf.Server.EnableAudit,
},
&cli.StringFlag{
Name: "audit-dir",
EnvVars: util.EnvVars("audit-dir"),
Value: "audit",
Usage: "container audit log dir path",
Destination: &conf.Server.AuditLogDir,
},
&cli.BoolFlag{
Name: "help",
Aliases: []string{"h"},
Usage: "show help",
},
}
sort.Sort(cli.FlagsByName(appFlags))
app := &cli.App{
Name: "container-web-tty",
Usage: "connect your containers via a web-tty",
UsageText: "container-web-tty [global options]",
Flags: appFlags,
HideHelp: true,
Authors: author,
Version: fmt.Sprintf("version: %s\tcommit: %s\tdate: %s",
Version, CommitID, BuildAt),
Action: func(c *cli.Context) error {
if c.Bool("help") {
return cli.ShowAppHelp(c)
}
// parse idleTime
t := c.String("idle-time")
idleTime, err := time.ParseDuration(t)
if err != nil && t != "" {
logrus.Fatalf("parse idle-time error: %s", err)
} else {
conf.Server.IdleTime = idleTime
}
// defaultArgs := "-e HISTCONTROL=ignoredups -e TERM=xterm"
ctl := conf.Server.Control
if ctl.Start || ctl.Stop || ctl.Restart || ctl.All {
conf.Server.Control.Enable = true
}
servers := strings.Split(c.String("grpc-servers"), ",")
if servers[0] != "" {
conf.Backend.GRPC.Servers = servers
}
if conf.Debug {
logrus.SetLevel(logrus.DebugLevel)
} else {
gin.SetMode(gin.ReleaseMode)
}
logrus.Debugf("got config: %+v", conf)
run(c, *conf)
return nil
},
}
app.Run(os.Args)
}