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 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 29 | def execute(self): |
| 30 | command_name = 'do_{}'.format(self.params.command.replace('-', '_')) |
| 31 | command_method = getattr(self, command_name) |
| 32 | command_method() |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 33 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 34 | def do_get_key(self): |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 35 | walk_models.remove_reclass_parameter( |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 36 | self.params.path, |
| 37 | self.params.key_name, |
| 38 | verbose=self.params.verbose, |
| 39 | pretend=True) |
| 40 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 41 | def do_add_key(self): |
| 42 | walk_models.add_reclass_parameter( |
| 43 | self.params.path, |
| 44 | self.params.key_name, |
| 45 | self.params.add_value, |
| 46 | verbose=self.params.verbose, |
| 47 | merge=self.params.merge) |
| 48 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 49 | def do_del_key(self): |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 50 | walk_models.remove_reclass_parameter( |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 51 | self.params.path, |
| 52 | self.params.key_name, |
| 53 | verbose=self.params.verbose, |
| 54 | pretend=False) |
| 55 | |
| 56 | def do_list_params(self): |
| 57 | results = walk_models.get_all_reclass_params( |
| 58 | self.params.path, |
| 59 | verbose=self.params.verbose) |
| 60 | print(yaml.dump(results)) |
| 61 | |
| 62 | def do_list_domains(self): |
| 63 | try: |
| 64 | from reclass_tools import reclass_models |
| 65 | except ImportError: |
| 66 | sys.exit("Please run this tool on the salt-master node " |
| 67 | "with installed 'reclass'") |
| 68 | inventory = reclass_models.inventory_list() |
| 69 | reclass_storage = reclass_models.reclass_storage(inventory=inventory) |
| 70 | print('\n'.join(sorted(reclass_storage.keys()))) |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 71 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 72 | def do_list_nodes(self): |
| 73 | try: |
| 74 | from reclass_tools import reclass_models |
| 75 | except ImportError: |
| 76 | sys.exit("Please run this tool on the salt-master node " |
| 77 | "with installed 'reclass'") |
| 78 | |
| 79 | inventory = reclass_models.inventory_list(domain=self.params.domain) |
| 80 | vcp_nodes = reclass_models.vcp_list(domain=self.params.domain, |
| 81 | inventory=inventory) |
| 82 | vcp_node_names = ['{0}.{1}'.format(name, domain) |
| 83 | for name, domain in vcp_nodes] |
| 84 | |
| 85 | if self.params.vcp_only: |
| 86 | print('\n'.join(sorted(vcp_node_names))) |
| 87 | elif self.params.non_vcp_only: |
| 88 | print('\n'.join(sorted((node_name for node_name in inventory.keys() |
| 89 | if node_name not in vcp_node_names)))) |
| 90 | else: |
| 91 | print('\n'.join(sorted(inventory.keys()))) |
| 92 | |
Dennis Dmitriev | 806706d | 2017-07-29 22:31:23 +0300 | [diff] [blame] | 93 | def do_trace_key(self): |
| 94 | try: |
| 95 | from reclass_tools import reclass_models |
| 96 | except ImportError: |
| 97 | sys.exit("Please run this tool on the salt-master node " |
| 98 | "with installed 'reclass'") |
| 99 | reclass_models.trace_key(key=self.params.key_name, |
| 100 | domain=self.params.domain, |
| 101 | node=self.params.node) |
| 102 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 103 | def do_show_context(self): |
| 104 | try: |
| 105 | from reclass_tools import create_inventory |
| 106 | except ImportError: |
| 107 | sys.exit("Please run this tool on the salt-master node " |
| 108 | "with installed 'reclass'") |
| 109 | |
| 110 | current_underlay_context = create_inventory.create_inventory_context( |
| 111 | domain=self.params.domain, keys=self.params.keys) |
| 112 | |
| 113 | print(yaml.dump(current_underlay_context, default_flow_style=False)) |
| 114 | |
| 115 | def do_render(self): |
Dennis Dmitriev | f260d15 | 2017-08-15 00:59:24 +0300 | [diff] [blame] | 116 | from reclass_tools import render |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 117 | |
| 118 | if not self.params.template_dir or not self.params.output_dir \ |
| 119 | or not self.params.contexts: |
| 120 | sys.exit("Missing parameters, see: reclass-tools render -h") |
| 121 | |
Dennis Dmitriev | f260d15 | 2017-08-15 00:59:24 +0300 | [diff] [blame] | 122 | render.render_dir(template_dir=self.params.template_dir, |
| 123 | output_dir=self.params.output_dir, |
| 124 | contexts=self.params.contexts, |
| 125 | env_name=self.params.env_name) |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 126 | |
| 127 | def get_params(self): |
| 128 | |
| 129 | verbose_parser = argparse.ArgumentParser(add_help=False) |
| 130 | verbose_parser.add_argument('--verbose', dest='verbose', |
| 131 | action='store_const', const=True, |
| 132 | help='Show verbosed output', default=False) |
| 133 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 134 | merge_parser = argparse.ArgumentParser(add_help=False) |
Dennis Dmitriev | 6792f7e | 2017-07-18 19:11:22 +0300 | [diff] [blame] | 135 | merge_parser.add_argument('--merge', dest='merge', |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 136 | action='store_const', const=True, |
Dennis Dmitriev | 6792f7e | 2017-07-18 19:11:22 +0300 | [diff] [blame] | 137 | help='Merge value to list or dict', |
| 138 | default=False) |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 139 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 140 | key_parser = argparse.ArgumentParser(add_help=False) |
| 141 | key_parser_help = ( |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 142 | 'Key name to find in reclass model files, for example:' |
| 143 | ' parameters.linux.network.interface') |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 144 | key_parser.add_argument('key_name', help=key_parser_help, default=None) |
| 145 | |
| 146 | keys_parser = argparse.ArgumentParser(add_help=False) |
| 147 | keys_parser.add_argument( |
| 148 | 'keys', |
| 149 | help='Key names to find in reclass model files', nargs='*') |
| 150 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 151 | add_value_parser = argparse.ArgumentParser(add_help=False) |
| 152 | add_value_parser.add_argument( |
| 153 | 'add_value', |
| 154 | help=('Value to add to the reclass model files, can be in the ' |
| 155 | 'inline YAML format')) |
| 156 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 157 | path_parser = argparse.ArgumentParser(add_help=False) |
| 158 | path_parser.add_argument( |
| 159 | 'path', |
| 160 | help='Path to search for *.yml files.', nargs='+') |
| 161 | |
| 162 | domain_parser = argparse.ArgumentParser(add_help=False) |
| 163 | domain_parser.add_argument( |
| 164 | '--domain', '-d', dest='domain', |
| 165 | help=('Show only the nodes which names are ended with the ' |
| 166 | 'specified domain, for example: example.local')) |
| 167 | |
Dennis Dmitriev | 806706d | 2017-07-29 22:31:23 +0300 | [diff] [blame] | 168 | node_parser = argparse.ArgumentParser(add_help=False) |
| 169 | node_parser.add_argument( |
| 170 | '--node', '-n', dest='node', |
| 171 | help=('Show only the specified node, for example: ' |
| 172 | 'ctl01.example.local')) |
| 173 | |
Dennis Dmitriev | 0cea570 | 2017-07-17 19:03:23 +0300 | [diff] [blame] | 174 | env_name_parser = argparse.ArgumentParser(add_help=False) |
| 175 | env_name_parser.add_argument( |
| 176 | '--env-name', '-e', dest='env_name', |
Dennis Dmitriev | 5598902 | 2017-07-17 19:23:16 +0300 | [diff] [blame] | 177 | help=("Name of the 'environment' to create or use"), |
| 178 | default=None) |
Dennis Dmitriev | 0cea570 | 2017-07-17 19:03:23 +0300 | [diff] [blame] | 179 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 180 | vcp_only_parser = argparse.ArgumentParser(add_help=False) |
| 181 | vcp_only_parser.add_argument( |
| 182 | '--vcp-only', dest='vcp_only', |
| 183 | action='store_const', const=True, |
| 184 | help=('Show only VCP nodes (present in ' |
| 185 | 'parameters.salt.control.cluster.internal.node)'), |
| 186 | default=False) |
| 187 | |
| 188 | non_vcp_only_parser = argparse.ArgumentParser(add_help=False) |
| 189 | non_vcp_only_parser.add_argument( |
| 190 | '--non-vcp-only', dest='non_vcp_only', |
| 191 | action='store_const', const=True, default=False, |
| 192 | help=('Show only non-VCP nodes (absent in ' |
| 193 | 'parameters.salt.control.cluster.internal.node)')) |
| 194 | |
| 195 | render_parser = argparse.ArgumentParser(add_help=False) |
| 196 | render_parser.add_argument( |
| 197 | '--template-dir', '-t', dest='template_dir', |
| 198 | help=('Coockiecutter-based template directory')) |
| 199 | render_parser.add_argument( |
| 200 | '--output-dir', '-o', dest='output_dir', |
| 201 | help=('Path to the directory where the rendered ' |
| 202 | 'template will be placed')) |
| 203 | render_parser.add_argument( |
Dennis Dmitriev | c253437 | 2017-10-09 15:36:21 +0300 | [diff] [blame] | 204 | '--context', '-c', dest='contexts', action='append', |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 205 | help=('YAML/JSON files with context data to render ' |
| 206 | 'the template')) |
| 207 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 208 | parser = argparse.ArgumentParser( |
| 209 | description="Manage virtual environments. " |
| 210 | "For additional help, use with -h/--help option") |
| 211 | subparsers = parser.add_subparsers(title="Operation commands", |
| 212 | help='available commands', |
| 213 | dest='command') |
| 214 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 215 | subparsers.add_parser('get-key', |
| 216 | parents=[key_parser, path_parser, |
| 217 | verbose_parser], |
| 218 | help="Find a key in YAMLs found in <path>", |
| 219 | description=("Get a key collected from " |
| 220 | "different YAMLs")) |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame] | 221 | subparsers.add_parser('add-key', |
| 222 | parents=[key_parser, add_value_parser, |
| 223 | path_parser, verbose_parser, |
| 224 | merge_parser], |
| 225 | help="Find a key in YAMLs found in <path>", |
| 226 | description=("Get a key collected from " |
| 227 | "different YAMLs")) |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 228 | subparsers.add_parser('del-key', |
| 229 | parents=[key_parser, path_parser, |
| 230 | verbose_parser], |
| 231 | help="Delete a key from YAMLs found in <path>", |
| 232 | description="Delete a key from different YAMLs") |
| 233 | subparsers.add_parser('list-params', |
| 234 | parents=[path_parser, verbose_parser], |
| 235 | help=("Collect all options for " |
| 236 | "'parameters._params' keys from YAMLs " |
| 237 | "found in <path>")) |
| 238 | subparsers.add_parser('list-nodes', |
| 239 | parents=[domain_parser, vcp_only_parser, |
| 240 | non_vcp_only_parser], |
| 241 | help=("List nodes that are available for " |
| 242 | "reclass. Use on salt-master node only!")) |
| 243 | subparsers.add_parser('list-domains', |
| 244 | help=("List domains that are available from " |
| 245 | "reclass models. Use on salt-master " |
| 246 | "node only!")) |
| 247 | subparsers.add_parser('show-context', |
| 248 | parents=[domain_parser, keys_parser], |
| 249 | help=("Show domain nodes with rendered content " |
| 250 | "for specified keys. Use on salt-master " |
| 251 | "node for already generated inventory " |
| 252 | "only!")) |
Dennis Dmitriev | 806706d | 2017-07-29 22:31:23 +0300 | [diff] [blame] | 253 | subparsers.add_parser('trace-key', |
| 254 | parents=[domain_parser, node_parser, key_parser], |
| 255 | help=("Use 'reclass' to merge the model and " |
| 256 | "show all the classes where the " |
| 257 | "specified key is overwritten, and updated" |
| 258 | " values during merging and after " |
| 259 | "interpolation. " |
| 260 | "Use on salt-master node only!")) |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 261 | subparsers.add_parser('render', |
Dennis Dmitriev | 0cea570 | 2017-07-17 19:03:23 +0300 | [diff] [blame] | 262 | parents=[render_parser, env_name_parser], |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 263 | help=("Render cookiecutter template using " |
| 264 | "multiple metadata sources")) |
| 265 | |
| 266 | if len(self.args) == 0: |
| 267 | self.args = ['-h'] |
| 268 | return parser.parse_args(self.args) |
| 269 | |
| 270 | |
| 271 | def main(args=None): |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 272 | if args is None: |
| 273 | args = sys.argv[1:] |
| 274 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 275 | shell = Shell(args) |
| 276 | shell.execute() |