Ejemplo n.º 1
0
def delete_pool(pool_name):
    """
    Deletes a system pool

    :param pool_name: System pool's name

    """
    pool = _get_pool_by_name(pool_name, lockmode='update')
    u = identity.current.user
    if not pool.can_edit(u):
        raise Forbidden403('Cannot delete pool %s' % pool_name)

    systems = System.query.filter(System.pools.contains(pool))
    System.record_bulk_activity(systems, user=identity.current.user,
                                service=u'HTTP', action=u'Removed',
                                field=u'Pool',
                                old=unicode(pool),
                                new=None)
    # Since we are deleting the pool, we will have to change the active
    # access policy for all systems using the pool's policy to their
    # custom policy
    systems = System.query.filter(System.active_access_policy == pool.access_policy)
    for system in systems:
        system.active_access_policy = system.custom_access_policy
    System.record_bulk_activity(systems, user=identity.current.user,
                                service=u'HTTP',
                                field=u'Active Access Policy', action=u'Changed',
                                old = 'Pool policy: %s' % pool_name,
                                new = 'Custom access policy')
    session.delete(pool)
    activity = Activity(u, u'HTTP', u'Deleted', u'Pool', pool_name)
    session.add(activity)
    return '', 204
Ejemplo n.º 2
0
    def remove(self, id, *args, **kw):
        labcontroller = LabController.by_id(id)
        labcontroller.removed = datetime.utcnow()
        # de-associate systems
        systems = System.query.filter(System.lab_controller == labcontroller)
        System.record_bulk_activity(systems, user=identity.current.user,
                service=u'WEBUI', action=u'Changed', field=u'lab_controller',
                old=labcontroller.fqdn, new=None)
        systems.update({'lab_controller_id': None}, synchronize_session=False)
        # cancel running recipes
        watchdogs = Watchdog.by_status(labcontroller=labcontroller, 
            status='active')
        for w in watchdogs:
            w.recipe.recipeset.job.cancel(msg='LabController %s has been deleted' % labcontroller.fqdn)
        # remove distro trees
        distro_tree_assocs = LabControllerDistroTree.query\
            .filter(LabControllerDistroTree.lab_controller == labcontroller)\
            .join(LabControllerDistroTree.distro_tree)
        DistroTree.record_bulk_activity(distro_tree_assocs, user=identity.current.user,
                service=u'WEBUI', action=u'Removed', field=u'lab_controller_assocs',
                old=labcontroller.fqdn, new=None)
        distro_tree_assocs.delete(synchronize_session=False)
        labcontroller.disabled = True
        labcontroller.record_activity(user=identity.current.user, service=u'WEBUI',
                field=u'Disabled', action=u'Changed', old=unicode(False), new=unicode(True))
        labcontroller.record_activity(user=identity.current.user, service=u'WEBUI',
                field=u'Removed', action=u'Changed', old=unicode(False), new=unicode(True))

        flash( _(u"%s removed") % labcontroller.fqdn )
        raise redirect(".")
Ejemplo n.º 3
0
def add_system():
    # We accept JSON or form-encoded for convenience
    if request.json:
        if 'fqdn' not in request.json:
            raise BadRequest400('Missing fqdn key')
        new_fqdn = request.json['fqdn']
    elif request.form:
        if 'fqdn' not in request.form:
            raise BadRequest400('Missing fqdn parameter')
        new_fqdn = request.form['fqdn']
    else:
        raise UnsupportedMediaType415
    with convert_internal_errors():
        if System.query.filter(System.fqdn == new_fqdn).count() != 0:
            raise Conflict409('System with fqdn %r already exists' % new_fqdn)
        system = System(fqdn=new_fqdn, owner=identity.current.user)
        session.add(system)
        # new systems are visible to everybody by default
        system.custom_access_policy = SystemAccessPolicy()
        system.custom_access_policy.add_rule(SystemPermission.view,
                everybody=True)
    # XXX this should be 201 with Location: /systems/FQDN/ but 302 is more 
    # convenient because it lets us use a traditional browser form without AJAX 
    # handling, and for now we're redirecting to /view/FQDN until that is moved 
    # to /systems/FQDN/
    return flask_redirect(url(u'/view/%s#essentials' % system.fqdn))
Ejemplo n.º 4
0
def add_system():
    # We accept JSON or form-encoded for convenience
    if request.json:
        if 'fqdn' not in request.json:
            raise BadRequest400('Missing fqdn key')
        new_fqdn = request.json['fqdn']
    elif request.form:
        if 'fqdn' not in request.form:
            raise BadRequest400('Missing fqdn parameter')
        new_fqdn = request.form['fqdn']
    else:
        raise UnsupportedMediaType415
    with convert_internal_errors():
        if System.query.filter(System.fqdn == new_fqdn).count() != 0:
            raise Conflict409('System with fqdn %r already exists' % new_fqdn)
        system = System(fqdn=new_fqdn, owner=identity.current.user)
        session.add(system)
        # new systems are visible to everybody by default
        system.custom_access_policy = SystemAccessPolicy()
        system.custom_access_policy.add_rule(SystemPermission.view,
                                             everybody=True)
    # XXX this should be 201 with Location: /systems/FQDN/ but 302 is more
    # convenient because it lets us use a traditional browser form without AJAX
    # handling, and for now we're redirecting to /view/FQDN until that is moved
    # to /systems/FQDN/
    return flask_redirect(url(u'/view/%s#essentials' % system.fqdn))
Ejemplo n.º 5
0
 def query(cls):
     for exclude in System.permissable_systems(ExcludeOSMajor.query.outerjoin('system','user')):
         if exclude.system:
             yield CSV_Exclude(exclude)
     for exclude in System.permissable_systems(ExcludeOSVersion.query.outerjoin('system','user')):
         if exclude.system:
             yield CSV_Exclude(exclude)
Ejemplo n.º 6
0
def delete_pool(pool_name):
    """
    Deletes a system pool

    :param pool_name: System pool's name

    """
    pool = _get_pool_by_name(pool_name, lockmode='update')
    u = identity.current.user
    if not pool.can_edit(u):
        raise Forbidden403('Cannot delete pool %s' % pool_name)

    systems = System.query.filter(System.pools.contains(pool))
    System.record_bulk_activity(systems, user=identity.current.user,
                                service=u'HTTP', action=u'Removed',
                                field=u'Pool',
                                old=unicode(pool),
                                new=None)
    # Since we are deleting the pool, we will have to change the active
    # access policy for all systems using the pool's policy to their
    # custom policy
    systems = System.query.filter(System.active_access_policy == pool.access_policy)
    for system in systems:
        system.active_access_policy = system.custom_access_policy
    System.record_bulk_activity(systems, user=identity.current.user,
                                service=u'HTTP',
                                field=u'Active Access Policy', action=u'Changed',
                                old = 'Pool policy: %s' % pool_name,
                                new = 'Custom access policy')
    session.delete(pool)
    activity = Activity(u, u'HTTP', u'Deleted', u'Pool', pool_name)
    session.add(activity)
    return '', 204
Ejemplo n.º 7
0
 def query(cls):        
     for key_int in System.permissable_systems(Key_Value_Int.query.outerjoin('system','user')):
         if key_int.system:
             yield CSV_KeyValue(key_int)
     for key_string in System.permissable_systems(Key_Value_String.query.outerjoin('system','user')):
         if key_string.system:
             yield CSV_KeyValue(key_string)
Ejemplo n.º 8
0
def remove_labcontroller(labcontroller):
    """
    Disables and marks a lab controller as removed.
    """
    labcontroller.removed = datetime.utcnow()
    systems = System.query.filter(System.lab_controller == labcontroller)

    # Record systems set to status=broken. Trigger any event listener listening
    # for status changes.
    for sys in systems:
        sys.mark_broken('Lab controller de-associated')
        sys.abort_queued_commands("System disassociated from lab controller")
    # de-associate systems
    System.record_bulk_activity(systems,
                                user=identity.current.user,
                                service=u'HTTP',
                                action=u'Changed',
                                field=u'Lab Controller',
                                old=labcontroller.fqdn,
                                new=None)
    systems.update({'lab_controller_id': None}, synchronize_session=False)

    # cancel running recipes
    watchdogs = Watchdog.by_status(labcontroller=labcontroller,
                                   status='active')
    for w in watchdogs:
        w.recipe.recipeset.job.cancel(
            msg='Lab controller %s has been deleted' % labcontroller.fqdn)

    # remove distro trees
    distro_tree_assocs = LabControllerDistroTree.query\
        .filter(LabControllerDistroTree.lab_controller == labcontroller)
    DistroTree.record_bulk_activity(distro_tree_assocs.join(
        LabControllerDistroTree.distro_tree),
                                    user=identity.current.user,
                                    service=u'HTTP',
                                    action=u'Removed',
                                    field=u'lab_controller_assocs',
                                    old=labcontroller.fqdn,
                                    new=None)
    distro_tree_assocs.delete(synchronize_session=False)
    labcontroller.disabled = True
    labcontroller.record_activity(user=identity.current.user,
                                  service=u'HTTP',
                                  field=u'Disabled',
                                  action=u'Changed',
                                  old=unicode(False),
                                  new=unicode(True))
    labcontroller.record_activity(user=identity.current.user,
                                  service=u'HTTP',
                                  field=u'Removed',
                                  action=u'Changed',
                                  old=unicode(False),
                                  new=unicode(True))
Ejemplo n.º 9
0
def remove_labcontroller(labcontroller):
    """
    Disables and marks a lab controller as removed.
    """
    labcontroller.removed = datetime.utcnow()

    # de-associate systems
    systems = System.query.filter(System.lab_controller == labcontroller)
    System.record_bulk_activity(systems,
                                user=identity.current.user,
                                service=u'HTTP',
                                action=u'Changed',
                                field=u'lab_controller',
                                old=labcontroller.fqdn,
                                new=None)
    systems.update({'lab_controller_id': None}, synchronize_session=False)

    # cancel running recipes
    watchdogs = Watchdog.by_status(labcontroller=labcontroller,
                                   status='active')
    for w in watchdogs:
        w.recipe.recipeset.job.cancel(msg='LabController %s has been deleted' %
                                      labcontroller.fqdn)

    # remove distro trees
    distro_tree_assocs = LabControllerDistroTree.query\
        .filter(LabControllerDistroTree.lab_controller == labcontroller)\
        .join(LabControllerDistroTree.distro_tree)
    DistroTree.record_bulk_activity(distro_tree_assocs,
                                    user=identity.current.user,
                                    service=u'HTTP',
                                    action=u'Removed',
                                    field=u'lab_controller_assocs',
                                    old=labcontroller.fqdn,
                                    new=None)
    distro_tree_assocs.delete(synchronize_session=False)
    labcontroller.disabled = True
    labcontroller.record_activity(user=identity.current.user,
                                  service=u'HTTP',
                                  field=u'Disabled',
                                  action=u'Changed',
                                  old=unicode(False),
                                  new=unicode(True))
    labcontroller.record_activity(user=identity.current.user,
                                  service=u'HTTP',
                                  field=u'Removed',
                                  action=u'Changed',
                                  old=unicode(False),
                                  new=unicode(True))
Ejemplo n.º 10
0
def doit():
    distro_trees = []
    for id in request.form.getlist('distro_tree_id'):
        try:
            distro_trees.append(DistroTree.by_id(id))
        except NoResultFound:
            raise BadRequest400('Distro tree %r does not exist' % id)
    job_details = {}
    job_details['pick'] = request.form.get('pick') or 'auto'
    if job_details['pick'] == 'fqdn':
        try:
            job_details['system'] = System.by_fqdn(request.form.get('system'),
                    identity.current.user)
        except NoResultFound:
            raise BadRequest400('System %s not found' % request.form.get('system'))
    elif job_details['pick'] == 'lab':
        try:
            job_details['lab'] = LabController.by_name(request.form.get('lab'))
        except NoResultFound:
            raise BadRequest400('Lab controller %s not found' % request.form.get('lab'))
    days = int(request.form.get('reserve_days') or DEFAULT_RESERVE_DAYS)
    days = min(days, MAX_DAYS_PROVISION)
    job_details['reservetime'] = days * 24 * 60 * 60
    job_details['whiteboard'] = request.form.get('whiteboard')
    job_details['ks_meta'] = request.form.get('ks_meta')
    job_details['koptions'] = request.form.get('koptions')
    job_details['koptions_post'] = request.form.get('koptions_post')
    with convert_internal_errors():
        job = Job.provision_system_job(distro_trees, **job_details)
    return 'Created %s' % job.t_id, 201, [('Location', absolute_url('/jobs/%s' % job.id))]
Ejemplo n.º 11
0
 def test_creating_a_system_with_hardware_essentials(self):
     s = requests.Session()
     s.post(get_server_base() + 'login',
            data={
                'user_name': self.user.user_name,
                'password': u'password'
            }).raise_for_status()
     fqdn = data_setup.unique_name(u'newsystem%s')
     data = {
         'fqdn': fqdn,
         'lab_controller_id': self.lc.id,
         'arches': [u'i386', u'x86_64'],
         'location': u'dummylocation',
         'lender': u'dummylender',
         'kernel_type': u'highbank'
     }
     response = post_json(get_server_base() + 'systems/',
                          session=s,
                          data=data)
     with session.begin():
         system = System.by_fqdn(fqdn, self.user)
         self.assertEquals(system.fqdn, fqdn)
         self.assertEquals(system.lab_controller_id, self.lc.id)
         self.assertTrue(Arch.by_name(u'i386') in system.arch)
         self.assertTrue(Arch.by_name(u'x86_64') in system.arch)
         self.assertEquals(system.location, u'dummylocation')
         self.assertEquals(system.lender, u'dummylender')
         self.assertEquals(system.kernel_type,
                           KernelType.by_name(u'highbank'))
