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
21 print('Usage %s <yaml file or directory>' % sys.argv[0])
25 def validate_service(filename, tpl):
26 if 'outputs' in tpl and 'role_data' in tpl['outputs']:
27 if 'value' not in tpl['outputs']['role_data']:
28 print('ERROR: invalid role_data for filename: %s'
31 role_data = tpl['outputs']['role_data']['value']
32 if 'service_name' not in role_data:
33 print('ERROR: service_name is required in role_data for %s.'
36 # service_name must match the filename, but with an underscore
37 if (role_data['service_name'] !=
38 os.path.basename(filename).split('.')[0].replace("-", "_")):
39 print('ERROR: service_name should match file name for service: %s.'
45 def validate(filename):
46 print('Validating %s' % filename)
49 tpl = yaml.load(open(filename).read())
51 if (filename.startswith('./puppet/services/') and
52 filename != './puppet/services/services.yaml'):
53 retval = validate_service(filename, tpl)
56 print(traceback.format_exc())
58 # yaml is OK, now walk the parameters and output a warning for unused ones
59 for p in tpl.get('parameters', {}):
61 in_resources = str_p in str(tpl.get('resources', {}))
62 in_outputs = str_p in str(tpl.get('outputs', {}))
63 if not in_resources and not in_outputs:
64 print('Warning: parameter %s in template %s appears to be unused'
72 path_args = sys.argv[1:]
76 for base_path in path_args:
77 if os.path.isdir(base_path):
78 for subdir, dirs, files in os.walk(base_path):
80 if f.endswith('.yaml'):
81 file_path = os.path.join(subdir, f)
82 failed = validate(file_path)
84 failed_files.append(file_path)
86 elif os.path.isfile(base_path) and base_path.endswith('.yaml'):
87 failed = validate(base_path)
89 failed_files.append(base_path)
92 print('Unexpected argument %s' % base_path)
96 print('Validation failed on:')
97 for f in failed_files:
100 print('Validation successful!')