def setUp(self):
        self.anonymous_user = AnonymousUserFactory()
        self.user = UserFactory.create(username='******')
        self.provider = ProviderFactory.create(type__name='mock')
        self.user_identity = IdentityFactory.create_identity(
            created_by=self.user, provider=self.provider)
        self.machine = ProviderMachineFactory.create_provider_machine(
            self.user, self.user_identity)
        start_date = timezone.now()
        self.active_instance = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date)
        self.status_active = InstanceStatusFactory.create(name='active')
        delta_time = timezone.timedelta(minutes=2)
        InstanceHistoryFactory.create(status=self.status_active,
                                      activity="",
                                      instance=self.active_instance,
                                      start_date=start_date + delta_time * 3)

        self.view = InstanceViewSet.as_view({'post': 'actions'})
        self.url = reverse('api:v2:instance-list')
        self.url += "/" + str(self.active_instance.provider_alias) + "/actions"
        self.mock_driver = get_esh_driver(self.user_identity)
        self.mock_driver.add_core_instance(self.active_instance)

        start_date_second = timezone.now()
        self.active_instance_second = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date_second)
        self.status_active_second = InstanceStatusFactory.create(name='active')
        delta_time = timezone.timedelta(minutes=2)
        self.size_small = SizeFactory.create(provider=self.provider,
                                             cpu=2,
                                             disk=20,
                                             root=0,
                                             mem=128)
        self.size_large = SizeFactory.create(provider=self.provider,
                                             cpu=4,
                                             disk=40,
                                             root=0,
                                             mem=256)
        InstanceHistoryFactory.create(status=self.status_active_second,
                                      size=self.size_small,
                                      activity="",
                                      instance=self.active_instance_second,
                                      start_date=start_date_second +
                                      delta_time * 3)
        self.mock_driver_second = get_esh_driver(self.user_identity)
        self.mock_driver.add_core_instance(self.active_instance_second)
def validate_identity(new_identity):
    try:
        driver = get_esh_driver(new_identity)
        driver.list_sizes()
    except:
        new_identity.delete()
        raise    # Exception("The driver created by this identity was invalid")
def redeploy_users(provider, users=[]):
    accounts = get_account_driver(provider)
    tenant_instances_map = accounts.tenant_instances_map(status_list=['deploy_error', 'networking','deploying','initializing'])
    for tenant, instance_list in tenant_instances_map.iteritems():
        username = tenant.name
        if users and username not in users:
            print "Found affected user:%s and Instances:%s - Skipping because they aren't in the list." % (username, instance_list)
            continue
        for instance in instance_list:
            metadata = instance._node.extra.get('metadata',{})
            instance_status = instance.extra.get('status')
            tmp_status = metadata.get('tmp_status','')
            print "Starting idempotent redeployment for %s - Instance: %s (%s - %s)" % (username, instance.id, instance_status, tmp_status)
            ident = Identity.objects.get(provider=provider, created_by__username=username)
            driver = get_esh_driver(ident)
            try:
                start_task = get_idempotent_deploy_chain(driver.__class__, driver.provider, driver.identity, instance, username)
                print "Starting idempotent redeployment: %s ..." % (start_task),
                start_task.apply_async()
            except Identity.DoesNotExist:
                print "Identity does not exist in this DB. SKIPPED."
                continue
            if DO_NOTHING:
                continue
            print " Sent"
Exemplo n.º 4
0
def prep_instance_for_snapshot(identity_id, instance_id, **celery_task_args):
    identity = Identity.objects.get(id=identity_id)
    try:
        celery_logger.debug("prep_instance_for_snapshot task started at %s." %
                            timezone.now())
        # NOTE: FIXMEIF the assumption that the 'linux username'
        # is the 'created_by' AtmosphereUser changes.
        username = identity.created_by.username
        driver = get_esh_driver(identity)
        instance = driver.get_instance(instance_id)
        if instance.extra.get('status', '') != 'active':
            celery_logger.info("prep_instance_for_snapshot skipped")
            return
        playbooks = deploy_prepare_snapshot(instance.ip, username, instance_id)
        celery_logger.info(playbooks.__dict__)
        hostname = build_host_name(instance.id, instance.ip)
        result = False if execution_has_failures(playbooks, hostname)\
            or execution_has_unreachable(playbooks, hostname) else True
        if not result:
            raise Exception(
                "Error encountered while preparing instance for snapshot: %s" %
                playbooks.stats.summarize(host=hostname))
    except Exception as exc:
        celery_logger.warn(exc)
        prep_instance_for_snapshot.retry(exc=exc)
Exemplo n.º 5
0
def suspend_all_instances(provider, action, sleep_time=None, dry_run=False):
    admin_driver = get_admin_driver(provider)
    all_insts = admin_driver.meta(admin_driver=admin_driver).all_instances()
    users = []
    bad_instances = []
    for i in all_insts:
        if 'creator' in i.extra['metadata']:
            users.append(i.extra['metadata']['creator'])
        else:
            bad_instances.append(i)
    if bad_instances:
        print "WARN: These instances are MISSING because they have incomplete metadata:\n%s" % (bad_instances,)
    all_users = sorted(list(OrderedDict.fromkeys(users)))
    for count, user in enumerate(all_users):
        ident = Identity.objects.filter(created_by__username=user, provider__id=4)
        if len(ident) > 1:
            print "WARN: User %s has >1 identity!" % user
        ident = ident[0]
        driver = get_esh_driver(ident)
        instances = driver.list_instances()
        print "Found %s instances for %s" % (len(instances), user)
        for inst in instances:
            if not sleep_time:
                sleep_for = random.uniform(SLEEP_MIN,SLEEP_MAX)
            else:
                sleep_for = sleep_time
            _execute_action(ident, inst, action, sleep_for, dry_run)
Exemplo n.º 6
0
def create_volume_or_fail(name,
                          size,
                          user,
                          provider,
                          identity,
                          description=None,
                          image_id=None,
                          snapshot_id=None):
    snapshot = None
    image = None
    driver = get_esh_driver(identity, username=user.username)

    if snapshot_id:
        snapshot = driver._connection.ex_get_snapshot(image_id)

    if image_id:
        image = driver.get_machine(image_id)
        restrict_size_by_image(size, image)

    #: Guard against both snapshot and image being present
    assert snapshot is None or image is None, (
        "A volume can only be constructed from a `snapshot` "
        "or an `image` not both.")

    #: Create the volume or raise an exception
    _, volume = create_volume(driver,
                              identity.uuid,
                              name,
                              size,
                              description=description,
                              snapshot=snapshot,
                              image=image,
                              raise_exception=True)
    return volume
def repair_instance(accounts, admin, instance, provider, new_fixed_ip=None):
    tenant_id = instance.extra['tenantId']
    tenant = accounts.user_manager.get_project_by_id(tenant_id)
    tenant_name = tenant.name
    identity = Identity.objects.get(created_by__username=tenant_name,
                                    provider__id=provider.id)
    network_init(identity)
    network_resources = accounts.network_manager.find_tenant_resources(
        tenant_id)
    network = network_resources['networks']
    if not network:
        network, subnet = accounts.create_network(identity)
    else:
        network = network[0]
        subnet = network_resources['subnets'][0]

    #Ensure the network,subnet exist
    if not new_fixed_ip:
        new_fixed_ip = get_next_ip(network_resources['ports'])

    user_driver = get_esh_driver(identity)
    port = accounts.network_manager.create_port(instance.id, network['id'],
                                                subnet['id'], new_fixed_ip,
                                                tenant_id)
    print "Created new port: %s" % port
    attached_intf = user_driver._connection.ex_attach_interface(
        instance.id, port['id'])
    print "Attached port to driver: %s" % attached_intf
def redeploy_users(provider, users=[]):
    accounts = get_account_driver(provider)
    tenant_instances_map = accounts.tenant_instances_map(status_list=[
        'deploy_error', 'networking', 'deploying', 'initializing'
    ])
    for tenant, instance_list in tenant_instances_map.iteritems():
        username = tenant.name
        if users and username not in users:
            print "Found affected user:%s and Instances:%s - Skipping because they aren't in the list." % (
                username, instance_list)
            continue
        for instance in instance_list:
            metadata = instance._node.extra.get('metadata', {})
            instance_status = instance.extra.get('status')
            tmp_status = metadata.get('tmp_status', '')
            print "Starting idempotent redeployment for %s - Instance: %s (%s - %s)" % (
                username, instance.id, instance_status, tmp_status)
            ident = Identity.objects.get(provider=provider,
                                         created_by__username=username)
            driver = get_esh_driver(ident)
            try:
                start_task = get_idempotent_deploy_chain(
                    driver.__class__, driver.provider, driver.identity,
                    instance, username)
                print "Starting idempotent redeployment: %s ..." % (
                    start_task),
                start_task.apply_async()
            except Identity.DoesNotExist:
                print "Identity does not exist in this DB. SKIPPED."
                continue
            if DO_NOTHING:
                continue
            print " Sent"