Ejemplo n.º 12
0
 def test_creating_a_system_with_hardware_details(self):
     s = requests.Session()
     s.post(get_server_base() + 'login',
            data={
                'user_name': self.user.user_name,
                'password': u'password'
            }).raise_for_status()
     fqdn = data_setup.unique_name(u'newsystem%s')
     data = {
         'fqdn': fqdn,
         'hypervisor': u'KVM',
         'vendor': u'dummyvendor',
         'location': u'dummylocation',
         'model': u'dummymodel',
         'serial_number': u'dummynumber',
         'mac_address': u'dummymacaddress',
         'memory': 111111,
         'numa_nodes': 5,
     }
     response = post_json(get_server_base() + 'systems/',
                          session=s,
                          data=data)
     with session.begin():
         system = System.by_fqdn(fqdn, self.user)
         self.assertEquals(system.fqdn, fqdn)
         self.assertEquals(system.hypervisor, Hypervisor.by_name(u'KVM'))
         self.assertEquals(system.location, u'dummylocation')
         self.assertEquals(system.serial, u'dummynumber')
         self.assertEquals(system.mac_address, u'dummymacaddress')
         self.assertEquals(system.memory, 111111)
         self.assertEquals(system.numa.nodes, 5)
Ejemplo n.º 13
0
 def test_power_quiescent_period_statefulness_not_elapsed(self):
     if daemons_running_externally():
         raise SkipTest('cannot examine logs of remote beaker-provision')
     provision_process, = [p for p in processes if p.name == \
         'beaker-provision']
     # Initial lookup of this system will reveal no state, so will delay
     # for the whole quiescent period
     try:
         provision_process.start_output_capture()
         with session.begin():
             system = data_setup.create_system(lab_controller=self.get_lc())
             system.power.power_type = PowerType.lazy_create(name=u'dummy')
             system.power.power_quiescent_period = 1
             system.power.power_id = u'' # make power script not sleep
             system.power.delay_until = None
             system.action_power(action=u'off', service=u'testdata')
         wait_for_commands_to_finish(system, timeout=10)
     finally:
         provision_output = provision_process.finish_output_capture()
     self.assertIn('Entering quiescent period, delaying 1 seconds for '
         'command %s'  % system.command_queue[0].id, provision_output)
     # Increase the quiescent period, to ensure we enter it
     try:
         provision_process.start_output_capture()
         with session.begin():
             system = System.by_id(system.id, User.by_user_name('admin'))
             system.power.power_quiescent_period = 10
             system.action_power(action=u'on', service=u'testdata')
         wait_for_commands_to_finish(system, timeout=15)
     finally:
         provision_output = provision_process.finish_output_capture()
     self.assertIn('Entering quiescent period', provision_output)
Ejemplo n.º 14
0
 def test_lookup_secret_fqdn(self):
     with session.begin():
         system = data_setup.create_system()
         system.private = True
     lab_controller_user = LabController.by_name(self.lc_fqdn).user
     system2 = System.by_fqdn(str(system.fqdn), user=lab_controller_user)
     self.assertEquals(system, system2)
Ejemplo n.º 15
0
 def test_creating_a_system_with_power_settings(self):
     s = requests.Session()
     s.post(get_server_base() + 'login', data={'user_name': self.user.user_name,
                                               'password': u'password'}).raise_for_status()
     fqdn = data_setup.unique_name(u'newsystem%s')
     data = {
         'fqdn': fqdn,
         'lab_controller_id': self.lc.id,
         'power_type': u'apc_snmp_then_etherwake',
         'power_address': u'dummyaddress',
         'power_user': u'dummyuser',
         'power_password': u'dummypassword',
         'power_id': u'dummyvm',
         'power_quiescent_period': 5,
         'release_action': u'LeaveOn',
         'reprovision_distro_tree': {'id': self.distro_tree.id},
     }
     response = post_json(get_server_base() + 'systems/', session=s, data=data)
     with session.begin():
         system = System.by_fqdn(fqdn, self.user)
         self.assertEquals(system.power.power_type, PowerType.by_name(u'apc_snmp_then_etherwake'))
         self.assertEquals(system.power.power_address, u'dummyaddress')
         self.assertEquals(system.power.power_user, u'dummyuser')
         self.assertEquals(system.power.power_passwd, u'dummypassword')
         self.assertEquals(system.power.power_id, u'dummyvm')
         self.assertEquals(system.power.power_quiescent_period, 5)
         self.assertEquals(system.release_action, ReleaseAction.leave_on)
         self.assertEquals(system.reprovision_distro_tree, self.distro_tree)
Ejemplo n.º 16
0
 def test_creating_a_system_with_hardware_details(self):
     s = requests.Session()
     s.post(get_server_base() + 'login', data={'user_name': self.user.user_name,
                                               'password': u'password'}).raise_for_status()
     fqdn = data_setup.unique_name(u'newsystem%s')
     data = {
         'fqdn': fqdn,
         'hypervisor': u'KVM',
         'vendor': u'dummyvendor',
         'location': u'dummylocation',
         'model': u'dummymodel',
         'serial_number': u'dummynumber',
         'mac_address': u'dummymacaddress',
         'memory': 111111,
         'numa_nodes': 5,
     }
     response = post_json(get_server_base() + 'systems/', session=s, data=data)
     with session.begin():
         system = System.by_fqdn(fqdn, self.user)
         self.assertEquals(system.fqdn, fqdn)
         self.assertEquals(system.hypervisor, Hypervisor.by_name(u'KVM'))
         self.assertEquals(system.location, u'dummylocation')
         self.assertEquals(system.serial, u'dummynumber')
         self.assertEquals(system.mac_address, u'dummymacaddress')
         self.assertEquals(system.memory, 111111)
         self.assertEquals(system.numa.nodes, 5)
Ejemplo n.º 17
0
def doit():
    distro_trees = []
    for id in request.form.getlist('distro_tree_id'):
        try:
            distro_trees.append(DistroTree.by_id(id))
        except NoResultFound:
            raise BadRequest400('Distro tree %r does not exist' % id)
    job_details = {}
    job_details['pick'] = request.form.get('pick') or 'auto'
    if job_details['pick'] == 'fqdn':
        try:
            job_details['system'] = System.by_fqdn(request.form.get('system'),
                                                   identity.current.user)
        except NoResultFound:
            raise BadRequest400('System %s not found' %
                                request.form.get('system'))
    elif job_details['pick'] == 'lab':
        try:
            job_details['lab'] = LabController.by_name(request.form.get('lab'))
        except NoResultFound:
            raise BadRequest400('Lab controller %s not found' %
                                request.form.get('lab'))
    days = int(request.form.get('reserve_days') or DEFAULT_RESERVE_DAYS)
    days = min(days, MAX_DAYS_PROVISION)
    job_details['reservetime'] = days * 24 * 60 * 60
    job_details['whiteboard'] = request.form.get('whiteboard')
    job_details['ks_meta'] = request.form.get('ks_meta')
    job_details['koptions'] = request.form.get('koptions')
    job_details['koptions_post'] = request.form.get('koptions_post')
    with convert_internal_errors():
        job = Job.provision_system_job(distro_trees, **job_details)
    return 'Created %s' % job.t_id, 201, [('Location',
                                           url('/jobs/%s' % job.id))]
