Skip to content

Instantly share code, notes, and snippets.

@flyte
Created November 11, 2014 12:38
Show Gist options
  • Save flyte/b010156776a470304ffb to your computer and use it in GitHub Desktop.
Save flyte/b010156776a470304ffb to your computer and use it in GitHub Desktop.
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)
@kelfish
Copy link

kelfish commented Feb 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment