Beispiel #1
0
def abort_dead_recipes(*args):
    filters = [not_(DistroTree.lab_controller_assocs.any())]
    if _virt_enabled():
        filters.append(
            and_(not_(Recipe.systems.any()),
                 Recipe.virt_status != RecipeVirtStatus.possible))
    else:
        filters.append(not_(Recipe.systems.any()))
    recipes = MachineRecipe.query\
            .join(MachineRecipe.recipeset).join(RecipeSet.job)\
            .filter(Job.dirty_version == Job.clean_version)\
            .outerjoin(Recipe.distro_tree)\
            .filter(Recipe.status == TaskStatus.queued)\
            .filter(or_(*filters))
    if not recipes.count():
        return False
    log.debug("Entering abort_dead_recipes")
    for recipe_id, in recipes.values(MachineRecipe.id):
        session.begin()
        try:
            abort_dead_recipe(recipe_id)
            session.commit()
        except exceptions.Exception, e:
            log.exception('Error in abort_dead_recipe(%s)', recipe_id)
            session.rollback()
        finally:
Beispiel #2
0
def abort_dead_recipes(*args):
    work_done = False
    with session.begin():
        filters = [not_(DistroTree.lab_controller_assocs.any())]
        if _virt_enabled():
            filters.append(
                and_(not_(Recipe.systems.any()),
                     Recipe.virt_status != RecipeVirtStatus.possible))
        else:
            filters.append(not_(Recipe.systems.any()))
        recipes = MachineRecipe.query\
                .join(MachineRecipe.recipeset).join(RecipeSet.job)\
                .filter(Job.dirty_version == Job.clean_version)\
                .outerjoin(Recipe.distro_tree)\
                .filter(Recipe.status == TaskStatus.queued)\
                .filter(or_(*filters))
        recipe_ids = [
            recipe_id for recipe_id, in recipes.values(MachineRecipe.id)
        ]
    if recipe_ids:
        log.debug('Aborting dead recipes [%s ... %s] (%d total)',
                  recipe_ids[0], recipe_ids[-1], len(recipe_ids))
    for recipe_id in recipe_ids:
        session.begin()
        try:
            abort_dead_recipe(recipe_id)
            session.commit()
        except exceptions.Exception, e:
            log.exception('Error in abort_dead_recipe(%s)', recipe_id)
            session.rollback()
        finally:
Beispiel #3
0
def provision_virt_recipes(*args):
    recipes = MachineRecipe.query\
            .join(Recipe.recipeset).join(RecipeSet.job)\
            .filter(Job.dirty_version == Job.clean_version)\
            .join(Recipe.distro_tree, DistroTree.lab_controller_assocs, LabController)\
            .filter(Recipe.status == TaskStatus.queued)\
            .filter(Recipe.virt_status == RecipeVirtStatus.possible)\
            .filter(LabController.disabled == False)\
            .filter(or_(RecipeSet.lab_controller == None,
                RecipeSet.lab_controller_id == LabController.id))\
            .order_by(RecipeSet.priority.desc(), Recipe.id.asc())
    if not recipes.count():
        return False
    log.debug("Entering provision_virt_recipes")
    for recipe_id, in recipes.values(Recipe.id.distinct()):
        session.begin()
        try:
            provision_virt_recipe(recipe_id)
            session.commit()
        except Exception, e:
            log.exception('Error in provision_virt_recipe(%s)', recipe_id)
            session.rollback()
            # As an added precaution, let's try and avoid this recipe in future
            with session.begin():
                recipe = Recipe.by_id(recipe_id)
                recipe.virt_status = RecipeVirtStatus.failed
        finally:
Beispiel #4
0
 def run(self):
     session.begin()
     recipe = Recipe.by_id(self.recipe_id)
     self.ready_evt.set()
     self.continue_evt.wait()
     recipe.tasks[-1].stop()
     session.commit()