Ejemplo n.º 18
0
    def get_installation_for_system(self, fqdn):
        system = System.by_fqdn(fqdn, identity.current.user)
        if not system.installations:
            raise ValueError('System %s has never been provisioned' % fqdn)
        installation = system.installations[0]
        distro_tree = installation.distro_tree
        distro_tree_url = distro_tree.url_in_lab(system.lab_controller, 'http')
        if not distro_tree_url:
            raise ValueError('No usable URL found for distro tree %s in lab %s'
                    % (distro_tree.id, system.lab_controller.fqdn))

        if system.kernel_type.uboot:
            by_kernel = ImageType.uimage
            by_initrd = ImageType.uinitrd
        else:
            by_kernel = ImageType.kernel
            by_initrd = ImageType.initrd

        kernel = distro_tree.image_by_type(by_kernel, system.kernel_type)
        if not kernel:
            raise ValueError('Kernel image not found for distro tree %s' % distro_tree.id)
        initrd = distro_tree.image_by_type(by_initrd, system.kernel_type)
        if not initrd:
            raise ValueError('Initrd image not found for distro tree %s' % distro_tree.id)
        return {
            'kernel_url': urlparse.urljoin(distro_tree_url, kernel.path),
            'initrd_url': urlparse.urljoin(distro_tree_url, initrd.path),
            'kernel_options': installation.kernel_options or '',
            'distro_tree_urls': [lca.url for lca in distro_tree.lab_controller_assocs
                    if lca.lab_controller == system.lab_controller],
        }
Ejemplo n.º 19
0
 def test_create_system_set_host_hypervisor(self):
     fqdn = data_setup.unique_name(u'mysystem%s')
     run_client(['bkr', 'system-create', fqdn, '--host-hypervisor=KVM'])
     with session.begin():
         system = System.by_fqdn(fqdn, User.by_user_name(u'admin'))
         self.assertEquals(str(system.hypervisor), u'KVM')
         self.assertEquals(system.activity[0].new_value, u'KVM')
Ejemplo n.º 20
0
 def test_power_quiescent_period_statefulness_not_elapsed(self):
     if daemons_running_externally():
         raise SkipTest('cannot examine logs of remote beaker-provision')
     provision_process, = [p for p in processes if p.name == \
         'beaker-provision']
     # Initial lookup of this system will reveal no state, so will delay
     # for the whole quiescent period
     try:
         provision_process.start_output_capture()
         with session.begin():
             system = data_setup.create_system(lab_controller=self.get_lc())
             system.power.power_type = PowerType.lazy_create(name=u'dummy')
             system.power.power_quiescent_period = 1
             system.power.power_id = u'' # make power script not sleep
             system.power.delay_until = None
             system.action_power(action=u'off', service=u'testdata')
         wait_for_commands_to_finish(system, timeout=10)
     finally:
         provision_output = provision_process.finish_output_capture()
     self.assertIn('Entering quiescent period, delaying 1 seconds for '
         'command %s'  % system.command_queue[0].id, provision_output)
     # Increase the quiescent period, to ensure we enter it
     try:
         provision_process.start_output_capture()
         with session.begin():
             system = System.by_id(system.id, User.by_user_name('admin'))
             system.power.power_quiescent_period = 10
             system.action_power(action=u'on', service=u'testdata')
         wait_for_commands_to_finish(system, timeout=15)
     finally:
         provision_output = provision_process.finish_output_capture()
     self.assertIn('Entering quiescent period', provision_output)
Ejemplo n.º 21
0
    def get_installation_for_system(self, fqdn):
        system = System.by_fqdn(fqdn, identity.current.user)
        if not system.installations:
            raise ValueError('System %s has never been provisioned' % fqdn)
        installation = system.installations[0]
        distro_tree = installation.distro_tree
        distro_tree_url = distro_tree.url_in_lab(system.lab_controller, 'http')
        if not distro_tree_url:
            raise ValueError('No usable URL found for distro tree %s in lab %s'
                    % (distro_tree.id, system.lab_controller.fqdn))

        if system.kernel_type.uboot:
            by_kernel = ImageType.uimage
            by_initrd = ImageType.uinitrd
        else:
            by_kernel = ImageType.kernel
            by_initrd = ImageType.initrd

        kernel = distro_tree.image_by_type(by_kernel, system.kernel_type)
        if not kernel:
            raise ValueError('Kernel image not found for distro tree %s' % distro_tree.id)
        initrd = distro_tree.image_by_type(by_initrd, system.kernel_type)
        if not initrd:
            raise ValueError('Initrd image not found for distro tree %s' % distro_tree.id)
        return {
            'kernel_url': urlparse.urljoin(distro_tree_url, kernel.path),
            'initrd_url': urlparse.urljoin(distro_tree_url, initrd.path),
            'kernel_options': installation.kernel_options or '',
            'distro_tree_urls': [lca.url for lca in distro_tree.lab_controller_assocs
                    if lca.lab_controller == system.lab_controller],
        }
Ejemplo n.º 22
0
 def test_create_system_set_host_hypervisor(self):
     fqdn = data_setup.unique_name(u'mysystem%s')
     run_client(['bkr', 'system-create', fqdn,
                 '--host-hypervisor=KVM'])
     with session.begin():
         system = System.by_fqdn(fqdn, User.by_user_name(u'admin'))
         self.assertEquals(str(system.hypervisor), u'KVM')
         self.assertEquals(system.activity[0].new_value, u'KVM')
Ejemplo n.º 23
0
 def save_system(self, **kw):
     try:
         with convert_db_lookup_error('No such system: %s' %
                                      kw['system']['text']):
             system = System.by_fqdn(kw['system']['text'],
                                     identity.current.user)
     except DatabaseLookupError, e:
         flash(unicode(e))
         redirect("./edit?group_id=%s" % kw['group_id'])
Ejemplo n.º 24
0
 def get_sys(x):
     systems = System.all(identity.current.user). \
               filter(System.groups.contains(x)). \
               filter(System.status != SystemStatus.removed).all()
     if len(systems):
         return make_link('systems?group_id=%s' % x.group_id,
                          u'System count: %s' % len(systems))
     else:
         return 'System count: 0'
