Example #1
0
    def render(self, session, vendor, comments, **arguments):
        validate_template_name("--vendor", vendor)
        Vendor.get_unique(session, vendor, preclude=True)

        dbv = Vendor(name=vendor, comments=comments)
        session.add(dbv)
        return
Example #2
0
    def render(self, session, vendor, comments, **arguments):
        valid = re.compile('^[a-zA-Z0-9_.-]+$')
        if (not valid.match(vendor)):
            raise ArgumentError("Vendor name '%s' is not valid." % vendor)

        Vendor.get_unique(session, vendor, preclude=True)

        dbv = Vendor(name=vendor, comments=comments)
        session.add(dbv)
        return
Example #3
0
def test_create_machines_for_test_interface():
    np = Building.get_unique(sess, 'np')
    assert isinstance(np, Building), 'no building in %s' % func_name()

    hp = Vendor.get_unique(sess, 'hp')
    assert isinstance(hp, Vendor), 'no vendor in %s' % func_name()

    am = Model.get_unique(sess, name='bl45p', vendor=hp)
    assert isinstance(am, Model), 'no model in %s' % func_name()

    cpu = sess.query(Cpu).first()
    assert isinstance(cpu, Cpu), 'no cpu in %s' % func_name()

    for i in xrange(NUM_MACHINES):
        machine = Machine(label='%s%s' % (MACHINE_NAME_PREFIX, i),
                          location=np,
                          model=am,
                          cpu=cpu,
                          cpu_quantity=2,
                          memory=32768)
        create(sess, machine)

    machines = sess.query(Machine).filter(
        Machine.label.like(MACHINE_NAME_PREFIX + '%')).all()

    eq_(len(machines), NUM_MACHINES)
Example #4
0
    def render(self, session, logger, model, vendor, **arguments):
        dbvendor = Vendor.get_unique(session, vendor, compel=True)
        dbmodel = Model.get_unique(session, name=model, vendor=dbvendor,
                                   compel=True)

        if dbmodel.model_type.isNic():
            q = session.query(Interface)
            q = q.filter_by(model=dbmodel)
            if q.first():
                raise ArgumentError("{0} is still in use and cannot be "
                                    "deleted.".format(dbmodel))
            q = session.query(MachineSpecs)
            q = q.filter_by(nic_model=dbmodel)
            if q.first():
                raise ArgumentError("{0} is still referenced by machine models and "
                                    "cannot be deleted.".format(dbmodel))
        else:
            q = session.query(HardwareEntity)
            q = q.filter_by(model=dbmodel)
            if q.first():
                raise ArgumentError("{0} is still in use and cannot be "
                                    "deleted.".format(dbmodel))

        if dbmodel.machine_specs:
            # FIXME: Log some details...
            logger.info("Before deleting model %s %s '%s', "
                        "removing machine specifications." %
                        (dbmodel.model_type, dbvendor.name, dbmodel.name))
            session.delete(dbmodel.machine_specs)
        session.delete(dbmodel)
        return
Example #5
0
    def render(self, session, logger, model, vendor, **arguments):
        dbvendor = Vendor.get_unique(session, vendor, compel=True)
        dbmodel = Model.get_unique(session,
                                   name=model,
                                   vendor=dbvendor,
                                   compel=True)

        if dbmodel.machine_type == 'nic':
            q = session.query(Interface)
            q = q.filter_by(model=dbmodel)
            if q.first():
                raise ArgumentError("{0} is still in use and cannot be "
                                    "deleted.".format(dbmodel))
            q = session.query(MachineSpecs)
            q = q.filter_by(nic_model=dbmodel)
            if q.first():
                raise ArgumentError(
                    "{0} is still referenced by machine models and "
                    "cannot be deleted.".format(dbmodel))
        else:
            q = session.query(HardwareEntity)
            q = q.filter_by(model=dbmodel)
            if q.first():
                raise ArgumentError("{0} is still in use and cannot be "
                                    "deleted.".format(dbmodel))

        if dbmodel.machine_specs:
            # FIXME: Log some details...
            logger.info("Before deleting model %s %s '%s', "
                        "removing machine specifications." %
                        (dbmodel.machine_type, dbvendor.name, dbmodel.name))
            session.delete(dbmodel.machine_specs)
        session.delete(dbmodel)
        return