Beispiel #5
0
def init():
    if (len(Work.query().all()) > 0):
        return

    log.info("initializing data")

    works = [
        dict(title="Mountain Stream",
             file_path="mountain_stream",
             description="Taken in Colorado during the summer of 1999.",
             purchases=0,
             id=0),
        dict(title="Graffiti",
             file_path="graffiti",
             description="SpongeBob and Patrick in St. Joseph, Missouri.",
             purchases=0,
             id=1),
        dict(title="Lenexa Conference Center",
             file_path="lenexa_conference_center",
             description="Taken to scout out a wedding reception location.",
             purchases=0,
             id=2),
        dict(title="Glass",
             file_path="glass",
             description="Somewhere in California",
             purchases=0,
             id=3),
    ]

    session.begin()
    for w in works:
        work = Work(dict=w)

    session.commit()
Beispiel #6
0
def provision_scheduled_recipesets(*args):
    """
    if All recipes in a recipeSet are in Scheduled state then move them to
     Running.
    """
    recipesets = RecipeSet.query.join(RecipeSet.job)\
            .filter(and_(Job.dirty_version == Job.clean_version, Job.deleted == None))\
            .filter(not_(RecipeSet.recipes.any(
                Recipe.status != TaskStatus.scheduled)))
    if not recipesets.count():
        return False
    log.debug("Entering provision_scheduled_recipesets")
    for rs_id, in recipesets.values(RecipeSet.id):
        log.info("scheduled_recipesets: RS:%s" % rs_id)
        session.begin()
        try:
            provision_scheduled_recipeset(rs_id)
            session.commit()
        except exceptions.Exception:
            log.exception('Error in provision_scheduled_recipeset(%s)', rs_id)
            session.rollback()
        finally:
            session.close()
    log.debug("Exiting provision_scheduled_recipesets")
    return True
Beispiel #7
0
 def run(self):
     session.begin()
     recipe = Job.by_id(job.id).recipesets[0].recipes[0]
     assert not recipe.watchdog
     assert not recipe.resource
     recipe.recipeset.cancel()
     self.ready_evt.set()
     self.continue_evt.wait()
     session.commit()
Beispiel #8
0
def provision_virt_recipe(recipe_id):
    log.debug('Attempting to provision dynamic virt guest for recipe %s', recipe_id)
    session.begin()
    try:
        recipe = Recipe.by_id(recipe_id)
        manager = dynamic_virt.VirtManager(recipe.recipeset.job.owner)
        available_flavors = manager.available_flavors()
        # We want them in order of smallest to largest, so that we can pick the
        # smallest flavor that satisfies the recipe's requirements. Sorting by RAM
        # is a decent approximation.
        possible_flavors = XmlHost.from_string(recipe.host_requires)\
            .filter_openstack_flavors(available_flavors, manager.lab_controller)
        if not possible_flavors:
            log.debug('No OpenStack flavors matched recipe %s, marking precluded',
                    recipe.id)
            recipe.virt_status = RecipeVirtStatus.precluded
            return
        possible_flavors = sorted(possible_flavors, key=lambda flavor: flavor.ram)
        flavor = possible_flavors[0]
        vm_name = '%srecipe-%s' % (
                ConfigItem.by_name(u'guest_name_prefix').current_value(u'beaker-'),
                recipe.id)
        # FIXME can we control use of virtio?
        #virtio_possible = True
        #if self.recipe.distro_tree.distro.osversion.osmajor.osmajor == "RedHatEnterpriseLinux3":
        #    virtio_possible = False
        vm = manager.create_vm(vm_name, flavor)
        vm.instance_created = datetime.utcnow()
        try:
            recipe.createRepo()
            recipe.systems = []
            recipe.watchdog = Watchdog()
            recipe.resource = vm
            recipe.recipeset.lab_controller = manager.lab_controller
            recipe.virt_status = RecipeVirtStatus.succeeded
            recipe.schedule()
            log.info("recipe ID %s moved from Queued to Scheduled by provision_virt_recipe" % recipe.id)
            recipe.waiting()
            recipe.provision()
            log.info("recipe ID %s moved from Scheduled to Waiting by provision_virt_recipe" % recipe.id)
        except:
            exc_type, exc_value, exc_tb = sys.exc_info()
            try:
                manager.destroy_vm(vm)
            except Exception:
                log.exception('Failed to clean up vm %s '
                        'during provision_virt_recipe, leaked!', vm.instance_id)
                # suppress this exception so the original one is not masked
            raise exc_type, exc_value, exc_tb
        session.commit()
    except Exception, e:
        log.exception('Error in provision_virt_recipe(%s)', recipe_id)
        session.rollback()
        # As an added precaution, let's try and avoid this recipe in future
        with session.begin():
            recipe = Recipe.by_id(recipe_id)
            recipe.virt_status = RecipeVirtStatus.failed
Beispiel #9
0
def commit_or_rollback_session(response):
    # Matches behaviour of TG's sa_rwt: commit on success or redirect,
    # roll back on error.
    if session.is_active:
        if response.status_code >= 200 and response.status_code < 400:
            session.commit()
        else:
            log.debug('Rolling back for %s response', response.status_code)
            session.rollback()
    return response
Beispiel #10
0
 def run(self):
     with app.test_request_context('/RPC2'):
         session.begin()
         self.ready_evt.set()
         self.start_evt.wait()
         lc_user = User.by_user_name(self.lc_user_name)
         identity.set_authentication(lc_user)
         controller.add_distro_tree(self.distro_data)
         self.commit_evt.wait()
         session.commit()
     self.success = True
Beispiel #11
0
def update_dirty_jobs():
    work_done = False
    dirty_jobs = Job.query.filter(Job.dirty_version != Job.clean_version)
    for job_id, in dirty_jobs.values(Job.id):
        session.begin()
        try:
            update_dirty_job(job_id)
            session.commit()
        except Exception, e:
            log.exception('Error in update_dirty_job(%s)', job_id)
            session.rollback()
        finally:
Beispiel #12
0
 def run(self):
     try:
         session.begin()
         system_local = System.query.get(system.id)
         assert system_local.status == SystemStatus.automated
         self.ready_evt.set()
         self.continue_evt.wait()
         system_local.mark_broken(reason=u'Murphy', service=u'testdata')
         session.commit()
     except:
         # We expect one thread to get an exception, don't care which one though.
         # Catching it here just prevents it spewing onto stderr.
         log.exception('Exception in MarkBrokenThread (one is expected)')
Beispiel #13
0
def process_new_recipes(*args):
    work_done = False
    recipes = MachineRecipe.query\
            .join(MachineRecipe.recipeset).join(RecipeSet.job)\
            .filter(Recipe.status == TaskStatus.new)
    for recipe_id, in recipes.values(MachineRecipe.id):
        session.begin()
        try:
            process_new_recipe(recipe_id)
            session.commit()
        except Exception, e:
            log.exception('Error in process_new_recipe(%s)', recipe_id)
            session.rollback()
        finally:
Beispiel #14
0
def update_dirty_jobs():
    dirty_jobs = Job.query.filter(Job.dirty_version != Job.clean_version)
    if not dirty_jobs.count():
        return False
    log.debug("Entering update_dirty_jobs")
    for job_id, in dirty_jobs.values(Job.id):
        session.begin()
        try:
            update_dirty_job(job_id)
            session.commit()
        except Exception, e:
            log.exception('Error in update_dirty_job(%s)', job_id)
            session.rollback()
        finally:
Beispiel #15
0
def schedule_pending_systems():
    work_done = False
    systems = System.query\
            .join(System.lab_controller)\
            .filter(LabController.disabled == False)\
            .filter(System.scheduler_status == SystemSchedulerStatus.pending)
    for system_id, in systems.values(System.id):
        session.begin()
        try:
            schedule_pending_system(system_id)
            session.commit()
        except Exception, e:
            log.exception('Error in schedule_pending_system(%s)', system_id)
            session.rollback()
        finally:
Beispiel #16
0
def queue_processed_recipesets(*args):
    work_done = False
    recipesets = RecipeSet.query.join(RecipeSet.job)\
            .filter(and_(Job.dirty_version == Job.clean_version, Job.deleted == None))\
            .filter(not_(RecipeSet.recipes.any(
                Recipe.status != TaskStatus.processed)))
    for rs_id, in recipesets.values(RecipeSet.id):
        session.begin()
        try:
            queue_processed_recipeset(rs_id)
            session.commit()
        except Exception, e:
            log.exception('Error in queue_processed_recipeset(%s)', rs_id)
            session.rollback()
        finally:
