labcalcoloctl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/python
  2. ##Author: Elisa Aliverti
  3. ##Last edit: 22/02/2017 - Silva
  4. from time import time
  5. start = time()
  6. import argparse
  7. import textwrap
  8. import os
  9. import subprocess, sys
  10. from threading import Thread
  11. from getpass import getpass
  12. choices = ('status','start','stop', 'doctor')
  13. parser = argparse.ArgumentParser(usage='labcalcolo {'+','.join(choices)+'} [options]')
  14. ## positional arguments
  15. parser.add_argument( 'cmd', nargs="?", choices=choices, default='status',
  16. help='Specify command to execute, default is \'status\'' )
  17. ## optional arguments
  18. parser.add_argument( '-a', '--all', action='store_true', dest='lcm',
  19. help='All LCM nodes are considered' )
  20. parser.add_argument( '-n', nargs='+', dest='node',
  21. help='Select one or more nodes (at least one)' )
  22. parser.add_argument( '-1', '--lcm1', action='store_true', dest='lcm1',
  23. help='LCM1 nodes are considered' )
  24. parser.add_argument( '-2', '--lcm2', action='store_true', dest='lcm2',
  25. help='LCM2 nodes are considered' )
  26. parser.add_argument( '-v', '--version', action='version', version='%(prog)s 1.3',
  27. help='Print program version' )
  28. ##
  29. args = parser.parse_args()
  30. def print_progressbar(index, num) :
  31. sys.stdout.write('\r ['
  32. + '='*index
  33. + '>'*(1-int(index/num))
  34. + ' '*(num-index-1) + ']')
  35. sys.stdout.flush()
  36. ## main path (you don't say)
  37. main_path = "/var/etc/vmctl"
  38. class Host(Thread):
  39. # Constructor
  40. def __init__(self, name, location):
  41. # Fork the thread first thing
  42. Thread.__init__(self)
  43. # Variables initialization
  44. self.hostname = name
  45. self.location = location
  46. self.running = False
  47. self.up = False
  48. # Run method called on Thread start. Check if host is up and if is running a VM
  49. def run(self) :
  50. if self.isup() :
  51. self.up = True
  52. self.running=self.vmstatus()
  53. # Ping the host to see if it's up
  54. def isup(self):
  55. # Is the host up?
  56. ping = os.popen("ping -w1 -c1 " + self.hostname, "r")
  57. # print("pinging " self.hostname)
  58. if "0 received" in ping.read():
  59. return False
  60. else:
  61. return True
  62. def sshcommand(self, command):
  63. if self.isup():
  64. ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command],
  65. shell = False,
  66. stdout = subprocess.PIPE,
  67. stderr = subprocess.PIPE )
  68. return ssh
  69. else:
  70. print self.hostname + ' is not up.'
  71. return False
  72. def vmstart(self):
  73. if not self.vmstatus():
  74. startcmd = main_path + " 1"
  75. self.sshcommand(startcmd)
  76. print 'VM is now starting on ' + self.hostname
  77. else:
  78. print 'VM is already running on ' + self.hostname
  79. def vmstop(self):
  80. stopcmd = main_path + " 0"
  81. self.sshcommand(stopcmd)
  82. def vmstatus(self):
  83. statuscmd = "ps aux | grep qemu | grep -v grep"
  84. if self.isup():
  85. ssh = self.sshcommand(statuscmd)
  86. result = [ l for l in ssh.stdout.readlines() if 'qemu' in l ]
  87. if result == []:
  88. self.running=False
  89. else:
  90. self.running=True
  91. else:
  92. self.up=False
  93. return self.running
  94. def vmdoctor(self) :
  95. to_search=['qemu', 'spicec']
  96. # It's ugly (and more or less useless), but it should not be necessary: passwords should not be visible from ps aux
  97. pw_remove=['s/,password=\w*$//', 's/-w \w*$//']
  98. # Build the status query with programs names and relative pw remove strings
  99. status_query="ps aux | grep -E '" + '|'.join(to_search) + "' | grep -v grep | sed '" + ';'.join(pw_remove)+ "'"
  100. if self.isup():
  101. ssh = self.sshcommand(status_query).stdout.readlines()
  102. qemu_status = [ l for l in ssh if 'qemu' in l ] # Filter for a single command
  103. if len(qemu_status) :
  104. print "Qemu command running on", self.hostname+":\n", qemu_status
  105. else :
  106. print "Qemu is not running on", self.hostname
  107. spicec_status = [ l for l in ssh if 'spicec' in l ]
  108. if not len(spicec_status) and len(qemu_status) :
  109. print "Spicec is not running on", self.hostname,
  110. if raw_input("Do you want to start it now? [y/n] ")=="y" :
  111. pw=getpass("Vm password: ")
  112. spiceccmd="export DISPLAY=:4 ; spicec -f -h 127.0.0.1 -p 5900 -w "+pw+" &"
  113. self.sshcommand(spiceccmd) # How to check if everything went as expected?
  114. # Check if everything works fine now
  115. spicec_status = [ l for l in self.sshcommand(status_query).stdout.readlines() if 'spicec' in l ]
  116. if len(spicec_status) :
  117. print "Spicec command running on", self.hostname+": ", spicec_status
  118. ### end class Host
  119. ## Host list
  120. Hosts = [
  121. Host('abe', 'LCM1'),
  122. Host('crash', 'LCM1'),
  123. Host('duke', 'LCM1'),
  124. Host('glados', 'LCM1'),
  125. Host('lara', 'LCM1'),
  126. Host('link', 'LCM1'),
  127. Host('king', 'LCM1'),
  128. Host('pang', 'LCM1'),
  129. Host('pong', 'LCM1'),
  130. Host('snake', 'LCM1'),
  131. Host('sonic', 'LCM1'),
  132. Host('spyro', 'LCM1'),
  133. Host('yoshi', 'LCM1'),
  134. Host('actarus', 'LCM2'),
  135. Host('elwood', 'LCM2'),
  136. Host('gex', 'LCM2'),
  137. Host('gin', 'LCM2'),
  138. Host('jake', 'LCM2'),
  139. Host('kirk', 'LCM2'),
  140. Host('martini', 'LCM2'),
  141. Host('picard', 'LCM2'),
  142. Host('q', 'LCM2'),
  143. Host('raziel', 'LCM2'),
  144. Host('sarek', 'LCM2'),
  145. Host('spock', 'LCM2'),
  146. Host('tron', 'LCM2'),
  147. Host('worf', 'LCM2'),
  148. Host('zombie', 'LCM2')
  149. ]
  150. nodes = []
  151. # Filter hostlist according to arguments
  152. if args.lcm:
  153. nodes = Hosts
  154. elif args.lcm1:
  155. nodes = [ host for host in Hosts if host.location == 'LCM1' ]
  156. elif args.lcm2:
  157. nodes = [ host for host in Hosts if host.location == 'LCM2' ]
  158. elif args.node:
  159. nodes = [ j for j in Hosts if j.hostname in args.node ]
  160. # Start the threads and run commands on nodes
  161. for n in nodes :
  162. n.start()
  163. running=[]
  164. down=[]
  165. if args.cmd == 'status':
  166. num=len(nodes)
  167. index=0
  168. print ' Querying ' + str(num) + ' hosts...'
  169. for i in nodes:
  170. # Rejoin them when their work is done
  171. i.join()
  172. if i.running:
  173. running.append(i.hostname)
  174. if not i.up :
  175. down.append(i.hostname)
  176. index += 1
  177. print_progressbar(index, num)
  178. # New line after progress bar
  179. print '\n Done... (%(t).3f s)' % {'t': (time() - start)}
  180. if len(running):
  181. print "VM(s) running on:"
  182. for i in running : print '\t',i
  183. else :
  184. print "No VMs are running"
  185. if len(down):
  186. print "Down nodes:"
  187. for i in down : print '\t',i
  188. elif args.cmd == 'start':
  189. for i in nodes: i.vmstart()
  190. elif args.cmd == 'stop':
  191. for i in nodes: i.vmstop()
  192. elif args.cmd == 'doctor' :
  193. for i in nodes : i.vmdoctor()