Example #6
0
    def render(self, session, cpu, vendor, speed, comments, **arguments):
        dbvendor = Vendor.get_unique(session, vendor, compel=True)

        Cpu.get_unique(session, name=cpu, vendor=dbvendor, speed=speed, preclude=True)

        dbcpu = Cpu(name=cpu, vendor=dbvendor, speed=speed, comments=comments)
        session.add(dbcpu)
        return
Example #7
0
 def render(self, session, vendor, **arguments):
     dbvendor = Vendor.get_unique(session, vendor, compel=True)
     if session.query(Model).filter_by(vendor=dbvendor).first():
         raise ArgumentError("Vendor %s is still in use by a model." %
                             dbvendor.name)
     if session.query(Cpu).filter_by(vendor=dbvendor).first():
         raise ArgumentError("Vendor %s is still in use by a CPU." %
                             dbvendor.name)
     session.delete(dbvendor)
     return
Example #8
0
 def render(self, session, vendor, **arguments):
     dbvendor = Vendor.get_unique(session, vendor, compel=True)
     if session.query(Model).filter_by(vendor=dbvendor).first():
         raise ArgumentError("Vendor %s is still in use by a model." %
                             dbvendor.name)
     if session.query(Cpu).filter_by(vendor=dbvendor).first():
         raise ArgumentError("Vendor %s is still in use by a CPU." %
                             dbvendor.name)
     session.delete(dbvendor)
     return
Example #9
0
    def render(self, session, cpu, vendor, speed, comments, **arguments):
        dbvendor = Vendor.get_unique(session, vendor, compel=True)

        Cpu.get_unique(session,
                       name=cpu,
                       vendor=dbvendor,
                       speed=speed,
                       preclude=True)

        dbcpu = Cpu(name=cpu, vendor=dbvendor, speed=speed, comments=comments)
        session.add(dbcpu)
        return
Example #10
0
 def render(self, session, cpu, vendor, speed, **arguments):
     q = session.query(Cpu)
     if cpu:
         q = q.filter(Cpu.name.like(cpu + '%'))
     if vendor:
         dbvendor = Vendor.get_unique(session, vendor, compel=True)
         q = q.filter_by(vendor=dbvendor)
     if speed is not None:
         q = q.filter_by(speed=speed)
     q = q.join(Vendor)
     q = q.options(undefer('comments'), contains_eager('vendor'))
     q = q.order_by(Vendor.name, Cpu.name)
     return q.all()
Example #11
0
    def render(self, session, model, vendor, type, cpuname, cpuvendor,
               cpuspeed, cpunum, memory, disktype, diskcontroller, disksize,
               nics, nicmodel, nicvendor, comments, **arguments):
        dbvendor = Vendor.get_unique(session, vendor, compel=True)
        Model.get_unique(session, name=model, vendor=dbvendor, preclude=True)

        # Specifically not allowing new models to be added that are of
        # type aurora_node - that is only meant for the dummy aurora_model.
        allowed_types = [
            "blade", "rackmount", "workstation", "switch", "chassis",
            "virtual_machine", "nic"
        ]
        if type not in allowed_types:
            raise ArgumentError(
                "The model's machine type must be one of: %s." %
                ", ".join(allowed_types))

        dbmodel = Model(name=model,
                        vendor=dbvendor,
                        machine_type=type,
                        comments=comments)
        session.add(dbmodel)
        session.flush()

        if cpuname or cpuvendor or cpuspeed is not None:
            dbcpu = Cpu.get_unique(session,
                                   name=cpuname,
                                   vendor=cpuvendor,
                                   speed=cpuspeed,
                                   compel=True)
            if nicmodel or nicvendor:
                dbnic = Model.get_unique(session,
                                         machine_type='nic',
                                         name=nicmodel,
                                         vendor=nicvendor,
                                         compel=True)
            else:
                dbnic = Model.default_nic_model(session)
            dbmachine_specs = MachineSpecs(model=dbmodel,
                                           cpu=dbcpu,
                                           cpu_quantity=cpunum,
                                           memory=memory,
                                           disk_type=disktype,
                                           controller_type=diskcontroller,
                                           disk_capacity=disksize,
                                           nic_count=nics,
                                           nic_model=dbnic)
            session.add(dbmachine_specs)
        return