Ejemplo n.º 25
0
 def test_create_system_set_labcontroller(self):
     fqdn = data_setup.unique_name(u'mysystem%s')
     lc = data_setup.create_labcontroller()
     run_client(['bkr', 'system-create',
                 '--lab-controller', str(lc.fqdn),
                 fqdn])
     with session.begin():
         system = System.by_fqdn(fqdn, User.by_user_name(u'admin'))
         self.assertTrue(system.lab_controller, lc)
Ejemplo n.º 26
0
 def test_create_system_set_labcontroller(self):
     fqdn = data_setup.unique_name(u'mysystem%s')
     lc = data_setup.create_labcontroller()
     run_client(['bkr', 'system-create',
                 '--lab-controller', str(lc.fqdn),
                 fqdn])
     with session.begin():
         system = System.by_fqdn(fqdn, User.by_user_name(u'admin'))
         self.assertTrue(system.lab_controller, lc)
Ejemplo n.º 27
0
    def update_loan(self, fqdn=None, loaned=None, loan_comment=None, **kw):
        """Update system loan and loan comment.

        Returns the loanee
        """
        # The formal param 'loaned' is dictated to us by widgets.SystemForm...
        loaning_to = loaned
        system = System.by_fqdn(fqdn, identity.current.user)
        system.change_loan(loaning_to, loan_comment)
        return loaning_to if loaning_to else ''
Ejemplo n.º 28
0
    def clear_netboot_form(self, fqdn):
        """Queues the clear netboot commands

        Enqueues the command to clear any netboot configuration for this
        system, and on success redirects to the system page.
        """
        system = System.by_fqdn(fqdn, identity.current.user)
        system.clear_netboot(service=u'WEBUI')
        flash(_(u'Clear netboot command enqueued'))
        redirect(u'../view/%s' % fqdn)
Ejemplo n.º 29
0
    def clear_netboot(self, fqdn):
        """
        Clears any netboot configuration in effect for the system with the
        given fully-qualified domain name.

        .. verisonadded:: 0.9
        """
        system = System.by_fqdn(fqdn, identity.current.user)
        system.clear_netboot(service=u'XMLRPC')
        return system.fqdn  # because turbogears makes us return something
Ejemplo n.º 30
0
    def clear_netboot(self, fqdn):
        """
        Clears any netboot configuration in effect for the system with the
        given fully-qualified domain name.

        .. verisonadded:: 0.9
        """
        system = System.by_fqdn(fqdn, identity.current.user)
        system.clear_netboot(service=u'XMLRPC')
        return system.fqdn # because turbogears makes us return something
Ejemplo n.º 31
0
 def query(cls):
     for system in System.all(identity.current.user):
         for key_int in (Key_Value_Int.query.join('system')
                                            .yield_per(10000)
                                            .filter(System.id == system.id)):
             yield CSV_KeyValue(key_int)
         for key_string in (Key_Value_String.query.join('system')
                                            .yield_per(10000)
                                            .filter(System.id == system.id)):
             yield CSV_KeyValue(key_string)
Ejemplo n.º 32
0
 def query(cls):
     for system in System.all(identity.current.user):
         for exclude in (ExcludeOSMajor.query.join('system')
                                             .yield_per(10000)
                                             .filter(System.id == system.id)):
             yield CSV_Exclude(exclude)
         for exclude in (ExcludeOSVersion.query.join('system')
                                               .yield_per(10000)
                                               .filter(System.id == system.id)):
             yield CSV_Exclude(exclude)
Ejemplo n.º 33
0
    def reserve(self, distro_tree_id, system_id=None, lab_controller_id=None):
        """ Either queue or provision the system now """
        if system_id == 'search':
            redirect('/reserve_system', distro_tree_id=distro_tree_id)
        elif system_id:
            try:
                system = System.by_id(system_id, identity.current.user)
            except InvalidRequestError:
                flash(_(u'Invalid System ID %s' % system_id))
            system_name = system.fqdn
        else:
            system_name = 'Any System'
        distro_names = [] 

        return_value = dict(
                            system_id = system_id, 
                            system = system_name,
                            distro = '',
                            distro_tree_ids = [],
                            )
        warn = None
        if not isinstance(distro_tree_id, list):
            distro_tree_id = [distro_tree_id]
        for id in distro_tree_id:
            try:
                distro_tree = DistroTree.by_id(id)
                if System.by_type(type=SystemType.machine,
                        systems=distro_tree.systems(user=identity.current.user))\
                        .count() < 1:
                    warn = _(u'No systems compatible with %s') % distro_tree
                distro_names.append(unicode(distro_tree))
                return_value['distro_tree_ids'].append(id)
            except NoResultFound:
                flash(_(u'Invalid distro tree ID %s') % id)
        distro = ", ".join(distro_names)
        return_value['distro'] = distro
        
        return dict(form=self.reserveform,
                    action='./doit',
                    value = return_value,
                    warn=warn,
                    options = None,
                    title='Reserve %s' % system_name)
Ejemplo n.º 34
0
 def test_create_system_set_arches(self):
     fqdn = data_setup.unique_name(u'mysystem%s')
     run_client(['bkr', 'system-create',
                 '--arch', u'i386',
                 '--arch', u'x86_64',
                 fqdn])
     with session.begin():
         system = System.by_fqdn(fqdn, User.by_user_name(u'admin'))
         self.assertIn(Arch.by_name(u'i386'), system.arch)
         self.assertIn(Arch.by_name(u'x86_64'), system.arch)
Ejemplo n.º 35
0
 def test_create_system_set_arches(self):
     fqdn = data_setup.unique_name(u'mysystem%s')
     run_client(['bkr', 'system-create',
                 '--arch', u'i386',
                 '--arch', u'x86_64',
                 fqdn])
     with session.begin():
         system = System.by_fqdn(fqdn, User.by_user_name(u'admin'))
         self.assertIn(Arch.by_name(u'i386'), system.arch)
         self.assertIn(Arch.by_name(u'x86_64'), system.arch)
Ejemplo n.º 36
0
 def test_create_system_set_condition(self):
     fqdn = data_setup.unique_name(u'mysystem%s')
     with session.begin():
         lc = data_setup.create_labcontroller()
     run_client(['bkr', 'system-create', fqdn,
                 '--lab-controller', str(lc.fqdn),
                 '--condition=Automated'])
     with session.begin():
         system = System.by_fqdn(fqdn, User.by_user_name(u'admin'))
         self.assertTrue(system.lab_controller, lc)
         self.assertEquals(str(system.status), u'Automated')
