def main():
    """
    TODO: Add argparse, --delete : Deletes existing users in openstack (Never use in PROD)
    """
    openstack = Provider.objects.filter(
        type__name__iexact="openstack").order_by("id")
    if not openstack:
        raise Provider.DoesNotExist("No OpenStack Provider Found")
    openstack = openstack[0]
    os_driver = OSAccountDriver(openstack)
    found = 0
    create = 0
    usernames = os_driver.list_usergroup_names()
    quota_dict = {'cpu': 10, 'memory': 20, 'storage': 10, 'storage_count': 10}
    higher_quota = Quota.objects.get_or_create(**quota_dict)[0]
    for user in usernames:
        # Openstack account exists, but we need the identity.
        ident = os_driver.create_account(user)
        if is_staff(ident):
            im = ident.identity_membership.all()[0]
            #Disable time allocation
            im.allocation = None
        #Raise everybody's quota
        im.quota = higher_quota
        im.save()
    print "Total users added to atmosphere:%s" % len(usernames)
def visible_applications(user):
    apps = []
    if not user:
        return apps
    from core.models import Provider, ProviderMachineMembership
    active_providers = Provider.get_active()
    now_time = timezone.now()
    #Look only for 'Active' private applications
    for app in Application.objects.filter(
            Q(end_date=None) | Q(end_date__gt=now_time),
            private=True):
        #Retrieve the machines associated with this app
        machine_set = app.providermachine_set.filter(
                   Q(end_date=None) | Q(end_date__gt=now_time))
        #Skip app if all their machines are on inactive providers.
        if all(not pm.provider.is_active() for pm in machine_set):
            continue
        #Add the application if 'user' is a member of the application or PM
        if app.members.filter(user=user):
            _add_app(apps, app)
        for pm in machine_set:
            if pm.members.filter(user=user):
                _add_app(apps, app)
                break
    return apps
Exemple #3
0
def remove_empty_networks():
    try:
        logger.debug("remove_empty_networks task started at %s." %
                     datetime.now())
        for provider in Provider.get_active(type_name='openstack'):
            os_driver = OSAccountDriver(provider)
            all_instances = os_driver.admin_driver.list_all_instances()
            project_map = os_driver.network_manager.project_network_map()
            projects_with_networks = project_map.keys()
            for project in projects_with_networks:
                network_name = project_map[project]['network']['name']
                logger.debug("Checking if network %s is in use" % network_name)
                if running_instances(network_name, all_instances):
                    continue
                #TODO: Will change when not using 'usergroups' explicitly.
                user = project
                try:
                    logger.debug("Removing project network for User:%s, Project:%s"
                                 % (user, project))
                    os_driver.network_manager.delete_project_network(user, project)
                except NeutronClientException:
                    logger.exception("Neutron unable to remove project"
                                     "network for %s-%s" % (user,project))
                except NeutronException:
                    logger.exception("Neutron unable to remove project"
                                     "network for %s-%s" % (user,project))
    except Exception as exc:
        logger.exception("Failed to run remove_empty_networks")
Exemple #4
0
def remove_empty_networks():
    try:
        logger.debug("remove_empty_networks task started at %s." %
                     datetime.now())
        for provider in Provider.get_active(type_name='openstack'):
            os_driver = OSAccountDriver(provider)
            all_instances = os_driver.admin_driver.list_all_instances()
            project_map = os_driver.network_manager.project_network_map()
            projects_with_networks = project_map.keys()
            for project in projects_with_networks:
                network_name = project_map[project]['network']['name']
                logger.debug("Checking if network %s is in use" % network_name)
                if running_instances(network_name, all_instances):
                    continue
                #TODO: Will change when not using 'usergroups' explicitly.
                user = project
                try:
                    logger.debug(
                        "Removing project network for User:%s, Project:%s" %
                        (user, project))
                    os_driver.network_manager.delete_project_network(
                        user, project)
                except NeutronClientException:
                    logger.exception("Neutron unable to remove project"
                                     "network for %s-%s" % (user, project))
                except NeutronException:
                    logger.exception("Neutron unable to remove project"
                                     "network for %s-%s" % (user, project))
    except Exception as exc:
        logger.exception("Failed to run remove_empty_networks")
 def get_queryset(self):
     """
     Filter out tags for deleted instances
     """
     user = self.request.user
     now = timezone.now()
     instances = Instance.objects.filter(Q(end_date__gt=now)
                                         | Q(end_date__isnull=True),
                                         start_date__lt=now,
                                         project__owner__user=user)
     active_provider_uuids = [ap.uuid for ap in Provider.get_active()]
     return instances.filter(pk__in=[
         i.id for i in instances
         if i.provider_uuid() in active_provider_uuids
     ])
 def get_queryset(self):
     """
     Filter out tags for deleted instances
     """
     user = self.request.user
     now = timezone.now()
     instances = Instance.objects.filter(
         Q(end_date__gt=now) |
         Q(end_date__isnull=True),
         start_date__lt=now,
         project__owner__user=user)
     active_provider_uuids = [ap.uuid for ap in Provider.get_active()]
     return instances.filter(
         pk__in=[i.id for i in instances
                 if i.provider_uuid() in active_provider_uuids])
Exemple #7
0
def remove_empty_networks():
    celery_logger.debug("remove_empty_networks task started at %s." %
                        datetime.now())
    for provider in Provider.get_active(type_name='openstack'):
        remove_empty_networks_for.apply_async(args=[provider.id])
#!/usr/bin/env python
"""
This script serves a very specific purpose:
    Nullifying all non-end-dated status history objects that are not the latest value.
"""

from core.models import Provider, Instance, InstanceStatusHistory
import django
django.setup()
provs = Provider.get_active()
for instance in Instance.objects.filter(source__provider__in=provs
                                       ).order_by('created_by'):
    prev_history = None
    try:
        last_history = instance.instancestatushistory_set.latest('pk')
    except InstanceStatusHistory.DoesNotExist:
        continue
    for history in instance.instancestatushistory_set.order_by('start_date'):
        if not history.end_date and history.id != last_history.id:
            print "Provider: %s" % instance.source.provider.location
            print "Owner: %s" % instance.created_by.username
            print "Instance: %s Bad History: %s" % (
                instance.provider_alias, history
            )
            history.end_date = history.start_date
            history.save()
    prev_history = None
    for history in instance.instancestatushistory_set.order_by('start_date'):
        if prev_history and prev_history.status.name == history.status.name\
                and history.end_date != history.start_date:
            print "Provider: %s" % instance.source.provider.location
Exemple #9
0
def remove_empty_networks():
    logger.debug("remove_empty_networks task started at %s." %
                 datetime.now())
    for provider in Provider.get_active(type_name='openstack'):
        remove_empty_networks_for.apply_async(args=[provider.id])