Example #12
0
def test_create_machines_for_test_host():
    np = Building.get_unique(sess, 'np')
    assert isinstance(np,Building), 'no building in %s' % func_name()

    hp = Vendor.get_unique(sess, 'hp')
    assert isinstance(hp, Vendor), 'no vendor in %s' % func_name()

    am = Model.get_unique(sess, name='bl45p', vendor=hp)
    assert isinstance(am, Model), 'no model in %s' % func_name()

    cpu = sess.query(Cpu).first()
    assert isinstance(cpu, Cpu), 'no cpu in %s' % func_name()

    for i in xrange(NUM_MACHINES):
        machine = Machine(label='%s%s'% (MACHINE_NAME_PREFIX, i), location=np,
                          model=am, cpu=cpu, cpu_quantity=2, memory=32768)
        create(sess, machine)

    machines = sess.query(Machine).filter(
        Machine.label.like(MACHINE_NAME_PREFIX+'%')).all()

    eq_(len(machines), NUM_MACHINES)
Example #13
0
    def render(self, session, model, vendor, type, cpuname, cpuvendor, cpuspeed,
               cpunum, memory, disktype, diskcontroller, disksize,
               nics, nicmodel, nicvendor,
               comments, **arguments):
        dbvendor = Vendor.get_unique(session, vendor, compel=True)
        Model.get_unique(session, name=model, vendor=dbvendor, preclude=True)

        # Specifically not allowing new models to be added that are of
        # type aurora_node - that is only meant for the dummy aurora_model.
        if type.isAuroraChassis() or type.isAuroraNode():
            raise ArgumentError("The model's machine type must not be"
                                " an aurora type")

        dbmodel = Model(name=model, vendor=dbvendor, model_type=type,
                        comments=comments)
        session.add(dbmodel)
        session.flush()

        if cpuname or cpuvendor or cpuspeed is not None:
            if not type.isMachineType():
                raise ArgumentError("Machine specfications are only valid"
                                    " for machine types")
            dbcpu = Cpu.get_unique(session, name=cpuname, vendor=cpuvendor,
                                   speed=cpuspeed, compel=True)
            if nicmodel or nicvendor:
                dbnic = Model.get_unique(session, model_type=NicType.Nic,
                                         name=nicmodel, vendor=nicvendor,
                                         compel=True)
            else:
                dbnic = Model.default_nic_model(session)
            dbmachine_specs = MachineSpecs(model=dbmodel, cpu=dbcpu,
                                           cpu_quantity=cpunum, memory=memory,
                                           disk_type=disktype,
                                           controller_type=diskcontroller,
                                           disk_capacity=disksize,
                                           nic_count=nics, nic_model=dbnic)
            session.add(dbmachine_specs)
        return
Example #14
0
    def render(self, session, model, vendor, type, cpuname, cpuvendor, cpuspeed,
               cpunum, memory, disktype, diskcontroller, disksize,
               nics, nicmodel, nicvendor,
               comments, **arguments):
        dbvendor = Vendor.get_unique(session, vendor, compel=True)
        Model.get_unique(session, name=model, vendor=dbvendor, preclude=True)

        # Specifically not allowing new models to be added that are of
        # type aurora_node - that is only meant for the dummy aurora_model.
        allowed_types = ["blade", "rackmount", "workstation", "switch",
                         "chassis", "virtual_machine", "nic"]
        if type not in allowed_types:
            raise ArgumentError("The model's machine type must be one of: %s." %
                                ", ".join(allowed_types))

        dbmodel = Model(name=model, vendor=dbvendor, machine_type=type,
                        comments=comments)
        session.add(dbmodel)
        session.flush()

        if cpuname or cpuvendor or cpuspeed is not None:
            dbcpu = Cpu.get_unique(session, name=cpuname, vendor=cpuvendor,
                                   speed=cpuspeed, compel=True)
            if nicmodel or nicvendor:
                dbnic = Model.get_unique(session, machine_type='nic',
                                         name=nicmodel, vendor=nicvendor,
                                         compel=True)
            else:
                dbnic = Model.default_nic_model(session)
            dbmachine_specs = MachineSpecs(model=dbmodel, cpu=dbcpu,
                                           cpu_quantity=cpunum, memory=memory,
                                           disk_type=disktype,
                                           controller_type=diskcontroller,
                                           disk_capacity=disksize,
                                           nic_count=nics, nic_model=dbnic)
            session.add(dbmachine_specs)
        return
Example #15
0
    def render(self, session, logger, model, vendor, newmodel, newvendor,
               comments, leave_existing, **arguments):
        for (arg, value) in arguments.items():
            # Cleaning the strings isn't strictly necessary but allows
            # for simple equality checks below and removes the need to
            # call refresh().
            if arg in ['newmodel', 'newvendor', 'machine_type',
                       'cpuname', 'cpuvendor', 'disktype', 'diskcontroller',
                       'nicmodel', 'nicvendor']:
                if value is not None:
                    arguments[arg] = value.lower().strip()

        dbmodel = Model.get_unique(session, name=model, vendor=vendor,
                                   compel=True)

        if leave_existing and (newmodel or newvendor):
            raise ArgumentError("Cannot update model name or vendor without "
                                "updating any existing machines.")

        fix_existing = not leave_existing
        dbmachines = set()

        # The sub-branching here is a little difficult to read...
        # Basically, there are three different checks to handle
        # setting a new vendor, a new name, or both.
        if newvendor:
            dbnewvendor = Vendor.get_unique(session, newvendor, compel=True)
            if newmodel:
                Model.get_unique(session, name=newmodel, vendor=dbnewvendor,
                                 preclude=True)
            else:
                Model.get_unique(session, name=dbmodel.name,
                                 vendor=dbnewvendor, preclude=True)
            dbmodel.vendor = dbnewvendor
        if newmodel:
            if not newvendor:
                Model.get_unique(session, name=newmodel, vendor=dbmodel.vendor,
                                 preclude=True)
            dbmodel.name = newmodel
        if newvendor or newmodel:
            q = session.query(Machine).filter_by(model=dbmodel)
            dbmachines.update(q.all())

        # For now, can't update machine_type.  There are too many spots
        # that special case things like aurora_node or virtual_machine to
        # know that the transistion is safe.  If there is enough need we
        # can always add those transitions later.
        if arguments['machine_type'] is not None:
            raise UnimplementedError("Cannot (yet) change a model's "
                                     "machine type.")

        if comments:
            dbmodel.comments = comments
            # The comments also do not affect the templates.

        cpu_args = ['cpuname', 'cpuvendor', 'cpuspeed']
        cpu_info = dict([(self.argument_lookup[arg], arguments[arg])
                         for arg in cpu_args])
        cpu_values = [v for v in cpu_info.values() if v is not None]
        nic_args = ['nicmodel', 'nicvendor']
        nic_info = dict([(self.argument_lookup[arg], arguments[arg])
                         for arg in nic_args])
        nic_values = [v for v in nic_info.values() if v is not None]
        spec_args = ['cpunum', 'memory', 'disktype', 'diskcontroller',
                     'disksize', 'nics']
        specs = dict([(self.argument_lookup[arg], arguments[arg])
                      for arg in spec_args])
        spec_values = [v for v in specs.values() if v is not None]

        if not dbmodel.machine_specs:
            if cpu_values or nic_values or spec_values:
                if not cpu_values or len(spec_values) < len(spec_args):
                    raise ArgumentError("Missing required parameters to store "
                                        "machine specs for the model.  Please "
                                        "give all CPU, disk, RAM, and NIC "
                                        "count information.")
                dbcpu = Cpu.get_unique(session, compel=True, **cpu_info)
                if nic_values:
                    dbnic = Model.get_unique(session, compel=True,
                                             machine_type='nic', **nic_info)
                else:
                    dbnic = Model.default_nic_model(session)
                dbmachine_specs = MachineSpecs(model=dbmodel, cpu=dbcpu,
                                               nic_model=dbnic, **specs)
                session.add(dbmachine_specs)

        # Anything below that updates specs should have been verified above.

        if cpu_values:
            dbcpu = Cpu.get_unique(session, compel=True, **cpu_info)
            self.update_machine_specs(model=dbmodel, dbmachines=dbmachines,
                                      attr='cpu', value=dbcpu,
                                      fix_existing=fix_existing)

        for arg in ['memory', 'cpunum']:
            if arguments[arg] is not None:
                self.update_machine_specs(model=dbmodel, dbmachines=dbmachines,
                                          attr=self.argument_lookup[arg],
                                          value=arguments[arg],
                                          fix_existing=fix_existing)

        if arguments['disktype']:
            if fix_existing:
                raise ArgumentError("Please specify --leave_existing to "
                                    "change the model disktype.  This cannot "
                                    "be converted automatically.")
            dbmodel.machine_specs.disk_type = arguments['disktype']

        for arg in ['diskcontroller', 'disksize']:
            if arguments[arg] is not None:
                self.update_disk_specs(model=dbmodel, dbmachines=dbmachines,
                                       attr=self.argument_lookup[arg],
                                       value=arguments[arg],
                                       fix_existing=fix_existing)

        if nic_values:
            dbnic = Model.get_unique(session, compel=True, **nic_info)
            self.update_interface_specs(model=dbmodel, dbmachines=dbmachines,
                                        value=dbnic, fix_existing=fix_existing)

        if arguments['nics'] is not None:
            dbmodel.machine_specs.nic_count = arguments['nics']

        session.flush()

        plenaries = PlenaryCollection(logger=logger)
        for dbmachine in dbmachines:
            plenaries.append(PlenaryMachineInfo(dbmachine, logger=logger))
        plenaries.write()

        return