Ejemplo n.º 37
0
def remove_labcontroller(labcontroller):
    """
    Disables and marks a lab controller as removed.
    """
    labcontroller.removed = datetime.utcnow()
    systems = System.query.filter(System.lab_controller == labcontroller)

    # Record systems set to status=broken. Trigger any event listener listening
    # for status changes.
    for sys in systems:
        sys.mark_broken('Lab controller de-associated')

    # de-associate systems
    System.record_bulk_activity(systems, user=identity.current.user,
                                service=u'HTTP', action=u'Changed', field=u'Lab Controller',
                                old=labcontroller.fqdn, new=None)
    systems.update({'lab_controller_id': None},
                   synchronize_session=False)

    # cancel running recipes
    watchdogs = Watchdog.by_status(labcontroller=labcontroller,
                                   status='active')
    for w in watchdogs:
        w.recipe.recipeset.job.cancel(msg='Lab controller %s has been deleted' % labcontroller.fqdn)

    # remove distro trees
    distro_tree_assocs = LabControllerDistroTree.query\
        .filter(LabControllerDistroTree.lab_controller == labcontroller)\
        .join(LabControllerDistroTree.distro_tree)
    DistroTree.record_bulk_activity(
        distro_tree_assocs, user=identity.current.user,
        service=u'HTTP', action=u'Removed', field=u'lab_controller_assocs',
        old=labcontroller.fqdn, new=None)
    distro_tree_assocs.delete(synchronize_session=False)
    labcontroller.disabled = True
    labcontroller.record_activity(
        user=identity.current.user, service=u'HTTP',
        field=u'Disabled', action=u'Changed', old=unicode(False), new=unicode(True))
    labcontroller.record_activity(
        user=identity.current.user, service=u'HTTP',
        field=u'Removed', action=u'Changed', old=unicode(False), new=unicode(True))
Ejemplo n.º 38
0
 def test_create_system_set_condition(self):
     fqdn = data_setup.unique_name(u'mysystem%s')
     with session.begin():
         lc = data_setup.create_labcontroller()
     run_client([
         'bkr', 'system-create', fqdn, '--lab-controller',
         str(lc.fqdn), '--condition=Automated'
     ])
     with session.begin():
         system = System.by_fqdn(fqdn, User.by_user_name(u'admin'))
         self.assertTrue(system.lab_controller, lc)
         self.assertEquals(str(system.status), u'Automated')
Ejemplo n.º 39
0
    def get_group_systems(self, group_id=None, *args, **kw):
        try:
            group = Group.by_id(group_id)
        except DatabaseLookupError:
            log.exception('Group id %s is not a valid group id' % group_id)
            response.status = 403
            return ['Invalid Group Id']

        systems = System.all(identity.current.user).filter(System.groups.contains(group)). \
                  filter(System.status != SystemStatus.removed)

        return [(system.id, system.fqdn) for system in systems]
Ejemplo n.º 40
0
 def filter(self, joins):
     arch_name = self.get_xml_attr('arch', unicode, None)
     try:
         arch = Arch.by_name(arch_name)
     except ValueError:
         return (joins, false())
     osmajor = self.get_xml_attr('osmajor', unicode, None)
     if not osmajor:
         return (joins, false())
     osminor = self.get_xml_attr('osminor', unicode, None) or None
     clause = System.compatible_with_distro_tree(arch, osmajor, osminor)
     return (joins, clause)
Ejemplo n.º 41
0
 def filter(self, joins):
     arch_name = self.get_xml_attr('arch', unicode, None)
     try:
         arch = Arch.by_name(arch_name)
     except ValueError:
         return (joins, false())
     osmajor = self.get_xml_attr('osmajor', unicode, None)
     if not osmajor:
         return (joins, false())
     osminor = self.get_xml_attr('osminor', unicode, None) or None
     clause = System.compatible_with_distro_tree(arch, osmajor, osminor)
     return (joins, clause)
Ejemplo n.º 42
0
    def systems(self,group_id=None,*args,**kw):
        try:
            group = Group.by_id(group_id)
        except NoResultFound:
            log.exception('Group id %s is not a valid group id' % group_id)
            flash(_(u'Need a valid group to search on'))
            redirect('../groups/mine')

        systems = System.all(identity.current.user).filter(System.groups.contains(group))
        title = 'Systems in Group %s' % group.group_name
        from bkr.server.controllers import Root
        return Root()._systems(systems,title, group_id = group_id,**kw)
Ejemplo n.º 43
0
 def add_completed_command(self, fqdn, action):
     # Reports completion of a command that was executed
     # synchronously by the lab controller
     user = identity.current.user
     system = System.by_fqdn(fqdn, user)
     cmd = Command(user=user, service=u"XMLRPC", action=action,
             status=CommandStatus.completed)
     cmd.start_time = cmd.finish_time = datetime.utcnow()
     system.command_queue.append(cmd)
     session.flush() # Populates cmd.system (needed for next call)
     cmd.log_to_system_history()
     return True
Ejemplo n.º 44
0
 def check_power_action(self, action):
     with session.begin():
         user = data_setup.create_user(password=u'password')
         system = data_setup.create_system()
         data_setup.configure_system_power(system)
         system.lab_controller = self.lab_controller
         system.user = None
     self.server.auth.login_password(user.user_name, 'password')
     self.server.systems.power(action, system.fqdn)
     self.assertEqual(
             System.by_fqdn(system.fqdn, user).command_queue[0].action,
             action)
Ejemplo n.º 45
0
def create_system(arch=u'i386', type=SystemType.machine, status=SystemStatus.automated,
        owner=None, fqdn=None, shared=True, exclude_osmajor=[],
        exclude_osversion=[], hypervisor=None, kernel_type=None,
        date_added=None,  **kw):
    if owner is None:
        owner = create_user()
    if fqdn is None:
        fqdn = unique_name(u'system%s.testdata')
    if System.query.filter(System.fqdn == fqdn).count():
        raise ValueError('Attempted to create duplicate system %s' % fqdn)
    system = System(fqdn=fqdn,type=type, owner=owner,
                status=status, **kw)
    if date_added is not None:
        system.date_added = date_added
    system.shared = shared
    system.arch.append(Arch.by_name(arch))
    configure_system_power(system)
    system.excluded_osmajor.extend(ExcludeOSMajor(arch=Arch.by_name(arch),
            osmajor=osmajor) for osmajor in exclude_osmajor)
    system.excluded_osversion.extend(ExcludeOSVersion(arch=Arch.by_name(arch),
            osversion=osversion) for osversion in exclude_osversion)
    if hypervisor:
        system.hypervisor = Hypervisor.by_name(hypervisor)
    if kernel_type:
        system.kernel_type = KernelType.by_name(kernel_type)
    system.date_modified = datetime.datetime.utcnow()
    log.debug('Created system %r', system)
    return system
Ejemplo n.º 46
0
 def add_completed_command(self, fqdn, action):
     # Reports completion of a command that was executed
     # synchronously by the lab controller
     user = identity.current.user
     system = System.by_fqdn(fqdn, user)
     cmd = CommandActivity(user=user,
                           service=u"XMLRPC",
                           action=action,
                           status=CommandStatus.completed)
     system.command_queue.append(cmd)
     session.flush() # Populates cmd.system (needed for next call)
     cmd.log_to_system_history()
     return True
