blob: 94d1e7a0509b168481dfda49e9e38b8ef034f449 [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
Dennis Dmitriev86750962017-07-11 19:44:05 +030029 def execute(self):
30 command_name = 'do_{}'.format(self.params.command.replace('-', '_'))
31 command_method = getattr(self, command_name)
32 command_method()
Dennis Dmitrieve56c8b92017-06-16 01:53:16 +030033
Dennis Dmitriev86750962017-07-11 19:44:05 +030034 def do_get_key(self):
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030035 walk_models.remove_reclass_parameter(
Dennis Dmitriev86750962017-07-11 19:44:05 +030036 self.params.path,
37 self.params.key_name,
38 verbose=self.params.verbose,
39 pretend=True)
40
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030041 def do_add_key(self):
Sergeyb50d1f02017-12-25 18:07:15 +040042 # Try convert to digits
43 try:
44 add_val = int(self.params.add_value)
45 except ValueError:
46 try:
47 add_val = float(self.params.add_value)
48 except ValueError:
49 add_val = self.params.add_value
50
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030051 walk_models.add_reclass_parameter(
52 self.params.path,
53 self.params.key_name,
Sergeyb50d1f02017-12-25 18:07:15 +040054 add_val,
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030055 verbose=self.params.verbose,
56 merge=self.params.merge)
57
Dennis Dmitriev86750962017-07-11 19:44:05 +030058 def do_del_key(self):
Dennis Dmitriev566db4b2017-07-18 18:13:07 +030059 walk_models.remove_reclass_parameter(
Dennis Dmitriev86750962017-07-11 19:44:05 +030060 self.params.path,
61 self.params.key_name,
62 verbose=self.params.verbose,
63 pretend=False)
64
65 def do_list_params(self):
66 results = walk_models.get_all_reclass_params(
67 self.params.path,
68 verbose=self.params.verbose)
69 print(yaml.dump(results))
70
71 def do_list_domains(self):
72 try:
73 from reclass_tools import reclass_models
74 except ImportError:
75 sys.exit("Please run this tool on the salt-master node "
76 "with installed 'reclass'")
77 inventory = reclass_models.inventory_list()
78 reclass_storage = reclass_models.reclass_storage(inventory=inventory)
79 print('\n'.join(sorted(reclass_storage.keys())))
Dennis Dmitrieve56c8b92017-06-16 01:53:16 +030080
Dennis Dmitriev86750962017-07-11 19:44:05 +030081 def do_list_nodes(self):
82 try:
83 from reclass_tools import reclass_models
84 except ImportError:
85 sys.exit("Please run this tool on the salt-master node "
86 "with installed 'reclass'")
87
88 inventory = reclass_models.inventory_list(domain=self.params.domain)
89 vcp_nodes = reclass_models.vcp_list(domain=self.params.domain,
90 inventory=inventory)
91 vcp_node_names = ['{0}.{1}'.format(name, domain)
92 for name, domain in vcp_nodes]
93
94 if self.params.vcp_only:
95 print('\n'.join(sorted(vcp_node_names)))
96 elif self.params.non_vcp_only:
97 print('\n'.join(sorted((node_name for node_name in inventory.keys()
98 if node_name not in vcp_node_names))))
99 else:
100 print('\n'.join(sorted(inventory.keys())))
101
Dennis Dmitriev806706d2017-07-29 22:31:23 +0300102 def do_trace_key(self):
103 try:
104 from reclass_tools import reclass_models
105 except ImportError:
106 sys.exit("Please run this tool on the salt-master node "
107 "with installed 'reclass'")
108 reclass_models.trace_key(key=self.params.key_name,
109 domain=self.params.domain,
110 node=self.params.node)
111
Dennis Dmitriev86750962017-07-11 19:44:05 +0300112 def do_show_context(self):
113 try:
114 from reclass_tools import create_inventory
115 except ImportError:
116 sys.exit("Please run this tool on the salt-master node "
117 "with installed 'reclass'")
118
119 current_underlay_context = create_inventory.create_inventory_context(
120 domain=self.params.domain, keys=self.params.keys)
121
122 print(yaml.dump(current_underlay_context, default_flow_style=False))
123
124 def do_render(self):
Dennis Dmitrievf260d152017-08-15 00:59:24 +0300125 from reclass_tools import render
Dennis Dmitriev86750962017-07-11 19:44:05 +0300126
127 if not self.params.template_dir or not self.params.output_dir \
128 or not self.params.contexts:
129 sys.exit("Missing parameters, see: reclass-tools render -h")
130
Dennis Dmitrievf260d152017-08-15 00:59:24 +0300131 render.render_dir(template_dir=self.params.template_dir,
132 output_dir=self.params.output_dir,
133 contexts=self.params.contexts,
134 env_name=self.params.env_name)
Dennis Dmitriev86750962017-07-11 19:44:05 +0300135
136 def get_params(self):
137
138 verbose_parser = argparse.ArgumentParser(add_help=False)
139 verbose_parser.add_argument('--verbose', dest='verbose',
140 action='store_const', const=True,
141 help='Show verbosed output', default=False)
142
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300143 merge_parser = argparse.ArgumentParser(add_help=False)
Dennis Dmitriev6792f7e2017-07-18 19:11:22 +0300144 merge_parser.add_argument('--merge', dest='merge',
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300145 action='store_const', const=True,
Dennis Dmitriev6792f7e2017-07-18 19:11:22 +0300146 help='Merge value to list or dict',
147 default=False)
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300148
Dennis Dmitriev86750962017-07-11 19:44:05 +0300149 key_parser = argparse.ArgumentParser(add_help=False)
150 key_parser_help = (
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300151 'Key name to find in reclass model files, for example:'
152 ' parameters.linux.network.interface')
Dennis Dmitriev86750962017-07-11 19:44:05 +0300153 key_parser.add_argument('key_name', help=key_parser_help, default=None)
154
155 keys_parser = argparse.ArgumentParser(add_help=False)
156 keys_parser.add_argument(
157 'keys',
158 help='Key names to find in reclass model files', nargs='*')
159
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300160 add_value_parser = argparse.ArgumentParser(add_help=False)
161 add_value_parser.add_argument(
162 'add_value',
163 help=('Value to add to the reclass model files, can be in the '
164 'inline YAML format'))
165
Dennis Dmitriev86750962017-07-11 19:44:05 +0300166 path_parser = argparse.ArgumentParser(add_help=False)
167 path_parser.add_argument(
168 'path',
169 help='Path to search for *.yml files.', nargs='+')
170
171 domain_parser = argparse.ArgumentParser(add_help=False)
172 domain_parser.add_argument(
173 '--domain', '-d', dest='domain',
174 help=('Show only the nodes which names are ended with the '
175 'specified domain, for example: example.local'))
176
Dennis Dmitriev806706d2017-07-29 22:31:23 +0300177 node_parser = argparse.ArgumentParser(add_help=False)
178 node_parser.add_argument(
179 '--node', '-n', dest='node',
180 help=('Show only the specified node, for example: '
181 'ctl01.example.local'))
182
Dennis Dmitriev0cea5702017-07-17 19:03:23 +0300183 env_name_parser = argparse.ArgumentParser(add_help=False)
184 env_name_parser.add_argument(
185 '--env-name', '-e', dest='env_name',
Dennis Dmitriev55989022017-07-17 19:23:16 +0300186 help=("Name of the 'environment' to create or use"),
187 default=None)
Dennis Dmitriev0cea5702017-07-17 19:03:23 +0300188
Dennis Dmitriev86750962017-07-11 19:44:05 +0300189 vcp_only_parser = argparse.ArgumentParser(add_help=False)
190 vcp_only_parser.add_argument(
191 '--vcp-only', dest='vcp_only',
192 action='store_const', const=True,
193 help=('Show only VCP nodes (present in '
194 'parameters.salt.control.cluster.internal.node)'),
195 default=False)
196
197 non_vcp_only_parser = argparse.ArgumentParser(add_help=False)
198 non_vcp_only_parser.add_argument(
199 '--non-vcp-only', dest='non_vcp_only',
200 action='store_const', const=True, default=False,
201 help=('Show only non-VCP nodes (absent in '
202 'parameters.salt.control.cluster.internal.node)'))
203
204 render_parser = argparse.ArgumentParser(add_help=False)
205 render_parser.add_argument(
206 '--template-dir', '-t', dest='template_dir',
207 help=('Coockiecutter-based template directory'))
208 render_parser.add_argument(
209 '--output-dir', '-o', dest='output_dir',
210 help=('Path to the directory where the rendered '
211 'template will be placed'))
212 render_parser.add_argument(
Dennis Dmitrievc2534372017-10-09 15:36:21 +0300213 '--context', '-c', dest='contexts', action='append',
Dennis Dmitriev86750962017-07-11 19:44:05 +0300214 help=('YAML/JSON files with context data to render '
215 'the template'))
216
Dennis Dmitriev86750962017-07-11 19:44:05 +0300217 parser = argparse.ArgumentParser(
218 description="Manage virtual environments. "
219 "For additional help, use with -h/--help option")
220 subparsers = parser.add_subparsers(title="Operation commands",
221 help='available commands',
222 dest='command')
223
Dennis Dmitriev86750962017-07-11 19:44:05 +0300224 subparsers.add_parser('get-key',
225 parents=[key_parser, path_parser,
226 verbose_parser],
227 help="Find a key in YAMLs found in <path>",
228 description=("Get a key collected from "
229 "different YAMLs"))
Dennis Dmitriev566db4b2017-07-18 18:13:07 +0300230 subparsers.add_parser('add-key',
231 parents=[key_parser, add_value_parser,
232 path_parser, verbose_parser,
233 merge_parser],
234 help="Find a key in YAMLs found in <path>",
235 description=("Get a key collected from "
236 "different YAMLs"))
Dennis Dmitriev86750962017-07-11 19:44:05 +0300237 subparsers.add_parser('del-key',
238 parents=[key_parser, path_parser,
239 verbose_parser],
240 help="Delete a key from YAMLs found in <path>",
241 description="Delete a key from different YAMLs")
242 subparsers.add_parser('list-params',
243 parents=[path_parser, verbose_parser],
244 help=("Collect all options for "
245 "'parameters._params' keys from YAMLs "
246 "found in <path>"))
247 subparsers.add_parser('list-nodes',
248 parents=[domain_parser, vcp_only_parser,
249 non_vcp_only_parser],
250 help=("List nodes that are available for "
251 "reclass. Use on salt-master node only!"))
252 subparsers.add_parser('list-domains',
253 help=("List domains that are available from "
254 "reclass models. Use on salt-master "
255 "node only!"))
256 subparsers.add_parser('show-context',
257 parents=[domain_parser, keys_parser],
258 help=("Show domain nodes with rendered content "
259 "for specified keys. Use on salt-master "
260 "node for already generated inventory "
261 "only!"))
Dennis Dmitriev806706d2017-07-29 22:31:23 +0300262 subparsers.add_parser('trace-key',
263 parents=[domain_parser, node_parser, key_parser],
264 help=("Use 'reclass' to merge the model and "
265 "show all the classes where the "
266 "specified key is overwritten, and updated"
267 " values during merging and after "
268 "interpolation. "
269 "Use on salt-master node only!"))
Dennis Dmitriev86750962017-07-11 19:44:05 +0300270 subparsers.add_parser('render',
Dennis Dmitriev0cea5702017-07-17 19:03:23 +0300271 parents=[render_parser, env_name_parser],
Dennis Dmitriev86750962017-07-11 19:44:05 +0300272 help=("Render cookiecutter template using "
273 "multiple metadata sources"))
274
275 if len(self.args) == 0:
276 self.args = ['-h']
277 return parser.parse_args(self.args)
278
279
280def main(args=None):
Dennis Dmitrieve56c8b92017-06-16 01:53:16 +0300281 if args is None:
282 args = sys.argv[1:]
283
Dennis Dmitriev86750962017-07-11 19:44:05 +0300284 shell = Shell(args)
285 shell.execute()