Example #16
0
def populate(sess, *args, **kw):

    if len(sess.query(Cpu).all()) < 1:
        import re
        m = re.compile('speed')

        cfg_base = kw['cfg_base']
        assert os.path.isdir(cfg_base), "no cfg path supplied"

        log = kw['log']

        #get all dir names immediately under template-king/hardware/cpu/
        base = os.path.join(str(cfg_base), 'hardware/cpu')
        cpus = []

        for i in os.listdir(base):
            for j in os.listdir(os.path.join(base, i)):
                name = j.rstrip('.tpl').strip()
                with open(os.path.join(base, i, j), 'r') as f:
                    assert (m)
                    for line in f.readlines():
                        a_match = m.search(line)
                        if a_match:
                            l, e, freq = line.partition('=')
                            assert (isinstance(freq, str))
                            speed = re.sub('\*MHz', '',
                                           freq.strip().rstrip(';'))
                            #TODO: better checking if freq is ok here
                            if speed.isdigit():
                                cpus.append([i, name, speed])
                                break
                            else:
                                Assert(False)
                    f.close()

        for vendor, name, speed in cpus:
            kw = {}
            vendor = sess.query(Vendor).filter_by(name=vendor).first()

            assert (vendor)
            assert (name)
            assert (speed)

            if vendor:
                kw['vendor'] = vendor
                kw['name'] = name
                kw['speed'] = int(speed)

                a = Cpu(**kw)

                assert (isinstance(a, Cpu))

                try:
                    sess.add(a)
                except Exception, e:
                    sess.rollback()
                    log.error(str(e))
                    continue
            else:
                log.error("CREATE CPU: cant find vendor '%s'" % (vendor))

        try:
            av = sess.query(Vendor).filter_by(name='aurora_vendor').one()
            a = Cpu(vendor=av,
                    name='aurora_cpu',
                    speed=0,
                    comments='Placeholder Aurora CPU type.')
            vv = Vendor.get_unique(sess, 'virtual')
            vc = Cpu(vendor=vv, name='virtual_cpu', speed=0)
            sess.add_all([a, vc])
        except Exception, e:
            sess.rollback()
            log.error(str(e))
