Skip to content

Instantly share code, notes, and snippets.

@tai271828
Created November 24, 2017 06:48
Show Gist options
  • Save tai271828/0252743a0c3ce90be3c1c1c860a9d75d to your computer and use it in GitHub Desktop.
Save tai271828/0252743a0c3ce90be3c1c1c860a9d75d to your computer and use it in GitHub Desktop.
Batch start vm by virsh
#!/usr/bin/env python
#
# Batch start vm by virsh
#
# usage:
# ./virsh-batch-start-vm.py <domain1> [<domain2> ... <domainN>]
#
#
import sys
import threading
import Queue
import commands
import time
# thread class to run a command
class ExampleThread(threading.Thread):
def __init__(self, cmd, queue):
threading.Thread.__init__(self)
self.cmd = cmd
self.queue = queue
def run(self):
# execute the command, queue the result
(status, output) = commands.getstatusoutput(self.cmd)
self.queue.put((self.cmd, output, status))
# queue where results are placed
result_queue = Queue.Queue()
cmds = []
for domain in sys.argv[1:]:
cmd = "virsh start %s" % (str(domain))
cmds.append(cmd)
for cmd in cmds:
thread = ExampleThread(cmd, result_queue)
thread.start()
# print results as we get them
while threading.active_count() > 1 or not result_queue.empty():
while not result_queue.empty():
(cmd, output, status) = result_queue.get()
print('%s:' % cmd)
print(output)
print('='*60)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment