Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 1 | # Copyright 2013 - 2016 Mirantis, Inc. |
| 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 |
| 18 | import os |
| 19 | import sys |
| 20 | import yaml |
| 21 | |
| 22 | from reclass_tools import walk_models |
| 23 | |
| 24 | |
| 25 | def execute(params): |
| 26 | |
| 27 | results = walk_models.get_all_reclass_params( |
| 28 | params.paths, |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 29 | verbose=params.verbose) |
| 30 | |
| 31 | print(yaml.dump(results)) |
| 32 | |
| 33 | |
| 34 | def dump_params(args=None): |
| 35 | if args is None: |
| 36 | args = sys.argv[1:] |
| 37 | |
| 38 | parser = argparse.ArgumentParser( |
| 39 | formatter_class=argparse.RawTextHelpFormatter, |
| 40 | description="") |
Dennis Dmitriev | e56c8b9 | 2017-06-16 01:53:16 +0300 | [diff] [blame] | 41 | parser.add_argument('--verbose', dest='verbose', action='store_const', const=True, |
| 42 | help='Show verbosed output.', default=False) |
| 43 | parser.add_argument('paths', help='Paths to search for *.yml files.', nargs='+') |
| 44 | |
| 45 | if len(args) == 0: |
| 46 | args = ['-h'] |
| 47 | |
| 48 | params = parser.parse_args(args) |
Dennis Dmitriev | 672bd44 | 2017-06-16 18:13:54 +0300 | [diff] [blame] | 49 | results = walk_models.get_all_reclass_params( |
| 50 | params.paths, |
Dennis Dmitriev | 672bd44 | 2017-06-16 18:13:54 +0300 | [diff] [blame] | 51 | verbose=params.verbose) |
| 52 | |
| 53 | print(yaml.dump(results)) |
| 54 | |
| 55 | |
Dennis Dmitriev | 6bd4425 | 2017-06-22 17:33:40 +0300 | [diff] [blame] | 56 | def show_key(args=None): |
| 57 | remove_key(args=args, pretend=True) |
| 58 | |
| 59 | |
| 60 | def remove_key(args=None, pretend=False): |
Dennis Dmitriev | 672bd44 | 2017-06-16 18:13:54 +0300 | [diff] [blame] | 61 | if args is None: |
| 62 | args = sys.argv[1:] |
| 63 | |
Dennis Dmitriev | 6bd4425 | 2017-06-22 17:33:40 +0300 | [diff] [blame] | 64 | key_parser = argparse.ArgumentParser(add_help=False) |
| 65 | if pretend: |
| 66 | key_parser_help = ( |
| 67 | 'Key name to find in reclass model files, for example:' |
| 68 | ' reclass-show-key parameters.linux.network.interface' |
| 69 | ' /path/to/model/') |
| 70 | else: |
| 71 | key_parser_help = ( |
| 72 | 'Key name to remove from reclass model files, for example:' |
| 73 | ' reclass-remove-key parameters.linux.network.interface' |
| 74 | ' /path/to/model/') |
| 75 | key_parser.add_argument('key_name', help=key_parser_help) |
| 76 | |
| 77 | parser = argparse.ArgumentParser(parents=[key_parser], |
Dennis Dmitriev | 672bd44 | 2017-06-16 18:13:54 +0300 | [diff] [blame] | 78 | formatter_class=argparse.RawTextHelpFormatter, |
| 79 | description="") |
Dennis Dmitriev | 672bd44 | 2017-06-16 18:13:54 +0300 | [diff] [blame] | 80 | parser.add_argument('--verbose', dest='verbose', action='store_const', const=True, |
| 81 | help='Show verbosed output.', default=False) |
| 82 | parser.add_argument('paths', help='Paths to search for *.yml files.', nargs='+') |
Dennis Dmitriev | 6bd4425 | 2017-06-22 17:33:40 +0300 | [diff] [blame] | 83 | |
Dennis Dmitriev | 672bd44 | 2017-06-16 18:13:54 +0300 | [diff] [blame] | 84 | if len(args) == 0: |
| 85 | args = ['-h'] |
| 86 | |
| 87 | params = parser.parse_args(args) |
| 88 | results = walk_models.remove_reclass_parameter( |
| 89 | params.paths, |
Dennis Dmitriev | 6bd4425 | 2017-06-22 17:33:40 +0300 | [diff] [blame] | 90 | params.key_name, |
| 91 | verbose=params.verbose, |
| 92 | pretend=pretend) |
Dennis Dmitriev | 1110ac5 | 2017-06-22 21:07:37 +0300 | [diff] [blame] | 93 | |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame^] | 94 | |
Dennis Dmitriev | 1110ac5 | 2017-06-22 21:07:37 +0300 | [diff] [blame] | 95 | def inventory_list(args=None): |
| 96 | try: |
| 97 | from reclass_tools import reclass_models |
| 98 | except ImportError: |
| 99 | print("Please run this tool on the salt-master node with installed 'reclass'") |
| 100 | return |
| 101 | |
| 102 | parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, |
| 103 | description="") |
Dennis Dmitriev | 94239b1 | 2017-06-23 13:18:38 +0300 | [diff] [blame] | 104 | parser.add_argument('--domain', '-d', dest='domain', |
| 105 | help=('Show only the nodes which names are ended with the specified domain, for example:' |
| 106 | ' reclass-inventory-list -d example.local')) |
Dennis Dmitriev | 1110ac5 | 2017-06-22 21:07:37 +0300 | [diff] [blame] | 107 | |
| 108 | params = parser.parse_args(args) |
| 109 | |
Dennis Dmitriev | de847d9 | 2017-06-26 18:58:05 +0300 | [diff] [blame^] | 110 | inventory = reclass_models.inventory_list(domain=params.domain) |
| 111 | |
| 112 | print('\n'.join(sorted(inventory.keys()))) |
| 113 | |
| 114 | def vcp_list(args=None): |
| 115 | try: |
| 116 | from reclass_tools import reclass_models |
| 117 | except ImportError: |
| 118 | print("Please run this tool on the salt-master node with installed 'reclass'") |
| 119 | return |
| 120 | |
| 121 | parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, |
| 122 | description="") |
| 123 | parser.add_argument('--domain', '-d', dest='domain', |
| 124 | help=('Show only the nodes which names are ended with the specified domain, for example:' |
| 125 | ' reclass-inventory-list -d example.local')) |
| 126 | |
| 127 | params = parser.parse_args(args) |
| 128 | |
| 129 | vcp_node_names = reclass_models.vcp_list(domain=params.domain) |
| 130 | print('\n'.join(sorted(vcp_node_names))) |
| 131 | |