Example #17
0
    def render(self, session, logger, model, vendor, newmodel, newvendor,
               comments, leave_existing, **arguments):
        for (arg, value) in arguments.items():
            # Cleaning the strings isn't strictly necessary but allows
            # for simple equality checks below and removes the need to
            # call refresh().
            if arg in [
                    'newmodel', 'newvendor', 'machine_type', 'cpuname',
                    'cpuvendor', 'disktype', 'diskcontroller', 'nicmodel',
                    'nicvendor'
            ]:
                if value is not None:
                    arguments[arg] = value.lower().strip()

        dbmodel = Model.get_unique(session,
                                   name=model,
                                   vendor=vendor,
                                   compel=True)

        if leave_existing and (newmodel or newvendor):
            raise ArgumentError("Cannot update model name or vendor without "
                                "updating any existing machines.")

        fix_existing = not leave_existing
        dbmachines = set()

        # The sub-branching here is a little difficult to read...
        # Basically, there are three different checks to handle
        # setting a new vendor, a new name, or both.
        if newvendor:
            dbnewvendor = Vendor.get_unique(session, newvendor, compel=True)
            if newmodel:
                Model.get_unique(session,
                                 name=newmodel,
                                 vendor=dbnewvendor,
                                 preclude=True)
            else:
                Model.get_unique(session,
                                 name=dbmodel.name,
                                 vendor=dbnewvendor,
                                 preclude=True)
            dbmodel.vendor = dbnewvendor
        if newmodel:
            if not newvendor:
                Model.get_unique(session,
                                 name=newmodel,
                                 vendor=dbmodel.vendor,
                                 preclude=True)
            dbmodel.name = newmodel
        if newvendor or newmodel:
            q = session.query(Machine).filter_by(model=dbmodel)
            dbmachines.update(q.all())

        # For now, can't update machine_type.  There are too many spots
        # that special case things like aurora_node or virtual_machine to
        # know that the transistion is safe.  If there is enough need we
        # can always add those transitions later.
        if arguments['machine_type'] is not None:
            raise UnimplementedError("Cannot (yet) change a model's "
                                     "machine type.")

        if comments:
            dbmodel.comments = comments
            # The comments also do not affect the templates.

        cpu_args = ['cpuname', 'cpuvendor', 'cpuspeed']
        cpu_info = dict([(self.argument_lookup[arg], arguments[arg])
                         for arg in cpu_args])
        cpu_values = [v for v in cpu_info.values() if v is not None]
        nic_args = ['nicmodel', 'nicvendor']
        nic_info = dict([(self.argument_lookup[arg], arguments[arg])
                         for arg in nic_args])
        nic_values = [v for v in nic_info.values() if v is not None]
        spec_args = [
            'cpunum', 'memory', 'disktype', 'diskcontroller', 'disksize',
            'nics'
        ]
        specs = dict([(self.argument_lookup[arg], arguments[arg])
                      for arg in spec_args])
        spec_values = [v for v in specs.values() if v is not None]

        if not dbmodel.machine_specs:
            if cpu_values or nic_values or spec_values:
                if not cpu_values or len(spec_values) < len(spec_args):
                    raise ArgumentError("Missing required parameters to store "
                                        "machine specs for the model.  Please "
                                        "give all CPU, disk, RAM, and NIC "
                                        "count information.")
                dbcpu = Cpu.get_unique(session, compel=True, **cpu_info)
                if nic_values:
                    dbnic = Model.get_unique(session,
                                             compel=True,
                                             machine_type='nic',
                                             **nic_info)
                else:
                    dbnic = Model.default_nic_model(session)
                dbmachine_specs = MachineSpecs(model=dbmodel,
                                               cpu=dbcpu,
                                               nic_model=dbnic,
                                               **specs)
                session.add(dbmachine_specs)

        # Anything below that updates specs should have been verified above.

        if cpu_values:
            dbcpu = Cpu.get_unique(session, compel=True, **cpu_info)
            self.update_machine_specs(model=dbmodel,
                                      dbmachines=dbmachines,
                                      attr='cpu',
                                      value=dbcpu,
                                      fix_existing=fix_existing)

        for arg in ['memory', 'cpunum']:
            if arguments[arg] is not None:
                self.update_machine_specs(model=dbmodel,
                                          dbmachines=dbmachines,
                                          attr=self.argument_lookup[arg],
                                          value=arguments[arg],
                                          fix_existing=fix_existing)

        if arguments['disktype']:
            if fix_existing:
                raise ArgumentError("Please specify --leave_existing to "
                                    "change the model disktype.  This cannot "
                                    "be converted automatically.")
            dbmodel.machine_specs.disk_type = arguments['disktype']

        for arg in ['diskcontroller', 'disksize']:
            if arguments[arg] is not None:
                self.update_disk_specs(model=dbmodel,
                                       dbmachines=dbmachines,
                                       attr=self.argument_lookup[arg],
                                       value=arguments[arg],
                                       fix_existing=fix_existing)

        if nic_values:
            dbnic = Model.get_unique(session, compel=True, **nic_info)
            self.update_interface_specs(model=dbmodel,
                                        dbmachines=dbmachines,
                                        value=dbnic,
                                        fix_existing=fix_existing)

        if arguments['nics'] is not None:
            dbmodel.machine_specs.nic_count = arguments['nics']

        session.flush()

        plenaries = PlenaryCollection(logger=logger)
        for dbmachine in dbmachines:
            plenaries.append(PlenaryMachineInfo(dbmachine, logger=logger))
        plenaries.write()

        return
