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',
21 'RoleName', 'RoleParameters']
23 envs_containing_endpoint_map = ['tls-endpoints-public-dns.yaml',
24 'tls-endpoints-public-ip.yaml',
25 'tls-everywhere-endpoints-dns.yaml']
26 ENDPOINT_MAP_FILE = 'endpoint_map.yaml'
27 REQUIRED_DOCKER_SECTIONS = ['service_name', 'docker_config', 'puppet_config',
28 'config_settings', 'step_config']
29 OPTIONAL_DOCKER_SECTIONS = ['docker_puppet_tasks', 'upgrade_tasks',
30 'service_config_settings', 'host_prep_tasks',
31 'metadata_settings', 'kolla_config']
32 REQUIRED_DOCKER_PUPPET_CONFIG_SECTIONS = ['config_volume', 'step_config',
34 OPTIONAL_DOCKER_PUPPET_CONFIG_SECTIONS = [ 'puppet_tags' ]
38 print('Usage %s <yaml file or directory>' % sys.argv[0])
42 def get_base_endpoint_map(filename):
44 tpl = yaml.load(open(filename).read())
45 return tpl['parameters']['EndpointMap']['default']
47 print(traceback.format_exc())
51 def get_endpoint_map_from_env(filename):
53 tpl = yaml.load(open(filename).read())
56 'map': tpl['parameter_defaults']['EndpointMap']
59 print(traceback.format_exc())
63 def validate_endpoint_map(base_map, env_map):
64 return sorted(base_map.keys()) == sorted(env_map.keys())
67 def validate_hci_compute_services_default(env_filename, env_tpl):
68 env_services_list = env_tpl['parameter_defaults']['ComputeServices']
69 env_services_list.remove('OS::TripleO::Services::CephOSD')
70 roles_filename = os.path.join(os.path.dirname(env_filename),
72 roles_tpl = yaml.load(open(roles_filename).read())
73 for role in roles_tpl:
74 if role['name'] == 'Compute':
75 roles_services_list = role['ServicesDefault']
76 if sorted(env_services_list) != sorted(roles_services_list):
77 print('ERROR: ComputeServices in %s is different '
78 'from ServicesDefault in roles_data.yaml' % env_filename)
83 def validate_mysql_connection(settings):
84 no_op = lambda *args: False
87 def mysql_protocol(items):
88 return items == ['EndpointMap', 'MysqlInternal', 'protocol']
90 def client_bind_address(item):
91 return 'read_default_file' in item and \
92 'read_default_group' in item
94 def validate_mysql_uri(key, items):
95 # Only consider a connection if it targets mysql
96 if key.endswith('connection') and \
97 search(items, mysql_protocol, no_op):
98 # Assume the "bind_address" option is one of
99 # the token that made up the uri
100 if not search(items, client_bind_address, no_op):
104 def search(item, check_item, check_key):
107 elif isinstance(item, list):
109 if search(i, check_item, check_key):
111 elif isinstance(item, dict):
112 for k in item.keys():
113 if check_key(k, item[k]):
115 elif search(item[k], check_item, check_key):
119 search(settings, no_op, validate_mysql_uri)
120 return error_status[0]
123 def validate_docker_service(filename, tpl):
124 if 'outputs' in tpl and 'role_data' in tpl['outputs']:
125 if 'value' not in tpl['outputs']['role_data']:
126 print('ERROR: invalid role_data for filename: %s'
129 role_data = tpl['outputs']['role_data']['value']
131 for section_name in REQUIRED_DOCKER_SECTIONS:
132 if section_name not in role_data:
133 print('ERROR: %s is required in role_data for %s.'
134 % (section_name, filename))
137 for section_name in role_data.keys():
138 if section_name in REQUIRED_DOCKER_SECTIONS:
141 if section_name in OPTIONAL_DOCKER_SECTIONS:
144 print('ERROR: %s is extra in role_data for %s.'
145 % (section_name, filename))
148 if 'puppet_config' in role_data:
149 puppet_config = role_data['puppet_config']
150 for key in puppet_config:
151 if key in REQUIRED_DOCKER_PUPPET_CONFIG_SECTIONS:
154 if key in OPTIONAL_DOCKER_PUPPET_CONFIG_SECTIONS:
157 print('ERROR: %s should not be in puppet_config section.'
160 for key in REQUIRED_DOCKER_PUPPET_CONFIG_SECTIONS:
161 if key not in puppet_config:
162 print('ERROR: %s is required in puppet_config for %s.'
166 if 'parameters' in tpl:
167 for param in required_params:
168 if param not in tpl['parameters']:
169 print('ERROR: parameter %s is required for %s.'
175 def validate_service(filename, tpl):
176 if 'outputs' in tpl and 'role_data' in tpl['outputs']:
177 if 'value' not in tpl['outputs']['role_data']:
178 print('ERROR: invalid role_data for filename: %s'
181 role_data = tpl['outputs']['role_data']['value']
182 if 'service_name' not in role_data:
183 print('ERROR: service_name is required in role_data for %s.'
186 # service_name must match the filename, but with an underscore
187 if (role_data['service_name'] !=
188 os.path.basename(filename).split('.')[0].replace("-", "_")):
189 print('ERROR: service_name should match file name for service: %s.'
192 # if service connects to mysql, the uri should use option
193 # bind_address to avoid issues with VIP failover
194 if 'config_settings' in role_data and \
195 validate_mysql_connection(role_data['config_settings']):
196 print('ERROR: mysql connection uri should use option bind_address')
198 if 'parameters' in tpl:
199 for param in required_params:
200 if param not in tpl['parameters']:
201 print('ERROR: parameter %s is required for %s.'
207 def validate(filename):
208 print('Validating %s' % filename)
211 tpl = yaml.load(open(filename).read())
213 # The template alias version should be used instead a date, this validation
214 # will be applied to all templates not just for those in the services folder.
215 if 'heat_template_version' in tpl and not str(tpl['heat_template_version']).isalpha():
216 print('ERROR: heat_template_version needs to be the release alias not a date: %s'
220 # qdr aliases rabbitmq service to provide alternative messaging backend
221 if (filename.startswith('./puppet/services/') and
222 filename not in ['./puppet/services/services.yaml',
223 './puppet/services/qdr.yaml']):
224 retval = validate_service(filename, tpl)
226 if (filename.startswith('./docker/services/') and
227 filename != './docker/services/services.yaml'):
228 retval = validate_docker_service(filename, tpl)
230 if filename.endswith('hyperconverged-ceph.yaml'):
231 retval = validate_hci_compute_services_default(filename, tpl)
234 print(traceback.format_exc())
236 # yaml is OK, now walk the parameters and output a warning for unused ones
237 if 'heat_template_version' in tpl:
238 for p in tpl.get('parameters', {}):
239 if p in required_params:
242 in_resources = str_p in str(tpl.get('resources', {}))
243 in_outputs = str_p in str(tpl.get('outputs', {}))
244 if not in_resources and not in_outputs:
245 print('Warning: parameter %s in template %s '
246 'appears to be unused' % (p, filename))
250 if len(sys.argv) < 2:
253 path_args = sys.argv[1:]
256 base_endpoint_map = None
257 env_endpoint_maps = list()
259 for base_path in path_args:
260 if os.path.isdir(base_path):
261 for subdir, dirs, files in os.walk(base_path):
263 if f.endswith('.yaml') and not f.endswith('.j2.yaml'):
264 file_path = os.path.join(subdir, f)
265 failed = validate(file_path)
267 failed_files.append(file_path)
269 if f == ENDPOINT_MAP_FILE:
270 base_endpoint_map = get_base_endpoint_map(file_path)
271 if f in envs_containing_endpoint_map:
272 env_endpoint_map = get_endpoint_map_from_env(file_path)
274 env_endpoint_maps.append(env_endpoint_map)
275 elif os.path.isfile(base_path) and base_path.endswith('.yaml'):
276 failed = validate(base_path)
278 failed_files.append(base_path)
281 print('Unexpected argument %s' % base_path)
284 if base_endpoint_map and \
285 len(env_endpoint_maps) == len(envs_containing_endpoint_map):
286 for env_endpoint_map in env_endpoint_maps:
287 matches = validate_endpoint_map(base_endpoint_map,
288 env_endpoint_map['map'])
290 print("ERROR: %s needs to be updated to match changes in base "
291 "endpoint map" % env_endpoint_map['file'])
292 failed_files.append(env_endpoint_map['file'])
295 print("%s matches base endpoint map" % env_endpoint_map['file'])
297 print("ERROR: Can't validate endpoint maps since a file is missing. "
298 "If you meant to delete one of these files you should update this "
300 if not base_endpoint_map:
301 failed_files.append(ENDPOINT_MAP_FILE)
302 if len(env_endpoint_maps) != len(envs_containing_endpoint_map):
303 matched_files = set(os.path.basename(matched_env_file['file'])
304 for matched_env_file in env_endpoint_maps)
305 failed_files.extend(set(envs_containing_endpoint_map) - matched_files)
309 print('Validation failed on:')
310 for f in failed_files:
313 print('Validation successful!')