forked from spacecloud-io/space-cloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
283 lines (258 loc) · 6.65 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"fmt"
"log"
"os"
"github.com/segmentio/ksuid"
"github.com/urfave/cli"
"github.com/spaceuptech/space-cloud/config"
"github.com/spaceuptech/space-cloud/utils"
"github.com/spaceuptech/space-cloud/utils/metrics"
"github.com/spaceuptech/space-cloud/utils/server"
)
var essentialFlags = []cli.Flag{
cli.StringFlag{
Name: "id",
Value: "none",
Usage: "The id to start space cloud with",
EnvVar: "NODE_ID",
},
cli.StringFlag{
Name: "config",
Value: "config.yaml",
Usage: "Load space cloud config from `FILE`",
EnvVar: "CONFIG",
},
cli.BoolFlag{
Name: "dev",
Usage: "Run space-cloud in development mode",
EnvVar: "DEV",
},
cli.BoolFlag{
Name: "disable-metrics",
Usage: "Disable anonymous metric collection",
EnvVar: "DISABLE_METRICS",
},
cli.BoolFlag{
Name: "profiler",
Usage: "Enable profiler endpoints for profiling",
EnvVar: "PROFILER",
},
cli.StringFlag{
Name: "cluster",
Usage: "The cluster id to start space-cloud with",
EnvVar: "CLUSTER_ID",
Value: "default-cluster",
},
cli.StringFlag{
Name: "advertise-addr",
Usage: "The address which will be broadcast to other space cloud instances",
EnvVar: "ADVERTISE_ADDR",
},
cli.StringFlag{
Name: "store-type",
Usage: "The config store to use for storing project configs and other meta data",
EnvVar: "STORE_TYPE",
Value: "none",
},
cli.IntFlag{
Name: "port",
EnvVar: "PORT",
Value: 4122,
},
cli.BoolFlag{
Name: "remove-project-scope",
Usage: "Removes the project level scope in the database and file storage modules",
EnvVar: "REMOVE_PROJECT_SCOPE",
},
cli.StringFlag{
Name: "ssl-cert",
Value: "none",
Usage: "Load ssl certificate from `FILE`",
EnvVar: "SSL_CERT",
},
cli.StringFlag{
Name: "ssl-key",
Value: "none",
Usage: "Load ssl key from `FILE`",
EnvVar: "SSL_KEY",
},
// flags for admin man
cli.StringFlag{
Name: "admin-user",
Usage: "Set the admin user name",
EnvVar: "ADMIN_USER",
Value: "",
},
cli.StringFlag{
Name: "admin-pass",
Usage: "Set the admin password",
EnvVar: "ADMIN_PASS",
Value: "",
},
cli.StringFlag{
Name: "admin-secret",
Usage: "Set the admin secret",
EnvVar: "ADMIN_SECRET",
Value: "",
},
// Flags for the metrics module
cli.BoolFlag{
Name: "enable-metrics",
Usage: "Enable the metrics module",
EnvVar: "ENABLE_METRICS",
},
cli.BoolFlag{
Name: "disable-bandwidth",
Usage: "disable the bandwidth measurement",
EnvVar: "DISABLE_BANDWIDTH",
},
cli.StringFlag{
Name: "metrics-sink",
Usage: "The sink to output metrics data to",
EnvVar: "METRICS_SINK",
},
cli.StringFlag{
Name: "metrics-conn",
Usage: "The connection string of the sink",
EnvVar: "METRICS_CONN",
},
cli.StringFlag{
Name: "metrics-scope",
Usage: "The database / topic to push the metrics to",
EnvVar: "METRICS_SCOPE",
},
}
func main() {
app := cli.NewApp()
app.Version = utils.BuildVersion
app.Name = "space-cloud"
app.Usage = "core binary to run space cloud"
app.Commands = []cli.Command{
{
Name: "run",
Usage: "runs the space cloud instance",
Action: actionRun,
Flags: essentialFlags,
},
{
Name: "init",
Usage: "creates a config file with sensible defaults",
Action: actionInit,
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func actionRun(c *cli.Context) error {
// Load cli flags
nodeID := c.String("id")
configPath := c.String("config")
isDev := c.Bool("dev")
disableMetrics := c.Bool("disable-metrics")
disableBandwidth := c.Bool("disable-bandwidth")
profiler := c.Bool("profiler")
// Load flag related to the port
port := c.Int("port")
removeProjectScope := c.Bool("remove-project-scope")
// Load flags related to ssl
sslKey := c.String("ssl-key")
sslCert := c.String("ssl-cert")
// Flags related to the admin details
adminUser := c.String("admin-user")
adminPass := c.String("admin-pass")
adminSecret := c.String("admin-secret")
// Load flags related to clustering
clusterID := c.String("cluster")
storeType := c.String("store-type")
advertiseAddr := c.String("advice-addr")
// Load the flags for the metrics module
enableMetrics := c.Bool("enable-metrics")
metricsSink := c.String("metrics-sink")
metricsConn := c.String("metrics-conn")
metricsScope := c.String("metrics-scope")
// Generate a new id if not provided
if nodeID == "none" {
nodeID = "auto-" + ksuid.New().String()
}
s, err := server.New(nodeID, clusterID, advertiseAddr, storeType, removeProjectScope,
&metrics.Config{IsEnabled: enableMetrics, SinkType: metricsSink, SinkConn: metricsConn, Scope: metricsScope, DisableBandwidth: disableBandwidth})
if err != nil {
return err
}
// Load the configFile from path if provided
conf, err := config.LoadConfigFromFile(configPath)
if err != nil {
conf = config.GenerateEmptyConfig()
}
// Save the config file path for future use
s.SetConfigFilePath(configPath)
// Override the admin config if provided
if adminUser != "" {
conf.Admin.Users[0].User = adminUser
}
if adminPass != "" {
conf.Admin.Users[0].Pass = adminPass
}
if adminSecret != "" {
conf.Admin.Secret = adminSecret
}
// Download and host mission control
staticPath, err := initMissionContol(utils.BuildVersion)
if err != nil {
return err
}
// Initialise the routes
s.InitRoutes(profiler, staticPath)
// Set the ssl config
if sslCert != "none" && sslKey != "none" {
s.InitSecureRoutes(profiler, staticPath)
conf.SSL = &config.SSL{Enabled: true, Crt: sslCert, Key: sslKey}
}
// Configure all modules
s.SetConfig(conf, !isDev)
return s.Start(disableMetrics, port)
}
func actionInit(*cli.Context) error {
return config.GenerateConfig("none")
}
func initMissionContol(version string) (string, error) {
homeDir := utils.UserHomeDir()
uiPath := homeDir + "/.space-cloud/mission-control-v" + version
_, err := os.Stat(uiPath)
if os.IsNotExist(err) {
fmt.Println("Could not find mission control")
_, err := os.Stat(homeDir + "/.space-cloud")
if err != nil && !os.IsNotExist(err) {
return "", err
}
if os.IsNotExist(err) {
err := os.Mkdir(homeDir+"/.space-cloud", os.ModePerm)
if err != nil {
return "", err
}
}
fmt.Println("Downloading...")
err = utils.DownloadFileFromURL("https://spaceuptech.com/downloads/mission-control/mission-control-v"+version+".zip", uiPath+".zip")
if err != nil {
return "", err
}
fmt.Println("Extracting...")
err = utils.Unzip(uiPath+".zip", uiPath)
if err != nil {
return "", err
}
fmt.Println("Done...")
err = os.Remove(uiPath + ".zip")
if err != nil {
return "", err
}
return uiPath + "/build", nil
}
if err != nil {
return "", err
}
return uiPath + "/build", nil
}