Skip to content

Instantly share code, notes, and snippets.

@fliphess
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fliphess/4b82eafff2534d2ad027 to your computer and use it in GitHub Desktop.
Save fliphess/4b82eafff2534d2ad027 to your computer and use it in GitHub Desktop.
test all puppet recipes
#!/usr/bin/env python
import sys
import time
import os.path
import subprocess
import multiprocessing.pool as mpool
puppet = '/usr/bin/puppet'
results = []
start = time.time()
def check_puppet_syntax(filename):
command = '%s parser validate %s' % (puppet, filename)
child = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = child.communicate()[0]
status = { 'exit': child.returncode, 'out': output, 'filename': filename}
return status
def get_puppet_recipes(directory):
puppet_files = []
for root, dirs, files in os.walk(directory):
for i in files:
if i.endswith('.pp'):
puppet_files.append(os.path.join(root, i))
return puppet_files
def callback(output):
results.append(output)
def make_results():
errcount = 0
exec_time = format(time.time() - start, '.4f')
for item in results:
if item['exit'] == 0:
continue
else:
errcount += 1
print "Errors detected in %s:\n%s\n" % (item['filename'], item['out'])
print "=" * 50
if errcount == 0:
print "All OKAY! - No syntax errors detected! - done in %s " % exec_time
return 0
else:
print "%d Errors detected! - done in %ss" % (errcount, exec_time)
return 1
def main():
if not len(sys.argv) > 1:
print "Usage: %s DIR" % sys.argv[0]
sys.exit(1)
directory = sys.argv[1]
puppet_files = get_puppet_recipes(directory)
draadjes = 10
print "Syntax check on all .pp files"
print "Checking %s puppet recipes with %s threads\n[Please Wait!]" % (len(puppet_files), draadjes)
print "=" * 50
pool = mpool.ThreadPool(draadjes)
for recipe in puppet_files:
pool.apply_async(check_puppet_syntax, args=(recipe,), callback=callback)
pool.close()
pool.join()
sys.exit(make_results())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment