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 | |
| 93 | def do_show_context(self): |
| 94 | try: |
| 95 | from reclass_tools import create_inventory |
| 96 | except ImportError: |
| 97 | sys.exit("Please run this tool on the salt-master node " |
| 98 | "with installed 'reclass'") |
| 99 | |
| 100 | current_underlay_context = create_inventory.create_inventory_context( |
| 101 | domain=self.params.domain, keys=self.params.keys) |
| 102 | |
| 103 | print(yaml.dump(current_underlay_context, default_flow_style=False)) |
| 104 | |
| 105 | def do_render(self): |
| 106 | try: |
| 107 | from reclass_tools import create_inventory |
| 108 | except ImportError: |
| 109 | sys.exit("Please run this tool on the salt-master node " |
| 110 | "with installed 'reclass'") |
| 111 | |
| 112 | if not self.params.template_dir or not self.params.output_dir \ |
| 113 | or not self.params.contexts: |
| 114 | sys.exit("Missing parameters, see: reclass-tools render -h") |
| 115 | |
| 116 | create_inventory.render_dir(template_dir=self.params.template_dir, |
| 117 | output_dir=self.params.output_dir, |
Dennis Dmitriev | 0cea570 | 2017-07-17 19:03:23 +0300 | [diff] [blame] | 118 | contexts=self.params.contexts, |
| 119 | env_name=self.params.env_name) |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 120 | |
| 121 | def get_params(self): |
| 122 | |
| 123 | verbose_parser = argparse.ArgumentParser(add_help=False) |
| 124 | verbose_parser.add_argument('--verbose', dest='verbose', |
| 125 | action='store_const', const=True, |
| 126 | help='Show verbosed output', default=False) |
| 127 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame^] | 128 | merge_parser = argparse.ArgumentParser(add_help=False) |
| 129 | merge_parser.add_argument('--merge', dest='verbose', |
| 130 | action='store_const', const=True, |
| 131 | help='Show verbosed output', default=False) |
| 132 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 133 | key_parser = argparse.ArgumentParser(add_help=False) |
| 134 | key_parser_help = ( |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame^] | 135 | 'Key name to find in reclass model files, for example:' |
| 136 | ' parameters.linux.network.interface') |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 137 | key_parser.add_argument('key_name', help=key_parser_help, default=None) |
| 138 | |
| 139 | keys_parser = argparse.ArgumentParser(add_help=False) |
| 140 | keys_parser.add_argument( |
| 141 | 'keys', |
| 142 | help='Key names to find in reclass model files', nargs='*') |
| 143 | |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame^] | 144 | add_value_parser = argparse.ArgumentParser(add_help=False) |
| 145 | add_value_parser.add_argument( |
| 146 | 'add_value', |
| 147 | help=('Value to add to the reclass model files, can be in the ' |
| 148 | 'inline YAML format')) |
| 149 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 150 | path_parser = argparse.ArgumentParser(add_help=False) |
| 151 | path_parser.add_argument( |
| 152 | 'path', |
| 153 | help='Path to search for *.yml files.', nargs='+') |
| 154 | |
| 155 | domain_parser = argparse.ArgumentParser(add_help=False) |
| 156 | domain_parser.add_argument( |
| 157 | '--domain', '-d', dest='domain', |
| 158 | help=('Show only the nodes which names are ended with the ' |
| 159 | 'specified domain, for example: example.local')) |
| 160 | |
Dennis Dmitriev | 0cea570 | 2017-07-17 19:03:23 +0300 | [diff] [blame] | 161 | env_name_parser = argparse.ArgumentParser(add_help=False) |
| 162 | env_name_parser.add_argument( |
| 163 | '--env-name', '-e', dest='env_name', |
Dennis Dmitriev | 5598902 | 2017-07-17 19:23:16 +0300 | [diff] [blame] | 164 | help=("Name of the 'environment' to create or use"), |
| 165 | default=None) |
Dennis Dmitriev | 0cea570 | 2017-07-17 19:03:23 +0300 | [diff] [blame] | 166 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 167 | vcp_only_parser = argparse.ArgumentParser(add_help=False) |
| 168 | vcp_only_parser.add_argument( |
| 169 | '--vcp-only', dest='vcp_only', |
| 170 | action='store_const', const=True, |
| 171 | help=('Show only VCP nodes (present in ' |
| 172 | 'parameters.salt.control.cluster.internal.node)'), |
| 173 | default=False) |
| 174 | |
| 175 | non_vcp_only_parser = argparse.ArgumentParser(add_help=False) |
| 176 | non_vcp_only_parser.add_argument( |
| 177 | '--non-vcp-only', dest='non_vcp_only', |
| 178 | action='store_const', const=True, default=False, |
| 179 | help=('Show only non-VCP nodes (absent in ' |
| 180 | 'parameters.salt.control.cluster.internal.node)')) |
| 181 | |
| 182 | render_parser = argparse.ArgumentParser(add_help=False) |
| 183 | render_parser.add_argument( |
| 184 | '--template-dir', '-t', dest='template_dir', |
| 185 | help=('Coockiecutter-based template directory')) |
| 186 | render_parser.add_argument( |
| 187 | '--output-dir', '-o', dest='output_dir', |
| 188 | help=('Path to the directory where the rendered ' |
| 189 | 'template will be placed')) |
| 190 | render_parser.add_argument( |
| 191 | '--context', '-c', dest='contexts', nargs='+', |
| 192 | help=('YAML/JSON files with context data to render ' |
| 193 | 'the template')) |
| 194 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 195 | parser = argparse.ArgumentParser( |
| 196 | description="Manage virtual environments. " |
| 197 | "For additional help, use with -h/--help option") |
| 198 | subparsers = parser.add_subparsers(title="Operation commands", |
| 199 | help='available commands', |
| 200 | dest='command') |
| 201 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 202 | subparsers.add_parser('get-key', |
| 203 | parents=[key_parser, path_parser, |
| 204 | verbose_parser], |
| 205 | help="Find a key in YAMLs found in <path>", |
| 206 | description=("Get a key collected from " |
| 207 | "different YAMLs")) |
Dennis Dmitriev | 566db4b | 2017-07-18 18:13:07 +0300 | [diff] [blame^] | 208 | subparsers.add_parser('add-key', |
| 209 | parents=[key_parser, add_value_parser, |
| 210 | path_parser, verbose_parser, |
| 211 | merge_parser], |
| 212 | help="Find a key in YAMLs found in <path>", |
| 213 | description=("Get a key collected from " |
| 214 | "different YAMLs")) |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 215 | subparsers.add_parser('del-key', |
| 216 | parents=[key_parser, path_parser, |
| 217 | verbose_parser], |
| 218 | help="Delete a key from YAMLs found in <path>", |
| 219 | description="Delete a key from different YAMLs") |
| 220 | subparsers.add_parser('list-params', |
| 221 | parents=[path_parser, verbose_parser], |
| 222 | help=("Collect all options for " |
| 223 | "'parameters._params' keys from YAMLs " |
| 224 | "found in <path>")) |
| 225 | subparsers.add_parser('list-nodes', |
| 226 | parents=[domain_parser, vcp_only_parser, |
| 227 | non_vcp_only_parser], |
| 228 | help=("List nodes that are available for " |
| 229 | "reclass. Use on salt-master node only!")) |
| 230 | subparsers.add_parser('list-domains', |
| 231 | help=("List domains that are available from " |
| 232 | "reclass models. Use on salt-master " |
| 233 | "node only!")) |
| 234 | subparsers.add_parser('show-context', |
| 235 | parents=[domain_parser, keys_parser], |
| 236 | help=("Show domain nodes with rendered content " |
| 237 | "for specified keys. Use on salt-master " |
| 238 | "node for already generated inventory " |
| 239 | "only!")) |
| 240 | subparsers.add_parser('render', |
Dennis Dmitriev | 0cea570 | 2017-07-17 19:03:23 +0300 | [diff] [blame] | 241 | parents=[render_parser, env_name_parser], |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 242 | help=("Render cookiecutter template using " |
| 243 | "multiple metadata sources")) |
| 244 | |
| 245 | if len(self.args) == 0: |
| 246 | self.args = ['-h'] |
| 247 | return parser.parse_args(self.args) |
| 248 | |
| 249 | |
| 250 | def main(args=None): |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 251 | if args is None: |
| 252 | args = sys.argv[1:] |
| 253 | |
Dennis Dmitriev | 8675096 | 2017-07-11 19:44:05 +0300 | [diff] [blame] | 254 | shell = Shell(args) |
| 255 | shell.execute() |