blob: 975b737b24d3721a00bb977ffd89d583ded5d141 [file] [log] [blame]
Dennis Dmitriev86750962017-07-11 19:44:05 +03001# Copyright 2013 - 2017 Mirantis, Inc.
Dennis Dmitrieve56c8b92017-06-16 01:53:16 +03002#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14
15from __future__ import print_function
16
17import argparse
Dennis Dmitrieve56c8b92017-06-16 01:53:16 +030018import sys
19import yaml
20
21from reclass_tools import walk_models
22
23
Dennis Dmitriev86750962017-07-11 19:44:05 +030024class Shell(object):
25 def __init__(self, args):
26 self.args = args
27 self.params = self.get_params()
Dennis Dmitrieve56c8b92017-06-16 01:53:16 +030028
Oleksii Grudev9d63bc12019-02-13 16:12:51 +020029 def str2bool(self, v):
30 return v.lower() in ("true")
31
Dennis Dmitriev86750962017-07-11 19:44:05 +030032 def execute(self):
33 command_name = 'do_{}'.format(self.params.command.replace('-', '_'))
34 command_method = getattr(self, command_name)
35 command_method()
Dennis Dmitrieve56c8b92017-06-16 01:53:16 +030036
Dennis Dmitriev86750962017-07-11 19:44:05 +030037 def do_get_key(self):
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030038 walk_models.remove_reclass_parameter(
Dennis Dmitriev86750962017-07-11 19:44:05 +030039 self.params.path,
40 self.params.key_name,
41 verbose=self.params.verbose,
42 pretend=True)
43
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030044 def do_add_key(self):
Sergeyb50d1f02017-12-25 18:07:15 +040045 # Try convert to digits
46 try:
47 add_val = int(self.params.add_value)
48 except ValueError:
Hanna Arhipova7e4abf32020-11-04 14:16:24 +020049 # Try convert to float
Sergeyb50d1f02017-12-25 18:07:15 +040050 try:
51 add_val = float(self.params.add_value)
52 except ValueError:
Hanna Arhipova7e4abf32020-11-04 14:16:24 +020053 try:
54 # Try convert to list
55 if type(eval(self.params.add_value)) == list:
56 add_val = eval(self.params.add_value)
57 except (SyntaxError, NameError):
58 add_val = self.params.add_value
Sergeyb50d1f02017-12-25 18:07:15 +040059
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030060 walk_models.add_reclass_parameter(
61 self.params.path,
62 self.params.key_name,
Sergeyb50d1f02017-12-25 18:07:15 +040063 add_val,
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030064 verbose=self.params.verbose,
65 merge=self.params.merge)
66
Oleksii Grudev9d63bc12019-02-13 16:12:51 +020067 def do_add_bool_key(self):
68 add_val = self.str2bool(self.params.add_value)
69
70 walk_models.add_reclass_parameter(
71 self.params.path,
72 self.params.key_name,
73 add_val,
74 verbose=self.params.verbose,
75 merge=self.params.merge)
76
Dennis Dmitriev86750962017-07-11 19:44:05 +030077 def do_del_key(self):
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030078 walk_models.remove_reclass_parameter(
Dennis Dmitriev86750962017-07-11 19:44:05 +030079 self.params.path,
80 self.params.key_name,
81 verbose=self.params.verbose,
82 pretend=False)
83
84 def do_list_params(self):
85 results = walk_models.get_all_reclass_params(
86 self.params.path,
87 verbose=self.params.verbose)
Dennis Dmitrieva7de8b52018-08-08 23:14:03 +030088 print(yaml.dump(results, width=255))
Dennis Dmitriev86750962017-07-11 19:44:05 +030089
90 def do_list_domains(self):
91 try:
92 from reclass_tools import reclass_models
93 except ImportError:
94 sys.exit("Please run this tool on the salt-master node "
95 "with installed 'reclass'")
96 inventory = reclass_models.inventory_list()
97 reclass_storage = reclass_models.reclass_storage(inventory=inventory)
98 print('\n'.join(sorted(reclass_storage.keys())))
Dennis Dmitrieve56c8b92017-06-16 01:53:16 +030099
Dennis Dmitriev86750962017-07-11 19:44:05 +0300100 def do_list_nodes(self):
101 try:
102 from reclass_tools import reclass_models
103 except ImportError:
104 sys.exit("Please run this tool on the salt-master node "
105 "with installed 'reclass'")
106
107 inventory = reclass_models.inventory_list(domain=self.params.domain)
108 vcp_nodes = reclass_models.vcp_list(domain=self.params.domain,
109 inventory=inventory)
110 vcp_node_names = ['{0}.{1}'.format(name, domain)
111 for name, domain in vcp_nodes]
112
113 if self.params.vcp_only:
114 print('\n'.join(sorted(vcp_node_names)))
115 elif self.params.non_vcp_only:
116 print('\n'.join(sorted((node_name for node_name in inventory.keys()
117 if node_name not in vcp_node_names))))
118 else:
119 print('\n'.join(sorted(inventory.keys())))
120
Dennis Dmitriev806706d2017-07-29 22:31:23 +0300121 def do_trace_key(self):
122 try:
123 from reclass_tools import reclass_models
124 except ImportError:
125 sys.exit("Please run this tool on the salt-master node "
126 "with installed 'reclass'")
127 reclass_models.trace_key(key=self.params.key_name,
128 domain=self.params.domain,
129 node=self.params.node)
130
Dennis Dmitriev86750962017-07-11 19:44:05 +0300131 def do_show_context(self):
132 try:
133 from reclass_tools import create_inventory
134 except ImportError:
135 sys.exit("Please run this tool on the salt-master node "
136 "with installed 'reclass'")
137
138 current_underlay_context = create_inventory.create_inventory_context(
139 domain=self.params.domain, keys=self.params.keys)
140
Dennis Dmitrieva7de8b52018-08-08 23:14:03 +0300141 print(yaml.dump(current_underlay_context, default_flow_style=False,
142 width=255))
Dennis Dmitriev86750962017-07-11 19:44:05 +0300143
144 def do_render(self):
Dennis Dmitrievf260d152017-08-15 00:59:24 +0300145 from reclass_tools import render
Dennis Dmitriev86750962017-07-11 19:44:05 +0300146
147 if not self.params.template_dir or not self.params.output_dir \
148 or not self.params.contexts:
149 sys.exit("Missing parameters, see: reclass-tools render -h")
150
Dennis Dmitrievf260d152017-08-15 00:59:24 +0300151 render.render_dir(template_dir=self.params.template_dir,
152 output_dir=self.params.output_dir,
153 contexts=self.params.contexts,
154 env_name=self.params.env_name)
Dennis Dmitriev86750962017-07-11 19:44:05 +0300155
Hanna Arhipova577ecc72021-02-15 19:28:47 +0200156 def do_merge_context(self):
157 content = yaml.load(open(self.params.yaml).read(), yaml.SafeLoader)
158 walk_models.merge_dict_to_reclass(
159 self.params.path,
160 content)
161
Dennis Dmitriev86750962017-07-11 19:44:05 +0300162 def get_params(self):
163
164 verbose_parser = argparse.ArgumentParser(add_help=False)
165 verbose_parser.add_argument('--verbose', dest='verbose',
166 action='store_const', const=True,
167 help='Show verbosed output', default=False)
168
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300169 merge_parser = argparse.ArgumentParser(add_help=False)
Dennis Dmitriev6792f7e2017-07-18 19:11:22 +0300170 merge_parser.add_argument('--merge', dest='merge',
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300171 action='store_const', const=True,
Dennis Dmitriev6792f7e2017-07-18 19:11:22 +0300172 help='Merge value to list or dict',
173 default=False)
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300174
Dennis Dmitriev86750962017-07-11 19:44:05 +0300175 key_parser = argparse.ArgumentParser(add_help=False)
176 key_parser_help = (
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300177 'Key name to find in reclass model files, for example:'
178 ' parameters.linux.network.interface')
Dennis Dmitriev86750962017-07-11 19:44:05 +0300179 key_parser.add_argument('key_name', help=key_parser_help, default=None)
180
181 keys_parser = argparse.ArgumentParser(add_help=False)
182 keys_parser.add_argument(
183 'keys',
184 help='Key names to find in reclass model files', nargs='*')
185
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300186 add_value_parser = argparse.ArgumentParser(add_help=False)
187 add_value_parser.add_argument(
188 'add_value',
189 help=('Value to add to the reclass model files, can be in the '
190 'inline YAML format'))
191
Hanna Arhipova577ecc72021-02-15 19:28:47 +0200192 yaml_parser = argparse.ArgumentParser(add_help=False)
193 yaml_parser.add_argument(
194 'yaml',
195 help='Path to YAML file with extra context')
196
Dennis Dmitriev86750962017-07-11 19:44:05 +0300197 path_parser = argparse.ArgumentParser(add_help=False)
198 path_parser.add_argument(
199 'path',
200 help='Path to search for *.yml files.', nargs='+')
201
202 domain_parser = argparse.ArgumentParser(add_help=False)
203 domain_parser.add_argument(
204 '--domain', '-d', dest='domain',
205 help=('Show only the nodes which names are ended with the '
206 'specified domain, for example: example.local'))
207
Dennis Dmitriev806706d2017-07-29 22:31:23 +0300208 node_parser = argparse.ArgumentParser(add_help=False)
209 node_parser.add_argument(
210 '--node', '-n', dest='node',
211 help=('Show only the specified node, for example: '
212 'ctl01.example.local'))
213
Dennis Dmitriev0cea5702017-07-17 19:03:23 +0300214 env_name_parser = argparse.ArgumentParser(add_help=False)
215 env_name_parser.add_argument(
216 '--env-name', '-e', dest='env_name',
Dennis Dmitriev55989022017-07-17 19:23:16 +0300217 help=("Name of the 'environment' to create or use"),
218 default=None)
Dennis Dmitriev0cea5702017-07-17 19:03:23 +0300219
Dennis Dmitriev86750962017-07-11 19:44:05 +0300220 vcp_only_parser = argparse.ArgumentParser(add_help=False)
221 vcp_only_parser.add_argument(
222 '--vcp-only', dest='vcp_only',
223 action='store_const', const=True,
224 help=('Show only VCP nodes (present in '
225 'parameters.salt.control.cluster.internal.node)'),
226 default=False)
227
228 non_vcp_only_parser = argparse.ArgumentParser(add_help=False)
229 non_vcp_only_parser.add_argument(
230 '--non-vcp-only', dest='non_vcp_only',
231 action='store_const', const=True, default=False,
232 help=('Show only non-VCP nodes (absent in '
233 'parameters.salt.control.cluster.internal.node)'))
234
235 render_parser = argparse.ArgumentParser(add_help=False)
236 render_parser.add_argument(
237 '--template-dir', '-t', dest='template_dir',
238 help=('Coockiecutter-based template directory'))
239 render_parser.add_argument(
240 '--output-dir', '-o', dest='output_dir',
241 help=('Path to the directory where the rendered '
242 'template will be placed'))
243 render_parser.add_argument(
Dennis Dmitrievc2534372017-10-09 15:36:21 +0300244 '--context', '-c', dest='contexts', action='append',
Dennis Dmitriev86750962017-07-11 19:44:05 +0300245 help=('YAML/JSON files with context data to render '
246 'the template'))
247
Dennis Dmitriev86750962017-07-11 19:44:05 +0300248 parser = argparse.ArgumentParser(
249 description="Manage virtual environments. "
250 "For additional help, use with -h/--help option")
251 subparsers = parser.add_subparsers(title="Operation commands",
252 help='available commands',
253 dest='command')
254
Dennis Dmitriev86750962017-07-11 19:44:05 +0300255 subparsers.add_parser('get-key',
256 parents=[key_parser, path_parser,
257 verbose_parser],
258 help="Find a key in YAMLs found in <path>",
259 description=("Get a key collected from "
260 "different YAMLs"))
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300261 subparsers.add_parser('add-key',
262 parents=[key_parser, add_value_parser,
263 path_parser, verbose_parser,
264 merge_parser],
Oleksii Grudev9d63bc12019-02-13 16:12:51 +0200265 help="Add a key in YAMLs found in <path>",
266 description=("Add a key to "
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300267 "different YAMLs"))
Oleksii Grudev9d63bc12019-02-13 16:12:51 +0200268
269 subparsers.add_parser('add-bool-key',
270 parents=[key_parser, add_value_parser,
271 path_parser, verbose_parser,
272 merge_parser],
273 help="Add a bool key in YAMLs found in <path>. "
274 "True/False string values are converted to bool",
275 description=("Add a bool key collected to "
276 "different YAMLs"))
277
Dennis Dmitriev86750962017-07-11 19:44:05 +0300278 subparsers.add_parser('del-key',
279 parents=[key_parser, path_parser,
280 verbose_parser],
281 help="Delete a key from YAMLs found in <path>",
282 description="Delete a key from different YAMLs")
283 subparsers.add_parser('list-params',
284 parents=[path_parser, verbose_parser],
285 help=("Collect all options for "
286 "'parameters._params' keys from YAMLs "
287 "found in <path>"))
288 subparsers.add_parser('list-nodes',
289 parents=[domain_parser, vcp_only_parser,
290 non_vcp_only_parser],
291 help=("List nodes that are available for "
292 "reclass. Use on salt-master node only!"))
293 subparsers.add_parser('list-domains',
294 help=("List domains that are available from "
295 "reclass models. Use on salt-master "
296 "node only!"))
297 subparsers.add_parser('show-context',
298 parents=[domain_parser, keys_parser],
299 help=("Show domain nodes with rendered content "
300 "for specified keys. Use on salt-master "
301 "node for already generated inventory "
302 "only!"))
Dennis Dmitriev806706d2017-07-29 22:31:23 +0300303 subparsers.add_parser('trace-key',
304 parents=[domain_parser, node_parser, key_parser],
305 help=("Use 'reclass' to merge the model and "
306 "show all the classes where the "
307 "specified key is overwritten, and updated"
308 " values during merging and after "
309 "interpolation. "
310 "Use on salt-master node only!"))
Dennis Dmitriev86750962017-07-11 19:44:05 +0300311 subparsers.add_parser('render',
Dennis Dmitriev0cea5702017-07-17 19:03:23 +0300312 parents=[render_parser, env_name_parser],
Dennis Dmitriev86750962017-07-11 19:44:05 +0300313 help=("Render cookiecutter template using "
314 "multiple metadata sources"))
Hanna Arhipova577ecc72021-02-15 19:28:47 +0200315 subparsers.add_parser('merge-context',
316 parents=[yaml_parser, path_parser],
317 help=("Merge nested content from YAML file "
318 "to specified reclass models"))
Dennis Dmitriev86750962017-07-11 19:44:05 +0300319
320 if len(self.args) == 0:
321 self.args = ['-h']
322 return parser.parse_args(self.args)
323
324
325def main(args=None):
Dennis Dmitrieve56c8b92017-06-16 01:53:16 +0300326 if args is None:
327 args = sys.argv[1:]
328
Dennis Dmitriev86750962017-07-11 19:44:05 +0300329 shell = Shell(args)
330 shell.execute()