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 DOCKER_PUPPET_CONFIG_SECTIONS = ['config_volume', 'puppet_tags', 'step_config',
36 print('Usage %s <yaml file or directory>' % sys.argv[0])
40 def get_base_endpoint_map(filename):
42 tpl = yaml.load(open(filename).read())
43 return tpl['parameters']['EndpointMap']['default']
45 print(traceback.format_exc())
49 def get_endpoint_map_from_env(filename):
51 tpl = yaml.load(open(filename).read())
54 'map': tpl['parameter_defaults']['EndpointMap']
57 print(traceback.format_exc())
61 def validate_endpoint_map(base_map, env_map):
62 return sorted(base_map.keys()) == sorted(env_map.keys())
65 def validate_hci_compute_services_default(env_filename, env_tpl):
66 env_services_list = env_tpl['parameter_defaults']['ComputeServices']
67 env_services_list.remove('OS::TripleO::Services::CephOSD')
68 roles_filename = os.path.join(os.path.dirname(env_filename),
70 roles_tpl = yaml.load(open(roles_filename).read())
71 for role in roles_tpl:
72 if role['name'] == 'Compute':
73 roles_services_list = role['ServicesDefault']
74 if sorted(env_services_list) != sorted(roles_services_list):
75 print('ERROR: ComputeServices in %s is different '
76 'from ServicesDefault in roles_data.yaml' % env_filename)
81 def validate_mysql_connection(settings):
82 no_op = lambda *args: False
85 def mysql_protocol(items):
86 return items == ['EndpointMap', 'MysqlInternal', 'protocol']
88 def client_bind_address(item):
89 return 'read_default_file' in item and \
90 'read_default_group' in item
92 def validate_mysql_uri(key, items):
93 # Only consider a connection if it targets mysql
94 if key.endswith('connection') and \
95 search(items, mysql_protocol, no_op):
96 # Assume the "bind_address" option is one of
97 # the token that made up the uri
98 if not search(items, client_bind_address, no_op):
102 def search(item, check_item, check_key):
105 elif isinstance(item, list):
107 if search(i, check_item, check_key):
109 elif isinstance(item, dict):
110 for k in item.keys():
111 if check_key(k, item[k]):
113 elif search(item[k], check_item, check_key):
117 search(settings, no_op, validate_mysql_uri)
118 return error_status[0]
121 def validate_docker_service(filename, tpl):
122 if 'outputs' in tpl and 'role_data' in tpl['outputs']:
123 if 'value' not in tpl['outputs']['role_data']:
124 print('ERROR: invalid role_data for filename: %s'
127 role_data = tpl['outputs']['role_data']['value']
129 for section_name in REQUIRED_DOCKER_SECTIONS:
130 if section_name not in role_data:
131 print('ERROR: %s is required in role_data for %s.'
132 % (section_name, filename))
135 for section_name in role_data.keys():
136 if section_name in REQUIRED_DOCKER_SECTIONS:
139 if section_name in OPTIONAL_DOCKER_SECTIONS:
142 print('ERROR: %s is extra in role_data for %s.'
143 % (section_name, filename))
146 if 'puppet_config' in role_data:
147 puppet_config = role_data['puppet_config']
148 for key in puppet_config:
149 if key in DOCKER_PUPPET_CONFIG_SECTIONS:
152 print('ERROR: %s should not be in puppet_config section.'
155 for key in DOCKER_PUPPET_CONFIG_SECTIONS:
156 if key not in puppet_config:
157 print('ERROR: %s is required in puppet_config for %s.'
161 if 'parameters' in tpl:
162 for param in required_params:
163 if param not in tpl['parameters']:
164 print('ERROR: parameter %s is required for %s.'
170 def validate_service(filename, tpl):
171 if 'outputs' in tpl and 'role_data' in tpl['outputs']:
172 if 'value' not in tpl['outputs']['role_data']:
173 print('ERROR: invalid role_data for filename: %s'
176 role_data = tpl['outputs']['role_data']['value']
177 if 'service_name' not in role_data:
178 print('ERROR: service_name is required in role_data for %s.'
181 # service_name must match the filename, but with an underscore
182 if (role_data['service_name'] !=
183 os.path.basename(filename).split('.')[0].replace("-", "_")):
184 print('ERROR: service_name should match file name for service: %s.'
187 # if service connects to mysql, the uri should use option
188 # bind_address to avoid issues with VIP failover
189 if 'config_settings' in role_data and \
190 validate_mysql_connection(role_data['config_settings']):
191 print('ERROR: mysql connection uri should use option bind_address')
193 if 'parameters' in tpl:
194 for param in required_params:
195 if param not in tpl['parameters']:
196 print('ERROR: parameter %s is required for %s.'
202 def validate(filename):
203 print('Validating %s' % filename)
206 tpl = yaml.load(open(filename).read())
208 # The template alias version should be used instead a date, this validation
209 # will be applied to all templates not just for those in the services folder.
210 if 'heat_template_version' in tpl and not str(tpl['heat_template_version']).isalpha():
211 print('ERROR: heat_template_version needs to be the release alias not a date: %s'
215 # qdr aliases rabbitmq service to provide alternative messaging backend
216 if (filename.startswith('./puppet/services/') and
217 filename not in ['./puppet/services/services.yaml',
218 './puppet/services/qdr.yaml']):
219 retval = validate_service(filename, tpl)
221 if (filename.startswith('./docker/services/') and
222 filename != './docker/services/services.yaml'):
223 retval = validate_docker_service(filename, tpl)
225 if filename.endswith('hyperconverged-ceph.yaml'):
226 retval = validate_hci_compute_services_default(filename, tpl)
229 print(traceback.format_exc())
231 # yaml is OK, now walk the parameters and output a warning for unused ones
232 if 'heat_template_version' in tpl:
233 for p in tpl.get('parameters', {}):
234 if p in required_params:
237 in_resources = str_p in str(tpl.get('resources', {}))
238 in_outputs = str_p in str(tpl.get('outputs', {}))
239 if not in_resources and not in_outputs:
240 print('Warning: parameter %s in template %s '
241 'appears to be unused' % (p, filename))
245 if len(sys.argv) < 2:
248 path_args = sys.argv[1:]
251 base_endpoint_map = None
252 env_endpoint_maps = list()
254 for base_path in path_args:
255 if os.path.isdir(base_path):
256 for subdir, dirs, files in os.walk(base_path):
258 if f.endswith('.yaml') and not f.endswith('.j2.yaml'):
259 file_path = os.path.join(subdir, f)
260 failed = validate(file_path)
262 failed_files.append(file_path)
264 if f == ENDPOINT_MAP_FILE:
265 base_endpoint_map = get_base_endpoint_map(file_path)
266 if f in envs_containing_endpoint_map:
267 env_endpoint_map = get_endpoint_map_from_env(file_path)
269 env_endpoint_maps.append(env_endpoint_map)
270 elif os.path.isfile(base_path) and base_path.endswith('.yaml'):
271 failed = validate(base_path)
273 failed_files.append(base_path)
276 print('Unexpected argument %s' % base_path)
279 if base_endpoint_map and \
280 len(env_endpoint_maps) == len(envs_containing_endpoint_map):
281 for env_endpoint_map in env_endpoint_maps:
282 matches = validate_endpoint_map(base_endpoint_map,
283 env_endpoint_map['map'])
285 print("ERROR: %s needs to be updated to match changes in base "
286 "endpoint map" % env_endpoint_map['file'])
287 failed_files.append(env_endpoint_map['file'])
290 print("%s matches base endpoint map" % env_endpoint_map['file'])
292 print("ERROR: Can't validate endpoint maps since a file is missing. "
293 "If you meant to delete one of these files you should update this "
295 if not base_endpoint_map:
296 failed_files.append(ENDPOINT_MAP_FILE)
297 if len(env_endpoint_maps) != len(envs_containing_endpoint_map):
298 matched_files = set(os.path.basename(matched_env_file['file'])
299 for matched_env_file in env_endpoint_maps)
300 failed_files.extend(set(envs_containing_endpoint_map) - matched_files)
304 print('Validation failed on:')
305 for f in failed_files:
308 print('Validation successful!')