Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 1 | # Copyright 2013 - 2017 Mirantis, Inc. |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 2 | # |
| 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 | |
| 15 | from __future__ import print_function |
| 16 | |
| 17 | import argparse |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 18 | import sys |
| 19 | import yaml |
| 20 | |
| 21 | from reclass_tools import walk_models |
| 22 | |
| 23 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 24 | class Shell(object): |
| 25 | def __init__(self, args): |
| 26 | self.args = args |
| 27 | self.params = self.get_params() |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 28 | |
Oleksii Grudev | 9d63bc1 | 2019-02-13 16:12:51 +0200 | [diff] [blame] | 29 | def str2bool(self, v): |
| 30 | return v.lower() in ("true") |
| 31 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 32 | def execute(self): |
| 33 | command_name = 'do_{}'.format(self.params.command.replace('-', '_')) |
| 34 | command_method = getattr(self, command_name) |
| 35 | command_method() |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 36 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 37 | def do_get_key(self): |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 38 | walk_models.remove_reclass_parameter( |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 39 | self.params.path, |
| 40 | self.params.key_name, |
| 41 | verbose=self.params.verbose, |
| 42 | pretend=True) |
| 43 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 44 | def do_add_key(self): |
Sergey | b50d1f0 | 2017-12-25 18:07:15 +0400 | [diff] [blame] | 45 | # Try convert to digits |
| 46 | try: |
| 47 | add_val = int(self.params.add_value) |
| 48 | except ValueError: |
Hanna Arhipova | 7e4abf3 | 2020-11-04 14:16:24 +0200 | [diff] [blame^] | 49 | # Try convert to float |
Sergey | b50d1f0 | 2017-12-25 18:07:15 +0400 | [diff] [blame] | 50 | try: |
| 51 | add_val = float(self.params.add_value) |
| 52 | except ValueError: |
Hanna Arhipova | 7e4abf3 | 2020-11-04 14:16:24 +0200 | [diff] [blame^] | 53 | 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 |
Sergey | b50d1f0 | 2017-12-25 18:07:15 +0400 | [diff] [blame] | 59 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 60 | walk_models.add_reclass_parameter( |
| 61 | self.params.path, |
| 62 | self.params.key_name, |
Sergey | b50d1f0 | 2017-12-25 18:07:15 +0400 | [diff] [blame] | 63 | add_val, |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 64 | verbose=self.params.verbose, |
| 65 | merge=self.params.merge) |
| 66 | |
Oleksii Grudev | 9d63bc1 | 2019-02-13 16:12:51 +0200 | [diff] [blame] | 67 | 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 Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 77 | def do_del_key(self): |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 78 | walk_models.remove_reclass_parameter( |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 79 | 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 Dmitriev | a7de8b5 | 2018-08-08 23:14:03 +0300 | [diff] [blame] | 88 | print(yaml.dump(results, width=255)) |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 89 | |
| 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 Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 99 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 100 | 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 Dmitriev | 806706d | 2017-07-29 22:31:23 +0300 | [diff] [blame] | 121 | 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 Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 131 | 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 Dmitriev | a7de8b5 | 2018-08-08 23:14:03 +0300 | [diff] [blame] | 141 | print(yaml.dump(current_underlay_context, default_flow_style=False, |
| 142 | width=255)) |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 143 | |
| 144 | def do_render(self): |
Dennis Dmitriev | f260d15 | 2017-08-15 00:59:24 +0300 | [diff] [blame] | 145 | from reclass_tools import render |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 146 | |
| 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 Dmitriev | f260d15 | 2017-08-15 00:59:24 +0300 | [diff] [blame] | 151 | 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 Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 155 | |
Hanna Arhipova | 577ecc7 | 2021-02-15 19:28:47 +0200 | [diff] [blame] | 156 | 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 Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 162 | 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 Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 169 | merge_parser = argparse.ArgumentParser(add_help=False) |
Dennis Dmitriev | 6792f7e | 2017-07-18 19:11:22 +0300 | [diff] [blame] | 170 | merge_parser.add_argument('--merge', dest='merge', |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 171 | action='store_const', const=True, |
Dennis Dmitriev | 6792f7e | 2017-07-18 19:11:22 +0300 | [diff] [blame] | 172 | help='Merge value to list or dict', |
| 173 | default=False) |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 174 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 175 | key_parser = argparse.ArgumentParser(add_help=False) |
| 176 | key_parser_help = ( |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 177 | 'Key name to find in reclass model files, for example:' |
| 178 | ' parameters.linux.network.interface') |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 179 | 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 Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 186 | 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 Arhipova | 577ecc7 | 2021-02-15 19:28:47 +0200 | [diff] [blame] | 192 | 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 Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 197 | 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 Dmitriev | 806706d | 2017-07-29 22:31:23 +0300 | [diff] [blame] | 208 | 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 Dmitriev | 0cea570 | 2017-07-17 19:03:23 +0300 | [diff] [blame] | 214 | env_name_parser = argparse.ArgumentParser(add_help=False) |
| 215 | env_name_parser.add_argument( |
| 216 | '--env-name', '-e', dest='env_name', |
Dennis Dmitriev | 5598902 | 2017-07-17 19:23:16 +0300 | [diff] [blame] | 217 | help=("Name of the 'environment' to create or use"), |
| 218 | default=None) |
Dennis Dmitriev | 0cea570 | 2017-07-17 19:03:23 +0300 | [diff] [blame] | 219 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 220 | 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 Dmitriev | c253437 | 2017-10-09 15:36:21 +0300 | [diff] [blame] | 244 | '--context', '-c', dest='contexts', action='append', |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 245 | help=('YAML/JSON files with context data to render ' |
| 246 | 'the template')) |
| 247 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 248 | 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 Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 255 | 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 Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 261 | subparsers.add_parser('add-key', |
| 262 | parents=[key_parser, add_value_parser, |
| 263 | path_parser, verbose_parser, |
| 264 | merge_parser], |
Oleksii Grudev | 9d63bc1 | 2019-02-13 16:12:51 +0200 | [diff] [blame] | 265 | help="Add a key in YAMLs found in <path>", |
| 266 | description=("Add a key to " |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 267 | "different YAMLs")) |
Oleksii Grudev | 9d63bc1 | 2019-02-13 16:12:51 +0200 | [diff] [blame] | 268 | |
| 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 Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 278 | 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 Dmitriev | 806706d | 2017-07-29 22:31:23 +0300 | [diff] [blame] | 303 | 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 Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 311 | subparsers.add_parser('render', |
Dennis Dmitriev | 0cea570 | 2017-07-17 19:03:23 +0300 | [diff] [blame] | 312 | parents=[render_parser, env_name_parser], |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 313 | help=("Render cookiecutter template using " |
| 314 | "multiple metadata sources")) |
Hanna Arhipova | 577ecc7 | 2021-02-15 19:28:47 +0200 | [diff] [blame] | 315 | 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 Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 319 | |
| 320 | if len(self.args) == 0: |
| 321 | self.args = ['-h'] |
| 322 | return parser.parse_args(self.args) |
| 323 | |
| 324 | |
| 325 | def main(args=None): |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 326 | if args is None: |
| 327 | args = sys.argv[1:] |
| 328 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 329 | shell = Shell(args) |
| 330 | shell.execute() |