def _get_admin_driver():
    from core.models import Credential
    from service.driver import get_esh_driver

    identity = Credential.objects.get(value=secrets.EUCA_ADMIN_SECRET).identity
    driver = get_esh_driver(identity, identity.created_by)
    return driver
Exemplo n.º 10
0
def destroy_volume_or_fail(volume, user, cascade=False):
    """
    Destroy the volume specified

    :param cascade: Cascades through and destroy volume snapshots
                    (defaults is False)
    :type cascade: ``bool``
    """
    identity = volume.instance_source.created_by_identity
    driver = get_esh_driver(identity, username=user.username)

    # retrieve volume or fail with not found
    esh_volume = driver.get_volume(volume.identifier)

    if esh_volume is None:
        raise exceptions.NotFound(
            "The `%s` could not be found."
            % volume.identifier)

    # if cascade True and snapshots exist delete all snapshots
    if cascade:
        snapshots = esh_volume.list_snapshots()
        for snapshot in snapshots:
            driver.destroy_snapshot(snapshot)

    # destroy the volume successfully or raise an exception
    if not driver.destroy_volume(esh_volume):
        raise Exception("Encountered an error destroying the volume.")
def repair_instance(accounts, admin, instance, provider, new_fixed_ip=None):
    tenant_id = instance.extra['tenantId']
    tenant = accounts.user_manager.get_project_by_id(tenant_id)
    tenant_name = tenant.name
    identity = Identity.objects.get(
        created_by__username=tenant_name,
        provider__id=provider.id)
    network_init(identity)
    network_resources = accounts.network_manager.find_tenant_resources(
        tenant_id)
    network = network_resources['networks']
    if not network:
        network, subnet = accounts.create_network(identity)
    else:
        network = network[0]
        subnet = network_resources['subnets'][0]

    # Ensure the network,subnet exist
    if not new_fixed_ip:
        new_fixed_ip = get_next_ip(network_resources['ports'])

    user_driver = get_esh_driver(identity)
    port = accounts.network_manager.create_port(
        instance.id,
        network['id'],
        subnet['id'],
        new_fixed_ip,
        tenant_id)
    print "Created new port: %s" % port
    attached_intf = user_driver._connection.ex_attach_interface(
        instance.id,
        port['id'])
    print "Attached port to driver: %s" % attached_intf
Exemplo n.º 12
0
def prep_instance_for_snapshot(identity_id, instance_id, **celery_task_args):
    identity = Identity.objects.get(id=identity_id)
    try:
        celery_logger.debug("prep_instance_for_snapshot task started at %s." % timezone.now())
        # NOTE: FIXMEIF the assumption that the 'linux username'
        # is the 'created_by' AtmosphereUser changes.
        username = identity.created_by.username
        driver = get_esh_driver(identity)
        instance = driver.get_instance(instance_id)
        if instance.extra.get('status','') != 'active':
            celery_logger.info("prep_instance_for_snapshot skipped")
            return
        playbooks = deploy_prepare_snapshot(
            instance.ip, username, instance_id)
        celery_logger.info(playbooks.__dict__)
        hostname = build_host_name(instance.id, instance.ip)
        result = False if execution_has_failures(playbooks, hostname)\
            or execution_has_unreachable(playbooks, hostname) else True
        if not result:
            raise Exception(
                "Error encountered while preparing instance for snapshot: %s"
                % playbooks.stats.summarize(host=hostname))
    except Exception as exc:
        celery_logger.warn(exc)
        prep_instance_for_snapshot.retry(exc=exc)
Exemplo n.º 13
0
def validate_identity(new_identity):
    try:
        driver = get_esh_driver(new_identity)
        driver.list_sizes()
    except:
        new_identity.delete()
        raise # Exception("The driver created by this identity was invalid")
Exemplo n.º 14
0
def destroy_volume_or_fail(volume, user, cascade=False):
    """
    Destroy the volume specified

    :param cascade: Cascades through and destroy volume snapshots
                    (defaults is False)
    :type cascade: ``bool``
    """
    identity = volume.instance_source.created_by_identity
    driver = get_esh_driver(identity, username=user.username)

    # retrieve volume or fail with not found
    esh_volume = driver.get_volume(volume.identifier)

    if esh_volume is None:
        raise exceptions.NotFound(
            "The `%s` could not be found." % volume.identifier
        )

    # if cascade True and snapshots exist delete all snapshots
    if cascade:
        snapshots = esh_volume.list_snapshots()
        for snapshot in snapshots:
            driver.destroy_snapshot(snapshot)

    # destroy the volume successfully or raise an exception
    if not driver.destroy_volume(esh_volume):
        raise Exception("Encountered an error destroying the volume.")
