|
@@ -0,0 +1,180 @@
|
|
|
+#!/usr/bin/python
|
|
|
+
|
|
|
+##Author: Elisa Aliverti
|
|
|
+##Last edit: 14/02/2017 - Silva
|
|
|
+
|
|
|
+from time import time
|
|
|
+import argparse
|
|
|
+import textwrap
|
|
|
+import os
|
|
|
+import subprocess, sys
|
|
|
+from threading import Thread
|
|
|
+
|
|
|
+parser = argparse.ArgumentParser(usage='labcalcolo [{status,start,stop}] [options]')
|
|
|
+
|
|
|
+## positional arguments
|
|
|
+parser.add_argument( 'cmd', nargs="?", choices=('status','start','stop'), default='status',
|
|
|
+ help='Specify command to execute, default is \'status\'' )
|
|
|
+
|
|
|
+## optional arguments
|
|
|
+parser.add_argument( '-a', '--all', action='store_true', dest='lcm',
|
|
|
+ help='All LCM nodes are considered' )
|
|
|
+parser.add_argument( '-n', nargs='+', dest='node',
|
|
|
+ help='Select one or more nodes (at least one)' )
|
|
|
+parser.add_argument( '-1', '--lcm1', action='store_true', dest='lcm1',
|
|
|
+ help='LCM1 nodes are considered' )
|
|
|
+parser.add_argument( '-2', '--lcm2', action='store_true', dest='lcm2',
|
|
|
+ help='LCM2 nodes are considered' )
|
|
|
+parser.add_argument( '-v', '--version', action='version', version='%(prog)s 1.3',
|
|
|
+ help='Print program version' )
|
|
|
+##
|
|
|
+args = parser.parse_args()
|
|
|
+
|
|
|
+def progress_bar(index, num) :
|
|
|
+ sys.stdout.write('\r ['
|
|
|
+ + '='*index
|
|
|
+ + '>'*(1-int(index/num))
|
|
|
+ + ' '*(num-index-1) + ']')
|
|
|
+ sys.stdout.flush()
|
|
|
+
|
|
|
+
|
|
|
+## main path (you don't say)
|
|
|
+main_path = "/var/etc/vmctl"
|
|
|
+
|
|
|
+class Host(Thread):
|
|
|
+ # Constructor
|
|
|
+ def __init__(self, name, location):
|
|
|
+ # Fork the thread first thing
|
|
|
+ Thread.__init__(self)
|
|
|
+ # Variables initialization
|
|
|
+ self.hostname = name
|
|
|
+ self.location = location
|
|
|
+
|
|
|
+ # Ping the host to see if it's up
|
|
|
+ def isup(self):
|
|
|
+ # Is the host up?
|
|
|
+ ping = os.popen("ping -w1 -c1 " + self.hostname, "r")
|
|
|
+ # print("pinging " self.hostname)
|
|
|
+ if "0 received" in ping.read():
|
|
|
+ return False
|
|
|
+ else:
|
|
|
+ return True
|
|
|
+
|
|
|
+ def sshcommand(self, command):
|
|
|
+ if self.isup():
|
|
|
+ ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command],
|
|
|
+ shell = False,
|
|
|
+ stdout = subprocess.PIPE,
|
|
|
+ stderr = subprocess.PIPE )
|
|
|
+ else:
|
|
|
+ print self.hostname + ' is not up.'
|
|
|
+
|
|
|
+ def vmstart(self):
|
|
|
+ if not self.vmstatus():
|
|
|
+ startcmd = main_path + " 1"
|
|
|
+ self.sshcommand(startcmd)
|
|
|
+ print 'VM is now starting on ' + self.hostname
|
|
|
+ else:
|
|
|
+ print 'VM is already running on ' + self.hostname
|
|
|
+
|
|
|
+ def vmstop(self):
|
|
|
+ stopcmd = main_path + " 0"
|
|
|
+ self.sshcommand(stopcmd)
|
|
|
+
|
|
|
+ def vmstatus(self):
|
|
|
+ statuscmd = "ps aux | grep qemu | grep -v grep"
|
|
|
+ if self.isup():
|
|
|
+ ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, statuscmd],
|
|
|
+ shell = False,
|
|
|
+ stdout = subprocess.PIPE,
|
|
|
+ stderr = subprocess.PIPE )
|
|
|
+ result = [ l for l in ssh.stdout.readlines() if 'qemu' in l ]
|
|
|
+ if result == []:
|
|
|
+ return False
|
|
|
+ else:
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ return -1
|
|
|
+### end class Host
|
|
|
+
|
|
|
+## Host list
|
|
|
+Hosts = [
|
|
|
+ Host('abe', 'LCM1'),
|
|
|
+ Host('crash', 'LCM1'),
|
|
|
+ Host('duke', 'LCM1'),
|
|
|
+ Host('glados', 'LCM1'),
|
|
|
+ Host('lara', 'LCM1'),
|
|
|
+ Host('link', 'LCM1'),
|
|
|
+ Host('king', 'LCM1'),
|
|
|
+ Host('pang', 'LCM1'),
|
|
|
+ Host('pong', 'LCM1'),
|
|
|
+ Host('snake', 'LCM1'),
|
|
|
+ Host('sonic', 'LCM1'),
|
|
|
+ Host('spyro', 'LCM1'),
|
|
|
+ Host('yoshi', 'LCM1'),
|
|
|
+ Host('actarus', 'LCM2'),
|
|
|
+ Host('elwood', 'LCM2'),
|
|
|
+ Host('gex', 'LCM2'),
|
|
|
+ Host('gin', 'LCM2'),
|
|
|
+ Host('jake', 'LCM2'),
|
|
|
+ Host('kirk', 'LCM2'),
|
|
|
+ Host('martini', 'LCM2'),
|
|
|
+ Host('picard', 'LCM2'),
|
|
|
+ Host('q', 'LCM2'),
|
|
|
+ Host('raziel', 'LCM2'),
|
|
|
+ Host('sarek', 'LCM2'),
|
|
|
+ Host('spock', 'LCM2'),
|
|
|
+ Host('tron', 'LCM2'),
|
|
|
+ Host('worf', 'LCM2'),
|
|
|
+ Host('zombie', 'LCM2')
|
|
|
+]
|
|
|
+
|
|
|
+nodes = []
|
|
|
+
|
|
|
+# Filter hostlist according to arguments
|
|
|
+if args.lcm:
|
|
|
+ nodes = Hosts
|
|
|
+elif args.lcm1:
|
|
|
+ nodes = [ host for host in Hosts if host.location == 'LCM1' ]
|
|
|
+elif args.lcm2:
|
|
|
+ nodes = [ host for host in Hosts if host.location == 'LCM2' ]
|
|
|
+elif args.node:
|
|
|
+ for i in args.node:
|
|
|
+ for j in Hosts:
|
|
|
+ if (i==j.hostname): nodes.append(j)
|
|
|
+
|
|
|
+
|
|
|
+# Run commands on nodes
|
|
|
+running=[]
|
|
|
+down=[]
|
|
|
+if args.cmd == 'status':
|
|
|
+ start = time()
|
|
|
+ num=len(nodes)
|
|
|
+ index=0
|
|
|
+ print ' Querying ' + str(num) + ' hosts...'
|
|
|
+ for i in nodes:
|
|
|
+ if i.vmstatus():
|
|
|
+ running.append(i.hostname)
|
|
|
+ if i.vmstatus()<0 :
|
|
|
+ down.append(i.hostname)
|
|
|
+ index += 1
|
|
|
+ progress_bar(index, num)
|
|
|
+ # New line after progress bar
|
|
|
+ print '\n Done... (%(t).3f s)' % {'t': (time() - start)}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if len(running):
|
|
|
+ print "VM running on:"
|
|
|
+ for i in running : print '\t',i
|
|
|
+ else :
|
|
|
+ print "No VMs are running"
|
|
|
+ if len(down):
|
|
|
+ print "Down nodes:"
|
|
|
+ for i in down : print '\t',i
|
|
|
+
|
|
|
+elif args.cmd == 'start':
|
|
|
+ for i in nodes: i.vmstart()
|
|
|
+elif args.cmd == 'stop':
|
|
|
+ for i in nodes: i.vmstop()
|
|
|
+
|