-
Notifications
You must be signed in to change notification settings - Fork 93
/
main.go
124 lines (100 loc) · 2.84 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
package main
import (
"context"
"os"
"github.com/Orientsoft/kubekit/utils"
"github.com/Orientsoft/kubekit/controllers"
"github.com/fatih/color"
cli "gopkg.in/urfave/cli.v1"
)
const (
FilePort = iota
ToolkitPort
VERSION = "0.3"
)
func initialize() {
//Remove the install log file
os.Remove("install.log")
utils.DisplayLogo(VERSION)
}
func main() {
initialize()
app := cli.NewApp()
app.Name = "KubeKit"
app.Usage = "A toolkit for Kubernetes & apps offline deployment."
app.Version = VERSION
app.Action = func(c *cli.Context) error {
return nil
}
app.Commands = []cli.Command{
{
Name: "init",
Aliases: []string{"i"},
Usage: "Initialize current server with Docker engine & Kubernetes master.",
ArgsUsage: "K8S_MASTER_IP FILE_SERVER_PORT TOOLKIT_SERVER_PORT",
Action: func(c *cli.Context) error {
masterIP := c.Args().Get(0)
if masterIP == "" {
color.Red("Please run kubekit with master IP: kubekit i K8S_MASTER_IP")
os.Exit(0)
}
color.Blue("Initialization process started, with kubernetes master IP: %s\r\n", masterIP)
utils.SaveMasterIP(masterIP)
filePort := getServerPort(c, FilePort, 1)
toolkitPort := getServerPort(c, ToolkitPort, 2)
masterAddr := masterIP + filePort
srv := utils.StartServer(filePort)
defer srv.Shutdown(context.Background())
if !utils.SetupDocker(masterAddr) {
color.Red("%sProgram terminated...", utils.CrossSymbol)
os.Exit(1)
}
if utils.SetupMaster(masterAddr) {
// Launch toolkit server
controllers.StartToolkitServer(filePort, toolkitPort)
}
return nil
},
},
{
Name: "server",
Aliases: []string{"s"},
Usage: "Start kubekit file server & toolkit server.",
ArgsUsage: "FILE_SERVER_PORT TOOLKIT_SERVER_PORT",
Action: func(c *cli.Context) error {
filePort := getServerPort(c, FilePort, 0)
toolkitPort := getServerPort(c, ToolkitPort, 1)
srv := utils.StartServer(filePort)
defer srv.Shutdown(context.Background())
// Launch toolkit server only
controllers.StartToolkitServer(filePort, toolkitPort)
return nil
},
},
}
app.Run(os.Args)
}
// getServerPort parse and get server port by port type and argument position
// portType: 1.File server 2.Toolkit server
func getServerPort(c *cli.Context, portType, argPos int) string {
var port string
if len(c.Args().Get(argPos)) > 0 {
if utils.IsValidPort(c.Args().Get(argPos)) {
port = ":" + c.Args().Get(argPos)
} else {
if portType == FilePort {
color.Red("%sPlease input a valid file server port!", utils.CrossSymbol)
} else if portType == ToolkitPort {
color.Red("%sPlease input a valid toolkit server port!", utils.CrossSymbol)
}
os.Exit(1)
}
} else {
if portType == FilePort {
port = ":8000"
} else if portType == ToolkitPort {
port = ":9000"
}
}
return port
}