Esempio n. 1
0
class Role(AbstractRole):
  
	def __init__(self, main_instance):
		
		AbstractRole.__init__(self, main_instance)
		self.virt_co = libvirt.open(Config.libvirt_uri)
		self.has_run = False
		self.queue = Queue.Queue()
		self.pool = Pool(Config.ulteo_pool_name,self.virt_co)
		self.virtual_machine = {}
		self.network = Network(Config.network_name, self.virt_co)
		self.webserver = None
	
	
	"""
	Check if the storage pool exist, if not create it, if yes, reload it 
	Check if the virtual machine network exist, if not create it
	Reload virtual machine in the list of VM
	Init the HTTP server use for communication between hypervisor and virtuals machines
	"""
	def init(self):
	  
		if not self.pool.exist():
			self.pool.create(Config.ulteo_pool_path)
		else:
			self.pool.reload()
			
		if not self.network.exist():
			self.network.create()
			
		self.reload_vm()
		
		self.webserver = HttpServer2((Config.lan, Config.port), self)
			
		return True
	
	
	@staticmethod
	def getName():
		return "Hypervisor"
	
	
	"""
	When role is stopped, all running virtuals machines are stopped
	and the http server is closed
	"""
	def force_stop(self):
		AbstractRole.force_stop(self)
		
		for vm in self.virtual_machine :
			vm = self.virtual_machine[vm]
			
			if vm.getStatus() == "RUNNING" :
				vm.shutdown()
				
		self.webserver.server_close()
	
	
	def finalize(self):
		Logger.info("Hypervisor:: stopping")
	
	
	def run(self):
		
		self.has_run = True
		
		self.status = Role.STATUS_RUNNING
		
		self.webserver.serve_forever()
		
		while self.loop:
			if self.status == Role.STATUS_STOPPING:
				break
			
			try:
				(request, obj) = self.queue.get(True, 4)
			except Queue.Empty, err:
				continue
			# This error is ue to the sigterm sended by the init script
			except TypeError:
				return
			except (EOFError, socket.error):
				return