Example #18
0
 def render(self, session, vendor, **arguments):
     return Vendor.get_unique(session, vendor, compel=True)
Example #19
0
    def render(self, session, model, vendor, machine_type, cpuname, cpuvendor, cpuspeed,
               cpunum, nicmodel, nicvendor, memory, disktype, diskcontroller,
               disksize, fullinfo, style, **arguments):
        q = session.query(Model)

        if model:
            q = q.filter(Model.name.like(model + '%'))
        if vendor:
            dbvendor = Vendor.get_unique(session, vendor)
            q = q.filter_by(vendor=dbvendor)
        if machine_type:
            q = q.filter_by(model_type=machine_type)

        # We need an explicit join for ORDER BY
        q = q.join(Vendor)
        q = q.options(contains_eager('vendor'))
        q = q.reset_joinpoint()

        if cpuname or cpuvendor or cpuspeed or cpunum or \
           nicmodel or nicvendor or \
           memory or disktype or diskcontroller or disksize:
            q = q.join((MachineSpecs, MachineSpecs.model_id == Model.id))
            q = q.options(contains_eager('machine_specs'))

            if cpuname or cpuvendor or cpuspeed is not None:
                q = q.join(Cpu)
                q = q.options(contains_eager('machine_specs.cpu'))
                if cpuname:
                    q = q.filter(Cpu.name == cpuname)
                if cpuvendor:
                    dbvendor = Vendor.get_unique(session, cpuvendor,
                                                 compel=True)
                    q = q.filter(Cpu.vendor == dbvendor)
                if cpuspeed:
                    q = q.filter(Cpu.speed == cpuspeed)
            if cpunum is not None:
                q = q.filter_by(cpu_quantity=cpunum)
            if memory is not None:
                q = q.filter_by(memory=memory)
            if nicmodel or nicvendor:
                NicModel = aliased(Model)
                q = q.join((NicModel, NicModel.id == MachineSpecs.nic_model_id))
                q = q.options(contains_eager('machine_specs.nic_model'))
                if nicmodel:
                    q = q.filter(NicModel.name == nicmodel)
                if nicvendor:
                    dbvendor = Vendor.get_unique(session, nicvendor,
                                                 compel=True)
                    q = q.filter(NicModel.vendor == dbvendor)
            if disktype:
                q = q.filter_by(disk_type=disktype)
            if diskcontroller:
                q = q.filter_by(controller_type=diskcontroller)
            if disksize:
                q = q.filter_by(disk_capacity=disksize)
        else:
            if fullinfo or style != 'raw':
                q = q.options(joinedload('machine_specs'),
                              joinedload('machine_specs.cpu'))
        q = q.order_by(Vendor.name, Model.name)

        if fullinfo or style != 'raw':
            return q.all()
        else:
            return StringAttributeList(q.all(),
                                       lambda x: "%s/%s" % (x.vendor.name,
                                                            x.name))
