labcalcoloctl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/python
  2. ##Author: Elisa Aliverti
  3. ##Last edit: 14/02/2017 - Silva
  4. from time import time
  5. import argparse
  6. import textwrap
  7. import os
  8. import subprocess, sys
  9. from threading import Thread
  10. parser = argparse.ArgumentParser(usage='labcalcolo [{status,start,stop}] [options]')
  11. ## positional arguments
  12. parser.add_argument( 'cmd', nargs="?", choices=('status','start','stop'), default='status',
  13. help='Specify command to execute, default is \'status\'' )
  14. ## optional arguments
  15. parser.add_argument( '-a', '--all', action='store_true', dest='lcm',
  16. help='All LCM nodes are considered' )
  17. parser.add_argument( '-n', nargs='+', dest='node',
  18. help='Select one or more nodes (at least one)' )
  19. parser.add_argument( '-1', '--lcm1', action='store_true', dest='lcm1',
  20. help='LCM1 nodes are considered' )
  21. parser.add_argument( '-2', '--lcm2', action='store_true', dest='lcm2',
  22. help='LCM2 nodes are considered' )
  23. parser.add_argument( '-v', '--version', action='version', version='%(prog)s 1.3',
  24. help='Print program version' )
  25. ##
  26. args = parser.parse_args()
  27. def progress_bar(index, num) :
  28. sys.stdout.write('\r ['
  29. + '='*index
  30. + '>'*(1-int(index/num))
  31. + ' '*(num-index-1) + ']')
  32. sys.stdout.flush()
  33. ## main path (you don't say)
  34. main_path = "/var/etc/vmctl"
  35. class Host(Thread):
  36. # Constructor
  37. def __init__(self, name, location):
  38. # Fork the thread first thing
  39. Thread.__init__(self)
  40. # Variables initialization
  41. self.hostname = name
  42. self.location = location
  43. # Ping the host to see if it's up
  44. def isup(self):
  45. # Is the host up?
  46. ping = os.popen("ping -w1 -c1 " + self.hostname, "r")
  47. # print("pinging " self.hostname)
  48. if "0 received" in ping.read():
  49. return False
  50. else:
  51. return True
  52. def sshcommand(self, command):
  53. if self.isup():
  54. ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command],
  55. shell = False,
  56. stdout = subprocess.PIPE,
  57. stderr = subprocess.PIPE )
  58. else:
  59. print self.hostname + ' is not up.'
  60. def vmstart(self):
  61. if not self.vmstatus():
  62. startcmd = main_path + " 1"
  63. self.sshcommand(startcmd)
  64. print 'VM is now starting on ' + self.hostname
  65. else:
  66. print 'VM is already running on ' + self.hostname
  67. def vmstop(self):
  68. stopcmd = main_path + " 0"
  69. self.sshcommand(stopcmd)
  70. def vmstatus(self):
  71. statuscmd = "ps aux | grep qemu | grep -v grep"
  72. if self.isup():
  73. ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, statuscmd],
  74. shell = False,
  75. stdout = subprocess.PIPE,
  76. stderr = subprocess.PIPE )
  77. result = [ l for l in ssh.stdout.readlines() if 'qemu' in l ]
  78. if result == []:
  79. return False
  80. else:
  81. return True
  82. else:
  83. return -1
  84. ### end class Host
  85. ## Host list
  86. Hosts = [
  87. Host('abe', 'LCM1'),
  88. Host('crash', 'LCM1'),
  89. Host('duke', 'LCM1'),
  90. Host('glados', 'LCM1'),
  91. Host('lara', 'LCM1'),
  92. Host('link', 'LCM1'),
  93. Host('king', 'LCM1'),
  94. Host('pang', 'LCM1'),
  95. Host('pong', 'LCM1'),
  96. Host('snake', 'LCM1'),
  97. Host('sonic', 'LCM1'),
  98. Host('spyro', 'LCM1'),
  99. Host('yoshi', 'LCM1'),
  100. Host('actarus', 'LCM2'),
  101. Host('elwood', 'LCM2'),
  102. Host('gex', 'LCM2'),
  103. Host('gin', 'LCM2'),
  104. Host('jake', 'LCM2'),
  105. Host('kirk', 'LCM2'),
  106. Host('martini', 'LCM2'),
  107. Host('picard', 'LCM2'),
  108. Host('q', 'LCM2'),
  109. Host('raziel', 'LCM2'),
  110. Host('sarek', 'LCM2'),
  111. Host('spock', 'LCM2'),
  112. Host('tron', 'LCM2'),
  113. Host('worf', 'LCM2'),
  114. Host('zombie', 'LCM2')
  115. ]
  116. nodes = []
  117. # Filter hostlist according to arguments
  118. if args.lcm:
  119. nodes = Hosts
  120. elif args.lcm1:
  121. nodes = [ host for host in Hosts if host.location == 'LCM1' ]
  122. elif args.lcm2:
  123. nodes = [ host for host in Hosts if host.location == 'LCM2' ]
  124. elif args.node:
  125. for i in args.node:
  126. for j in Hosts:
  127. if (i==j.hostname): nodes.append(j)
  128. # Run commands on nodes
  129. running=[]
  130. down=[]
  131. if args.cmd == 'status':
  132. start = time()
  133. num=len(nodes)
  134. index=0
  135. print ' Querying ' + str(num) + ' hosts...'
  136. for i in nodes:
  137. if i.vmstatus():
  138. running.append(i.hostname)
  139. if i.vmstatus()<0 :
  140. down.append(i.hostname)
  141. index += 1
  142. progress_bar(index, num)
  143. # New line after progress bar
  144. print '\n Done... (%(t).3f s)' % {'t': (time() - start)}
  145. if len(running):
  146. print "VM running on:"
  147. for i in running : print '\t',i
  148. else :
  149. print "No VMs are running"
  150. if len(down):
  151. print "Down nodes:"
  152. for i in down : print '\t',i
  153. elif args.cmd == 'start':
  154. for i in nodes: i.vmstart()
  155. elif args.cmd == 'stop':
  156. for i in nodes: i.vmstop()