labcalcoloctl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #!/usr/bin/python
  2. ##Author: Elisa Aliverti
  3. ##Last edit: May 2019 - nicolopalazzini
  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(description="Simple tool to handle LabCalcolo's VMs.", usage=sys.argv[0]+' {'+','.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.4',
  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 vmstatus(self):
  63. statuscmd = "ps aux | grep qemu | grep -v grep"
  64. ssh = self.sshcommand(statuscmd)
  65. result = [ l for l in ssh.stdout.readlines() if 'qemu' in l ]
  66. if result == []:
  67. return False
  68. else:
  69. return True
  70. def sshcommand(self, command):
  71. if self.up:
  72. ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
  73. return ssh
  74. else:
  75. print self.hostname + ' is not up.'
  76. return False
  77. def vmstart(self):
  78. if self.up:
  79. if not self.running:
  80. startcmd = main_path + " 1"
  81. self.sshcommand(startcmd)
  82. print 'VM is now starting on ' + self.hostname
  83. else:
  84. print 'VM is already running on ' + self.hostname
  85. else:
  86. print self.hostname + ' is not up.'
  87. def vmstop(self):
  88. if self.up:
  89. if self.running:
  90. stopcmd = main_path + " 0"
  91. self.sshcommand(stopcmd)
  92. else:
  93. print self.hostname + ' is not up.'
  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.up:
  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. # Show usage if no arguments
  152. if len(sys.argv) < 2:
  153. parser.print_usage()
  154. print "\nSimple tool to handle LabCalcolo's VMs."
  155. print '\nTry: "'+sys.argv[0]+' --help" to display help message.'
  156. sys.exit(1)
  157. # Filter hostlist according to arguments
  158. if args.lcm:
  159. nodes = Hosts
  160. elif args.lcm1:
  161. nodes = [ host for host in Hosts if host.location == 'LCM1' ]
  162. elif args.lcm2:
  163. nodes = [ host for host in Hosts if host.location == 'LCM2' ]
  164. elif args.node:
  165. nodes = [ j for j in Hosts if j.hostname in args.node ]
  166. # Start the threads and run commands on nodes
  167. for n in nodes :
  168. n.start()
  169. running=[]
  170. down=[]
  171. num=len(nodes)
  172. index=0
  173. print ' Querying ' + str(num) + ' hosts...'
  174. for i in nodes:
  175. # Rejoin them when their work is done
  176. i.join()
  177. if i.running:
  178. running.append(i.hostname)
  179. if not i.up :
  180. down.append(i.hostname)
  181. index += 1
  182. print_progressbar(index, num)
  183. # New line after progress bar
  184. print '\n Done... (%(t).3f s)' % {'t': (time() - start)}
  185. if args.cmd == 'status':
  186. if len(running):
  187. print "VM(s) running on:"
  188. for i in running : print '\t',i
  189. else :
  190. print "No VMs are running"
  191. if len(down):
  192. print "Down nodes:"
  193. for i in down : print '\t',i
  194. #########################################################################################################
  195. ## Since lcm2 nodes have no VMs installed a control is added to make sure operator knows what he's doing.
  196. ## When future (or present?) admins will fix the issue this part should be deleted.
  197. elif args.cmd == 'start':
  198. yes = {'yes','y','ye'}
  199. no = {'no','n'}
  200. control = True
  201. parsed = False
  202. for n in nodes:
  203. if n.location == 'LCM2' and parsed == False :
  204. print 'You are trying to start a VM on lcm2 nodes. Are you sure you want to continue?'
  205. while True:
  206. choice = raw_input().lower()
  207. if choice in yes:
  208. parsed = True
  209. break
  210. elif choice in no:
  211. control = False
  212. parsed = True
  213. break
  214. else:
  215. sys.stdout.write("Be kind, respond with 'yes' or 'no'")
  216. if control == True:
  217. for i in nodes: i.vmstart()
  218. #########################################################################################################
  219. ## Uncomment this part when the lcm2 VMs issue will be fixed.
  220. #elif args.cmd == 'start':
  221. # for i in nodes: i.vmstart()
  222. #########################################################################################################
  223. elif args.cmd == 'stop':
  224. for i in nodes: i.vmstop()
  225. elif args.cmd == 'doctor' :
  226. for i in nodes : i.vmdoctor()