Exemplo n.º 15
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--user",
                        required=True,
                        help="Username that instance belongs to.")
    parser.add_argument("--provider",
                        type=int,
                        required=True,
                        help="Provider instance is running in.")
    parser.add_argument("--instance",
                        required=True,
                        help="OpenStack instance id.")
    parser.add_argument("--resume",
                        action="store_true",
                        help="Resume the instance instead of suspending it.")
    args = parser.parse_args()
    user = args.user
    instance = args.instance
    provider = args.provider
    try:
        ident = Identity.objects.get(provider__id=provider,
                                     created_by__username=user)
    except Exception as e:
        print("Identity could not be found for user: %s on provider: %s" %
              (user, provider))
        print_exc()
        return 1
    driver = get_esh_driver(ident)
    try:
        inst = driver.get_instance(instance)
    except Exception as e:
        print("Instance %s was not found." % (instance))
        print_exc()
        return 2
    if args.resume:
        try:
            resume_instance(driver, inst, ident.provider.id, ident.id,
                            ident.created_by)
        except Exception as e:
            print("Resume failed.")
            print(
                "Calling service.instance.resume_instance failed for instance %s."
                % (instance))
            print_exc()
            return 3
        print("Resumed %s." % (instance))
    else:
        try:
            suspend_instance(driver, inst, ident.provider.id, ident.id,
                             ident.created_by)
        except Exception as e:
            print("Suspend failed.")
            print(
                "Calling service.instance.suspend_instance failed for instance %s."
                % (instance))
            print_exc()
            return 4
        print("Suspended %s." % (instance))
    return 0
Exemplo n.º 16
0
def create_volume_or_fail(
    name,
    size,
    user,
    provider,
    identity,
    description=None,
    project=None,
    image_id=None,
    snapshot_id=None
):
    snapshot = None
    image = None
    driver = get_esh_driver(identity, username=user.username)

    if snapshot_id:
        snapshot = driver._connection.ex_get_snapshot(image_id)

    if image_id:
        image = driver.get_machine(image_id)
        restrict_size_by_image(size, image)

    #: Guard against both snapshot and image being present
    assert snapshot is None or image is None, (
        "A volume can only be constructed from a `snapshot` "
        "or an `image` not both."
    )

    #: Create the volume or raise an exception
    #NOTE: username can be removed when 'quota' is not linked to IdentityMembership
    _, esh_volume = create_esh_volume(
        driver,
        user.username,
        identity.uuid,
        name,
        size,
        description=description,
        snapshot=snapshot,
        image=image,
        raise_exception=True
    )
    identifier = esh_volume.id
    source = InstanceSource.objects.create(
        identifier=identifier,
        provider=provider,
        created_by=user,
        created_by_identity=identity
    )

    kwargs = {
        "name": name,
        "size": size,
        "description": description,
        "instance_source": source,
    }
    volume = Volume.objects.create(**kwargs)
    if project:
        project.volumes.add(volume)
    return volume
Exemplo n.º 17
0
def destroy_instance(instance_alias, core_identity_uuid):
    from service import instance as instance_service
    from rtwo.driver import OSDriver
    try:
        logger.debug("destroy_instance task started at %s." % datetime.now())
        node_destroyed = instance_service.destroy_instance(
            core_identity_uuid, instance_alias)
        core_identity = Identity.objects.get(uuid=core_identity_uuid)
        driver = get_esh_driver(core_identity)
        if isinstance(driver, OSDriver):
            #Spawn off the last two tasks
            logger.debug("OSDriver Logic -- Remove floating ips and check"
                         " for empty project")
            driverCls = driver.__class__
            provider = driver.provider
            identity = driver.identity
            instances = driver.list_instances()
            active = [driver._is_active_instance(inst) for inst in instances]
            if not active:
                logger.debug("Driver shows 0 of %s instances are active" %
                             (len(instances), ))
                #For testing ONLY.. Test cases ignore countdown..
                if app.conf.CELERY_ALWAYS_EAGER:
                    logger.debug("Eager task waiting 1 minute")
                    time.sleep(60)
                clean_task = clean_empty_ips.si(driverCls,
                                                provider,
                                                identity,
                                                immutable=True,
                                                countdown=5)
                remove_task = remove_empty_network.si(driverCls,
                                                      provider,
                                                      identity,
                                                      core_identity_uuid,
                                                      immutable=True,
                                                      countdown=60)
                clean_task.join(remove_task)
                clean_task.apply_async()
            else:
                logger.debug("Driver shows %s of %s instances are active" %
                             (len(active), len(instances)))
                #For testing ONLY.. Test cases ignore countdown..
                if app.conf.CELERY_ALWAYS_EAGER:
                    logger.debug("Eager task waiting 15 seconds")
                    time.sleep(15)
                destroy_chain = clean_empty_ips.si(driverCls,
                                                   provider,
                                                   identity,
                                                   immutable=True,
                                                   countdown=5)
                destroy_chain.apply_async()
        logger.debug("destroy_instance task finished at %s." % datetime.now())
        return node_destroyed
    except Exception as exc:
        logger.exception(exc)
        destroy_instance.retry(exc=exc)
