Skip to content

Instantly share code, notes, and snippets.

@steverobbins
Last active August 29, 2016 19:49
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 steverobbins/f33e0d864e897b265c495383f7ad41d4 to your computer and use it in GitHub Desktop.
Save steverobbins/f33e0d864e897b265c495383f7ad41d4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import subprocess
import sys
import re
bin_vbox = '/usr/local/bin/VBoxManage'
box = 'archiveteam-warrior-2'
def main():
state = getState()
print 'Box "%s" is in state "%s"' % (box, state)
if state == 'running':
print 'Already running, nothing to do.'
elif state == 'powered off' or state == 'saved' or state == 'aborted':
print 'Starting box...'
print shell('%s startvm %s --type headless' % (bin_vbox, box))
else:
print 'Not implemented'
sys.exit(1)
def shell(cmd):
return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout.read().strip()
def getState():
output = shell('%s showvminfo %s | grep State' % (bin_vbox, box))
match = re.search(r'State: +(.*) \(', output)
if match:
return match.group(1)
print 'Failed to retrieve state of box "%s"' % box
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment