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 if (filename.startswith('./puppet/services/') and
216 filename != './puppet/services/services.yaml'):
217 retval = validate_service(filename, tpl)
219 if (filename.startswith('./docker/services/') and
220 filename != './docker/services/services.yaml'):
221 retval = validate_docker_service(filename, tpl)
223 if filename.endswith('hyperconverged-ceph.yaml'):
224 retval = validate_hci_compute_services_default(filename, tpl)
227 print(traceback.format_exc())
229 # yaml is OK, now walk the parameters and output a warning for unused ones
230 if 'heat_template_version' in tpl:
231 for p in tpl.get('parameters', {}):
232 if p in required_params:
235 in_resources = str_p in str(tpl.get('resources', {}))
236 in_outputs = str_p in str(tpl.get('outputs', {}))
237 if not in_resources and not in_outputs:
238 print('Warning: parameter %s in template %s '
239 'appears to be unused' % (p, filename))
243 if len(sys.argv) < 2:
246 path_args = sys.argv[1:]
249 base_endpoint_map = None
250 env_endpoint_maps = list()
252 for base_path in path_args:
253 if os.path.isdir(base_path):
254 for subdir, dirs, files in os.walk(base_path):
256 if f.endswith('.yaml') and not f.endswith('.j2.yaml'):
257 file_path = os.path.join(subdir, f)
258 failed = validate(file_path)
260 failed_files.append(file_path)
262 if f == ENDPOINT_MAP_FILE:
263 base_endpoint_map = get_base_endpoint_map(file_path)
264 if f in envs_containing_endpoint_map:
265 env_endpoint_map = get_endpoint_map_from_env(file_path)
267 env_endpoint_maps.append(env_endpoint_map)
268 elif os.path.isfile(base_path) and base_path.endswith('.yaml'):
269 failed = validate(base_path)
271 failed_files.append(base_path)
274 print('Unexpected argument %s' % base_path)
277 if base_endpoint_map and \
278 len(env_endpoint_maps) == len(envs_containing_endpoint_map):
279 for env_endpoint_map in env_endpoint_maps:
280 matches = validate_endpoint_map(base_endpoint_map,
281 env_endpoint_map['map'])
283 print("ERROR: %s needs to be updated to match changes in base "
284 "endpoint map" % env_endpoint_map['file'])
285 failed_files.append(env_endpoint_map['file'])
288 print("%s matches base endpoint map" % env_endpoint_map['file'])
290 print("ERROR: Can't validate endpoint maps since a file is missing. "
291 "If you meant to delete one of these files you should update this "
293 if not base_endpoint_map:
294 failed_files.append(ENDPOINT_MAP_FILE)
295 if len(env_endpoint_maps) != len(envs_containing_endpoint_map):
296 matched_files = set(os.path.basename(matched_env_file['file'])
297 for matched_env_file in env_endpoint_maps)
298 failed_files.extend(set(envs_containing_endpoint_map) - matched_files)
302 print('Validation failed on:')
303 for f in failed_files:
306 print('Validation successful!')