def _execute_action(identity, instance, action, dry_run=False):
    driver = get_esh_driver(identity)
    if action == 'stop':
        if not dry_run:
            stop_instance(driver, instance, identity.provider.id, identity.id, identity.created_by)
        print "Shutoff instanceance %s" % (instance.id,)
    elif action == 'suspend':
        print "Attempt to suspend instanceance %s in state %s" % (instance.id, instance._node.extra['status'])
        if not dry_run:
            suspend_instance(driver, instance, identity.provider.id, identity.id, identity.created_by)
        print "Suspended instanceance %s" % (instance.id)
Exemplo n.º 19
0
def main():
    for ident in Identity.objects.filter(provider__id=1):
        driver = get_esh_driver(ident)
        try:
            vols = driver.list_volumes()
        except:
            print 'No volumes found for %s' % ident.created_by.username
        if not vols:
            continue
        print('%s\n---\n' % ident.created_by.username)
        for vol in vols:
            print('%s\t%s' % (vol.alias, vol.extra['createTime']))
Exemplo n.º 20
0
def main():
    for ident in Identity.objects.filter(provider__id=1):
        driver = get_esh_driver(ident)
        try:
            vols = driver.list_volumes()
        except:
            print 'No volumes found for %s' % ident.created_by.username
        if not vols:
            continue
        print ('%s\n---\n' % ident.created_by.username)
        for vol in vols:
            print ('%s\t%s' % (vol.alias, vol.extra['createTime']))
Exemplo n.º 21
0
def _execute_action(identity, instance, action, dry_run=False):
    driver = get_esh_driver(identity)
    if action == 'stop':
        if not dry_run:
            stop_instance(driver, instance, identity.provider.id, identity.id,
                          identity.created_by)
        print "Shutoff instanceance %s" % (instance.id, )
    elif action == 'suspend':
        print "Attempt to suspend instanceance %s in state %s" % (
            instance.id, instance._node.extra['status'])
        if not dry_run:
            suspend_instance(driver, instance, identity.provider.id,
                             identity.id, identity.created_by)
        print "Suspended instanceance %s" % (instance.id)
Exemplo n.º 22
0
    def validate_token_with_driver(self, provider_uuid, username, project_name, new_token_key):
        ident = self._find_identity_match(provider_uuid, username, project_name)
        if not ident:
            # Can't validate driver if identity can't be created.
            return

        try:
            driver = get_esh_driver(ident, identity_kwargs={'ex_force_auth_token': new_token_key})
            if not driver.is_valid():
                raise serializers.ValidationError(
                    "Token returned from keystone could not create an rtwo driver")
        except Exception as exc:
                raise serializers.ValidationError(
                        "Driver could not be created: %s" % exc)
Exemplo n.º 23
0
    def _init_by_provider(self, provider, *args, **kwargs):
        from service.driver import get_esh_driver

        self.core_provider = provider

        provider_creds = provider.get_credentials()
        self.provider_creds = provider_creds
        admin_identity = provider.admin
        admin_creds = admin_identity.get_credentials()
        self.admin_driver = get_esh_driver(admin_identity)
        admin_creds = self._libcloud_to_openstack(admin_creds)
        all_creds = {'location': provider.get_location()}
        all_creds.update(admin_creds)
        all_creds.update(provider_creds)
        return all_creds
Exemplo n.º 24
0
    def _init_by_provider(self, provider, *args, **kwargs):
        from service.driver import get_esh_driver

        self.core_provider = provider

        provider_creds = provider.get_credentials()
        self.provider_creds = provider_creds
        admin_identity = provider.get_admin_identity()
        admin_creds = admin_identity.get_credentials()
        self.admin_driver = get_esh_driver(admin_identity)
        admin_creds = self._libcloud_to_openstack(admin_creds)
        all_creds = {'location': provider.get_location()}
        all_creds.update(admin_creds)
        all_creds.update(provider_creds)
        return all_creds
