Esempio n. 1
0
	def migrateRun(self, host=None):
		if self.state == State.CREATED:
			self._unassignHost()
			self._unassignVmid()
			return
		task = tasks.get_current_task()
		#save src data
		src_host = self.host
		src_vmid = self.getVmid()
		self.setAttribute("migration", {src_host.name: src_vmid})
		#assign new host and vmid
		self.host = None
		self.setVmid(None)
		if host:
			self.host = host
		else:
			self._assignHost()
		self._assignVmid()
		dst_host = self.host
		dst_vmid = self.getVmid()
		self.setAttribute("migration", {src_host.name: src_vmid, dst_host.name: dst_vmid})
		#reassign host and vmid
		self.host = src_host
		self.setVmid(src_vmid)
		#destroy all connectors and save their state
		constates={}
		for iface in self.interfaceSetAll():
			if iface.isConnected():
				con = iface.connection.connector.upcast()
				if con.name in constates:
					continue
				constates[con.name] = con.state
				if con.state == State.STARTED:
					con.stop(True, noProcess=True)
				if con.state == State.PREPARED:
					con.destroy(True, noProcess=True)
		tasks.set_current_task(task)
		#actually migrate the vm
		if self.state == State.STARTED:
			self._stopVnc()
			try:
				self.state = State.PREPARED
				self._triggerConnections()
			finally:
				self.state = State.STARTED
		ifaces = map(lambda x: x.name, self.interfaceSetAll())
		try:
			vzctl.migrate(src_host, src_vmid, dst_host, dst_vmid, self.getTemplate(), ifaces)
		except:
			# reverted to SRC host
			if self.state == State.STARTED:
				self._assignVncPort()
				self._startVnc()
			raise
		#switch host and vmid
		self.host = dst_host
		self.setVmid(dst_vmid)
		src_host.giveId("vmid", src_vmid)
		self.save()
		self.deleteAttribute("migration")
		self._configureVm()
		if self.state == State.STARTED:
			self._assignVncPort()
			self._startVnc()
		#redeploy all connectors
		for iface in self.interfaceSetAll():
			if iface.isConnected():
				con = iface.connection.connector.upcast()
				if not con.name in constates:
					continue
				state = constates[con.name]
				del constates[con.name]
				if state == State.PREPARED or state == State.STARTED:
					con.prepare(True, noProcess=True)
				if state == State.STARTED:
					con.start(True, noProcess=True)
				if self.state == State.STARTED:
					iface.upcast().connectToBridge()
Esempio n. 2
0
File: openvz.py Progetto: m3z/ToMaTo
	def migrateRun(self, host=None):
		if self.state == State.CREATED:
			self._unassignHost()
			self._unassignVmid()
			return
		task = tasks.get_current_task()
		#save src data
		src_host = self.host
		src_vmid = resources.get(self, self.VMID_SLOT)
		#assign new host and vmid
		self.host = None
		if host:
			self.host = host
		else:
			self._assignHost()
		dst_host = self.host
		dst_vmid = resources.take(dst_host, "vmid", self, "migration")
		#reassign host and vmid
		self.host = src_host
		#destroy all connectors and save their state
		constates={}
		for iface in self.interfaceSetAll():
			if iface.isConnected():
				con = iface.connection.connector.upcast()
				if con.name in constates:
					continue
				constates[con.name] = con.state
				if con.state == State.STARTED:
					con.stop(True, noProcess=True)
				if con.state == State.PREPARED:
					con.destroy(True, noProcess=True)
		tasks.set_current_task(task)
		#actually migrate the vm
		if self.state == State.STARTED:
			self._stopVnc()
			try:
				self.state = State.PREPARED
				for iface in self.interfaceSetAll():
					if iface.isConnected():
						con = iface.connection.upcast()
						con.destroyBridge()
			finally:
				self.state = State.STARTED
		ifaces = map(lambda x: x.name, self.interfaceSetAll())
		try:
			vzctl.migrate(src_host, src_vmid.num, dst_host, dst_vmid.num, self.getTemplate(), ifaces)
		except:
			# reverted to SRC host
			if self.state == State.STARTED:
				self._startVnc()
			raise
		#switch host and vmid
		self.host = dst_host
		self.vmid = dst_vmid
		self.save()
		resources.give(self, self.VMID_SLOT)
		self.vmid.slot = self.VMID_SLOT
		self.vmid.save()
		self.save()
		self._configureVm()
		if self.state == State.STARTED:
			self._startVnc()			
		#redeploy all connectors
		for iface in self.interfaceSetAll():
			if iface.isConnected():
				con = iface.connection.connector.upcast()
				if not con.name in constates:
					continue
				state = constates[con.name]
				del constates[con.name]
				if state == State.PREPARED or state == State.STARTED:
					con.prepare(True, noProcess=True)
				if state == State.STARTED:
					con.start(True, noProcess=True)
				if self.state == State.STARTED:
					self.connectToBridge(iface, self.getBridge(iface))