-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
333 lines (317 loc) · 13.2 KB
/
Copy pathmain.go
File metadata and controls
333 lines (317 loc) · 13.2 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package main
import (
"flag"
"fmt"
"github.com/hellgate75/k8s-cli/common"
"github.com/hellgate75/k8s-cli/executor"
"github.com/hellgate75/k8s-cli/io"
"github.com/hellgate75/k8s-cli/model"
"os"
"strings"
"time"
)
var format string
var command string
var subcommand string
var subsubcommand string
var clusterName string
var clusterConfigYaml string
var nodeName string
var hostName string
var nodeSlots int
var verifySlots bool
var instanceName string
var namespace string
var dataDir string
func initHelp() {
flag.StringVar(&command, "command", "help", "Required executor action (show, details, discover, add, remove, verify, prepare. ensure, help, -clean-lock)")
flag.StringVar(&subcommand, "subject", "", "Required executor action subject (cluster, node, instance) or executor in case of help")
flag.StringVar(&dataDir, "config-dir", common.ConfigDir(), "Configuration folder")
flag.StringVar(&subsubcommand, "details", "", "Required executor action subject (cluster, node, instance) only in case of help")
flag.StringVar(&format, "format", "json", "Required output format (json, yaml), in case of error or missing will be used JSON")
flag.BoolVar(&verifySlots, "verify-slots", false, "Retrun information about Free slots for nodes and clusters")
flag.StringVar(&clusterName, "cluster-name", "default", "Cluster name")
flag.StringVar(&clusterConfigYaml, "kubectl-yaml-file", "", "Kubectl Yaml file")
flag.StringVar(&nodeName, "node-name", "", "Cluster node name")
flag.StringVar(&hostName, "node-host-name", "", "Cluster node host name")
flag.IntVar(&nodeSlots, "node-slots", 2, "Cluster node max number of prepareations")
flag.StringVar(&instanceName, "instance-name", "", "Cluster node instance name")
flag.StringVar(&namespace, "namespace", "", "Cluster node instance namespace name")
}
func showCommandInit(subCommand string) *flag.FlagSet {
fmt.Println("Show clusters, nodes or instances details:")
fset := flag.NewFlagSet(fmt.Sprintf("k8s-cli (cmd: show %s)", subCommand), flag.ContinueOnError)
fset.StringVar(&command, "command", "show", "Required executor action : show")
fset.StringVar(&subcommand, "subject", "clusters", "Required executor action subject (clusters, nodes, instances)")
fset.StringVar(&dataDir, "config-dir", common.ConfigDir(), "Configuration folder")
fset.StringVar(&format, "format", "json", "Required output format (json, yaml), in case of error or missing will be used JSON")
fset.BoolVar(&verifySlots, "verify-slots", false, "Retrun information about Free slots for nodes and clusters")
if subCommand == "nodes" || subCommand == "instances" {
fset.StringVar(&clusterName, "cluster-name", "default", "Cluster name")
if subCommand == "instances" {
fset.StringVar(&nodeName, "node-name", "", "Cluster node name")
}
}
return fset
}
func detailsCommandInit(subCommand string) *flag.FlagSet {
fmt.Println("Show specific cluster, node or instance details:")
fset := flag.NewFlagSet(fmt.Sprintf("k8s-cli (cmd: details %s)", subCommand), flag.ContinueOnError)
fset.StringVar(&command, "command", "details", "Required executor action : details")
fset.StringVar(&subcommand, "subject", "clusters", "Required executor action subject (clusters, nodes, instances)")
fset.StringVar(&dataDir, "config-dir", common.ConfigDir(), "Configuration folder")
fset.StringVar(&format, "format", "json", "Required output format (json, yaml), in case of error or missing will be used JSON")
fset.BoolVar(&verifySlots, "verify-slots", false, "Retrun information about Free slots for nodes and clusters")
fset.StringVar(&clusterName, "cluster-name", "default", "Cluster name")
if subCommand == "nodes" || subCommand == "instances" {
fset.StringVar(&nodeName, "node-name", "", "Cluster node name")
if subCommand == "instances" {
fset.StringVar(&instanceName, "instance-name", "", "Cluster node name")
}
}
return fset
}
func discoverCommandInit(subCommand string) *flag.FlagSet {
fmt.Println("Makes a lookup in the remote kubernetes cluster and check/add (eventually) new nodes, using defaul or given number of slots:")
fset := flag.NewFlagSet(fmt.Sprintf("k8s-cli (cmd: discover %s)", subCommand), flag.ContinueOnError)
fset.StringVar(&command, "command", "discover", "Required executor action : discover")
fset.StringVar(&subcommand, "subject", "nodes", "Required executor action subject (nodes)")
fset.StringVar(&dataDir, "config-dir", common.ConfigDir(), "Configuration folder")
fset.StringVar(&format, "format", "json", "Required output format (json, yaml), in case of error or missing will be used JSON")
fset.IntVar(&nodeSlots, "node-slots", 2, "Cluster node max number of prepareations")
fset.StringVar(&clusterName, "cluster-name", "default", "Cluster name")
return fset
}
func ensureCommandInit(subCommand string) *flag.FlagSet {
fmt.Println("Calculate first node available for a new instance:")
fset := flag.NewFlagSet(fmt.Sprintf("k8s-cli (cmd: ensure %s)", subCommand), flag.ContinueOnError)
fset.StringVar(&command, "command", "ensure", "Required executor action : ensure")
fset.StringVar(&subcommand, "subject", "cluster", "Optional executor action subject (instance)")
fset.StringVar(&dataDir, "config-dir", common.ConfigDir(), "Configuration folder")
fset.StringVar(&format, "format", "json", "Required output format (json, yaml), in case of error or missing will be used JSON")
fset.StringVar(&clusterName, "cluster-name", "default", "Cluster name")
return fset
}
func addCommandInit(subCommand string) *flag.FlagSet {
fmt.Println("Add a new cluster, node or instance:")
fset := flag.NewFlagSet(fmt.Sprintf("k8s-cli (cmd: add %s)", subCommand), flag.ContinueOnError)
fset.StringVar(&command, "command", "add", "Required executor action : show")
fset.StringVar(&subcommand, "subject", "cluster", "Required executor action subject (cluster, node, instance)")
fset.StringVar(&clusterName, "cluster-name", "default", "Cluster name")
fset.StringVar(&clusterConfigYaml, "kubectl-yaml-file", "", "Kubectl Yaml file")
fset.StringVar(&dataDir, "config-dir", common.ConfigDir(), "Configuration folder")
fset.StringVar(&format, "format", "json", "Required output format (json, yaml), in case of error or missing will be used JSON")
if subCommand == "node" || subCommand == "instance" {
fset.StringVar(&nodeName, "node-name", "", "Cluster node name")
fset.StringVar(&hostName, "node-host-name", "", "Cluster node host name")
fset.IntVar(&nodeSlots, "node-slots", 2, "Cluster node max number of prepareations")
if subCommand == "instance" {
fset.StringVar(&instanceName, "instance-name", "", "Cluster node instance name")
fset.StringVar(&namespace, "namespace", "", "Cluster node instance namespace name")
}
}
return fset
}
func removeCommandInit(subCommand string) *flag.FlagSet {
fmt.Println("Remove an existing cluster, node or instance:")
fset := flag.NewFlagSet(fmt.Sprintf("k8s-cli (cmd: remove %s)", subCommand), flag.ContinueOnError)
fset.StringVar(&command, "command", "remove", "Required executor action : remove")
fset.StringVar(&subcommand, "subject", "cluster", "Required executor action subject (cluster, node, instance)")
fset.StringVar(&clusterName, "cluster-name", "default", "Cluster name")
fset.StringVar(&dataDir, "config-dir", common.ConfigDir(), "Configuration folder")
fset.StringVar(&format, "format", "json", "Required output format (json, yaml), in case of error or missing will be used JSON")
if subCommand == "node" || subCommand == "instance" {
fset.StringVar(&nodeName, "node-name", "", "Cluster node name")
if subCommand == "instance" {
fset.StringVar(&instanceName, "instance-name", "", "Cluster node instance name")
}
}
return fset
}
func verifyCommandInit(subCommand string) *flag.FlagSet {
fmt.Println("Verify healthy state of an existing cluster, node or instance:")
fset := flag.NewFlagSet(fmt.Sprintf("k8s-cli (cmd: verify %s)", subCommand), flag.ContinueOnError)
fset.StringVar(&command, "command", "verify", "Required executor action : verify")
fset.StringVar(&subcommand, "subject", "cluster", "Required executor action subject (cluster, node, instance)")
fset.StringVar(&clusterName, "cluster-name", "default", "Cluster name")
fset.StringVar(&dataDir, "config-dir", common.ConfigDir(), "Configuration folder")
fset.StringVar(&format, "format", "json", "Required output format (json, yaml), in case of error or missing will be used JSON")
if subCommand == "node" || subCommand == "instance" {
fset.StringVar(&nodeName, "node-name", "", "Cluster node name")
if subCommand == "instance" {
fset.StringVar(&instanceName, "instance-name", "", "Cluster node instance name")
}
}
return fset
}
func prepareCommandInit(subCommand string) *flag.FlagSet {
fmt.Println("Prepare a deployment environment file:")
fset := flag.NewFlagSet(fmt.Sprintf("k8s-cli (cmd: prepare %s)", subCommand), flag.ContinueOnError)
fset.StringVar(&command, "command", "prepare", "Required executor action : prepare")
fset.StringVar(&subcommand, "subject", "cluster", "Required executor action subject (instance)")
fset.StringVar(&clusterName, "cluster-name", "default", "Cluster name")
fset.StringVar(&dataDir, "config-dir", common.ConfigDir(), "Configuration folder")
fset.StringVar(&format, "format", "json", "Required output format (json, yaml), in case of error or missing will be used JSON")
if subCommand == "instance" {
fset.StringVar(&nodeName, "node-name", "", "Cluster node name")
fset.StringVar(&instanceName, "instance-name", "", "Cluster node instance name")
}
return fset
}
func waitApp(folder string) {
if _, err := os.Stat(folder); err != nil {
_ = os.MkdirAll(folder, 0660)
}
var lockFile = fmt.Sprintf("%s%c%s", folder, os.PathSeparator, ".lock")
_, err := os.Stat(lockFile)
if err == nil {
fmt.Println("{\"code\": 120, \"error\": \"k8s-cli process locked from abother process...\"}")
}
for err == nil {
time.Sleep(5 * time.Second)
_, err = os.Stat(lockFile)
}
}
func unlockApp(folder string) bool {
if _, err := os.Stat(folder); err != nil {
_ = os.MkdirAll(folder, 0660)
}
var lockFile = fmt.Sprintf("%s%c%s", folder, os.PathSeparator, ".lock")
if _, err := os.Stat(lockFile); err == nil {
err := os.Remove(lockFile)
return err == nil
}
return false
}
func lockApp(folder string) bool {
if _, err := os.Stat(folder); err != nil {
_ = os.MkdirAll(folder, 0660)
}
var lockFile = fmt.Sprintf("%s%c%s", folder, os.PathSeparator, ".lock")
if _, err := os.Stat(lockFile); err != nil {
f, err := os.Create(lockFile)
defer func() {
if f != nil && err == nil {
f.Close()
}
}()
return err == nil
}
return false
}
func main() {
initHelp()
var args = []string(os.Args)
fmt.Sprintf("Args: %v", args)
if len(os.Args) > 1 && os.Args[1] == "-clean-lock" {
os.Args = args[1:]
}
flag.Parse()
if len(args) > 1 && args[1] == "-clean-lock" {
unlockApp(dataDir)
fmt.Println("Lock removed, if present")
os.Exit(0)
}
switch strings.ToLower(command) {
case "help":
fmt.Println(command)
fmt.Println(subcommand)
fmt.Println(subsubcommand)
switch strings.ToLower(subcommand) {
case "show":
fset := showCommandInit(subsubcommand)
fset.Parse(args)
fset.Usage()
case "details":
fset := detailsCommandInit(subsubcommand)
fset.Parse(args)
fset.Usage()
case "discover":
fset := discoverCommandInit(subsubcommand)
fset.Parse(args)
fset.Usage()
case "add":
fset := addCommandInit(subsubcommand)
fset.Parse(args)
fset.Usage()
case "ensure":
fset := ensureCommandInit(subsubcommand)
fset.Parse(args)
fset.Usage()
case "remove":
fset := removeCommandInit(subsubcommand)
fset.Parse(args)
fset.Usage()
case "verify":
fset := verifyCommandInit(subsubcommand)
fset.Parse(args)
fset.Usage()
case "prepare":
fset := prepareCommandInit(subsubcommand)
fset.Parse(args)
fset.Usage()
default:
fmt.Printf("Requested help of unknown subject: %s\n", subcommand)
flag.Usage()
}
case "show", "details", "discover", "add", "remove", "verify", "ensure", "prepare":
waitApp(dataDir)
_ = lockApp(dataDir)
defer func() {
if r := recover(); r != nil {
fmt.Println("Error:", r)
}
_ = unlockApp(dataDir)
}()
exec := executor.New(dataDir, model.CommandRequest{
Command: command,
SubCommand: subcommand,
ClusterName: clusterName,
KubeCtlFile: clusterConfigYaml,
NodeName: nodeName,
HostName: hostName,
NodeSlots: nodeSlots,
Instance: instanceName,
Namespace: namespace,
Format: common.FixOutputType(format),
VerifySlots: verifySlots,
})
err := exec.Init()
if err != nil {
errResp := model.ErrorResponse{
Command: command,
Subject: subcommand,
Status: "Error",
Code: 401,
Message: fmt.Sprintf("%v", err),
}
var dt []byte
if common.FixOutputType(format) == "yaml" {
dt, _ = io.ToYaml(errResp)
} else {
dt, _ = io.ToJson(errResp)
}
fmt.Sprintf("%s", string(dt))
}
err = exec.Execute()
if err != nil {
errResp := model.ErrorResponse{
Command: command,
Subject: subcommand,
Status: "Error",
Code: 402,
Message: fmt.Sprintf("%v", err),
}
var dt []byte
if common.FixOutputType(format) == "yaml" {
dt, _ = io.ToYaml(errResp)
} else {
dt, _ = io.ToJson(errResp)
}
fmt.Printf("%s\n", string(dt))
}
default:
fmt.Printf("Unknown executor : %s\n", command)
flag.Usage()
}
}