Beispiel #17
0
def queue_processed_recipesets(*args):
    work_done = False
    recipesets = RecipeSet.query.join(RecipeSet.job)\
            .filter(not_(Job.is_deleted))\
            .filter(not_(RecipeSet.recipes.any(
                Recipe.status != TaskStatus.processed)))\
            .order_by(RecipeSet.priority.desc())\
            .order_by(RecipeSet.id)
    for rs_id, in recipesets.values(RecipeSet.id):
        session.begin()
        try:
            queue_processed_recipeset(rs_id)
            session.commit()
        except Exception, e:
            log.exception('Error in queue_processed_recipeset(%s)', rs_id)
            session.rollback()
        finally:
Beispiel #18
0
def update_dirty_jobs():
    work_done = False
    with session.begin():
        dirty_jobs = Job.query.filter(Job.is_dirty)
        job_ids = [job_id for job_id, in dirty_jobs.values(Job.id)]
    if job_ids:
        log.debug('Updating dirty jobs [%s ... %s] (%d total)', job_ids[0],
                  job_ids[-1], len(job_ids))
    for job_id in job_ids:
        session.begin()
        try:
            update_dirty_job(job_id)
            session.commit()
        except Exception, e:
            log.exception('Error in update_dirty_job(%s)', job_id)
            session.rollback()
        finally:
Beispiel #19
0
def process_new_recipes(*args):
    recipes = MachineRecipe.query\
            .join(MachineRecipe.recipeset).join(RecipeSet.job)\
            .filter(Job.dirty_version == Job.clean_version)\
            .filter(Recipe.status == TaskStatus.new)
    if not recipes.count():
        return False
    log.debug("Entering process_new_recipes")
    for recipe_id, in recipes.values(MachineRecipe.id):
        session.begin()
        try:
            process_new_recipe(recipe_id)
            session.commit()
        except Exception, e:
            log.exception('Error in process_new_recipe(%s)', recipe_id)
            session.rollback()
        finally:
Beispiel #20
0
    def sync_tasks(self, urls_to_sync):
        """Syncs remote tasks to the local task library.

        sync_local_tasks() downloads tasks in batches and syncs
        them to the local task library. If the operation fails at some point
        any batches that have already been processed will be preserved.
        """
        def write_data_from_url(task_url):
            def _write_data_from_url(f):
                siphon(urllib2.urlopen(task_url), f)
                f.flush()

            return _write_data_from_url

        urls_to_sync.sort()
        tasks_and_writes = []
        for task_url in urls_to_sync:
            task_rpm_name = os.path.split(task_url)[1]
            tasks_and_writes.append((
                task_rpm_name,
                write_data_from_url(task_url),
            ))
        # We section the batch processing up to allow other processes
        # that may be queueing for the flock to have access, and to limit
        # wastage of time if an error occurs
        total_number_of_rpms = len(tasks_and_writes)
        rpms_synced = 0
        while rpms_synced < total_number_of_rpms:
            session.begin()
            try:
                tasks_and_writes_current_batch = \
                    tasks_and_writes[rpms_synced:rpms_synced+self.batch_size]
                self.tasklib.update_tasks(tasks_and_writes_current_batch)
            except Exception, e:
                session.rollback()
                session.close()
                self.logger.exception('Error syncing tasks. Got error %s' %
                                      (unicode(e)))
                break
            session.commit()
            self.logger.debug('Synced %s tasks' %
                              len(tasks_and_writes_current_batch))
            rpms_synced += self.batch_size
Beispiel #21
0
def queue_processed_recipesets(*args):
    work_done = False
    with session.begin():
        recipesets = RecipeSet.by_recipe_status(TaskStatus.processed)\
                .order_by(RecipeSet.priority.desc())\
                .order_by(RecipeSet.id)
        recipeset_ids = [rs_id for rs_id, in recipesets.values(RecipeSet.id)]
    if recipeset_ids:
        log.debug('Queuing processed recipe sets [%s ... %s] (%d total)',
                  recipeset_ids[0], recipeset_ids[-1], len(recipeset_ids))
    for rs_id in recipeset_ids:
        session.begin()
        try:
            queue_processed_recipeset(rs_id)
            session.commit()
        except Exception, e:
            log.exception('Error in queue_processed_recipeset(%s)', rs_id)
            session.rollback()
        finally:
