This repository has been archived by the owner on Dec 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
308 lines (285 loc) · 8.12 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (c) 2017-2021 Ingram Micro Inc.
package main
import (
"fmt"
"os"
"sort"
"github.com/ingrammicro/cio/agentsecret"
"github.com/ingrammicro/cio/audit"
"github.com/ingrammicro/cio/blueprint"
"github.com/ingrammicro/cio/bootstrapping"
"github.com/ingrammicro/cio/brownfield"
"github.com/ingrammicro/cio/clientbrownfield"
"github.com/ingrammicro/cio/cloud"
"github.com/ingrammicro/cio/cloudapplications"
"github.com/ingrammicro/cio/cloudspecificextensions"
"github.com/ingrammicro/cio/cmdpolling"
"github.com/ingrammicro/cio/converge"
"github.com/ingrammicro/cio/dispatcher"
"github.com/ingrammicro/cio/firewall"
"github.com/ingrammicro/cio/kubernetes"
"github.com/ingrammicro/cio/labels"
"github.com/ingrammicro/cio/network"
"github.com/ingrammicro/cio/settings"
"github.com/ingrammicro/cio/storage"
"github.com/ingrammicro/cio/utils"
"github.com/ingrammicro/cio/utils/format"
"github.com/ingrammicro/cio/wizard"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var serverCommands = []cli.Command{
{
Name: "bootstrap",
Usage: "Manages bootstrapping commands",
Subcommands: append(bootstrapping.SubCommands()),
},
{
Name: "brownfield",
Usage: "Manages registration and configuration within an imported brownfield Host",
Subcommands: append(brownfield.SubCommands()),
},
{
Name: "converge",
Usage: "Converges Host to original Blueprint",
Action: converge.CmbConverge,
},
{
Name: "firewall",
Usage: "Manages Firewall Policies within a Host",
Subcommands: append(firewall.SubCommands()),
},
{
Name: "polling",
Usage: "Manages polling commands",
Subcommands: append(cmdpolling.SubCommands()),
},
{
Name: "scripts",
Usage: "Manages Execution Scripts within a Host",
Subcommands: append(dispatcher.SubCommands()),
},
{
Name: "secret",
Usage: "Accesses to secrets within a Host",
Subcommands: append(agentsecret.SubCommands()),
},
}
var clientCommands = []cli.Command{
{
Name: "blueprint",
ShortName: "bl",
Usage: "Manages blueprint commands for scripts, cookbook versions and templates",
Subcommands: append(blueprint.SubCommands()),
},
{
Name: "brownfield",
ShortName: "bf",
Usage: "Manages brownfield resources, allowing users to discover and import servers, VPCs, floating IPs, " +
"volumes and policies from different cloud accounts into the system.",
Subcommands: append(clientbrownfield.SubCommands()),
},
{
Name: "cloud-applications",
ShortName: "ca",
Usage: "Manages cloud application templates -CATs- and deployments",
Subcommands: append(cloudapplications.SubCommands()),
},
{
Name: "cloud",
ShortName: "clo",
Usage: "Manages cloud related commands for server arrays, servers, generic images, ssh profiles, " +
"cloud providers, realms, server plans and infrastructure archives",
Subcommands: append(cloud.SubCommands()),
},
{
Name: "cloud-specific-extensions",
ShortName: "cse",
Usage: "Manages cloud specific extensions -CSEs- templates and deployments",
Subcommands: append(cloudspecificextensions.SubCommands()),
},
{
Name: "events",
ShortName: "ev",
Usage: "Events allow the user to track their actions and the state of their servers",
Subcommands: append(audit.SubCommands()),
},
{
Name: "labels",
ShortName: "lbl",
Usage: "Provides information about labels",
Subcommands: append(labels.SubCommands()),
},
{
Name: "network",
ShortName: "net",
Usage: "Manages network related commands",
Subcommands: append(network.SubCommands()),
},
{
Name: "storage",
ShortName: "st",
Usage: "Manages storage commands for plans and volumes",
Subcommands: append(storage.SubCommands()),
},
{
Name: "kubernetes",
ShortName: "k8s",
Usage: "Manages kubernetes commands for clusters and node pools",
Subcommands: append(kubernetes.SubCommands()),
},
{
Name: "settings",
ShortName: "set",
Usage: "Provides settings for cloud accounts and policies",
Subcommands: append(settings.SubCommands()),
},
{
Name: "wizard",
ShortName: "wiz",
Usage: "Manages wizard related commands for apps, locations, cloud providers, server plans",
Subcommands: append(wizard.SubCommands()),
},
}
var appFlags = []cli.Flag{
cli.BoolFlag{
Name: "debug, D",
Usage: "Enable debug mode",
},
cli.StringFlag{
EnvVar: "CONCERTO_CA_CERT",
Name: "ca-cert",
Usage: "CA to verify remote connections",
},
cli.StringFlag{
EnvVar: "CONCERTO_CLIENT_CERT",
Name: "client-cert",
Usage: "Client cert to use for Concerto",
},
cli.StringFlag{
EnvVar: "CONCERTO_CLIENT_KEY",
Name: "client-key",
Usage: "Private key used in client Concerto auth",
},
cli.StringFlag{
EnvVar: "CONCERTO_CONFIG",
Name: "concerto-config",
Usage: "Concerto Config File",
},
cli.StringFlag{
EnvVar: "CONCERTO_ENDPOINT",
Name: "concerto-endpoint",
Usage: "Concerto Endpoint",
},
cli.StringFlag{
EnvVar: "CONCERTO_URL",
Name: "concerto-url",
Usage: "Concerto Web URL",
},
cli.StringFlag{
EnvVar: "CONCERTO_BROWNFIELD_TOKEN",
Name: "concerto-brownfield-token",
Usage: "Concerto Brownfield Token",
},
cli.StringFlag{
EnvVar: "CONCERTO_COMMAND_POLLING_TOKEN",
Name: "concerto-command-polling-token",
Usage: "Concerto Command Polling Token",
},
cli.StringFlag{
EnvVar: "CONCERTO_SERVER_ID",
Name: "concerto-server-id",
Usage: "Concerto Server ID",
},
cli.StringFlag{
EnvVar: "CONCERTO_FORMATTER",
Name: "formatter",
Usage: "Output formatter [ text | json ] ",
Value: "text",
},
}
func excludeFlags(visibleFlags []cli.Flag, arr []string) (flags []cli.Flag) {
for _, flag := range visibleFlags {
bFound := false
for _, a := range arr {
if a == flag.GetName() {
bFound = true
break
}
}
if !bFound {
flags = append(flags, flag)
}
}
return
}
func cmdNotFound(c *cli.Context, command string) {
log.Fatalf(
"%s: '%s' is not a %s command. See '%s --help'.",
c.App.Name,
command,
c.App.Name,
c.App.Name,
)
}
func prepareFlags(c *cli.Context) error {
if c.Bool("debug") {
if err := os.Setenv("DEBUG", "1"); err != nil {
log.Errorf("Couldn't set environment debug mode: %s", err)
return err
}
log.SetOutput(os.Stderr)
log.SetLevel(log.DebugLevel)
}
// try to read configuration
config, err := utils.InitializeConcertoConfig(c)
if err != nil {
log.Errorf("Error reading Concerto configuration: %s", err)
return err
}
// validate formatter
if c.String("formatter") != "text" && c.String("formatter") != "json" {
log.Errorf("Unrecognized formatter %s. Please, use one of [ text | json ]", c.String("formatter"))
return fmt.Errorf("unrecognized formatter %s. Please, use one of [ text | json ]", c.String("formatter"))
}
format.InitializeFormatter(c.String("formatter"), os.Stdout)
if config.IsAgentMode() {
log.Debug("Setting server commands to concerto")
c.App.Commands = serverCommands
} else {
log.Debug("Setting client commands to concerto")
c.App.Commands = clientCommands
// Excluding Server/Agent contextual flags
c.App.Flags = excludeFlags(c.App.VisibleFlags(), []string{
"concerto-brownfield-token", "concerto-command-polling-token", "concerto-server-id",
})
}
sort.Sort(cli.CommandsByName(c.App.Commands))
sort.Sort(cli.FlagsByName(c.App.Flags))
// hack: substitute commands in category ... we should evaluate cobra/viper
cat := c.App.Categories()
for _, category := range cat {
category.Commands = category.Commands[:0]
}
for _, command := range c.App.Commands {
cat = cat.AddCommand(command.Category, command)
}
return nil
}
func main() {
app := cli.NewApp()
app.Name = "cio"
app.Author = "Ingram Micro"
app.Email = "https://github.com/ingrammicro/cio"
app.CommandNotFound = cmdNotFound
app.Usage = "Manages communication between Host and IMCO Platform"
app.Version = utils.VERSION
// set client commands by default to populate categories
app.Commands = clientCommands
app.Flags = appFlags
app.Before = prepareFlags
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}