33d12eec0c06f0fd82e8f7f83c5577eecac29e54
[apex-tripleo-heat-templates.git] / tools / yaml-validate.py
1 #!/usr/bin/env python
2 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
3 #    not use this file except in compliance with the License. You may obtain
4 #    a copy of the License at
5 #
6 #         http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #    Unless required by applicable law or agreed to in writing, software
9 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 #    License for the specific language governing permissions and limitations
12 #    under the License.
13
14 import os
15 import sys
16 import traceback
17 import yaml
18
19
20 required_params = ['EndpointMap', 'ServiceNetMap', 'DefaultPasswords',
21                    'RoleName', 'RoleParameters', 'ServiceData']
22
23 # NOTE(bnemec): The duplication in this list is intentional.  For the
24 # transition to generated environments we have two copies of these files,
25 # so they need to be listed twice.  Once the deprecated version can be removed
26 # the duplicate entries can be as well.
27 envs_containing_endpoint_map = ['tls-endpoints-public-dns.yaml',
28                                 'tls-endpoints-public-ip.yaml',
29                                 'tls-everywhere-endpoints-dns.yaml',
30                                 'tls-endpoints-public-dns.yaml',
31                                 'tls-endpoints-public-ip.yaml',
32                                 'tls-everywhere-endpoints-dns.yaml']
33 ENDPOINT_MAP_FILE = 'endpoint_map.yaml'
34 REQUIRED_DOCKER_SECTIONS = ['service_name', 'docker_config', 'puppet_config',
35                             'config_settings', 'step_config']
36 OPTIONAL_DOCKER_SECTIONS = ['docker_puppet_tasks', 'upgrade_tasks',
37                             'service_config_settings', 'host_prep_tasks',
38                             'metadata_settings', 'kolla_config']
39 REQUIRED_DOCKER_PUPPET_CONFIG_SECTIONS = ['config_volume', 'step_config',
40                                           'config_image']
41 OPTIONAL_DOCKER_PUPPET_CONFIG_SECTIONS = [ 'puppet_tags', 'volumes' ]
42 # Mapping of parameter names to a list of the fields we should _not_ enforce
43 # consistency across files on.  This should only contain parameters whose
44 # definition we cannot change for backwards compatibility reasons.  New
45 # parameters to the templates should not be added to this list.
46 PARAMETER_DEFINITION_EXCLUSIONS = {'ManagementNetCidr': ['default'],
47                                    'ManagementAllocationPools': ['default'],
48                                    'ExternalNetCidr': ['default'],
49                                    'ExternalAllocationPools': ['default'],
50                                    'StorageNetCidr': ['default'],
51                                    'StorageAllocationPools': ['default'],
52                                    'StorageMgmtNetCidr': ['default',
53                                                           # FIXME
54                                                           'description'],
55                                    'StorageMgmtAllocationPools': ['default'],
56                                    'TenantNetCidr': ['default'],
57                                    'TenantAllocationPools': ['default'],
58                                    'InternalApiNetCidr': ['default'],
59                                    'UpdateIdentifier': ['description'],
60                                    # TODO(bnemec): Address these existing
61                                    # inconsistencies.
62                                    'NeutronMetadataProxySharedSecret': [
63                                        'description', 'hidden'],
64                                    'ServiceNetMap': ['description', 'default'],
65                                    'RedisPassword': ['description'],
66                                    'EC2MetadataIp': ['default'],
67                                    'network': ['default'],
68                                    'ControlPlaneIP': ['default',
69                                                       'description'],
70                                    'ControlPlaneIp': ['default',
71                                                       'description'],
72                                    'NeutronBigswitchLLDPEnabled': ['default'],
73                                    'NeutronEnableL2Pop': ['description'],
74                                    'NeutronWorkers': ['description'],
75                                    'TenantIpSubnet': ['description'],
76                                    'ExternalNetName': ['description'],
77                                    'AdminToken': ['description'],
78                                    'ControlPlaneDefaultRoute': ['default'],
79                                    'StorageMgmtNetName': ['description'],
80                                    'ServerMetadata': ['description'],
81                                    'InternalApiIpUri': ['description'],
82                                    'UpgradeLevelNovaCompute': ['default'],
83                                    'StorageMgmtIpUri': ['description'],
84                                    'server': ['description'],
85                                    'servers': ['description'],
86                                    'FixedIPs': ['description'],
87                                    'ExternalIpSubnet': ['description'],
88                                    'NeutronBridgeMappings': ['description'],
89                                    'ExtraConfig': ['description'],
90                                    'InternalApiIpSubnet': ['description'],
91                                    'DefaultPasswords': ['description',
92                                                         'default'],
93                                    'BondInterfaceOvsOptions': ['description',
94                                                                'default',
95                                                                'constraints'],
96                                    'KeyName': ['constraints'],
97                                    'TenantNetName': ['description'],
98                                    'StorageIpSubnet': ['description'],
99                                    'OVNSouthboundServerPort': ['description'],
100                                    'ExternalInterfaceDefaultRoute':
101                                        ['description', 'default'],
102                                    'ExternalIpUri': ['description'],
103                                    'IPPool': ['description'],
104                                    'ControlPlaneNetwork': ['description'],
105                                    'SSLCertificate': ['description',
106                                                       'default',
107                                                       'hidden'],
108                                    'HostCpusList': ['default', 'constraints'],
109                                    'InternalApiAllocationPools': ['default'],
110                                    'NodeIndex': ['description'],
111                                    'SwiftPassword': ['description'],
112                                    'name': ['description', 'default'],
113                                    'StorageNetName': ['description'],
114                                    'ManagementNetName': ['description'],
115                                    'NeutronPublicInterface': ['description'],
116                                    'RoleParameters': ['description'],
117                                    'AdminPassword': ['description', 'hidden'],
118                                    'ManagementInterfaceDefaultRoute':
119                                        ['default'],
120                                    'NovaPassword': ['description'],
121                                    'image': ['description', 'default'],
122                                    'NeutronBigswitchAgentEnabled': ['default'],
123                                    'EndpointMap': ['description', 'default'],
124                                    'DockerManilaConfigImage': ['description',
125                                                                'default'],
126                                    'NetworkName': ['default', 'description'],
127                                    'StorageIpUri': ['description'],
128                                    'InternalApiNetName': ['description'],
129                                    'NeutronTunnelTypes': ['description'],
130                                    'replacement_policy': ['default'],
131                                    'StorageMgmtIpSubnet': ['description'],
132                                    'CloudDomain': ['description', 'default'],
133                                    'key_name': ['default', 'description'],
134                                    'EnableLoadBalancer': ['description'],
135                                    'ControllerExtraConfig': ['description'],
136                                    'NovaComputeExtraConfig': ['description'],
137                                    'controllerExtraConfig': ['description'],
138                                    'DockerSwiftConfigImage': ['default'],
139                                    }
140
141 PREFERRED_CAMEL_CASE = {
142     'ec2api': 'Ec2Api',
143     'haproxy': 'HAProxy',
144 }
145
146
147 def exit_usage():
148     print('Usage %s <yaml file or directory>' % sys.argv[0])
149     sys.exit(1)
150
151
152 def to_camel_case(string):
153     return PREFERRED_CAMEL_CASE.get(string, ''.join(s.capitalize() or '_' for
154                                                     s in string.split('_')))
155
156
157 def get_base_endpoint_map(filename):
158     try:
159         tpl = yaml.load(open(filename).read())
160         return tpl['parameters']['EndpointMap']['default']
161     except Exception:
162         print(traceback.format_exc())
163     return None
164
165
166 def get_endpoint_map_from_env(filename):
167     try:
168         tpl = yaml.load(open(filename).read())
169         return {
170             'file': filename,
171             'map': tpl['parameter_defaults']['EndpointMap']
172         }
173     except Exception:
174         print(traceback.format_exc())
175     return None
176
177
178 def validate_endpoint_map(base_map, env_map):
179     return sorted(base_map.keys()) == sorted(env_map.keys())
180
181
182 def validate_hci_compute_services_default(env_filename, env_tpl):
183     env_services_list = env_tpl['parameter_defaults']['ComputeServices']
184     env_services_list.remove('OS::TripleO::Services::CephOSD')
185     roles_filename = os.path.join(os.path.dirname(env_filename),
186                                   '../roles/Compute.yaml')
187     roles_tpl = yaml.load(open(roles_filename).read())
188     for role in roles_tpl:
189         if role['name'] == 'Compute':
190             roles_services_list = role['ServicesDefault']
191             if sorted(env_services_list) != sorted(roles_services_list):
192                 print('ERROR: ComputeServices in %s is different from '
193                       'ServicesDefault in roles/Compute.yaml' % env_filename)
194                 return 1
195     return 0
196
197
198 def validate_hci_computehci_role(hci_role_filename, hci_role_tpl):
199     compute_role_filename = os.path.join(os.path.dirname(hci_role_filename),
200                                          './Compute.yaml')
201     compute_role_tpl = yaml.load(open(compute_role_filename).read())
202     compute_role_services = compute_role_tpl[0]['ServicesDefault']
203     for role in hci_role_tpl:
204         if role['name'] == 'ComputeHCI':
205             hci_role_services = role['ServicesDefault']
206             hci_role_services.remove('OS::TripleO::Services::CephOSD')
207             if sorted(hci_role_services) != sorted(compute_role_services):
208                 print('ERROR: ServicesDefault in %s is different from'
209                       'ServicesDefault in roles/Compute.yaml' % hci_role_filename)
210                 return 1
211     return 0
212
213
214 def validate_mysql_connection(settings):
215     no_op = lambda *args: False
216     error_status = [0]
217
218     def mysql_protocol(items):
219         return items == ['EndpointMap', 'MysqlInternal', 'protocol']
220
221     def client_bind_address(item):
222         return 'read_default_file' in item and \
223                'read_default_group' in item
224
225     def validate_mysql_uri(key, items):
226         # Only consider a connection if it targets mysql
227         if key.endswith('connection') and \
228            search(items, mysql_protocol, no_op):
229             # Assume the "bind_address" option is one of
230             # the token that made up the uri
231             if not search(items, client_bind_address, no_op):
232                 error_status[0] = 1
233         return False
234
235     def search(item, check_item, check_key):
236         if check_item(item):
237             return True
238         elif isinstance(item, list):
239             for i in item:
240                 if search(i, check_item, check_key):
241                     return True
242         elif isinstance(item, dict):
243             for k in item.keys():
244                 if check_key(k, item[k]):
245                     return True
246                 elif search(item[k], check_item, check_key):
247                     return True
248         return False
249
250     search(settings, no_op, validate_mysql_uri)
251     return error_status[0]
252
253
254 def validate_docker_service(filename, tpl):
255     if 'outputs' in tpl and 'role_data' in tpl['outputs']:
256         if 'value' not in tpl['outputs']['role_data']:
257             print('ERROR: invalid role_data for filename: %s'
258                   % filename)
259             return 1
260         role_data = tpl['outputs']['role_data']['value']
261
262         for section_name in REQUIRED_DOCKER_SECTIONS:
263             if section_name not in role_data:
264                 print('ERROR: %s is required in role_data for %s.'
265                       % (section_name, filename))
266                 return 1
267
268         for section_name in role_data.keys():
269             if section_name in REQUIRED_DOCKER_SECTIONS:
270                 continue
271             else:
272                 if section_name in OPTIONAL_DOCKER_SECTIONS:
273                     continue
274                 else:
275                     print('ERROR: %s is extra in role_data for %s.'
276                           % (section_name, filename))
277                     return 1
278
279         if 'puppet_config' in role_data:
280             puppet_config = role_data['puppet_config']
281             for key in puppet_config:
282                 if key in REQUIRED_DOCKER_PUPPET_CONFIG_SECTIONS:
283                     continue
284                 else:
285                     if key in OPTIONAL_DOCKER_PUPPET_CONFIG_SECTIONS:
286                         continue
287                     else:
288                       print('ERROR: %s should not be in puppet_config section.'
289                             % key)
290                       return 1
291             for key in REQUIRED_DOCKER_PUPPET_CONFIG_SECTIONS:
292               if key not in puppet_config:
293                   print('ERROR: %s is required in puppet_config for %s.'
294                         % (key, filename))
295                   return 1
296
297             config_volume = puppet_config.get('config_volume')
298             expected_config_image_parameter = "Docker%sConfigImage" % to_camel_case(config_volume)
299             if config_volume and not expected_config_image_parameter in tpl.get('parameters', []):
300                 print('ERROR: Missing %s heat parameter for %s config_volume.'
301                       % (expected_config_image_parameter, config_volume))
302                 return 1
303
304         if 'docker_config' in role_data:
305             docker_config = role_data['docker_config']
306             for _, step in docker_config.items():
307                 if not isinstance(step, dict):
308                     # NOTE(mandre) this skips everything that is not a dict
309                     # so we may ignore some containers definitions if they
310                     # are in a map_merge for example
311                     continue
312                 for _, container in step.items():
313                     if not isinstance(container, dict):
314                         continue
315                     command = container.get('command', '')
316                     if isinstance(command, list):
317                         command = ' '.join(map(str, command))
318                     if 'bootstrap_host_exec' in command \
319                             and container.get('user') != 'root':
320                       print('ERROR: bootstrap_host_exec needs to run as the root user.')
321                       return 1
322
323     if 'parameters' in tpl:
324         for param in required_params:
325             if param not in tpl['parameters']:
326                 print('ERROR: parameter %s is required for %s.'
327                       % (param, filename))
328                 return 1
329     return 0
330
331
332 def validate_service(filename, tpl):
333     if 'outputs' in tpl and 'role_data' in tpl['outputs']:
334         if 'value' not in tpl['outputs']['role_data']:
335             print('ERROR: invalid role_data for filename: %s'
336                   % filename)
337             return 1
338         role_data = tpl['outputs']['role_data']['value']
339         if 'service_name' not in role_data:
340             print('ERROR: service_name is required in role_data for %s.'
341                   % filename)
342             return 1
343         # service_name must match the filename, but with an underscore
344         if (role_data['service_name'] !=
345                 os.path.basename(filename).split('.')[0].replace("-", "_")):
346             print('ERROR: service_name should match file name for service: %s.'
347                   % filename)
348             return 1
349         # if service connects to mysql, the uri should use option
350         # bind_address to avoid issues with VIP failover
351         if 'config_settings' in role_data and \
352            validate_mysql_connection(role_data['config_settings']):
353             print('ERROR: mysql connection uri should use option bind_address')
354             return 1
355     if 'parameters' in tpl:
356         for param in required_params:
357             if param not in tpl['parameters']:
358                 print('ERROR: parameter %s is required for %s.'
359                       % (param, filename))
360                 return 1
361     return 0
362
363
364 def validate(filename, param_map):
365     """Validate a Heat template
366
367     :param filename: The path to the file to validate
368     :param param_map: A dict which will be populated with the details of the
369                       parameters in the template.  The dict will have the
370                       following structure:
371
372                           {'ParameterName': [
373                                {'filename': ./file1.yaml,
374                                 'data': {'description': '',
375                                          'type': string,
376                                          'default': '',
377                                          ...}
378                                 },
379                                {'filename': ./file2.yaml,
380                                 'data': {'description': '',
381                                          'type': string,
382                                          'default': '',
383                                          ...}
384                                 },
385                                 ...
386                            ]}
387     """
388     print('Validating %s' % filename)
389     retval = 0
390     try:
391         tpl = yaml.load(open(filename).read())
392
393         # The template alias version should be used instead a date, this validation
394         # will be applied to all templates not just for those in the services folder.
395         if 'heat_template_version' in tpl and not str(tpl['heat_template_version']).isalpha():
396             print('ERROR: heat_template_version needs to be the release alias not a date: %s'
397                   % filename)
398             return 1
399
400         # qdr aliases rabbitmq service to provide alternative messaging backend
401         if (filename.startswith('./puppet/services/') and
402                 filename not in ['./puppet/services/qdr.yaml']):
403             retval = validate_service(filename, tpl)
404
405         if filename.startswith('./docker/services/'):
406             retval = validate_docker_service(filename, tpl)
407
408         if filename.endswith('hyperconverged-ceph.yaml'):
409             retval = validate_hci_compute_services_default(filename, tpl)
410
411         if filename.startswith('./roles/ComputeHCI.yaml'):
412             retval = validate_hci_computehci_role(filename, tpl)
413
414     except Exception:
415         print(traceback.format_exc())
416         return 1
417     # yaml is OK, now walk the parameters and output a warning for unused ones
418     if 'heat_template_version' in tpl:
419         for p, data in tpl.get('parameters', {}).items():
420             definition = {'data': data, 'filename': filename}
421             param_map.setdefault(p, []).append(definition)
422             if p in required_params:
423                 continue
424             str_p = '\'%s\'' % p
425             in_resources = str_p in str(tpl.get('resources', {}))
426             in_outputs = str_p in str(tpl.get('outputs', {}))
427             if not in_resources and not in_outputs:
428                 print('Warning: parameter %s in template %s '
429                       'appears to be unused' % (p, filename))
430
431     return retval
432
433 if len(sys.argv) < 2:
434     exit_usage()
435
436 path_args = sys.argv[1:]
437 exit_val = 0
438 failed_files = []
439 base_endpoint_map = None
440 env_endpoint_maps = list()
441 param_map = {}
442
443 for base_path in path_args:
444     if os.path.isdir(base_path):
445         for subdir, dirs, files in os.walk(base_path):
446             if '.tox' in dirs:
447                 dirs.remove('.tox')
448             for f in files:
449                 if f.endswith('.yaml') and not f.endswith('.j2.yaml'):
450                     file_path = os.path.join(subdir, f)
451                     failed = validate(file_path, param_map)
452                     if failed:
453                         failed_files.append(file_path)
454                     exit_val |= failed
455                     if f == ENDPOINT_MAP_FILE:
456                         base_endpoint_map = get_base_endpoint_map(file_path)
457                     if f in envs_containing_endpoint_map:
458                         env_endpoint_map = get_endpoint_map_from_env(file_path)
459                         if env_endpoint_map:
460                             env_endpoint_maps.append(env_endpoint_map)
461     elif os.path.isfile(base_path) and base_path.endswith('.yaml'):
462         failed = validate(base_path, param_map)
463         if failed:
464             failed_files.append(base_path)
465         exit_val |= failed
466     else:
467         print('Unexpected argument %s' % base_path)
468         exit_usage()
469
470 if base_endpoint_map and \
471         len(env_endpoint_maps) == len(envs_containing_endpoint_map):
472     for env_endpoint_map in env_endpoint_maps:
473         matches = validate_endpoint_map(base_endpoint_map,
474                                         env_endpoint_map['map'])
475         if not matches:
476             print("ERROR: %s needs to be updated to match changes in base "
477                   "endpoint map" % env_endpoint_map['file'])
478             failed_files.append(env_endpoint_map['file'])
479             exit_val |= 1
480         else:
481             print("%s matches base endpoint map" % env_endpoint_map['file'])
482 else:
483     print("ERROR: Did not find expected number of environments containing the "
484           "EndpointMap parameter.  If you meant to add or remove one of these "
485           "environments then you also need to update this tool.")
486     if not base_endpoint_map:
487         failed_files.append(ENDPOINT_MAP_FILE)
488     if len(env_endpoint_maps) != len(envs_containing_endpoint_map):
489         matched_files = set(os.path.basename(matched_env_file['file'])
490                             for matched_env_file in env_endpoint_maps)
491         failed_files.extend(set(envs_containing_endpoint_map) - matched_files)
492     exit_val |= 1
493
494 # Validate that duplicate parameters defined in multiple files all have the
495 # same definition.
496 mismatch_count = 0
497 for p, defs in param_map.items():
498     # Nothing to validate if the parameter is only defined once
499     if len(defs) == 1:
500         continue
501     check_data = [d['data'] for d in defs]
502     # Override excluded fields so they don't affect the result
503     exclusions = PARAMETER_DEFINITION_EXCLUSIONS.get(p, [])
504     ex_dict = {}
505     for field in exclusions:
506         ex_dict[field] = 'IGNORED'
507     for d in check_data:
508         d.update(ex_dict)
509     # If all items in the list are not == the first, then the check fails
510     if check_data.count(check_data[0]) != len(check_data):
511         mismatch_count += 1
512         exit_val |= 1
513         failed_files.extend([d['filename'] for d in defs])
514         print('Mismatched parameter definitions found for "%s"' % p)
515         print('Definitions found:')
516         for d in defs:
517             print('  %s:\n    %s' % (d['filename'], d['data']))
518 print('Mismatched parameter definitions: %d' % mismatch_count)
519
520 if failed_files:
521     print('Validation failed on:')
522     for f in failed_files:
523         print(f)
524 else:
525     print('Validation successful!')
526 sys.exit(exit_val)