flyte
11/11/2014 - 12:38 PM

Find all Vagrantfiles and check their status

Find all Vagrantfiles and check their status

from subprocess import check_output
import argparse
import os
import re

BASE_DIR = os.path.abspath(".")
STATUS_LINE_RE = r"^(\w+?)\s+?(\w.+?)\s\((.+?)\)$"

if __name__ == "__main__":
	p = argparse.ArgumentParser()

	vagrantfiles = check_output("find . -type f -name Vagrantfile".split()).split("\n")
	print "Found %d Vagrantfile(s):" % len(vagrantfiles)

	for vf in vagrantfiles:
		if not vf:
			continue
		directory = os.path.dirname(vf)
		os.chdir(os.path.join(BASE_DIR, directory))
		try:
			output = check_output("vagrant status".split()).split("\n")
		except Exception, e:
			print "Error checking vagrant status in dir %s: %s" % (directory, e)
			continue
		try:
			match = re.match(STATUS_LINE_RE, output[2])
			name, status, handler = match.group(1, 2, 3)
		except Exception, e:
			print "Error parsing vagrant status in dir %s: %s" % (directory, output[2])
		print "%s: %s" % (directory, status)