Exemplo n.º 25
0
def main():
    driver = get_esh_driver(Identity.objects.get(provider__id=4, created_by__username='******'))
    for app in Application.objects.all():
        pms = app.providermachine_set.filter(provider__id=4)
        if len(pms) >= 2:
            for pm in pms:
                print "%s shares application %s" % (pm.identifier, app.name)
                mach = driver.get_machine(pm.identifier)
                if not mach:
                    print "%s doesnt exist" % pm.identifier
                    continue
                if mach.name != app.name:
                     new_app = create_application(pm.identifier, 4, mach.name)
                     pm.application = new_app
                     pm.save()
                     print 'New app created:%s' % new_app.name
Exemplo n.º 26
0
def main():
    driver = get_esh_driver(Identity.objects.get(provider__id=4, created_by__username='******'))
    for app in Application.objects.all():
        pms = app.providermachine_set.filter(provider__id=4)
        if len(pms) >= 2:
            for pm in pms:
                print "%s shares application %s" % (pm.identifier, app.name)
                mach = driver.get_machine(pm.identifier)
                if not mach:
                    print "%s doesnt exist" % pm.identifier
                    continue
                if mach.name != app.name:
                     new_app = create_application(pm.identifier, 4, mach.name)
                     pm.application = new_app
                     pm.save()
                     print 'New app created:%s' % new_app.name
Exemplo n.º 27
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--user", required=True,
                        help="Username that instance belongs to.")
    parser.add_argument("--provider", type=int, required=True,
                        help="Provider instance is running in.")
    parser.add_argument("--instance", required=True,
                        help="OpenStack instance id.")
    parser.add_argument("--resume", action="store_true",
                        help="Resume the instance instead of suspending it.")
    args = parser.parse_args()
    user = args.user
    instance = args.instance
    provider = args.provider
    try:
        ident = Identity.objects.get(provider__id=provider, created_by__username=user)
    except Exception as e:
        print("Identity could not be found for user: %s on provider: %s" % (user, provider))
        print_exc()
        return 1
    driver = get_esh_driver(ident)
    try:
        inst = driver.get_instance(instance)
    except Exception as e:
        print("Instance %s was not found." % (instance))
        print_exc()
        return 2
    if args.resume:
        try:
            resume_instance(driver, inst, ident.provider.id, ident.id, ident.created_by)
        except Exception as e:
            print("Resume failed.")
            print("Calling service.instance.resume_instance failed for instance %s." % (instance))
            print_exc()
            return 3
        print("Resumed %s." % (instance))
    else:
        try:
            suspend_instance(driver, inst, ident.provider.id, ident.id, ident.created_by)
        except Exception as e:
            print("Suspend failed.")
            print("Calling service.instance.suspend_instance failed for instance %s." % (instance))
            print_exc()
            return 4
        print("Suspended %s." % (instance))
    return 0
Exemplo n.º 28
0
def clear_empty_ips_for(core_identity_uuid, username=None):
    """
    RETURN: (number_ips_removed, delete_network_called)
    """
    from api import get_esh_driver
    from rtwo.driver import OSDriver
    #Initialize the drivers
    core_identity = Identity.objects.get(uuid=core_identity_uuid)
    driver = get_esh_driver(core_identity)
    if not isinstance(driver, OSDriver):
        return (0, False)
    os_acct_driver = get_account_driver(core_identity.provider)
    logger.info("Initialized account driver")
    # Get useful info
    creds = core_identity.get_credentials()
    tenant_name = creds['ex_tenant_name']
    logger.info("Checking Identity %s" % tenant_name)
    # Attempt to clean floating IPs
    num_ips_removed = _remove_extra_floating_ips(driver, tenant_name)
    #Test for active/inactive instances
    instances = driver.list_instances()
    #Active IFF ANY instance is 'active'
    active = any(driver._is_active_instance(inst) for inst in instances)
    #Inactive IFF ALL instances are suspended/stopped
    inactive = all(driver._is_inactive_instance(inst) for inst in instances)
    _remove_ips_from_inactive_instances(driver, instances)
    if active and not inactive:
        #User has >1 active instances AND not all instances inactive
        return (num_ips_removed, False)
    network_id = os_acct_driver.network_manager.get_network_id(
        os_acct_driver.network_manager.neutron, '%s-net' % tenant_name)
    if network_id:
        #User has 0 active instances OR all instances are inactive
        #Network exists, attempt to dismantle as much as possible
        # Remove network=False IFF inactive=True..
        remove_network = not inactive
        if remove_network:
            _remove_network(os_acct_driver,
                            core_identity,
                            tenant_name,
                            remove_network=True)
            return (num_ips_removed, True)
        return (num_ips_removed, False)
    else:
        logger.info("No Network found. Skipping %s" % tenant_name)
        return (num_ips_removed, False)