Beispiel #22
0
def schedule_pending_systems():
    work_done = False
    with session.begin():
        systems = System.query\
                .join(System.lab_controller)\
                .filter(LabController.disabled == False)\
                .filter(System.scheduler_status == SystemSchedulerStatus.pending)
        system_ids = [system_id for system_id, in systems.values(System.id)]
    if system_ids:
        log.debug('Scheduling pending systems (%d total)', len(system_ids))
    for system_id in system_ids:
        session.begin()
        try:
            schedule_pending_system(system_id)
            session.commit()
        except Exception, e:
            log.exception('Error in schedule_pending_system(%s)', system_id)
            session.rollback()
        finally:
Beispiel #23
0
def abort_dead_recipes(*args):
    work_done = False
    with session.begin():
        filters = [not_(DistroTree.lab_controller_assocs.any())]
        if _virt_enabled():
            filters.append(
                and_(not_(Recipe.systems.any()),
                     Recipe.virt_status != RecipeVirtStatus.possible))
        else:
            filters.append(not_(Recipe.systems.any()))

        # Following query is looking for recipes stuck in Queued state.
        # This may be caused by no longer valid distribution in Lab Controller
        # or no machines available.
        # However, we have to account that custom distribution can be
        # used and this distribution is not stored in Database at all.
        recipes = MachineRecipe.query\
                .join(MachineRecipe.recipeset).join(RecipeSet.job)\
                .filter(not_(Job.is_dirty))\
                .outerjoin(Recipe.distro_tree)\
                .outerjoin(Recipe.systems) \
                .filter(Recipe.status == TaskStatus.queued)\
                .filter(or_(DistroTree.id.isnot(None), System.status == SystemStatus.broken)) \
                .filter(or_(*filters))
        recipe_ids = [
            recipe_id for recipe_id, in recipes.values(MachineRecipe.id)
        ]
    if recipe_ids:
        log.debug('Aborting dead recipes [%s ... %s] (%d total)',
                  recipe_ids[0], recipe_ids[-1], len(recipe_ids))
    for recipe_id in recipe_ids:
        session.begin()
        try:
            abort_dead_recipe(recipe_id)
            session.commit()
        except exceptions.Exception as e:
            log.exception('Error in abort_dead_recipe(%s)', recipe_id)
            session.rollback()
        finally:
            session.close()
        work_done = True
    return work_done
Beispiel #24
0
def process_new_recipes(*args):
    work_done = False
    with session.begin():
        recipes = MachineRecipe.query\
                .join(MachineRecipe.recipeset).join(RecipeSet.job)\
                .filter(Recipe.status == TaskStatus.new)
        recipe_ids = [
            recipe_id for recipe_id, in recipes.values(MachineRecipe.id)
        ]
    if recipe_ids:
        log.debug('Processing new recipes [%s ... %s] (%d total)',
                  recipe_ids[0], recipe_ids[-1], len(recipe_ids))
    for recipe_id in recipe_ids:
        session.begin()
        try:
            process_new_recipe(recipe_id)
            session.commit()
        except Exception, e:
            log.exception('Error in process_new_recipe(%s)', recipe_id)
            session.rollback()
        finally:
Beispiel #25
0
def provision_scheduled_recipesets(*args):
    """
    if All recipes in a recipeSet are in Scheduled state then move them to
     Running.
    """
    work_done = False
    recipesets = RecipeSet.query.join(RecipeSet.job)\
            .filter(not_(Job.is_deleted))\
            .filter(not_(RecipeSet.recipes.any(
                Recipe.status != TaskStatus.scheduled)))
    for rs_id, in recipesets.values(RecipeSet.id):
        session.begin()
        try:
            provision_scheduled_recipeset(rs_id)
            session.commit()
        except exceptions.Exception:
            log.exception('Error in provision_scheduled_recipeset(%s)', rs_id)
            session.rollback()
        finally:
            session.close()
        work_done = True
    return work_done
Beispiel #26
0
def update_products(xml_file):
    dom = etree.parse(xml_file)
    xpath_string = '//cpe'
    cpes = dom.xpath(xpath_string)

    session.begin()
    try:
        to_add = {}
        dupe_errors = []
        for cpe in cpes:
            cpe_text = cpe.text

            if cpe_text in to_add:
                dupe_errors.append(cpe_text)
            else:
                to_add[cpe_text] = 1

        for cpe_to_add in to_add:
            prod = Product.lazy_create(name=unicode(cpe_to_add))
        session.commit()
    finally:
        session.rollback()