Example #20
0
    def render(self, session, model, vendor, machine_type, cpuname, cpuvendor,
               cpuspeed, cpunum, nicmodel, nicvendor, memory, disktype,
               diskcontroller, disksize, fullinfo, style, **arguments):
        q = session.query(Model)

        if model:
            q = q.filter(Model.name.like(model + '%'))
        if vendor:
            dbvendor = Vendor.get_unique(session, vendor)
            q = q.filter_by(vendor=dbvendor)
        if machine_type:
            q = q.filter_by(machine_type=machine_type)

        # We need an explicit join for ORDER BY
        q = q.join(Vendor)
        q = q.options(contains_eager('vendor'))
        q = q.reset_joinpoint()

        if cpuname or cpuvendor or cpuspeed or cpunum or \
           nicmodel or nicvendor or \
           memory or disktype or diskcontroller or disksize:
            q = q.join((MachineSpecs, MachineSpecs.model_id == Model.id))
            q = q.options(contains_eager('machine_specs'))

            if cpuname or cpuvendor or cpuspeed is not None:
                q = q.join(Cpu)
                q = q.options(contains_eager('machine_specs.cpu'))
                if cpuname:
                    q = q.filter(Cpu.name == cpuname)
                if cpuvendor:
                    dbvendor = Vendor.get_unique(session,
                                                 cpuvendor,
                                                 compel=True)
                    q = q.filter(Cpu.vendor == dbvendor)
                if cpuspeed:
                    q = q.filter(Cpu.speed == cpuspeed)
            if cpunum is not None:
                q = q.filter_by(cpu_quantity=cpunum)
            if memory is not None:
                q = q.filter_by(memory=memory)
            if nicmodel or nicvendor:
                NicModel = aliased(Model)
                q = q.join(
                    (NicModel, NicModel.id == MachineSpecs.nic_model_id))
                q = q.options(contains_eager('machine_specs.nic_model'))
                if nicmodel:
                    q = q.filter(NicModel.name == nicmodel)
                if nicvendor:
                    dbvendor = Vendor.get_unique(session,
                                                 nicvendor,
                                                 compel=True)
                    q = q.filter(NicModel.vendor == dbvendor)
            if disktype:
                q = q.filter_by(disk_type=disktype)
            if diskcontroller:
                q = q.filter_by(controller_type=diskcontroller)
            if disksize:
                q = q.filter_by(disk_capacity=disksize)
        else:
            if fullinfo or style != 'raw':
                q = q.options(joinedload('machine_specs'),
                              joinedload('machine_specs.cpu'))
        q = q.order_by(Vendor.name, Model.name)

        if fullinfo or style != 'raw':
            return q.all()
        else:
            return SimpleModelList(q.all())
Example #21
0
def populate(sess, *args, **kw):

    if len(sess.query(Cpu).all()) < 1:
        import re

        m = re.compile("speed")

        cfg_base = kw["cfg_base"]
        assert os.path.isdir(cfg_base), "no cfg path supplied"

        log = kw["log"]

        # get all dir names immediately under template-king/hardware/cpu/
        base = os.path.join(str(cfg_base), "hardware/cpu")
        cpus = []

        for i in os.listdir(base):
            for j in os.listdir(os.path.join(base, i)):
                name = j.rstrip(".tpl").strip()
                with open(os.path.join(base, i, j), "r") as f:
                    assert m
                    for line in f.readlines():
                        a_match = m.search(line)
                        if a_match:
                            l, e, freq = line.partition("=")
                            assert isinstance(freq, str)
                            speed = re.sub("\*MHz", "", freq.strip().rstrip(";"))
                            # TODO: better checking if freq is ok here
                            if speed.isdigit():
                                cpus.append([i, name, speed])
                                break
                            else:
                                Assert(False)
                    f.close()

        for vendor, name, speed in cpus:
            kw = {}
            vendor = sess.query(Vendor).filter_by(name=vendor).first()

            assert vendor
            assert name
            assert speed

            if vendor:
                kw["vendor"] = vendor
                kw["name"] = name
                kw["speed"] = int(speed)

                a = Cpu(**kw)

                assert isinstance(a, Cpu)

                try:
                    sess.add(a)
                except Exception, e:
                    sess.rollback()
                    log.error(str(e))
                    continue
            else:
                log.error("CREATE CPU: cant find vendor '%s'" % (vendor))

        try:
            av = sess.query(Vendor).filter_by(name="aurora_vendor").one()
            a = Cpu(vendor=av, name="aurora_cpu", speed=0, comments="Placeholder Aurora CPU type.")
            vv = Vendor.get_unique(sess, "virtual")
            vc = Cpu(vendor=vv, name="virtual_cpu", speed=0)
            sess.add_all([a, vc])
        except Exception, e:
            sess.rollback()
            log.error(str(e))