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
6 # http://www.apache.org/licenses/LICENSE-2.0
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
20 required_params = ['EndpointMap', 'ServiceNetMap', 'DefaultPasswords']
22 envs_containing_endpoint_map = ['tls-endpoints-public-dns.yaml',
23 'tls-endpoints-public-ip.yaml',
24 'tls-everywhere-endpoints-dns.yaml']
25 ENDPOINT_MAP_FILE = 'endpoint_map.yaml'
26 REQUIRED_DOCKER_SECTIONS = ['service_name', 'docker_config', 'puppet_config',
27 'config_settings', 'step_config']
28 OPTIONAL_DOCKER_SECTIONS = ['docker_puppet_tasks', 'upgrade_tasks',
29 'service_config_settings', 'host_prep_tasks',
30 'metadata_settings', 'kolla_config']
31 REQUIRED_DOCKER_PUPPET_CONFIG_SECTIONS = ['config_volume', 'step_config',
33 OPTIONAL_DOCKER_PUPPET_CONFIG_SECTIONS = [ 'puppet_tags' ]
37 print('Usage %s <yaml file or directory>' % sys.argv[0])
41 def get_base_endpoint_map(filename):
43 tpl = yaml.load(open(filename).read())
44 return tpl['parameters']['EndpointMap']['default']
46 print(traceback.format_exc())
50 def get_endpoint_map_from_env(filename):
52 tpl = yaml.load(open(filename).read())
55 'map': tpl['parameter_defaults']['EndpointMap']
58 print(traceback.format_exc())
62 def validate_endpoint_map(base_map, env_map):
63 return sorted(base_map.keys()) == sorted(env_map.keys())
66 def validate_hci_compute_services_default(env_filename, env_tpl):
67 env_services_list = env_tpl['parameter_defaults']['ComputeServices']
68 env_services_list.remove('OS::TripleO::Services::CephOSD')
69 roles_filename = os.path.join(os.path.dirname(env_filename),
71 roles_tpl = yaml.load(open(roles_filename).read())
72 for role in roles_tpl:
73 if role['name'] == 'Compute':
74 roles_services_list = role['ServicesDefault']
75 if sorted(env_services_list) != sorted(roles_services_list):
76 print('ERROR: ComputeServices in %s is different '
77 'from ServicesDefault in roles_data.yaml' % env_filename)
82 def validate_mysql_connection(settings):
83 no_op = lambda *args: False
86 def mysql_protocol(items):
87 return items == ['EndpointMap', 'MysqlInternal', 'protocol']
89 def client_bind_address(item):
90 return 'read_default_file' in item and \
91 'read_default_group' in item
93 def validate_mysql_uri(key, items):
94 # Only consider a connection if it targets mysql
95 if key.endswith('connection') and \
96 search(items, mysql_protocol, no_op):
97 # Assume the "bind_address" option is one of
98 # the token that made up the uri
99 if not search(items, client_bind_address, no_op):
103 def search(item, check_item, check_key):
106 elif isinstance(item, list):
108 if search(i, check_item, check_key):
110 elif isinstance(item, dict):
111 for k in item.keys():
112 if check_key(k, item[k]):
114 elif search(item[k], check_item, check_key):
118 search(settings, no_op, validate_mysql_uri)
119 return error_status[0]
122 def validate_docker_service(filename, tpl):
123 if 'outputs' in tpl and 'role_data' in tpl['outputs']:
124 if 'value' not in tpl['outputs']['role_data']:
125 print('ERROR: invalid role_data for filename: %s'
128 role_data = tpl['outputs']['role_data']['value']
130 for section_name in REQUIRED_DOCKER_SECTIONS:
131 if section_name not in role_data:
132 print('ERROR: %s is required in role_data for %s.'
133 % (section_name, filename))
136 for section_name in role_data.keys():
137 if section_name in REQUIRED_DOCKER_SECTIONS:
140 if section_name in OPTIONAL_DOCKER_SECTIONS:
143 print('ERROR: %s is extra in role_data for %s.'
144 % (section_name, filename))
147 if 'puppet_config' in role_data:
148 puppet_config = role_data['puppet_config']
149 for key in puppet_config:
150 if key in REQUIRED_DOCKER_PUPPET_CONFIG_SECTIONS:
153 if key in OPTIONAL_DOCKER_PUPPET_CONFIG_SECTIONS:
156 print('ERROR: %s should not be in puppet_config section.'
159 for key in REQUIRED_DOCKER_PUPPET_CONFIG_SECTIONS:
160 if key not in puppet_config:
161 print('ERROR: %s is required in puppet_config for %s.'
165 if 'parameters' in tpl:
166 for param in required_params:
167 if param not in tpl['parameters']:
168 print('ERROR: parameter %s is required for %s.'
174 def validate_service(filename, tpl):
175 if 'outputs' in tpl and 'role_data' in tpl['outputs']:
176 if 'value' not in tpl['outputs']['role_data']:
177 print('ERROR: invalid role_data for filename: %s'
180 role_data = tpl['outputs']['role_data']['value']
181 if 'service_name' not in role_data:
182 print('ERROR: service_name is required in role_data for %s.'
185 # service_name must match the filename, but with an underscore
186 if (role_data['service_name'] !=
187 os.path.basename(filename).split('.')[0].replace("-", "_")):
188 print('ERROR: service_name should match file name for service: %s.'
191 # if service connects to mysql, the uri should use option
192 # bind_address to avoid issues with VIP failover
193 if 'config_settings' in role_data and \
194 validate_mysql_connection(role_data['config_settings']):
195 print('ERROR: mysql connection uri should use option bind_address')
197 if 'parameters' in tpl:
198 for param in required_params:
199 if param not in tpl['parameters']:
200 print('ERROR: parameter %s is required for %s.'
206 def validate(filename):
207 print('Validating %s' % filename)
210 tpl = yaml.load(open(filename).read())
212 # The template alias version should be used instead a date, this validation
213 # will be applied to all templates not just for those in the services folder.
214 if 'heat_template_version' in tpl and not str(tpl['heat_template_version']).isalpha():
215 print('ERROR: heat_template_version needs to be the release alias not a date: %s'
219 # qdr aliases rabbitmq service to provide alternative messaging backend
220 if (filename.startswith('./puppet/services/') and
221 filename not in ['./puppet/services/services.yaml',
222 './puppet/services/qdr.yaml']):
223 retval = validate_service(filename, tpl)
225 if (filename.startswith('./docker/services/') and
226 filename != './docker/services/services.yaml'):
227 retval = validate_docker_service(filename, tpl)
229 if filename.endswith('hyperconverged-ceph.yaml'):
230 retval = validate_hci_compute_services_default(filename, tpl)
233 print(traceback.format_exc())
235 # yaml is OK, now walk the parameters and output a warning for unused ones
236 if 'heat_template_version' in tpl:
237 for p in tpl.get('parameters', {}):
238 if p in required_params:
241 in_resources = str_p in str(tpl.get('resources', {}))
242 in_outputs = str_p in str(tpl.get('outputs', {}))
243 if not in_resources and not in_outputs:
244 print('Warning: parameter %s in template %s '
245 'appears to be unused' % (p, filename))
249 if len(sys.argv) < 2:
252 path_args = sys.argv[1:]
255 base_endpoint_map = None
256 env_endpoint_maps = list()
258 for base_path in path_args:
259 if os.path.isdir(base_path):
260 for subdir, dirs, files in os.walk(base_path):
262 if f.endswith('.yaml') and not f.endswith('.j2.yaml'):
263 file_path = os.path.join(subdir, f)
264 failed = validate(file_path)
266 failed_files.append(file_path)
268 if f == ENDPOINT_MAP_FILE:
269 base_endpoint_map = get_base_endpoint_map(file_path)
270 if f in envs_containing_endpoint_map:
271 env_endpoint_map = get_endpoint_map_from_env(file_path)
273 env_endpoint_maps.append(env_endpoint_map)
274 elif os.path.isfile(base_path) and base_path.endswith('.yaml'):
275 failed = validate(base_path)
277 failed_files.append(base_path)
280 print('Unexpected argument %s' % base_path)
283 if base_endpoint_map and \
284 len(env_endpoint_maps) == len(envs_containing_endpoint_map):
285 for env_endpoint_map in env_endpoint_maps:
286 matches = validate_endpoint_map(base_endpoint_map,
287 env_endpoint_map['map'])
289 print("ERROR: %s needs to be updated to match changes in base "
290 "endpoint map" % env_endpoint_map['file'])
291 failed_files.append(env_endpoint_map['file'])
294 print("%s matches base endpoint map" % env_endpoint_map['file'])
296 print("ERROR: Can't validate endpoint maps since a file is missing. "
297 "If you meant to delete one of these files you should update this "
299 if not base_endpoint_map:
300 failed_files.append(ENDPOINT_MAP_FILE)
301 if len(env_endpoint_maps) != len(envs_containing_endpoint_map):
302 matched_files = set(os.path.basename(matched_env_file['file'])
303 for matched_env_file in env_endpoint_maps)
304 failed_files.extend(set(envs_containing_endpoint_map) - matched_files)
308 print('Validation failed on:')
309 for f in failed_files:
312 print('Validation successful!')