Beispiel #27
0
def provision_scheduled_recipesets(*args):
    """
    if All recipes in a recipeSet are in Scheduled state then move them to
     Running.
    """
    work_done = False
    with session.begin():
        recipesets = RecipeSet.by_recipe_status(TaskStatus.scheduled)
        recipeset_ids = [rs_id for rs_id, in recipesets.values(RecipeSet.id)]
    if recipeset_ids:
        log.debug('Provisioning scheduled recipe sets [%s ... %s] (%d total)',
                  recipeset_ids[0], recipeset_ids[-1], len(recipeset_ids))
    for rs_id in recipeset_ids:
        session.begin()
        try:
            provision_scheduled_recipeset(rs_id)
            session.commit()
        except exceptions.Exception:
            log.exception('Error in provision_scheduled_recipeset(%s)', rs_id)
            session.rollback()
        finally:
            session.close()
        work_done = True
    return work_done
Beispiel #28
0
def schedule_queued_recipes(*args):
    work_done = False
    session.begin()
    try:
        # This query returns a queued host recipe and and the guest which has
        # the most recent distro tree. It is to be used as a derived table.
        latest_guest_distro = select([machine_guest_map.c.machine_recipe_id.label('host_id'),
            func.max(DistroTree.date_created).label('latest_distro_date')],
            from_obj=[machine_guest_map.join(GuestRecipe.__table__,
                    machine_guest_map.c.guest_recipe_id==GuestRecipe.__table__.c.id). \
                join(Recipe.__table__).join(DistroTree.__table__)],
            whereclause=Recipe.status=='Queued',
            group_by=machine_guest_map.c.machine_recipe_id).alias()

        hosts_lab_controller_distro_map = aliased(LabControllerDistroTree)
        hosts_distro_tree = aliased(DistroTree)
        guest_recipe = aliased(Recipe)
        guests_distro_tree = aliased(DistroTree)
        guests_lab_controller = aliased(LabController)

        # This query will return queued recipes that are eligible to be scheduled.
        # They are determined to be eligible if:
        # * They are clean
        # * There are systems available (see the filter criteria) in lab controllers where
        #   the recipe's distro tree is available.
        # * If it is a host recipe, the most recently created distro of all
        #   the guest recipe's distros is available in at least one of the same
        #   lab controllers as that of the host's distro tree.
        #
        # Also note that we do not try to handle the situation where the guest and host never
        # have a common labcontroller. In that situation the host and guest would stay queued
        # until that situation was rectified.
        recipes = MachineRecipe.query\
            .join(Recipe.recipeset, RecipeSet.job)\
            .filter(Job.dirty_version == Job.clean_version)\
            .outerjoin((guest_recipe, MachineRecipe.guests))\
            .outerjoin((guests_distro_tree, guest_recipe.distro_tree_id == guests_distro_tree.id))\
            .outerjoin((latest_guest_distro,
                and_(latest_guest_distro.c.host_id == MachineRecipe.id,
                    latest_guest_distro.c.latest_distro_date == \
                    guests_distro_tree.date_created)))\
            .outerjoin(guests_distro_tree.lab_controller_assocs, guests_lab_controller)\
            .join(Recipe.systems)\
            .join((hosts_distro_tree, hosts_distro_tree.id == MachineRecipe.distro_tree_id))\
            .join((hosts_lab_controller_distro_map, hosts_distro_tree.lab_controller_assocs),
                (LabController, and_(
                    hosts_lab_controller_distro_map.lab_controller_id == LabController.id,
                    System.lab_controller_id == LabController.id)))\
            .filter(
                and_(Recipe.status == TaskStatus.queued,
                    System.user == None,
                    LabController.disabled == False,
                    or_(
                        RecipeSet.lab_controller == None,
                        RecipeSet.lab_controller_id == System.lab_controller_id,
                       ),
                    or_(
                        System.loan_id == None,
                        System.loan_id == Job.owner_id,
                       ),
                    or_(
                        # We either have no guest
                        guest_recipe.id == None,
                        # Or we have a guest of which the latest
                        # is in a common lab controller.
                        and_(guests_lab_controller.id == LabController.id,
                            latest_guest_distro.c.latest_distro_date != None
                            ),
                        ) # or
                    ) # and
                  )
        # This should be the guest recipe with the latest distro.
        # We return it in this query, to save us from re-running the
        # derived table query in schedule_queued_recipe()
        recipes = recipes.add_column(guest_recipe.id)
        # Effective priority is given in the following order:
        # * Multi host recipes with already scheduled siblings
        # * Priority level (i.e Normal, High etc)
        # * RecipeSet id
        # * Recipe id
        recipes = recipes.order_by(RecipeSet.lab_controller == None). \
            order_by(RecipeSet.priority.desc()). \
            order_by(RecipeSet.id). \
            order_by(MachineRecipe.id)
        # Don't do a GROUP BY before here, it is not needed.
        recipes = recipes.group_by(MachineRecipe.id)
        for recipe_id, guest_recipe_id in recipes.values(
                MachineRecipe.id, guest_recipe.id):
            session.begin(nested=True)
            try:
                schedule_queued_recipe(recipe_id, guest_recipe_id)
                session.commit()
            except (StaleSystemUserException, InsufficientSystemPermissions,
                    StaleTaskStatusException), e:
                # Either
                # System user has changed before
                # system allocation
                # or
                # System permissions have changed before
                # system allocation
                # or
                # Something has moved our status on from queued
                # already.
                log.warn(str(e))
                session.rollback()
            except Exception, e:
                log.exception('Error in schedule_queued_recipe(%s)', recipe_id)
                session.rollback()
                session.begin(nested=True)
                try:
                    recipe = MachineRecipe.by_id(recipe_id)
                    recipe.recipeset.abort(
                        u"Aborted in schedule_queued_recipe: %s" % e)
                    session.commit()
                except Exception, e:
                    log.exception(
                        "Error during error handling in schedule_queued_recipe: %s"
                        % e)
                    session.rollback()
