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', 'kolla_config',
27 'puppet_config', 'config_settings', 'step_config']
28 OPTIONAL_DOCKER_SECTIONS = ['docker_puppet_tasks', 'upgrade_tasks',
29 'service_config_settings']
30 DOCKER_PUPPET_CONFIG_SECTIONS = ['config_volume', 'puppet_tags', 'step_config',
35 print('Usage %s <yaml file or directory>' % sys.argv[0])
39 def get_base_endpoint_map(filename):
41 tpl = yaml.load(open(filename).read())
42 return tpl['parameters']['EndpointMap']['default']
44 print(traceback.format_exc())
48 def get_endpoint_map_from_env(filename):
50 tpl = yaml.load(open(filename).read())
53 'map': tpl['parameter_defaults']['EndpointMap']
56 print(traceback.format_exc())
60 def validate_endpoint_map(base_map, env_map):
61 return sorted(base_map.keys()) == sorted(env_map.keys())
64 def validate_hci_compute_services_default(env_filename, env_tpl):
65 env_services_list = env_tpl['parameter_defaults']['ComputeServices']
66 env_services_list.remove('OS::TripleO::Services::CephOSD')
67 roles_filename = os.path.join(os.path.dirname(env_filename),
69 roles_tpl = yaml.load(open(roles_filename).read())
70 for role in roles_tpl:
71 if role['name'] == 'Compute':
72 roles_services_list = role['ServicesDefault']
73 if sorted(env_services_list) != sorted(roles_services_list):
74 print('ERROR: ComputeServices in %s is different '
75 'from ServicesDefault in roles_data.yaml' % env_filename)
80 def validate_mysql_connection(settings):
81 no_op = lambda *args: False
84 def mysql_protocol(items):
85 return items == ['EndpointMap', 'MysqlInternal', 'protocol']
87 def client_bind_address(item):
88 return 'read_default_file' in item and \
89 'read_default_group' in item
91 def validate_mysql_uri(key, items):
92 # Only consider a connection if it targets mysql
93 if key.endswith('connection') and \
94 search(items, mysql_protocol, no_op):
95 # Assume the "bind_address" option is one of
96 # the token that made up the uri
97 if not search(items, client_bind_address, no_op):
101 def search(item, check_item, check_key):
104 elif isinstance(item, list):
106 if search(i, check_item, check_key):
108 elif isinstance(item, dict):
109 for k in item.keys():
110 if check_key(k, item[k]):
112 elif search(item[k], check_item, check_key):
116 search(settings, no_op, validate_mysql_uri)
117 return error_status[0]
120 def validate_docker_service(filename, tpl):
121 if 'outputs' in tpl and 'role_data' in tpl['outputs']:
122 if 'value' not in tpl['outputs']['role_data']:
123 print('ERROR: invalid role_data for filename: %s'
126 role_data = tpl['outputs']['role_data']['value']
128 for section_name in REQUIRED_DOCKER_SECTIONS:
129 if section_name not in role_data:
130 print('ERROR: %s is required in role_data for %s.'
131 % (section_name, filename))
134 for section_name in role_data.keys():
135 if section_name in REQUIRED_DOCKER_SECTIONS:
138 if section_name in OPTIONAL_DOCKER_SECTIONS:
141 print('ERROR: %s is extra in role_data for %s.'
142 % (section_name, filename))
145 if 'puppet_config' in role_data:
146 puppet_config = role_data['puppet_config']
147 for key in puppet_config:
148 if key in DOCKER_PUPPET_CONFIG_SECTIONS:
151 print('ERROR: %s should not be in puppet_config section.'
154 for key in DOCKER_PUPPET_CONFIG_SECTIONS:
155 if key not in puppet_config:
156 print('ERROR: %s is required in puppet_config for %s.'
160 if 'parameters' in tpl:
161 for param in required_params:
162 if param not in tpl['parameters']:
163 print('ERROR: parameter %s is required for %s.'
169 def validate_service(filename, tpl):
170 if 'outputs' in tpl and 'role_data' in tpl['outputs']:
171 if 'value' not in tpl['outputs']['role_data']:
172 print('ERROR: invalid role_data for filename: %s'
175 role_data = tpl['outputs']['role_data']['value']
176 if 'service_name' not in role_data:
177 print('ERROR: service_name is required in role_data for %s.'
180 # service_name must match the filename, but with an underscore
181 if (role_data['service_name'] !=
182 os.path.basename(filename).split('.')[0].replace("-", "_")):
183 print('ERROR: service_name should match file name for service: %s.'
186 # if service connects to mysql, the uri should use option
187 # bind_address to avoid issues with VIP failover
188 if 'config_settings' in role_data and \
189 validate_mysql_connection(role_data['config_settings']):
190 print('ERROR: mysql connection uri should use option bind_address')
192 if 'parameters' in tpl:
193 for param in required_params:
194 if param not in tpl['parameters']:
195 print('ERROR: parameter %s is required for %s.'
201 def validate(filename):
202 print('Validating %s' % filename)
205 tpl = yaml.load(open(filename).read())
207 # The template alias version should be used instead a date, this validation
208 # will be applied to all templates not just for those in the services folder.
209 if 'heat_template_version' in tpl and not str(tpl['heat_template_version']).isalpha():
210 print('ERROR: heat_template_version needs to be the release alias not a date: %s'
214 if (filename.startswith('./puppet/services/') and
215 filename != './puppet/services/services.yaml'):
216 retval = validate_service(filename, tpl)
218 if (filename.startswith('./docker/services/') and
219 filename != './docker/services/services.yaml'):
220 retval = validate_docker_service(filename, tpl)
222 if filename.endswith('hyperconverged-ceph.yaml'):
223 retval = validate_hci_compute_services_default(filename, tpl)
226 print(traceback.format_exc())
228 # yaml is OK, now walk the parameters and output a warning for unused ones
229 if 'heat_template_version' in tpl:
230 for p in tpl.get('parameters', {}):
231 if p in required_params:
234 in_resources = str_p in str(tpl.get('resources', {}))
235 in_outputs = str_p in str(tpl.get('outputs', {}))
236 if not in_resources and not in_outputs:
237 print('Warning: parameter %s in template %s '
238 'appears to be unused' % (p, filename))
242 if len(sys.argv) < 2:
245 path_args = sys.argv[1:]
248 base_endpoint_map = None
249 env_endpoint_maps = list()
251 for base_path in path_args:
252 if os.path.isdir(base_path):
253 for subdir, dirs, files in os.walk(base_path):
255 if f.endswith('.yaml') and not f.endswith('.j2.yaml'):
256 file_path = os.path.join(subdir, f)
257 failed = validate(file_path)
259 failed_files.append(file_path)
261 if f == ENDPOINT_MAP_FILE:
262 base_endpoint_map = get_base_endpoint_map(file_path)
263 if f in envs_containing_endpoint_map:
264 env_endpoint_map = get_endpoint_map_from_env(file_path)
266 env_endpoint_maps.append(env_endpoint_map)
267 elif os.path.isfile(base_path) and base_path.endswith('.yaml'):
268 failed = validate(base_path)
270 failed_files.append(base_path)
273 print('Unexpected argument %s' % base_path)
276 if base_endpoint_map and \
277 len(env_endpoint_maps) == len(envs_containing_endpoint_map):
278 for env_endpoint_map in env_endpoint_maps:
279 matches = validate_endpoint_map(base_endpoint_map,
280 env_endpoint_map['map'])
282 print("ERROR: %s needs to be updated to match changes in base "
283 "endpoint map" % env_endpoint_map['file'])
284 failed_files.append(env_endpoint_map['file'])
287 print("%s matches base endpoint map" % env_endpoint_map['file'])
289 print("ERROR: Can't validate endpoint maps since a file is missing. "
290 "If you meant to delete one of these files you should update this "
292 if not base_endpoint_map:
293 failed_files.append(ENDPOINT_MAP_FILE)
294 if len(env_endpoint_maps) != len(envs_containing_endpoint_map):
295 matched_files = set(os.path.basename(matched_env_file['file'])
296 for matched_env_file in env_endpoint_maps)
297 failed_files.extend(set(envs_containing_endpoint_map) - matched_files)
301 print('Validation failed on:')
302 for f in failed_files:
305 print('Validation successful!')