Ejemplo n.º 1
0
    def find_match(self, hw_items):
        '''Finds an hardware profile matching the hardware items in the state

If a profiles matches, its count is decremented.

Returns the name of the matching profile.
'''
        idx = 0
        times = '*'
        name = None
        valid_roles = []
        for name, times in self._data:
            LOG.info('testing %s' % name)
            if times == '*' or int(times) > 0:
                valid_roles.append(name)
                specs = self._load_specs(name)
                var = {}
                var2 = {}
                if matcher.match_all(hw_items, specs, var, var2):
                    LOG.info('Specs %s matches' % name)

                    forced = (var2 != {})

                    if var2 == {}:
                        var2 = var

                    if times != '*':
                        self._data[idx] = (name, int(times) - 1)
                        LOG.info('Decrementing %s to %d' %
                                 (name, int(times) - 1))

                    dbase = cmdb.load_cmdb(self._cfg_dir, name)
                    if dbase:
                        if cmdb.update_cmdb(dbase, var, var2, forced):
                            cmdb.save_cmdb(self._cfg_dir, name, dbase)
                        else:
                            idx += 1
                            continue

                    return name, var
            idx += 1
        else:
            if not valid_roles:
                raise StateError('No more role available in %s' %
                                 (self._state_filename,))
            else:
                raise StateError(
                    'Unable to match requirements on the following available '
                    'roles in %s: %s'
                    % (self._cfg_dir, ', '.join(valid_roles)))
Ejemplo n.º 2
0
    def find_match(self, hw_items):
        '''Finds an hardware profile matching the hardware items in the state

If a profiles matches, its count is decremented.

Returns the name of the matching profile.
'''
        idx = 0
        times = '*'
        name = None
        valid_roles = []
        for name, times in self._data:
            LOG.info('testing %s' % name)
            if times == '*' or int(times) > 0:
                valid_roles.append(name)
                specs = self._load_specs(name)
                var = {}
                var2 = {}
                if matcher.match_all(hw_items, specs, var, var2):
                    LOG.info('Specs %s matches' % name)

                    forced = (var2 != {})

                    if var2 == {}:
                        var2 = var

                    if times != '*':
                        self._data[idx] = (name, int(times) - 1)
                        LOG.info('Decrementing %s to %d' %
                                 (name, int(times) - 1))

                    dbase = cmdb.load_cmdb(self._cfg_dir, name)
                    if dbase:
                        if cmdb.update_cmdb(dbase, var, var2, forced):
                            cmdb.save_cmdb(self._cfg_dir, name, dbase)
                        else:
                            idx += 1
                            continue

                    return name, var
            idx += 1
        else:
            if not valid_roles:
                raise StateError('No more role available in %s' %
                                 (self._state_filename,))
            else:
                raise StateError(
                    'Unable to match requirements on the following available '
                    'roles in %s: %s'
                    % (self._cfg_dir, ', '.join(valid_roles)))
Ejemplo n.º 3
0
    def hardware_info(self, hostname):
        '''Get hardware informations for a hostname.

Lookup cmdb to find the correct specs file and extract info from it.
'''
        # step 1: find the name of the profile by looking into the cmdb
        info = None
        for name, _ in self._data:
            cfgdb = cmdb.load_cmdb(self._cfg_dir, name)
            if cfgdb:
                for rec in cfgdb:
                    if 'hostname' in rec and rec['hostname'] == hostname:
                        info = rec
                        break
            if info:
                break
        else:
            return {}

        # step 2: lookup hardware info from the specs file
        specs = self._load_specs(name)
        data = {}

        mem = State._get_memory(specs)
        if mem:
            data['memory'] = mem

        ncpus = State._get_ncpus(specs)
        if ncpus:
            data['ncpus'] = ncpus

        disks = State._get_disks(specs)
        if disks:
            data['disks'] = disks

        nics = State._get_nics(specs, info)
        if nics:
            data['nics'] = nics

        return data
Ejemplo n.º 4
0
    def hardware_info(self, hostname):
        '''Get hardware informations for a hostname.

Lookup cmdb to find the correct specs file and extract info from it.
'''
        # step 1: find the name of the profile by looking into the cmdb
        info = None
        for name, _ in self._data:
            cfgdb = cmdb.load_cmdb(self._cfg_dir, name)
            if cfgdb:
                for rec in cfgdb:
                    if 'hostname' in rec and rec['hostname'] == hostname:
                        info = rec
                        break
            if info:
                break
        else:
            return {}

        # step 2: lookup hardware info from the specs file
        specs = self._load_specs(name)
        data = {}

        mem = State._get_memory(specs)
        if mem:
            data['memory'] = mem

        ncpus = State._get_ncpus(specs)
        if ncpus:
            data['ncpus'] = ncpus

        disks = State._get_disks(specs)
        if disks:
            data['disks'] = disks

        nics = State._get_nics(specs, info)
        if nics:
            data['nics'] = nics

        return data