Beispiel #29
0
def populate_db(user_name=None, password=None, user_display_name=None,
                user_email_address=None):
    logger.info('Populating tables with pre-defined values if necessary')
    session.begin()

    try:
        admin = Group.by_name(u'admin')
    except InvalidRequestError:
        admin = Group(group_name=u'admin', display_name=u'Admin')
        session.add(admin)

    try:
        lab_controller = Group.by_name(u'lab_controller')
    except InvalidRequestError:
        lab_controller = Group(group_name=u'lab_controller',
                               display_name=u'Lab Controller')
        session.add(lab_controller)

    # Setup User account
    if user_name:
        user = User.lazy_create(user_name=user_name.decode('utf8'))
        if password:
            user.password = password.decode('utf8')
        if user_display_name:
            user.display_name = user_display_name.decode('utf8')
        if user_email_address:
            user.email_address = user_email_address.decode('utf8')
        # Ensure the user is in the 'admin' group as an owner.
        # Flush for lazy_create.
        session.flush()
        user_group_assoc = UserGroup.lazy_create(
            user_id=user.user_id, group_id=admin.group_id)
        user_group_assoc.is_owner = True

    # Create distro_expire perm if not present
    try:
        _ = Permission.by_name(u'distro_expire')
    except NoResultFound:
        distro_expire_perm = Permission(u'distro_expire')
        session.add(distro_expire_perm)

    # Create proxy_auth perm if not present
    try:
        _ = Permission.by_name(u'proxy_auth')
    except NoResultFound:
        proxy_auth_perm = Permission(u'proxy_auth')
        session.add(proxy_auth_perm)

    # Create tag_distro perm if not present
    try:
        _ = Permission.by_name(u'tag_distro')
    except NoResultFound:
        tag_distro_perm = Permission(u'tag_distro')
        admin.permissions.append(tag_distro_perm)

    # Create stop_task perm if not present
    try:
        _ = Permission.by_name(u'stop_task')
    except NoResultFound:
        stop_task_perm = Permission(u'stop_task')
        lab_controller.permissions.append(stop_task_perm)
        admin.permissions.append(stop_task_perm)

    # Create secret_visible perm if not present
    try:
        _ = Permission.by_name(u'secret_visible')
    except NoResultFound:
        secret_visible_perm = Permission(u'secret_visible')
        lab_controller.permissions.append(secret_visible_perm)
        admin.permissions.append(secret_visible_perm)

    # Create change_prio perm if not present
    try:
        _ = Permission.by_name(u'change_prio')
    except NoResultFound:
        change_prio_perm = Permission(u'change_prio')
        session.add(change_prio_perm)

    # Setup Hypervisors Table
    if Hypervisor.query.count() == 0:
        for h in [u'KVM', u'Xen', u'HyperV', u'VMWare']:
            session.add(Hypervisor(hypervisor=h))

    # Setup kernel_type Table
    if KernelType.query.count() == 0:
        for type in [u'default', u'highbank', u'imx', u'omap', u'tegra']:
            session.add(KernelType(kernel_type=type, uboot=False))
        for type in [u'mvebu']:
            session.add(KernelType(kernel_type=type, uboot=True))

    # Setup base Architectures
    if Arch.query.count() == 0:
        for arch in [u'i386', u'x86_64', u'ia64', u'ppc', u'ppc64', u'ppc64le',
                     u's390', u's390x', u'armhfp', u'aarch64', u'arm']:
            session.add(Arch(arch))

    # Setup base power types
    if PowerType.query.count() == 0:
        for power_type in [u'apc_snmp', u'apc_snmp_then_etherwake',
                           u'bladecenter', u'bladepap', u'drac', u'ether_wake', u'hyper-v',
                           u'ilo', u'integrity', u'ipmilan', u'ipmitool', u'lpar', u'rsa',
                           u'virsh', u'wti']:
            session.add(PowerType(power_type))

    # Setup key types
    if Key.query.count() == 0:
        session.add(Key(u'DISKSPACE', True))
        session.add(Key(u'COMMENT'))
        session.add(Key(u'CPUFAMILY', True))
        session.add(Key(u'CPUFLAGS'))
        session.add(Key(u'CPUMODEL'))
        session.add(Key(u'CPUMODELNUMBER', True))
        session.add(Key(u'CPUSPEED', True))
        session.add(Key(u'CPUVENDOR'))
        session.add(Key(u'DISK', True))
        session.add(Key(u'FORMFACTOR'))
        session.add(Key(u'HVM'))
        session.add(Key(u'MEMORY', True))
        session.add(Key(u'MODEL'))
        session.add(Key(u'MODULE'))
        session.add(Key(u'NETWORK'))
        session.add(Key(u'NR_DISKS', True))
        session.add(Key(u'NR_ETH', True))
        session.add(Key(u'NR_IB', True))
        session.add(Key(u'PCIID'))
        session.add(Key(u'PROCESSORS', True))
        session.add(Key(u'RTCERT'))
        session.add(Key(u'SCRATCH'))
        session.add(Key(u'STORAGE'))
        session.add(Key(u'USBID'))
        session.add(Key(u'VENDOR'))
        session.add(Key(u'XENCERT'))
        session.add(Key(u'NETBOOT_METHOD'))

    if RetentionTag.query.count() == 0:
        session.add(RetentionTag(tag=u'scratch', is_default=1, expire_in_days=30))
        session.add(RetentionTag(tag=u'60days', needs_product=False, expire_in_days=60))
        session.add(RetentionTag(tag=u'120days', needs_product=False, expire_in_days=120))
        session.add(RetentionTag(tag=u'active', needs_product=True))
        session.add(RetentionTag(tag=u'audit', needs_product=True))

    config_items = [
        # name, description, numeric
        (u'root_password', u'Plaintext root password for provisioned systems', False),
        (u'root_password_validity', u"Maximum number of days a user's root password is valid for",
         True),
        (u'guest_name_prefix', u'Prefix for names of dynamic guests in OpenStack', False),
        (u'guest_private_network', u'Network address in CIDR format for private networks'
                                   ' of dynamic guests in OpenStack.', False),
    ]
    for name, description, numeric in config_items:
        ConfigItem.lazy_create(name=name, description=description, numeric=numeric)
    if ConfigItem.by_name(u'root_password').current_value() is None:
        ConfigItem.by_name(u'root_password').set(u'beaker', user=admin.users[0])
    if ConfigItem.by_name(u'guest_private_network').current_value() is None:
        ConfigItem.by_name(u'guest_private_network').set(u'192.168.10.0/24',
                                                         user=admin.users[0])

    session.commit()
    session.close()
    logger.info('Pre-defined values populated')
Beispiel #30
0
 def tearDown(self):
     session.commit()