Check the minion cache to make sure that old minion data is cleared

        Optionally, pass in a list of minions which should have their caches
        preserved. To preserve all caches, set __opts__['preserve_minion_cache']
497900
def check_minion_cache(self, preserve_minions=None):
        '''
        
        '''
        if preserve_minions is None:
            preserve_minions = []
        keys = self.list_keys()
        minions = []
        for key, val in six.iteritems(keys):
            minions.extend(val)
        if not self.opts.get('preserve_minion_cache', False):
            m_cache = os.path.join(self.opts['cachedir'], self.ACC)
            if os.path.isdir(m_cache):
                for minion in os.listdir(m_cache):
                    if minion not in minions and minion not in preserve_minions:
                        try:
                            shutil.rmtree(os.path.join(m_cache, minion))
                        except (OSError, IOError) as ex:
                            log.warning('Key: Delete cache for %s got OSError/IOError: %s \n',
                                        minion,
                                        ex)
                            continue
            cache = salt.cache.factory(self.opts)
            clist = cache.list(self.ACC)
            if clist:
                for minion in clist:
                    if minion not in minions and minion not in preserve_minions:
                        cache.flush('{0}/{1}'.format(self.ACC, minion))
958142
def save_load(jid, load, minions=None):
    '''
    
    '''
    serv = _get_serv(ret=None)
    serv.set(jid, salt.utils.json.dumps(load))
    _append_list(serv, 'jids', jid)
1181035
def minion_runner(self, clear_load):
        '''
        
        '''
        load = self.__verify_load(clear_load, ('fun', 'arg', 'id', 'tok'))
        if load is False:
            return {}
        else:
            return self.masterapi.minion_runner(clear_load)
433667
def _create_minion_object(self, opts, timeout, safe,
                              io_loop=None, loaded_base_name=None,
                              jid_queue=None):
        '''
        
        '''
        return Minion(opts,
                      timeout,
                      safe,
                      io_loop=io_loop,
                      loaded_base_name=loaded_base_name,
                      jid_queue=jid_queue)
257218
def check_cache(cache):
    
    if isinstance(cache, BaseCache):
        return cache
    elif cache is False:
        return DictCache()
    elif cache is None:
        return DummyCache()
    else:
        raise ValueError('Provided cache must implement BaseCache')
179886
def clearCache():
		
		if os.path.exists(CachedDataManager._cacheDir()) == True:
			shutil.rmtree(CachedDataManager._cacheDir())
700315
def flush(self):
        
        if self._cache_modified_count > 0:
            self.storage.write(self.cache)
            self._cache_modified_count = 0
509509
def save_load(jid, load, minions=None):
    '''
    
    '''
    conn, mdb = _get_conn(ret=None)
    to_save = _safe_copy(load)

    if PYMONGO_VERSION > _LooseVersion('2.3'):
        #using .copy() to ensure original data for load is unchanged
        mdb.jobs.insert_one(to_save)
    else:
        mdb.jobs.insert(to_save)
1295371
def purge_cache(self):
        
        if len(self._cache) > self.max_cache_size:
            items = sorted(self._cache.items(), key=lambda (k, v): v['expiry'])
            self._cache = {k: v for k, v in items[self.max_cache_size:]
                           if not self._expired(v)}
250135
def _all_minions(self, expr=None):
        '''
        
        '''
        mlist = []
        for fn_ in salt.utils.data.sorted_ignorecase(os.listdir(os.path.join(self.opts['pki_dir'], self.acc))):
            if not fn_.startswith('.') and os.path.isfile(os.path.join(self.opts['pki_dir'], self.acc, fn_)):
                mlist.append(fn_)
        return {'minions': mlist, 'missing': []}
1175726
def process_minion_update(self, event_data):
        '''
        
        '''
        tag = event_data['tag']
        event_info = event_data['data']

        mid = tag.split('/')[-1]

        if not self.minions.get(mid, None):
            self.minions[mid] = {}

        minion = self.minions[mid]

        minion.update({'grains': event_info['return']})
        log.debug("In process minion grains update with minions=%s", self.minions)
        self.publish_minions()
