Browse Source

Merge branch 'python3' of lcmstaff/labcalcoloctl into master

After some more in-depth testing, I rule the port was successful. Merging with master.
Alessandro De Gennaro 4 years ago
parent
commit
b02b9cdda3
1 changed files with 27 additions and 27 deletions
  1. 27 27
      labcalcoloctl

+ 27 - 27
labcalcoloctl

@@ -1,7 +1,7 @@
-#!/usr/bin/python
+#!/usr/bin/env python3
 
 ##Author: Elisa Aliverti
-##Last edit: May 2019 - nicolopalazzini
+##Last edit: January 2020 - Matteo Bonamassa
 
 from time import time
 start = time()
@@ -28,7 +28,7 @@ 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.4',
+parser.add_argument( '-v', '--version', action='version', version='%(prog)s 2.0',
                      help='Print program version' )
 ##
 args = parser.parse_args()
@@ -82,10 +82,10 @@ class Host(Thread):
 
 	def sshcommand(self, command):
 		if self.up:
-			ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
+			ssh = subprocess.Popen( ["ssh", "%s" % self.hostname, command], shell = False, stdout = subprocess.PIPE, stderr = subprocess.PIPE, universal_newlines=True )
 			return ssh
 		else:
-			print self.hostname + ' is not up.'
+			print(self.hostname + ' is not up.')
 			return False
 
 	def vmstart(self):
@@ -93,11 +93,11 @@ class Host(Thread):
 			if not self.running:
 				startcmd = main_path + " 1"
 				self.sshcommand(startcmd)
-				print 'VM is now starting on ' + self.hostname
+				print('VM is now starting on ' + self.hostname)
 			else:
-				print 'VM is already running on ' + self.hostname
+				print('VM is already running on ' + self.hostname)
 		else:
-			print self.hostname + ' is not up.'
+			print(self.hostname + ' is not up.')
 
 
 	def vmstop(self):
@@ -105,11 +105,11 @@ class Host(Thread):
 			if self.running:
 				stopcmd = main_path + " 0"
 				self.sshcommand(stopcmd)
-				print 'VM stopped on ' + self.hostname
+				print('VM stopped on ' + self.hostname)
 			else:
-				print 'VM is not running on ' + self.hostname
+				print('VM is not running on ' + self.hostname)
 		else:
-			print self.hostname + ' is not up.'
+			print(self.hostname + ' is not up.')
 
 	def vmdoctor(self) :
 		to_search=['qemu', 'spicec']
@@ -122,21 +122,21 @@ class Host(Thread):
 
 			qemu_status = [ l for l in ssh if 'qemu' in l ] # Filter for a single command
 			if len(qemu_status) :
-				print "Qemu command running on", self.hostname+":\n", qemu_status
+				print("Qemu command running on", self.hostname+":\n", *qemu_status)
 			else :
-				print "Qemu is not running on", self.hostname
+				print("Qemu is not running on", self.hostname)
 
 			spicec_status = [ l for l in ssh if 'spicec' in l ]
 			if not len(spicec_status) and len(qemu_status) :
-				print "Spicec is not running on", self.hostname,
-				if raw_input("Do you want to start it now? [y/n] ")=="y" :
+				print("Spicec is not running on", self.hostname)
+				if input("Do you want to start it now? [y/n] ")=="y" :
 					pw=getpass("Vm password: ")
 					spiceccmd="export DISPLAY=:4 ; spicec -f -h 127.0.0.1 -p 5900 -w "+pw+" &"
 					self.sshcommand(spiceccmd) # How to check if everything went as expected?
 					# Check if everything works fine now
 					spicec_status = [ l for l in self.sshcommand(status_query).stdout.readlines() if 'spicec' in l ]
 			if len(spicec_status) :
-				print "Spicec command running on", self.hostname+": ", spicec_status
+				print("Spicec command running on", self.hostname+": ", *spicec_status)
 
 ### end class Host
 
@@ -177,8 +177,8 @@ nodes = []
 # Show usage if no arguments
 if len(sys.argv) < 2:
 	parser.print_usage()
-	print "\nSimple tool to handle LabCalcolo's VMs."
-	print '\nTry: "labcalcoloctl --help" to display help message.'
+	print("\nSimple tool to handle LabCalcolo's VMs.")
+	print('\nTry: "labcalcoloctl --help" to display help message.')
 	sys.exit(1)
 
 # Filter hostlist according to arguments
@@ -200,7 +200,7 @@ down=[]
 
 num=len(nodes)
 index=0
-print ' Querying ' + str(num) + ' hosts...'
+print(' Querying ' + str(num) + ' hosts...')
 for i in nodes:
 	# Rejoin them when their work is done
 	i.join()
@@ -211,18 +211,18 @@ for i in nodes:
 	index += 1
 	print_progressbar(index, num)
 # New line after progress bar
-print '\n Done... (%(t).3f s)' % {'t': (time() - start)}
+print('\n Done... (%(t).3f s)' % {'t': (time() - start)})
 
 
 if args.cmd == 'status':
     if len(running):
-        print "VM(s) running on:"
-        for i in running : print '\t',i
+        print("VM(s) running on:")
+        for i in running : print('\t',i)
     else :
-        print "No VMs are running"
+        print("No VMs are running")
     if len(down):
-        print "Down nodes:"
-        for i in down :  print '\t',i
+        print("Down nodes:")
+        for i in down :  print('\t',i)
 
 #########################################################################################################
 ## Since lcm2 nodes have no VMs installed a control is added to make sure operator knows what he's doing.
@@ -235,9 +235,9 @@ elif args.cmd == 'start':
 	parsed = False
 	for n in nodes:
 		if n.location == 'LCM2' and parsed == False :
-			print 'You are trying to start a VM on lcm2 nodes. Are you sure you want to continue? [yes/no]'
+			print('You are trying to start a VM on lcm2 nodes. Are you sure you want to continue? [yes/no]')
 			while True:
-				choice = raw_input().lower()
+				choice = input().lower()
 				if choice in yes:
 					parsed = True
 					break