labcalcolo.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/python
  2. ##Author: Elisa Aliverti
  3. ##Last modified: 20/09/2016 - andreatsh - version 1.3
  4. 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. ## main path (you don't say)
  28. main_path = "/var/etc/vmctl"
  29. class Host(Thread):
  30. # Constructor
  31. def __init__(self, name, location):
  32. # Fork the thread first thing
  33. Thread.__init__(self)
  34. # Variables initialization
  35. self.hostname = name
  36. self.location = location
  37. # Ping the host to see if it's up
  38. def isup(self):
  39. # Is the host up?
  40. ping = os.popen("ping -w1 -c1 " + self.hostname, "r")
  41. # print("pinging " self.hostname)
  42. if "0 received" in ping.read():
  43. return False
  44. else:
  45. return True
  46. def sshcommand(self, command):
  47. if self.isup():
  48. ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command],
  49. shell = False,
  50. stdout = subprocess.PIPE,
  51. stderr = subprocess.PIPE )
  52. else:
  53. print self.hostname + ' is not up.'
  54. def vmstart(self):
  55. if not self.vmstatus():
  56. startcmd = main_path + " 1"
  57. self.sshcommand(startcmd)
  58. print 'VM is now starting on ' + self.hostname
  59. else:
  60. print 'VM is already running on ' + self.hostname
  61. def vmstop(self):
  62. stopcmd = main_path + " 0"
  63. self.sshcommand(stopcmd)
  64. def vmstatus(self):
  65. statuscmd = "ps aux | grep qemu | grep -v grep"
  66. if self.isup():
  67. ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, statuscmd],
  68. shell = False,
  69. stdout = subprocess.PIPE,
  70. stderr = subprocess.PIPE )
  71. result = ssh.stdout.readlines()
  72. if result == []:
  73. return False
  74. else:
  75. return True
  76. else:
  77. print self.hostname + ' is not up.'
  78. ### end class Host
  79. ## Host list
  80. Hosts = [
  81. Host('abe', 'LCM1'),
  82. Host('crash', 'LCM1'),
  83. Host('duke', 'LCM1'),
  84. Host('glados', 'LCM1'),
  85. Host('lara', 'LCM1'),
  86. Host('link', 'LCM1'),
  87. Host('king', 'LCM1'),
  88. Host('pang', 'LCM1'),
  89. Host('pong', 'LCM1'),
  90. Host('snake', 'LCM1'),
  91. Host('sonic', 'LCM1'),
  92. Host('spyro', 'LCM1'),
  93. Host('yoshi', 'LCM1'),
  94. Host('actarus', 'LCM2'),
  95. Host('elwood', 'LCM2'),
  96. Host('gex', 'LCM2'),
  97. Host('gin', 'LCM2'),
  98. Host('jake', 'LCM2'),
  99. Host('kirk', 'LCM2'),
  100. Host('martini', 'LCM2'),
  101. Host('picard', 'LCM2'),
  102. Host('q', 'LCM2'),
  103. Host('raziel', 'LCM2'),
  104. Host('sarek', 'LCM2'),
  105. Host('spock', 'LCM2'),
  106. Host('tron', 'LCM2'),
  107. Host('worf', 'LCM2'),
  108. Host('zombie', 'LCM2')
  109. ]
  110. nodes = []
  111. # Filter hostlist according to arguments
  112. if args.lcm:
  113. nodes = Hosts
  114. elif args.lcm1:
  115. nodes = [ host for host in Hosts if host.location == 'LCM1' ]
  116. elif args.lcm2:
  117. nodes = [ host for host in Hosts if host.location == 'LCM2' ]
  118. elif args.node:
  119. for i in args.node:
  120. for j in Hosts:
  121. if (i==j.hostname): nodes.append(j)
  122. # Run commands on nodes
  123. if args.cmd == 'status':
  124. for i in nodes:
  125. if i.vmstatus():
  126. print("VM is running on %s" % i.hostname)
  127. else :
  128. print("VM is not running on %s" % i.hostname)
  129. elif args.cmd == 'start':
  130. for i in nodes: i.vmstart()
  131. elif args.cmd == 'stop':
  132. for i in nodes: i.vmstop()