Exemplo n.º 29
0
def freeze_instance_task(identity_id, instance_id, **celery_task_args):
    identity = Identity.objects.get(id=identity_id)
    driver = get_esh_driver(identity)
    kwargs = {}
    private_key = "/opt/dev/atmosphere/extras/ssh/id_rsa"
    kwargs.update({'ssh_key': private_key})
    kwargs.update({'timeout': 120})

    si_script = sync_instance()
    kwargs.update({'deploy': si_script})

    instance = driver.get_instance(instance_id)
    driver.deploy_to(instance, **kwargs)

    fi_script = freeze_instance()
    kwargs.update({'deploy': fi_script})
    driver.deploy_to(instance, **kwargs)
def freeze_instance_task(identity_id, instance_id, **celery_task_args):
    identity = Identity.objects.get(id=identity_id)
    driver = get_esh_driver(identity)
    kwargs = {}
    private_key = "/opt/dev/atmosphere/extras/ssh/id_rsa"
    kwargs.update({'ssh_key': private_key})
    kwargs.update({'timeout': 120})

    si_script = sync_instance()
    kwargs.update({'deploy': si_script})

    instance = driver.get_instance(instance_id)
    driver.deploy_to(instance, **kwargs)

    fi_script = freeze_instance()
    kwargs.update({'deploy': fi_script})
    driver.deploy_to(instance, **kwargs)
    def validate_token_with_driver(
        self, provider_uuid, username, project_name, new_token_key
    ):
        ident = self._find_identity_match(provider_uuid, username, project_name)
        if not ident:
            # Can't validate driver if identity can't be created.
            return

        try:
            driver = get_esh_driver(
                ident, identity_kwargs={'ex_force_auth_token': new_token_key}
            )
            if not driver.is_valid():
                raise serializers.ValidationError(
                    "Token returned from keystone could not create an rtwo driver"
                )
        except Exception as exc:
            raise serializers.ValidationError(
                "Driver could not be created: %s" % exc
            )
Exemplo n.º 32
0
def create_volume_or_fail(name, size, user, provider, identity,
                          description=None, image_id=None, snapshot_id=None):
    snapshot = None
    image = None
    driver = get_esh_driver(identity, username=user.username)

    if snapshot_id:
        snapshot = driver._connection.ex_get_snapshot(image_id)

    if image_id:
        image = driver.get_machine(image_id)
        restrict_size_by_image(size, image)

    #: Guard against both snapshot and image being present
    assert snapshot is None or image is None, (
        "A volume can only be constructed from a `snapshot` "
        "or an `image` not both.")

    #: Create the volume or raise an exception
    _, volume = create_volume(driver, identity.uuid, name, size,
                              description=description,
                              snapshot=snapshot, image=image,
                              raise_exception=True)
    return volume
Exemplo n.º 33
0
def _get_cached_driver(provider=None, identity=None, force=True):
    if provider:
        return _get_cached_admin_driver(provider, force)
    if not drivers.get(identity) or force:
        drivers[identity] = get_esh_driver(identity)
    return drivers[identity]
Exemplo n.º 34
0
    def setUp(self):
        self.anonymous_user = AnonymousUserFactory()
        self.user = UserFactory.create(username='******')
        self.provider = ProviderFactory.create(type__name='mock')
        self.user_identity = IdentityFactory.create_identity(
            created_by=self.user, provider=self.provider
        )
        self.machine = ProviderMachineFactory.create_provider_machine(
            self.user, self.user_identity
        )
        start_date = timezone.now()
        self.active_instance = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date
        )
        self.size_small = SizeFactory.create(
            provider=self.provider, cpu=2, disk=20, root=0, mem=128
        )
        self.status_active = InstanceStatusFactory.create(name='active')
        delta_time = timezone.timedelta(minutes=2)
        InstanceHistoryFactory.create(
            status=self.status_active,
            size=self.size_small,
            activity="",
            instance=self.active_instance,
            start_date=start_date + delta_time * 3
        )

        self.view = InstanceViewSet.as_view({'post': 'actions'})
        self.url = reverse('api:v2:instance-list')
        self.url += "/" + str(self.active_instance.provider_alias) + "/actions"
        self.mock_driver = get_esh_driver(self.user_identity)
        self.mock_driver.add_core_instance(self.active_instance)

        start_date_second = timezone.now()
        self.active_instance_second = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date_second
        )
        delta_time = timezone.timedelta(minutes=2)
        self.size_small = SizeFactory.create(
            provider=self.provider, cpu=2, disk=20, root=0, mem=128
        )
        self.size_large = SizeFactory.create(
            provider=self.provider, cpu=4, disk=40, root=0, mem=256
        )
        InstanceHistoryFactory.create(
            status=self.status_active,
            size=self.size_small,
            activity="",
            instance=self.active_instance_second,
            start_date=start_date_second + delta_time * 3
        )
        self.mock_driver.add_core_instance(self.active_instance_second)
        self.allocation_source_1 = AllocationSourceFactory.create(
            name='TEST_INSTANCE_ALLOCATION_SOURCE_01', compute_allowed=1000
        )
        UserAllocationSource.objects.create(
            allocation_source=self.allocation_source_1, user=self.user
        )
        UserAllocationSnapshot.objects.create(
            allocation_source=self.allocation_source_1,
            user=self.user,
            burn_rate=1,
            compute_used=0
        )
        AllocationSourceSnapshot.objects.create(
            allocation_source=self.allocation_source_1,
            compute_used=0,
            compute_allowed=168,
            global_burn_rate=1
        )
        InstanceAllocationSourceSnapshot.objects.update_or_create(
            instance=self.active_instance,
            allocation_source=self.allocation_source_1
        )