Ejemplo n.º 47
0
 def reserve(self, action='.', *args, **kw):
     from bkr.server.controllers import Root
     default_columns = ('System/Name',
                        'System/Reserved',
                        'System/User',
                        'System/Pools',
                        'System/LoanedTo',)
     return Root()._systems(systems=System.all(identity.current.user)
                            .join('open_reservation')
                            .options(contains_eager(System.open_reservation)),
                            title=u'Reserve Report',
                            default_result_columns=default_columns,
                            *args, **kw)
Ejemplo n.º 48
0
    def release(self, fqdn):
        """
        Releases a reservation on the system with the given fully-qualified 
        domain name.

        The caller must be the current user of a system (i.e. must have 
        successfully reserved it previously).

        .. versionadded:: 0.6
        """
        system = System.by_fqdn(fqdn, identity.current.user)
        system.unreserve_manually_reserved(service=u'XMLRPC')
        return system.fqdn  # because turbogears makes us return something
Ejemplo n.º 49
0
    def release(self, fqdn):
        """
        Releases a reservation on the system with the given fully-qualified 
        domain name.

        The caller must be the current user of a system (i.e. must have 
        successfully reserved it previously).

        .. versionadded:: 0.6
        """
        system = System.by_fqdn(fqdn, identity.current.user)
        system.unreserve_manually_reserved(service=u'XMLRPC')
        return system.fqdn # because turbogears makes us return something
Ejemplo n.º 50
0
 def _import_row(self, data, log):
     if data['csv_type'] in system_types and ('fqdn' in data
                                              or 'id' in data):
         if data.get('id', None):
             try:
                 system = System.query.filter(System.id == data['id']).one()
             except InvalidRequestError as e:
                 raise ValueError('Non-existent system id')
         else:
             try:
                 system = System.query.filter(
                     System.fqdn == data['fqdn']).one()
             except InvalidRequestError:
                 # Create new system with some defaults
                 # Assume the system is broken until proven otherwise.
                 # Also assumes its a machine.  we have to pick something
                 system = System(fqdn=data['fqdn'],
                                 owner=identity.current.user,
                                 type=SystemType.machine,
                                 status=SystemStatus.broken)
                 session.add(system)
                 # new systems are visible to everybody by default
                 system.custom_access_policy = SystemAccessPolicy()
                 system.custom_access_policy.add_rule(SystemPermission.view,
                                                      everybody=True)
         if not system.can_edit(identity.current.user):
             raise ValueError('You are not the owner of %s' % system.fqdn)
         # we change the FQDN only when a valid system id is supplied
         if not data.get('id', None):
             data.pop('fqdn')
         self.from_csv(system, data, log)
     elif data['csv_type'] == 'user_group' and 'user' in data:
         user = User.by_user_name(data['user'])
         if user is None:
             raise ValueError('%s is not a valid user' % data['user'])
         CSV_GroupUser.from_csv(user, data, log)
     else:
         raise ValueError('Invalid csv_type %s or missing required fields' %
                          data['csv_type'])
Ejemplo n.º 51
0
    def reserve(self, fqdn):
        """
        "Reserves" (a.k.a. "takes") the system with the given fully-qualified domain 
        name. The caller then becomes the user of the system, and can 
        provision it at will.

        A system may only be reserved when: its condition is 'Manual', it is not 
        currently in use, and the caller has permission to use the system.

        .. versionadded:: 0.6
        """
        system = System.by_fqdn(fqdn, identity.current.user)
        system.reserve_manually(service=u'XMLRPC')
        return system.fqdn # because turbogears makes us return something
Ejemplo n.º 52
0
    def systems(self, group_id=None, *args, **kw):
        try:
            group = Group.by_id(group_id)
        except DatabaseLookupError:
            log.exception('Group id %s is not a valid group id' % group_id)
            flash(_(u'Need a valid group to search on'))
            redirect('../groups/mine')

        systems = System.all(identity.current.user). \
                  filter(System.groups.contains(group)). \
                  filter(System.status != SystemStatus.removed)
        title = 'Systems in Group %s' % group.group_name
        from bkr.server.controllers import Root
        return Root()._systems(systems, title, group_id=group_id, **kw)
Ejemplo n.º 53
0
    def reserve(self, fqdn):
        """
        "Reserves" (a.k.a. "takes") the system with the given fully-qualified domain 
        name. The caller then becomes the user of the system, and can 
        provision it at will.

        A system may only be reserved when: its condition is 'Manual', it is not 
        currently in use, and the caller has permission to use the system.

        .. versionadded:: 0.6
        """
        system = System.by_fqdn(fqdn, identity.current.user)
        system.reserve_manually(service=u'XMLRPC')
        return system.fqdn  # because turbogears makes us return something
Ejemplo n.º 54
0
    def history(self, fqdn, since=None):
        """
        Returns the history for the given system.
        If the *since* argument is given, all history entries between that 
        timestamp and the present are returned. By default, history entries 
        from the past 24 hours are returned.

        History entries are returned as a list of structures (dicts), each of 
        which has the following keys:

            'created'
                Timestamp of the activity
            'user'
                Username of the user who performed the action
            'service'
                Service by which the action was performed (e.g. 'XMLRPC')
            'action'
                Action which was performed (e.g. 'Changed')
            'field_name'
                Name of the field which was acted upon
            'old_value'
                Value of the field before the action (if any)
            'new_value'
                Value of the field after the action (if any)

        Note that field names and actions are recorded in human-readable form, 
        which might not be ideal for machine parsing.

        All timestamps are expressed in UTC.

        .. versionadded:: 0.6.6
        """
        if since is None:
            since = datetime.datetime.utcnow() - datetime.timedelta(days=1)
        else:
            if not isinstance(since, datetime.datetime):
                raise TypeError("'since' must be an XML-RPC datetime")
        system = System.by_fqdn(fqdn, identity.current.user)
        activities = SystemActivity.query.filter(
            and_(SystemActivity.object == system,
                 SystemActivity.created >= since))
        return [
            dict(created=a.created,
                 user=a.user.user_name,
                 service=a.service,
                 action=a.action,
                 field_name=a.field_name,
                 old_value=a.old_value,
                 new_value=a.new_value) for a in activities
        ]