624237
def _check_nodegroup_minions(self, expr, greedy):  # pylint: disable=unused-argument
        '''
        
        '''
        return self._check_compound_minions(nodegroup_comp(expr, self.opts['nodegroups']),
            DEFAULT_TARGET_DELIM,
            greedy)
908360
function shouldCache(options) {
    options = options || {};
    return (options.hasOwnProperty('cache') && !options.cache) || !self.options.cache;
  }
979497
def _check_ipcidr_minions(self, expr, greedy):
        '''
        
        '''
        cache_enabled = self.opts.get('minion_data_cache', False)

        if greedy:
            minions = self._pki_minions()
        elif cache_enabled:
            minions = self.cache.list('minions')
        else:
            return {'minions': [],
                    'missing': []}

        if cache_enabled:
            if greedy:
                cminions = self.cache.list('minions')
            else:
                cminions = minions
            if cminions is None:
                return {'minions': minions,
                        'missing': []}

            tgt = expr
            try:
                # Target is an address?
                tgt = ipaddress.ip_address(tgt)
            except Exception:
                try:
                    # Target is a network?
                    tgt = ipaddress.ip_network(tgt)
                except Exception:
                    log.error('Invalid IP/CIDR target: %s', tgt)
                    return {'minions': [],
                            'missing': []}
            proto = 'ipv{0}'.format(tgt.version)

            minions = set(minions)
            for id_ in cminions:
                mdata = self.cache.fetch('minions/{0}'.format(id_), 'data')
                if mdata is None:
                    if not greedy:
                        minions.remove(id_)
                    continue
                grains = mdata.get('grains')
                if grains is None or proto not in grains:
                    match = False
                elif isinstance(tgt, (ipaddress.IPv4Address, ipaddress.IPv6Address)):
                    match = six.text_type(tgt) in grains[proto]
                else:
                    match = salt.utils.network.in_subnet(tgt, grains[proto])

                if not match and id_ in minions:
                    minions.remove(id_)

        return {'minions': list(minions),
                'missing': []}
195982
def _check_list_minions(self, expr, greedy, ignore_missing=False):  # pylint: disable=unused-argument
        '''
        
        '''
        if isinstance(expr, six.string_types):
            expr = [m for m in expr.split(',') if m]
        minions = self._pki_minions()
        return {'minions': [x for x in expr if x in minions],
                'missing': [] if ignore_missing else [x for x in expr if x not in minions]}
733023
def cachedir_index_del(minion_id, base=None):
    '''
    
    '''
    base = init_cachedir(base)
    index_file = os.path.join(base, 'index.p')
    lock_file(index_file)

    if os.path.exists(index_file):
        mode = 'rb' if six.PY3 else 'r'
        with salt.utils.files.fopen(index_file, mode) as fh_:
            index = salt.utils.data.decode(
                salt.utils.msgpack.load(fh_, encoding=MSGPACK_ENCODING))
    else:
        return

    if minion_id in index:
        del index[minion_id]

    mode = 'wb' if six.PY3 else 'w'
    with salt.utils.files.fopen(index_file, mode) as fh_:
        salt.utils.msgpack.dump(index, fh_, encoding=MSGPACK_ENCODING)

    unlock_file(index_file)
563515
def cache_jobs(opts, jid, ret):
    '''
    
    '''
    serial = salt.payload.Serial(opts=opts)

    fn_ = os.path.join(
        opts['cachedir'],
        'minion_jobs',
        jid,
        'return.p')
    jdir = os.path.dirname(fn_)
    if not os.path.isdir(jdir):
        os.makedirs(jdir)
    with salt.utils.files.fopen(fn_, 'w+b') as fp_:
        fp_.write(serial.dumps(ret))