Exemplo n.º 35
0
    def setUp(self):
        self.anonymous_user = AnonymousUserFactory()
        self.user = UserFactory.create(username='******')
        self.provider = ProviderFactory.create(type__name='mock')
        self.user_identity = IdentityFactory.create_identity(
            created_by=self.user,
            provider=self.provider)
        self.machine = ProviderMachineFactory.create_provider_machine(self.user, self.user_identity)
        start_date = timezone.now()
        self.active_instance = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date)
        self.size_small = SizeFactory.create(provider=self.provider, cpu=2, disk=20, root=0, mem=128)
        self.status_active = InstanceStatusFactory.create(name='active')
        delta_time = timezone.timedelta(minutes=2)
        InstanceHistoryFactory.create(
                status=self.status_active,
                size=self.size_small,
                activity="",
                instance=self.active_instance,
                start_date=start_date + delta_time*3)

        self.view = InstanceViewSet.as_view({'post': 'actions'})
        self.url = reverse('api:v2:instance-list')
        self.url += "/" + str(self.active_instance.provider_alias) + "/actions"
        self.mock_driver = get_esh_driver(self.user_identity)
        self.mock_driver.add_core_instance(self.active_instance)

        start_date_second = timezone.now()
        self.active_instance_second = InstanceFactory.create(
            name="Instance in active",
            provider_alias=uuid.uuid4(),
            source=self.machine.instance_source,
            created_by=self.user,
            created_by_identity=self.user_identity,
            start_date=start_date_second)
        delta_time = timezone.timedelta(minutes=2)
        self.size_small = SizeFactory.create(provider=self.provider, cpu=2, disk=20, root=0, mem=128)
        self.size_large = SizeFactory.create(provider=self.provider, cpu=4, disk=40, root=0, mem=256)
        InstanceHistoryFactory.create(
                status=self.status_active,
                size=self.size_small,
                activity="",
                instance=self.active_instance_second,
                start_date=start_date_second + delta_time*3)
        self.mock_driver.add_core_instance(self.active_instance_second)
        self.allocation_source_1 = AllocationSourceFactory.create(name='TEST_INSTANCE_ALLOCATION_SOURCE_01',
                                                                  compute_allowed=1000)
        UserAllocationSource.objects.create(
                allocation_source=self.allocation_source_1,
                user=self.user)
        UserAllocationSnapshot.objects.create(
                allocation_source=self.allocation_source_1,
                user=self.user,
                burn_rate=1,
                compute_used=0)
        AllocationSourceSnapshot.objects.create(
            allocation_source=self.allocation_source_1,
            compute_used=0,
            compute_allowed=168,
            global_burn_rate=1
        )
        InstanceAllocationSourceSnapshot.objects.update_or_create(instance=self.active_instance,
                                                                  allocation_source=self.allocation_source_1)
Exemplo n.º 36
0
def _get_cached_driver(provider=None, identity=None, force=True):
    if provider:
        return _get_cached_admin_driver(provider, force)
    if not drivers.get(identity) or force:
        drivers[identity] = get_esh_driver(identity)
    return drivers[identity]
def _get_admin_driver():
    from core.models import Credential
    from service.driver import get_esh_driver
    identity = Credential.objects.get(value=secrets.EUCA_ADMIN_SECRET).identity
    driver = get_esh_driver(identity, identity.created_by)
    return driver