Ejemplo n.º 55
0
 def _import_row(self, data, log):
     if data['csv_type'] in system_types and ('fqdn' in data or 'id' in data):
         if data.get('id', None):
             try:
                 system = System.query.filter(System.id == data['id']).one()
             except InvalidRequestError as e:
                 raise ValueError('Non-existent system id')
         else:
             try:
                 system = System.query.filter(System.fqdn == data['fqdn']).one()
             except InvalidRequestError:
                 # Create new system with some defaults
                 # Assume the system is broken until proven otherwise.
                 # Also assumes its a machine.  we have to pick something
                 system = System(fqdn=data['fqdn'],
                             owner=identity.current.user,
                             type=SystemType.machine,
                             status=SystemStatus.broken)
                 session.add(system)
                 # new systems are visible to everybody by default
                 system.custom_access_policy = SystemAccessPolicy()
                 system.custom_access_policy.add_rule(
                         SystemPermission.view, everybody=True)
         if not system.can_edit(identity.current.user):
             raise ValueError('You are not the owner of %s' % system.fqdn)
         # we change the FQDN only when a valid system id is supplied
         if not data.get('id', None):
             data.pop('fqdn')
         self.from_csv(system, data, log)
     elif data['csv_type'] == 'user_group' and 'user' in data:
         user = User.by_user_name(data['user'])
         if user is None:
             raise ValueError('%s is not a valid user' % data['user'])
         CSV_GroupUser.from_csv(user, data, log)
     else:
         raise ValueError('Invalid csv_type %s or missing required fields'
                 % data['csv_type'])
Ejemplo n.º 56
0
 def test_create_system_defaults(self):
     fqdn = data_setup.unique_name(u'mysystem%s')
     run_client(['bkr', 'system-create', fqdn])
     with session.begin():
         system = System.by_fqdn(fqdn, User.by_user_name(u'admin'))
         self.assertTrue(system.owner.user_name, data_setup.ADMIN_USER)
         self.assertTrue(system.custom_access_policy.grants_everybody(
                 SystemPermission.view))
     # duplicate
     try:
         run_client(['bkr', 'system-create', fqdn])
         self.fail('Must fail')
     except ClientError as e:
         self.assertIn("System with fqdn %r already exists" % fqdn,
                       e.stderr_output)
Ejemplo n.º 57
0
 def test_create_system_defaults(self):
     fqdn = data_setup.unique_name(u'mysystem%s')
     run_client(['bkr', 'system-create', fqdn])
     with session.begin():
         system = System.by_fqdn(fqdn, User.by_user_name(u'admin'))
         self.assertTrue(system.owner.user_name, data_setup.ADMIN_USER)
         self.assertTrue(system.custom_access_policy.grants_everybody(
                 SystemPermission.view))
     # duplicate
     try:
         run_client(['bkr', 'system-create', fqdn])
         self.fail('Must fail')
     except ClientError as e:
         self.assertIn("System with fqdn %r already exists" % fqdn,
                       e.stderr_output)
Ejemplo n.º 58
0
def create_system(arch=u'i386', type=SystemType.machine, status=SystemStatus.automated,
        owner=None, fqdn=None, shared=True, exclude_osmajor=[],
        exclude_osversion=[], hypervisor=None, kernel_type=None,
        date_added=None, return_existing=False, private=False, with_power=True, **kw):
    if owner is None:
        owner = create_user()
    if fqdn is None:
        fqdn = unique_name(u'system%s.testdata')

    if System.query.filter(System.fqdn == fqdn).count():
        if return_existing:
            system = System.query.filter(System.fqdn == fqdn).first()
            for property, value in kw.iteritems():
               setattr(system, property, value)
        else:
            raise ValueError('Attempted to create duplicate system %s' % fqdn)
    else:
        system = System(fqdn=fqdn,type=type, owner=owner,
            status=status, **kw)
        session.add(system)

    if date_added is not None:
        system.date_added = date_added
    system.custom_access_policy = SystemAccessPolicy()
    if not private:
        system.custom_access_policy.add_rule(SystemPermission.view, everybody=True)
    if shared:
        system.custom_access_policy.add_rule(
                permission=SystemPermission.reserve, everybody=True)
    if isinstance(arch, list):
        for a in arch:
            system.arch.append(Arch.by_name(a))
            system.excluded_osmajor.extend(ExcludeOSMajor(arch=Arch.by_name(a),
                                                          osmajor=osmajor) for osmajor in exclude_osmajor)
            system.excluded_osversion.extend(ExcludeOSVersion(arch=Arch.by_name(a),
                                                              osversion=osversion) for osversion in exclude_osversion)
    else:
        system.arch.append(Arch.by_name(arch))
        system.excluded_osmajor.extend(ExcludeOSMajor(arch=Arch.by_name(arch),
                                                      osmajor=osmajor) for osmajor in exclude_osmajor)
        system.excluded_osversion.extend(ExcludeOSVersion(arch=Arch.by_name(arch),
                                                          osversion=osversion) for osversion in exclude_osversion)
    if with_power:
        configure_system_power(system)
    if hypervisor:
        system.hypervisor = Hypervisor.by_name(hypervisor)
    if kernel_type:
        system.kernel_type = KernelType.by_name(kernel_type)
    system.date_modified = datetime.datetime.utcnow()
    log.debug('Created system %r', system)
    return system
Ejemplo n.º 59
0
def submit_inventory_job():
    """
    Submit a inventory job with the most suitable distro selected automatically.

    Returns a dictionary consisting of the job_id, recipe_id, status (recipe status) 
    and the job XML. If ``dryrun`` is set to ``True`` in the request, the first three 
    are set to ``None``.

    :jsonparam string fqdn: Fully-qualified domain name for the system.
    :jsonparam bool dryrun: If True, do not submit the job
    """
    if 'fqdn' not in request.json:
        raise BadRequest400('Missing the fqdn parameter')
    fqdn = request.json['fqdn']
    if 'dryrun' in request.json:
        dryrun = request.json['dryrun']
    else:
        dryrun = False
    try:
        system = System.by_fqdn(fqdn, identity.current.user)
    except NoResultFound:
        raise BadRequest400('System not found: %s' % fqdn)
    if system.find_current_hardware_scan_recipe():
        raise Conflict409('Hardware scanning already in progress')
    distro = system.distro_tree_for_inventory()
    if not distro:
        raise BadRequest400(
            'Could not find a compatible distro for hardware scanning available to this system'
        )
    job_details = {}
    job_details['system'] = system
    job_details['whiteboard'] = 'Update Inventory for %s' % fqdn
    with convert_internal_errors():
        job_xml = Job.inventory_system_job(distro,
                                           dryrun=dryrun,
                                           **job_details)
    r = {}
    if not dryrun:
        r = system.find_current_hardware_scan_recipe().__json__()
    else:
        r = {
            'recipe_id': None,
            'status': None,
            'job_id': None,
        }
    r['job_xml'] = job_xml
    r = jsonify(r)
    return r
Ejemplo n.º 60
0
 def reserve(self, action='.', *args, **kw):
     from bkr.server.controllers import Root
     default_columns = (
         'System/Name',
         'System/Reserved',
         'System/User',
         'System/Pools',
         'System/LoanedTo',
     )
     return Root()._systems(systems=System.all(
         identity.current.user).join('open_reservation').options(
             contains_eager(System.open_reservation)),
                            title=u'Reserve Report',
                            default_result_columns=default_columns,
                            *args,
                            **kw)