Skip to content

Instantly share code, notes, and snippets.

@jphalip
Last active October 18, 2021 21:54
Show Gist options
  • Select an option

  • Save jphalip/6025225 to your computer and use it in GitHub Desktop.

Select an option

Save jphalip/6025225 to your computer and use it in GitHub Desktop.

Revisions

  1. jphalip revised this gist Oct 29, 2019. No changes.
  2. jphalip revised this gist Sep 4, 2013. 1 changed file with 33 additions and 32 deletions.
    65 changes: 33 additions & 32 deletions poweroff.py
    Original file line number Diff line number Diff line change
    @@ -12,38 +12,39 @@

    def get_running_vms():
    """
    Returns the list of ids for the VMs currently running in VirtualBox.
    """
    output = subprocess.check_output(['VBoxManage', 'list', 'runningvms'])
    lines = output.split('\n')
    vms = []
    for line in lines:
    pattern = re.compile(r'.*{(.*)}')
    match = pattern.match(line)
    if match:
    vms.append(match.group(1))
    return vms
    Returns the list of ids for the VMs currently running in VirtualBox.
    """
    output = subprocess.Popen(['VBoxManage', 'list', 'runningvms'], stdout=subprocess.PIPE).communicate()[0]
    vms = []
    if output is not None:
    lines = output.split('\n')
    for line in lines:
    pattern = re.compile(r'.*{(.*)}')
    match = pattern.match(line)
    if match:
    vms.append(match.group(1))
    return vms


    def poweroff_vm(vm_id):
    """
    Issues a 'poweroff' command to VirtualBox for the given VM.
    """
    print "Powering off VM: %s..." % vm_id
    subprocess.call(['VBoxManage', 'controlvm', vm_id, 'poweroff'])
    """
    Issues a 'poweroff' command to VirtualBox for the given VM.
    """
    print "Powering off VM: %s..." % vm_id
    subprocess.call(['VBoxManage', 'controlvm', vm_id, 'poweroff'])


    def find_machines_dir():
    """
    Walks up from the current path untils it finds a '.vagrant/machines/' directory.
    """
    path = os.getcwd()
    while path != '/':
    machines_dir = os.path.join(path, '.vagrant/machines/')
    if os.path.isdir(machines_dir):
    return machines_dir
    # Try one level up
    path = os.path.abspath(os.path.join(path, os.pardir))
    """
    Walks up from the current path untils it finds a '.vagrant/machines/' directory.
    """
    path = os.getcwd()
    while path != '/':
    machines_dir = os.path.join(path, '.vagrant/machines/')
    if os.path.isdir(machines_dir):
    return machines_dir
    # Try one level up
    path = os.path.abspath(os.path.join(path, os.pardir))

    def find_vm_ids(machines_dir):
    """
    @@ -65,12 +66,12 @@ def find_vm_ids(machines_dir):
    running_vms = get_running_vms()

    if args.all:
    if len(running_vms):
    print "%s VM(s) currently running..." % len(running_vms)
    for vm in running_vms:
    poweroff_vm(vm)
    else:
    print "No VMs are currently running."
    if len(running_vms):
    print "%s VM(s) currently running..." % len(running_vms)
    for vm in running_vms:
    poweroff_vm(vm)
    else:
    print "No VMs are currently running."
    else:
    machines_dir = find_machines_dir()
    if machines_dir:
  3. jphalip revised this gist Jul 30, 2013. 1 changed file with 58 additions and 48 deletions.
    106 changes: 58 additions & 48 deletions poweroff.py
    Original file line number Diff line number Diff line change
    @@ -3,72 +3,82 @@
    import re
    import os
    import subprocess

    try:
    import json
    except ImportError:
    import simplejson as json


    def get_running_vms():
    """
    Returns the list of ids for the VMs currently running in VirtualBox.
    """
    output = subprocess.check_output(['VBoxManage', 'list', 'runningvms'])
    lines = output.split('\n')
    vms = []
    for line in lines:
    pattern = re.compile(r'.*{(.*)}')
    match = pattern.match(line)
    if match:
    vms.append(match.group(1))
    return vms
    Returns the list of ids for the VMs currently running in VirtualBox.
    """
    output = subprocess.check_output(['VBoxManage', 'list', 'runningvms'])
    lines = output.split('\n')
    vms = []
    for line in lines:
    pattern = re.compile(r'.*{(.*)}')
    match = pattern.match(line)
    if match:
    vms.append(match.group(1))
    return vms


    def poweroff_vm(vm_id):
    """
    Issues a 'poweroff' command to VirtualBox for the given VM.
    """
    print "Powering off VM: %s..." % vm_id
    subprocess.call(['VBoxManage', 'controlvm', vm_id, 'poweroff'])


    def find_machines_dir():
    """
    Walks up from the current path untils it finds a '.vagrant/machines/' directory.
    """
    path = os.getcwd()
    while path != '/':
    machines_dir = os.path.join(path, '.vagrant/machines/')
    if os.path.isdir(machines_dir):
    return machines_dir
    # Try one level up
    path = os.path.abspath(os.path.join(path, os.pardir))

    def find_vm_ids(machines_dir):
    """
    Issues a 'poweroff' command to VirtualBox for the given VM.
    """
    print "Powering off VM: %s..." % vm_id
    subprocess.call(['VBoxManage', 'controlvm', vm_id, 'poweroff'])


    def find_dot_vagrant():
    Returns all the VM id files found under the given .vagrant/machines/ directory.
    """
    Walks up from the current path untils it finds a '.vagrant' file.
    """
    path = os.getcwd()
    while path != '/':
    dot_vagrant = os.path.join(path, '.vagrant')
    if os.path.isfile(dot_vagrant):
    return dot_vagrant
    # Try one level up
    path = os.path.abspath(os.path.join(path, os.pardir))


    matches = []
    for root, dirnames, filenames in os.walk(machines_dir):
    for filename in filenames:
    if filename == 'id':
    matches.append(os.path.join(root, filename))
    return matches


    # Parse the command's arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--all', action='store_true')
    args = parser.parse_args()

    running_vms = get_running_vms()

    if args.all:
    if len(running_vms):
    print "%s VM(s) currently running..." % len(running_vms)
    for vm in running_vms:
    poweroff_vm(vm)
    else:
    print "No VMs are currently running."
    if len(running_vms):
    print "%s VM(s) currently running..." % len(running_vms)
    for vm in running_vms:
    poweroff_vm(vm)
    else:
    print "No VMs are currently running."
    else:
    dot_vagrant = find_dot_vagrant()
    if dot_vagrant:
    content = open(dot_vagrant).read()
    dictionary = json.loads(content)
    for key, vm_id in dictionary['active'].items():
    machines_dir = find_machines_dir()
    if machines_dir:
    for vm_id in find_vm_ids(machines_dir):
    vm_id = open(vm_id).read()
    if vm_id in running_vms:
    poweroff_vm(vm_id)
    else:
    print "VM %s is already powered off." % vm_id
    else:
    print "Cannot find any '.vagrant' file..."
    print "Cannot find any '.vagrant/machines/' directory..."
  4. jphalip created this gist Jul 17, 2013.
    74 changes: 74 additions & 0 deletions poweroff.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,74 @@
    #!/usr/bin/env python
    import argparse
    import re
    import os
    import subprocess

    try:
    import json
    except ImportError:
    import simplejson as json


    def get_running_vms():
    """
    Returns the list of ids for the VMs currently running in VirtualBox.
    """
    output = subprocess.check_output(['VBoxManage', 'list', 'runningvms'])
    lines = output.split('\n')
    vms = []
    for line in lines:
    pattern = re.compile(r'.*{(.*)}')
    match = pattern.match(line)
    if match:
    vms.append(match.group(1))
    return vms


    def poweroff_vm(vm_id):
    """
    Issues a 'poweroff' command to VirtualBox for the given VM.
    """
    print "Powering off VM: %s..." % vm_id
    subprocess.call(['VBoxManage', 'controlvm', vm_id, 'poweroff'])


    def find_dot_vagrant():
    """
    Walks up from the current path untils it finds a '.vagrant' file.
    """
    path = os.getcwd()
    while path != '/':
    dot_vagrant = os.path.join(path, '.vagrant')
    if os.path.isfile(dot_vagrant):
    return dot_vagrant
    # Try one level up
    path = os.path.abspath(os.path.join(path, os.pardir))


    # Parse the command's arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('--all', action='store_true')
    args = parser.parse_args()

    running_vms = get_running_vms()

    if args.all:
    if len(running_vms):
    print "%s VM(s) currently running..." % len(running_vms)
    for vm in running_vms:
    poweroff_vm(vm)
    else:
    print "No VMs are currently running."
    else:
    dot_vagrant = find_dot_vagrant()
    if dot_vagrant:
    content = open(dot_vagrant).read()
    dictionary = json.loads(content)
    for key, vm_id in dictionary['active'].items():
    if vm_id in running_vms:
    poweroff_vm(vm_id)
    else:
    print "VM %s is already powered off." % vm_id
    else:
    print "Cannot find any '.vagrant' file..."