Example #1
0
def delete_server(request, server_id):

	if (not request.user.is_superuser):
        
		return simple.direct_to_template(request,
				template = 'not_admin.html',
				extra_context = {'user':request.user},
		)
	if request.method == 'POST':
		try:
			VTDriver.deleteServer(VTDriver.getServerById(server_id))
			return HttpResponseRedirect(reverse('dashboard'))

		except Exception as e:
			logging.error(e)
			e = HttpUtils.processException(e)
			return simple.direct_to_template(request,
				template = 'servers/delete_server.html',
				extra_context = {'user':request.user, 'exception':e, 'next':reverse("admin_servers")},
				)	
	elif request.method == 'GET':
		return simple.direct_to_template(request,
				template = 'servers/delete_server.html',
				extra_context = {'user':request.user, 'next':reverse("admin_servers"),'object':VTDriver.getServerById(server_id)},
		)
Example #2
0
def list_vms(request, server_id):

	if (not request.user.is_superuser):
        
		return simple.direct_to_template(request,
						template = 'not_admin.html',
						extra_context = {'user':request.user},
					)
	vmProjects = {}
	vmSlices = {}
	try:
		for vm in VTDriver.getVMsInServer(VTDriver.getServerById(server_id)):
			if vm.projectName not in vmProjects:
				vmProjects[vm.projectName] = vm.projectId
			if vm.sliceName not in vmSlices:
				vmSlices[vm.sliceName] = vm.sliceId
	except Exception as e:
		print e
		pass

	server = get_object_or_404(VTServer, pk=server_id)
			
	context = { 'vmProjects': vmProjects, 'vmSlices': vmSlices,'server':server}

	return simple.direct_to_template(
		request,
		template="servers/servers_list_vms.html",
		extra_context=context,
	)
 def forceListActiveVMs(serverID='None', vmID='None'):
     print serverID
     if serverID != 'None':
         server = VTDriver.getServerById(serverID)
         vms = server.getVMs()
     else:
         if vmID != 'None':
             servers = VTDriver.getAllServers()
             vms = list()
             for server in servers:
                 for vm in server.getVMs():
                     print vm.name, vm.id, int(vmID), type(
                         vm.id), vm.id == int(vmID)
                 vms = server.getVMs(id=int(vmID))
                 vmID = vms[0].getUUID()
                 break
             if not vms:
                 raise Exception("VM not Found")
     xmlrpc_server = xmlrpclib.Server(server.getAgentURL())
     vms_info = xmlrpc_server.force_list_active_vms(
         server.getAgentPassword(), vmID)
     updated_vms = list()
     simple_actions = dict()
     for vm in vms:
         if vm.getUUID() in vms_info.keys():
             vm.setState("running")
             vm.save()
             simple_actions[vm.getUUID()] = "running"
         else:
             if vm.getState() in [
                     'deleting...', 'failed', 'on queue', 'unknown'
             ]:
                 child = vm.getChildObject()
                 server = vm.Server.get()
                 #Action.objects.all().filter(objectUUID = vm.uuid).delete()
                 server.deleteVM(vm)
                 # Keep actions table up-to-date after each deletion
                 vm_uuids = [vm.uuid for vm in VirtualMachine.objects.all()]
                 Action.objects.all().exclude(
                     objectUUID__in=vm_uuids).delete()
                 simple_actions[vm.getUUID()] = "deleted"
             elif vm.getState() in [
                     'running', "starting...", "stopping..."
             ]:
                 vm.setState('stopped')
                 vm.save()
                 simple_actions[vm.getUUID()] = "stopped"
             else:
                 continue
     return vms_info
Example #4
0
 def forceListActiveVMs(serverID='None', vmID='None'):
     if serverID != 'None':
         server = VTDriver.getServerById(serverID)
         vtam_vms = server.getVMs()
     else:
         if vmID != 'None':
             servers = VTDriver.getAllServers()
             vtam_vms = list()
             for server in servers:
                 vtam_vms = server.getVMs(id=int(vmID))
                 if vtam_vms:
                     vmID = vtam_vms[0].getUUID()
                     break
             if not vtam_vms:
                 raise Exception("VM not found")
     xmlrpc_server = xmlrpclib.Server(server.getAgentURL())
     # Handle safely the connection against the agent
     try:
         server_active_vms = xmlrpc_server.force_list_active_vms(
             server.getAgentPassword(), vmID)
         for vm in vtam_vms:
             if vm.getUUID() in server_active_vms.keys():
                 vm.setState("running")
                 vm.save()
             else:
                 # XXX: avoiding "on queue" and "unknown" states to avoid bad management
                 #if vm.getState() in ['deleting...', 'failed', 'on queue', 'unknown']:
                 if vm.getState() in ["deleting...", "failed"]:
                     child = vm.getChildObject()
                     server = vm.Server.get()
                     #Action.objects.all().filter(objectUUID = vm.uuid).delete()
                     server.deleteVM(vm)
                     # Keep actions table up-to-date after each deletion
                     vm_uuids = [
                         vm.uuid for vm in VirtualMachine.objects.all()
                     ]
                     Action.objects.all().exclude(
                         objectUUID__in=vm_uuids).delete()
                 elif vm.getState() in [
                         "running", "starting...", "stopping..."
                 ]:
                     vm.setState("stopped")
                     vm.save()
                 else:
                     continue
     except:
         server_active_vms = dict()
     return server_active_vms
Example #5
0
 def forceListActiveVMs(serverID='None', vmID='None'):
         print serverID
         if serverID != 'None':
             server = VTDriver.getServerById(serverID)
             vms = server.getVMs()
         else: 
             if vmID != 'None':
                 servers = VTDriver.getAllServers()
                 vms = list()
                 for server in servers:
                     for vm in server.getVMs():
                         print vm.name, vm.id, int(vmID), type(vm.id), vm.id==int(vmID)  
                     vms = server.getVMs(id=int(vmID))
                     vmID = vms[0].getUUID()
                     break
                 if not vms:
                     raise Exception("VM not Found")
         xmlrpc_server = xmlrpclib.Server(server.getAgentURL())
         vms_info = xmlrpc_server.force_list_active_vms(server.getAgentPassword(), vmID)
         updated_vms = list()
         simple_actions = dict() 
         for vm in vms:
             if vm.getUUID() in vms_info.keys():
                 vm.setState("running")
                 vm.save()
                 simple_actions[vm.getUUID()] = "running"
             else:
                 if vm.getState() in ['deleting...', 'failed', 'on queue', 'unknown']:
                     child = vm.getChildObject()
                     server = vm.Server.get()
                     #Action.objects.all().filter(objectUUID = vm.uuid).delete()
                     server.deleteVM(vm)
                     # Keep actions table up-to-date after each deletion
                     vm_uuids = [ vm.uuid for vm in VirtualMachine.objects.all() ]
                     Action.objects.all().exclude(objectUUID__in = vm_uuids).delete()
                     simple_actions[vm.getUUID()] = "deleted"
                 elif vm.getState() in ['running', "starting...", "stopping..."] :
                     vm.setState('stopped')
                     vm.save()
                     simple_actions[vm.getUUID()] = "stopped"
                 else:
                     continue
         return vms_info
Example #6
0
 def forceListActiveVMs(serverID='None', vmID='None'):
         if serverID != 'None':
             server = VTDriver.getServerById(serverID)
             vtam_vms = server.getVMs()
         else: 
             if vmID != 'None':
                 servers = VTDriver.getAllServers()
                 vtam_vms = list()
                 for server in servers:
                     vtam_vms = server.getVMs(id=int(vmID))
                     if vtam_vms:
                         vmID = vtam_vms[0].getUUID()
                         break
                 if not vtam_vms:
                     raise Exception("VM not found")
         xmlrpc_server = xmlrpclib.Server(server.getAgentURL())
         # Handle safely the connection against the agent
         try:
             server_active_vms = xmlrpc_server.force_list_active_vms(server.getAgentPassword(), vmID)
             for vm in vtam_vms:
                 if vm.getUUID() in server_active_vms.keys():
                     vm.setState("running")
                     vm.save()
                 else:
                     # XXX: avoiding "on queue" and "unknown" states to avoid bad management
                     #if vm.getState() in ['deleting...', 'failed', 'on queue', 'unknown']:
                     if vm.getState() in ["deleting...", "failed"]:
                         child = vm.getChildObject()
                         server = vm.Server.get()
                         #Action.objects.all().filter(objectUUID = vm.uuid).delete()
                         server.deleteVM(vm)
                         # Keep actions table up-to-date after each deletion
                         vm_uuids = [ vm.uuid for vm in VirtualMachine.objects.all() ]
                         Action.objects.all().exclude(objectUUID__in = vm_uuids).delete()
                     elif vm.getState() in ["running", "starting...", "stopping..."] :
                         vm.setState("stopped")
                         vm.save()
                     else:
                         continue
         except:
             server_active_vms = dict()
         return server_active_vms
Example #7
0
def servers_crud(request, server_id=None):

	"""Show a page for the user to add/edit an  VTServer """

	if (not request.user.is_superuser):
        
		return simple.direct_to_template(request,
						template = 'not_admin.html',
						extra_context = {'user':request.user},
					)
	vmProjects = {}
	vmSlices = {}
	try:
		for vm in VTDriver.getVMsInServer(VTDriver.getServerById(server_id)):
			if vm.projectName not in vmProjects:
				vmProjects[vm.projectName] = vm.projectId
			if vm.sliceName not in vmSlices:
				vmSlices[vm.sliceName] = vm.sliceId
	except Exception as e:
		print e
		pass
	
	serverFormClass = HttpUtils.getFormFromModel(VTServer)
	ifaceFormClass = HttpUtils.getFormFromModel(NetworkInterface)
	IfaceFormSetClass = modelformset_factory(NetworkInterface)

	if server_id != None:
		server = get_object_or_404(VTServer, pk=server_id)
	else:
		server = None
	
	if request.method == "GET":
		#serverForm = serverFormClass(instance=server)
		serverForm = ServerForm(instance=server, prefix ="server")

		if server != None:
			mgmt = server.getNetworkInterfaces().filter(isMgmt = True)
			if mgmt:
				mgmt = mgmt.get()
				mgmtIfaceForm = MgmtBridgeForm({'mgmtBridge-name':mgmt.getName(), 'mgmtBridge-mac':mgmt.getMacStr()}, prefix ="mgmtBridge")
			else:
				mgmtIfaceForm = MgmtBridgeForm(prefix ="mgmtBridge")
			
			data = server.getNetworkInterfaces().filter(isMgmt = False)
			if data:
				IfaceFormSetClass = modelformset_factory(NetworkInterface,extra = 0)
			ifaceformset = IfaceFormSetClass(queryset= data)

		else:
			mgmtIfaceForm = MgmtBridgeForm(prefix ="mgmtBridge")
			ifaceformset = IfaceFormSetClass(queryset= NetworkInterface.objects.none())
			
	elif request.method == "POST":
		#serverForm = serverFormClass(request.POST, instance=server)
		serverForm = ServerForm(request.POST, instance=server, prefix ="server")
		ifaceformset = IfaceFormSetClass(request.POST)
		mgmtIfaceForm = MgmtBridgeForm(request.POST, prefix ="mgmtBridge")
		
		if serverForm.is_valid() and ifaceformset.is_valid() and mgmtIfaceForm.is_valid():
			ifaces = ifaceformset.save(commit = False)
			if server == None:
				server = serverForm.save(commit = False)
			try:
				server = VTDriver.crudServerFromInstance(server)
				VTDriver.setMgmtBridge(request, server)
				VTDriver.crudDataBridgeFromInstance(server, ifaces,request.POST.getlist("DELETE"))
			except Exception as e:
				print e
				e = HttpUtils.processException(e)	
				context = {"exception":e, "serverForm": serverForm, 'vmProjects': vmProjects, 'vmSlices': vmSlices,'ifaceformset' : ifaceformset, 'mgmtIfaceForm' : mgmtIfaceForm}
				if server_id != None: context["server"] = server
				return simple.direct_to_template(
				        request,
				        template="servers/servers_crud.html",
				        extra_context=context,
				    )

			# Returns to server's admin page and rollback transactions
			return HttpResponseRedirect('/servers/admin/')
	else:
		return HttpResponseNotAllowed("GET", "POST")

	context = {"serverForm": serverForm, 'vmProjects': vmProjects, 'vmSlices': vmSlices,'ifaceformset' : ifaceformset, 'mgmtIfaceForm' : mgmtIfaceForm}
	if server_id != None: context["server"] = server

	return simple.direct_to_template(
		request,
		template="servers/servers_crud.html",
		extra_context=context,
	)