-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolkit.py
executable file
·276 lines (253 loc) · 10.5 KB
/
toolkit.py
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
#!/usr/bin/env python3
"""
Copyright 2022 NetApp, Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import gc
import sys
import tkSrc
from astraSDK.common import getConfig
def tkMain(argv=sys.argv, config=None):
# The various functions to populate the lists used for choices() in the options are
# expensive. argparse provides no way to know what subcommand was selected prior to
# parsing the options. By then it's too late to decide which functions to run to
# populate the various choices the differing options for each subcommand needs. So
# we just go around argparse's back and inspect sys.argv directly.
acl = tkSrc.classes.ArgparseChoicesLists()
ard = tkSrc.classes.AstraResourceDicts()
plaidMode = False
v3 = False
v3_skip_tls_verify = False
if len(argv) > 1:
# global_args must manually be kept in sync with __init__() parser args in tkSrc/parser.py
global_args = [
"-v",
"--verbose",
"-o",
"--output",
"-q",
"--quiet",
"-f",
"--fast",
"--v3",
"--dry-run",
"--insecure-skip-tls-verify",
]
# verbs must manually be kept in sync with top_level_commands() in tkSrc/parser.py
verbs = {
"deploy": False,
"clone": False,
"restore": False,
"ipr": False,
"list": False,
"get": False,
"create": False,
"copy": False,
"manage": False,
"define": False,
"destroy": False,
"unmanage": False,
"update": False,
}
firstverbfoundPosition = None
verbPosition = None
cookedlistofVerbs = [x for x in verbs]
for verb in verbs:
if verb not in argv:
# no need to iterate over the arg list for a verb that isn't in there
continue
if verbPosition:
# once we've found the first verb we can stop looking
break
for counter, item in enumerate(argv):
if item == verb:
if firstverbfoundPosition is None:
# firstverbfoundPosition exists to prevent
# "toolkit.py create deploy create deploy" from deciding the second create
# is the first verb found
firstverbfoundPosition = counter
else:
if counter > firstverbfoundPosition:
continue
else:
firstverbfoundPosition = counter
# Why are we jumping through hoops here to remove the verb we found
# from the list of verbs? Consider the input "toolkit.py deploy deploy"
# When we loop over the args we find the first "deploy"
# verb["deploy"] gets set to True, we loop over the slice of sys.argv
# previous to "deploy" and find no other verbs so verb["deploy"] remains True
# Then we find the second deploy. We loop over the slice of sys.argv previous
# to *that* and sure enough, the first "deploy" is in verbs so
# verb["deploy"] gets set to False
try:
cookedlistofVerbs.remove(item)
except ValueError:
pass
verbs[verb] = True
verbPosition = counter
for item2 in argv[:(counter)]:
# argv[:(counter)] is a slice of sys.argv of all the items
# before the one we found
if item2 in cookedlistofVerbs:
# deploy wasn't the verb, it was a symbolic name of an object
verbs[verb] = False
verbPosition = None
# Handle plaidMode (-f/--fast) and --v3 use-cases
for counter, item in enumerate(argv):
if verbPosition and counter < verbPosition and (item == "-f" or item == "--fast"):
plaidMode = True
if ((verbPosition and counter < verbPosition) or (verbPosition is None)) and (
item == "--v3"
):
v3 = True
v3Position = counter
if ((verbPosition and counter < verbPosition) or (verbPosition is None)) and (
item == "--insecure-skip-tls-verify"
):
v3_skip_tls_verify = True
# Argparse cares about capitalization, kubectl does not, so transparently fix appvault
if verbPosition and len(argv) - verbPosition >= 2 and argv[verbPosition + 1] == "appvault":
argv[verbPosition + 1] = "appVault"
if verbPosition and len(argv) - verbPosition >= 2 and argv[verbPosition + 1] == "appvaults":
argv[verbPosition + 1] = "appVaults"
# Transparently change "protectionpolicy" to "protection" for backwards compatibility
if (
verbPosition
and len(argv) - verbPosition >= 2
and argv[verbPosition + 1] == "protectionpolicy"
):
argv[verbPosition + 1] = "protection"
# If v3, build the context@kubeconfig choices list (we want this outside of
# tkSrc.choices.main as it should be generated regardless of plaidMode)
if v3:
v3, verbPosition = tkSrc.choices.kube_config(
argv, acl, verbPosition, v3Position, global_args
)
# If not v3, set up the Astra Control config, which includes a requests Session
elif config is None:
config = getConfig().main()
# Enabling comma separated listing of objects, like:
# 'toolkit.py list apps,backups,snapshots'
if (verbs["list"] or verbs["get"]) and len(argv) > (verbPosition + 1):
if "," in argv[verbPosition + 1]:
listTypeArray = argv[verbPosition + 1].split(",")
for lt in listTypeArray:
argv[verbPosition + 1] = lt
main(argv=argv, config=config)
sys.exit(0)
# As long as we're not --fast/plaidMode, build the argparse choices lists
if not plaidMode:
tkSrc.choices.main(
argv,
verbs,
verbPosition,
ard,
acl,
v3,
v3_skip_tls_verify=v3_skip_tls_verify,
config=config,
)
else:
raise SystemExit(
f"{argv[0]}: error: please specify a subcommand. Run '{argv[0]} -h' for "
"parser information."
)
# Manually passing args into argparse via parse_args() shouldn't include the function name
argv = argv[1:] if "toolkit" in argv[0] else argv
tkParser = tkSrc.parser.ToolkitParser(acl, plaidMode=plaidMode, v3=v3).main()
args = tkParser.parse_args(args=argv)
# Memory optimization
tkParser, acl = None, None
gc.collect()
if args.v3:
v3_dict = {"deploy": ["acp", "chart"]}
v3_dict.update(
dict.fromkeys(
["create", "destroy"],
[
"backup",
"exechok",
"hook",
"protection",
"schedule",
"snapshot",
],
)
)
v3_dict["destroy"].extend(["credential", "secret"])
v3_dict.update(dict.fromkeys(["clone", "ipr", "restore"], True))
v3_dict.update(
dict.fromkeys(
["define", "manage", "unmanage"],
["app", "application", "appVault", "bucket", "cluster"],
)
)
v3_dict.update(
dict.fromkeys(
["list", "get"],
[
"apps",
"applications",
"appVaults",
"astraconnectors",
"backups",
"buckets",
"connectors",
"credentials",
"exechooks",
"exechooksruns",
"hooks",
"hooksruns",
"inplacerestores",
"iprs",
"namespaces",
"protections",
"restores",
"schedules",
"secrets",
"snapshots",
],
)
)
tkSrc.helpers.checkv3Support(args, v3_dict)
if args.dry_run and not args.v3:
tkSrc.helpers.parserError("--dry-run can only be used in conjunction with --v3")
elif args.skip_tls_verify and not args.v3:
tkSrc.helpers.parserError(
"--insecure-skip-tls-verify can only be used in conjunction with --v3"
)
if args.subcommand == "deploy":
tkSrc.deploy.main(args, ard, config=config)
elif args.subcommand == "clone" or args.subcommand == "restore":
tkSrc.clone.main(args, ard, config=config)
elif args.subcommand == "ipr":
tkSrc.ipr.main(args, ard, config=config)
elif args.subcommand == "list" or args.subcommand == "get":
tkSrc.list.main(args, config=config)
elif args.subcommand == "copy":
tkSrc.copy.main(args, config=config)
elif args.subcommand == "create":
tkSrc.create.main(args, ard, config=config)
elif args.subcommand == "manage" or args.subcommand == "define":
tkSrc.manage.main(args, ard, config=config)
elif args.subcommand == "destroy":
tkSrc.destroy.main(args, ard, config=config)
elif args.subcommand == "unmanage":
tkSrc.unmanage.main(args, ard, config=config)
elif args.subcommand == "update":
tkSrc.update.main(args, ard, config=config)
def main(argv=sys.argv, config=None):
try:
tkMain(argv=argv, config=config)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()