Skip to content

Instantly share code, notes, and snippets.

@lingo
Created June 12, 2014 01:32
Show Gist options
  • Save lingo/b7054fb70a1074a39c37 to your computer and use it in GitHub Desktop.
Save lingo/b7054fb70a1074a39c37 to your computer and use it in GitHub Desktop.
Suspend all running vagrant machines
#!/usr/bin/python
"""
Suspend all running vagrant machines, based on what is found in
user's ~/.vagrant.d files
Usage:
vagrant_suspend_all
"""
from json import load as loadJSON
import sys
import os
index = None
fh = None
machines = None
home = os.path.expanduser('~')
# Default path, you may have to alter this for different vagrant versions
# or OSs.
index_path = '%s/.vagrant.d/data/machine-index/index' % home
# Load index file and parse
try:
fh = open(index_path, 'r')
index = loadJSON(fh)
fh.close()
except:
print "Exception while parsing and processing %s" % index_path
print sys.exc_info()
sys.exit(1)
index = index['machines']
# Find running machines and map to local path
machines = [index[k]['local_data_path'] for k in index if index[k]['state'] == 'running']
for path in machines:
print "Suspending vagrant machine for dir %s" % path
os.chdir(path)
os.system('vagrant suspend')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment