def get_errors(request): """ Returns all errors that have ever been generated for clusters/vms and then sends them to the errors page. """ user = request.user if user.is_superuser: clusters = Cluster.objects.all() else: clusters = user.get_objects_all_perms(Cluster, ['admin', ]) admin = user.is_superuser or clusters # Get all of the PKs from VMs that this user may administer. vms = vm_qs_for_admins(user).values("pk") # build list of job errors. Include jobs from any vm the user has access # to # If the user has admin on any cluster then those clusters and it's objects # must be included too. # # XXX all jobs have the cluster listed, filtering by cluster includes jobs # for both the cluster itself and any of its VMs or Nodes error_clause = Q(status='error') vm_type = ContentType.objects.get_for_model(VirtualMachine) select_clause = Q(content_type=vm_type, object_id__in=vms) if admin: select_clause |= Q(cluster__in=clusters) job_errors = Job.objects.filter(error_clause & select_clause) # Build the list of job errors. Include jobs from any VMs for which the # user has access. qs = GanetiError.objects ganeti_errors = qs.get_errors(obj=vms) # If the user is an admin on any cluster, then include administrated # clusters and related objects. if admin: ganeti_errors |= qs.get_errors(obj=clusters) # merge error lists errors = merge_errors(ganeti_errors, job_errors) return render_to_response("ganeti/errors.html", { 'admin': admin, 'cluster_list': clusters, 'user': request.user, 'errors': errors, }, context_instance=RequestContext(request))
def test_admin_summary(self): """ Tests that the vm summary for a user with admin permissions on a cluster is correct. """ self.client.login(username=self.admin.username, password='******') response = self.client.get(self.summary_url) vm_summary = response.context['vm_summary'] vms = vm_qs_for_admins(self.admin) expected_summary = { unicode(self.cluster.hostname): { 'total': len(vms), 'running': len(vms.filter(status='running')), 'cluster__slug': unicode(self.cluster.slug) } } self.assertEqual(vm_summary, expected_summary)
def test_standard_summary_vm_perms(self): """ Tests that the vm summary for a user with admin permissions on a single VM (not cluster) is correct """ self.standard.grant('admin', self.vm2) self.client.login(username=self.standard.username, password='******') response = self.client.get(self.summary_url) vm_summary = response.context['vm_summary'] vms = vm_qs_for_admins(self.standard) expected_summary = { unicode(self.cluster.hostname): { 'total': len(vms), 'running': len(vms.filter(status='running')), 'cluster__slug': unicode(self.cluster.slug) } } self.assertEqual(vm_summary, expected_summary)
def overview(request, rest=False): """ Status page """ user = request.user # Get all clusters a user is an administrator of (admin/create_vm perms) if user.is_superuser: clusters = Cluster.objects.all() else: clusters = user.get_objects_any_perms(Cluster, ['admin', 'create_vm', ]) admin = user.is_superuser or clusters #orphaned, ready to import, missing if admin: # build list of admin tasks for this user's clusters orphaned, import_ready, missing = get_vm_counts(clusters) else: orphaned = import_ready = missing = 0 # Get all of the PKs from VMs that this user may administer. vms = vm_qs_for_admins(user).values("pk") # build list of job errors. Include jobs from any vm the user has access # to if the user has admin on any cluster then those clusters and it's # objects must be included too. # XXX all jobs have the cluster listed, filtering by cluster includes jobs # for both the cluster itself and any of its VMs or Nodes error_clause = Q(status='error') vm_type = ContentType.objects.get_for_model(VirtualMachine) select_clause = Q(content_type=vm_type, object_id__in=vms) if admin: select_clause |= Q(cluster__in=clusters) job_errors = Job.objects.filter(error_clause & select_clause) \ .order_by("-finished")[:5] # Build the list of job errors. Include jobs from any VMs for which the # user has access. qs = GanetiError.objects.filter(cleared=False) ganeti_errors = qs.get_errors(obj=vms) # If the user is an admin on any cluster, then include administrated # clusters and related objects. if admin: ganeti_errors |= qs.get_errors(obj=clusters) # merge error lists errors = merge_errors(ganeti_errors, job_errors) # get vm summary - running and totals need to be done as separate queries # and then merged into a single list vms_running = (vms.filter(status='running') .order_by() .values('cluster__hostname', 'cluster__slug') .annotate(running=Count('pk'))) vms_total = (vms.order_by() .values('cluster__hostname', 'cluster__slug') .annotate(total=Count('pk'))) vm_summary = {} for cluster in vms_total: name = cluster.pop('cluster__hostname') vm_summary[name] = cluster for cluster in vms_running: name = cluster['cluster__hostname'] vm_summary[name]['running'] = cluster['running'] # get list of personas for the user: All groups, plus the user. # include the user if they own a vm or have perms on at least one cluster profile = user.get_profile() personas = list(Organization.objects.filter(group__user=user)) owns_vm = profile.virtual_machines.count() has_perms = user.has_any_perms(Cluster, ['admin', 'create_vm'], False) if (owns_vm or has_perms or not personas): personas.insert(0, profile) # get resources used per cluster from the first persona in the list resources = get_used_resources(personas[0]) if rest: return clusters else: context = { 'admin': admin, 'cluster_list': clusters, 'user': request.user, 'errors': errors, 'orphaned': orphaned, 'import_ready': import_ready, 'missing': missing, 'resources': resources, 'vm_summary': vm_summary, 'personas': personas, } return render_to_response("ganeti/overview.html", context, context_instance=RequestContext(request))