Instruction:
Can you write a function in Python 3 where it
processes a command sent via an AES key.

Response:
def _handle_aes(self, data):
        '''
        Process a command sent via an AES key

        :param str load: Encrypted payload
        :return: The result of passing the load to a function in AESFuncs corresponding to
                 the command specified in the load's 'cmd' key.
        '''
        if 'cmd' not in data:
            log.error('Received malformed command %s', data)
            return {}
        cmd = data['cmd']
        log.trace('AES payload received with command %s', data['cmd'])
        if cmd.startswith('__'):
            return False
        if self.opts['master_stats']:
            start = time.time()

        def run_func(data):
            return self.aes_funcs.run_func(data['cmd'], data)

        with StackContext(functools.partial(RequestContext,
                                            {'data': data,
                                             'opts': self.opts})):
            ret = run_func(data)

        if self.opts['master_stats']:
            stats = salt.utils.event.update_stats(self.stats, start, data)
            self._post_stats(stats)
        return ret


Instruction:
Can you generate a brief explanation for the following Python 3 code
def run(self):
        '''
        Start a Master Worker
        '''
        salt.utils.process.appendproctitle(self.name)
        self.clear_funcs = ClearFuncs(
           self.opts,
           self.key,
           )
        self.aes_funcs = AESFuncs(self.opts)
        salt.utils.crypt.reinit_crypto()
        self.__bind()

Response: Start a Master Worker


Instruction:
Create a Python 3 function to
set the local file objects from the file server interface

Response:
def __setup_fileserver(self):
        '''
        Set the local file objects from the file server interface
        '''
        # Avoid circular import
        import salt.fileserver
        self.fs_ = salt.fileserver.Fileserver(self.opts)
        self._serve_file = self.fs_.serve_file
        self._file_find = self.fs_._find_file
        self._file_hash = self.fs_.file_hash
        self._file_hash_and_stat = self.fs_.file_hash_and_stat
        self._file_list = self.fs_.file_list
        self._file_list_emptydirs = self.fs_.file_list_emptydirs
        self._dir_list = self.fs_.dir_list
        self._symlink_list = self.fs_.symlink_list
        self._file_envs = self.fs_.file_envs


Instruction:
Here you have a function in Python 3, explain what it does
def __verify_minion(self, id_, token):
        '''
        Take a minion id and a string signed with the minion private key
        The string needs to verify as 'salt' with the minion public key

        :param str id_: A minion ID
        :param str token: A string signed with the minion private key

        :rtype: bool
        :return: Boolean indicating whether or not the token can be verified.
        '''
        if not salt.utils.verify.valid_id(self.opts, id_):
            return False
        pub_path = os.path.join(self.opts['pki_dir'], 'minions', id_)

        try:
            pub = salt.crypt.get_rsa_pub_key(pub_path)
        except (IOError, OSError):
            log.warning(
                'Salt minion claiming to be %s attempted to communicate with '
                'master, but key could not be read and verification was denied.',
                id_
            )
            return False
        except (ValueError, IndexError, TypeError) as err:
            log.error('Unable to load public key "%s": %s', pub_path, err)
        try:
            if salt.crypt.public_decrypt(pub, token) == b'salt':
                return True
        except ValueError as err:
            log.error('Unable to decrypt token: %s', err)

        log.error(
            'Salt minion claiming to be %s has attempted to communicate with '
            'the master and could not be verified', id_
        )
        return False

Response: Verify a minion ID and a string signed with the minion private key


Instruction:
Can you create a Python 3 function that
verifies that the passed information authorized a minion to execute a publish command in the load

Response:
def __verify_minion_publish(self, clear_load):
        '''
        Verify that the passed information authorized a minion to execute

        :param dict clear_load: A publication load from a minion

        :rtype: bool
        :return: A boolean indicating if the minion is allowed to publish the command in the load
        '''
        # Verify that the load is valid
        if 'peer' not in self.opts:
            return False
        if not isinstance(self.opts['peer'], dict):
            return False
        if any(key not in clear_load for key in ('fun', 'arg', 'tgt', 'ret', 'tok', 'id')):
            return False
        # If the command will make a recursive publish don't run
        if clear_load['fun'].startswith('publish.'):
            return False
        # Check the permissions for this minion
        if not self.__verify_minion(clear_load['id'], clear_load['tok']):
            # The minion is not who it says it is!
            # We don't want to listen to it!
            log.warning(
                'Minion id %s is not who it says it is and is attempting '
                'to issue a peer command', clear_load['id']
            )
            return False
        clear_load.pop('tok')
        perms = []
        for match in self.opts['peer']:
            if re.match(match, clear_load['id']):
                # This is the list of funcs/modules!
                if isinstance(self.opts['peer'][match], list):
                    perms.extend(self.opts['peer'][match])
        if ',' in clear_load['fun']:
            # 'arg': [['cat', '/proc/cpuinfo'], [], ['foo']]
            clear_load['fun'] = clear_load['fun'].split(',')
            arg_ = []
            for arg in clear_load['arg']:
                arg_.append(arg.split())
            clear_load['arg'] = arg_

        # finally, check the auth of the load
        return self.ckminions.auth_check(
            perms,
            clear_load['fun'],
            clear_load['arg'],
            clear_load['tgt'],
            clear_load.get('tgt_type', 'glob'),
            publish_validate=True)


Instruction:
Can you generate the documentation for the following Python 3 function
def _master_tops(self, load):
        '''
        Return the results from an external node classifier if one is
        specified

        :param dict load: A payload received from a minion
        :return: The results from an external node classifier
        '''
        load = self.__verify_load(load, ('id', 'tok'))
        if load is False:
            return {}
        return self.masterapi._master_tops(load, skip_verify=True)

Response:
Return the results from an external node classifier if one is
        specified


Instruction:
Create a Python 3 function for
gathering the data from the specified minions

Response:
def _mine_get(self, load):
        '''
        Gathers the data from the specified minions' mine

        :param dict load: A payload received from a minion

        :rtype: dict
        :return: Mine data from the specified minions
        '''
        load = self.__verify_load(load, ('id', 'tgt', 'fun', 'tok'))
        if load is False:
            return {}
        else:
            return self.masterapi._mine_get(load, skip_verify=True)


Instruction:
Here you have a function in Python 3, explain what it does
def _mine(self, load):
        '''
        Store the mine data

        :param dict load: A payload received from a minion

        :rtype: bool
        :return: True if the data has been stored in the mine
        '''
        load = self.__verify_load(load, ('id', 'data', 'tok'))
        if load is False:
            return {}
        return self.masterapi._mine(load, skip_verify=True)

Response: Store the mine data in the mine data store


Instruction:
Can you tell what is the following Python 3 function doing
def _mine_delete(self, load):
        '''
        Allow the minion to delete a specific function from its own mine

        :param dict load: A payload received from a minion

        :rtype: bool
        :return: Boolean indicating whether or not the given function was deleted from the mine
        '''
        load = self.__verify_load(load, ('id', 'fun', 'tok'))
        if load is False:
            return {}
        else:
            return self.masterapi._mine_delete(load)

Response: Allow the minion to delete a specific function from its own mine


Instruction:
Write a Python 3 script for
allowing the minion to delete all of its own mine contents

Response:
def _mine_flush(self, load):
        '''
        Allow the minion to delete all of its own mine contents

        :param dict load: A payload received from a minion
        '''
        load = self.__verify_load(load, ('id', 'tok'))
        if load is False:
            return {}
        else:
            return self.masterapi._mine_flush(load, skip_verify=True)


Instruction:
How would you explain what the following Python 3 function does
def _file_recv(self, load):
        '''
        Allows minions to send files to the master, files are sent to the
        master file cache
        '''
        if any(key not in load for key in ('id', 'path', 'loc')):
            return False
        if not isinstance(load['path'], list):
            return False
        if not self.opts['file_recv']:
            return False
        if not salt.utils.verify.valid_id(self.opts, load['id']):
            return False
        if 'loc' in load and load['loc'] < 0:
            log.error('Invalid file pointer: load[loc] < 0')
            return False

        if len(load['data']) + load.get('loc', 0) > self.opts['file_recv_max_size'] * 0x100000:
            log.error(
                'file_recv_max_size limit of %d MB exceeded! %s will be '
                'truncated. To successfully push this file, adjust '
                'file_recv_max_size to an integer (in MB) large enough to '
                'accommodate it.', self.opts['file_recv_max_size'], load['path']
            )
            return False
        if 'tok' not in load:
            log.error(
                'Received incomplete call from %s for \'%s\', missing \'%s\'',
                load['id'], inspect_stack()['co_name'], 'tok'
            )
            return False
        if not self.__verify_minion(load['id'], load['tok']):
            # The minion is not who it says it is!
            # We don't want to listen to it!
            log.warning('Minion id %s is not who it says it is!', load['id'])
            return {}
        load.pop('tok')

        # Join path
        sep_path = os.sep.join(load['path'])

        # Path normalization should have been done by the sending
        # minion but we can't guarantee it. Re-do it here.
        normpath = os.path.normpath(sep_path)

        # Ensure that this safety check is done after the path
        # have been normalized.
        if os.path.isabs(normpath) or '../' in load['path']:
            # Can overwrite master files!!
            return False

        cpath = os.path.join(
            self.opts['cachedir'],
            'minions',
            load['id'],
            'files',
            normpath)
        # One last safety check here
        if not os.path.normpath(cpath).startswith(self.opts['cachedir']):
            log.warning(
                'Attempt to write received file outside of master cache '
                'directory! Requested path: %s. Access denied.', cpath
            )
            return False
        cdir = os.path.dirname(cpath)
        if not os.path.isdir(cdir):
            try:
                os.makedirs(cdir)
            except os.error:
                pass
        if os.path.isfile(cpath) and load['loc'] != 0:
            mode = 'ab'
        else:
            mode = 'wb'
        with salt.utils.files.fopen(cpath, mode) as fp_:
            if load['loc']:
                fp_.seek(load['loc'])

            fp_.write(salt.utils.stringutils.to_bytes(load['data']))
        return True

Response: Internal method to send files to the master file cache


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _pillar(self, load):
        '''
        Return the pillar data for the minion

        :param dict load: Minion payload

        :rtype: dict
        :return: The pillar data for the minion
        '''
        if any(key not in load for key in ('id', 'grains')):
            return False
        if not salt.utils.verify.valid_id(self.opts, load['id']):
            return False
        load['grains']['id'] = load['id']

        pillar = salt.pillar.get_pillar(
            self.opts,
            load['grains'],
            load['id'],
            load.get('saltenv', load.get('env')),
            ext=load.get('ext'),
            pillar_override=load.get('pillar_override', {}),
            pillarenv=load.get('pillarenv'),
            extra_minion_data=load.get('extra_minion_data'))
        data = pillar.compile_pillar()
        self.fs_.update_opts()
        if self.opts.get('minion_data_cache', False):
            self.masterapi.cache.store('minions/{0}'.format(load['id']),
                                       'data',
                                       {'grains': load['grains'],
                                        'pillar': data})
            if self.opts.get('minion_data_cache_events') is True:
                self.event.fire_event({'Minion data cache refresh': load['id']}, tagify(load['id'], 'refresh', 'minion'))
        return data

Response: Return the pillar data for the minion


Instruction:
Can you generate the documentation for the following Python 3 function
def _minion_event(self, load):
        '''
        Receive an event from the minion and fire it on the master event
        interface

        :param dict load: The minion payload
        '''
        load = self.__verify_load(load, ('id', 'tok'))
        if load is False:
            return {}
        # Route to master event bus
        self.masterapi._minion_event(load)
        # Process locally
        self._handle_minion_event(load)

Response: Handle an event from the minion and fire it on the master event bus


Instruction:
Can you create a Python 3 function that
handles a minion event from the minions cache

Response:
def _handle_minion_event(self, load):
        '''
        Act on specific events from minions
        '''
        id_ = load['id']
        if load.get('tag', '') == '_salt_error':
            log.error(
                'Received minion error from [%s]: %s',
                id_, load['data']['message']
            )

        for event in load.get('events', []):
            event_data = event.get('data', {})
            if 'minions' in event_data:
                jid = event_data.get('jid')
                if not jid:
                    continue
                minions = event_data['minions']
                try:
                    salt.utils.job.store_minions(
                        self.opts,
                        jid,
                        minions,
                        mminion=self.mminion,
                        syndic_id=id_)
                except (KeyError, salt.exceptions.SaltCacheError) as exc:
                    log.error(
                        'Could not add minion(s) %s for job %s: %s',
                        minions, jid, exc
                    )


Instruction:
Implement a function in Python 3 to
handle the return data sent from the master and returns it.

Response:
def _return(self, load):
        '''
        Handle the return data sent from the minions.

        Takes the return, verifies it and fires it on the master event bus.
        Typically, this event is consumed by the Salt CLI waiting on the other
        end of the event bus but could be heard by any listener on the bus.

        :param dict load: The minion payload
        '''
        if self.opts['require_minion_sign_messages'] and 'sig' not in load:
            log.critical(
                '_return: Master is requiring minions to sign their '
                'messages, but there is no signature in this payload from '
                '%s.', load['id']
            )
            return False

        if 'sig' in load:
            log.trace('Verifying signed event publish from minion')
            sig = load.pop('sig')
            this_minion_pubkey = os.path.join(self.opts['pki_dir'], 'minions/{0}'.format(load['id']))
            serialized_load = salt.serializers.msgpack.serialize(load)
            if not salt.crypt.verify_signature(this_minion_pubkey, serialized_load, sig):
                log.info('Failed to verify event signature from minion %s.', load['id'])
                if self.opts['drop_messages_signature_fail']:
                    log.critical(
                        'Drop_messages_signature_fail is enabled, dropping '
                        'message from %s', load['id']
                    )
                    return False
                else:
                    log.info('But \'drop_message_signature_fail\' is disabled, so message is still accepted.')
            load['sig'] = sig

        try:
            salt.utils.job.store_job(
                self.opts, load, event=self.event, mminion=self.mminion)
        except salt.exceptions.SaltCacheError:
            log.error('Could not store job information for load: %s', load)


Instruction:
Write a Python 3 script for
receiving a syndic minion return and format it to look like returns from individual minions.

Response:
def _syndic_return(self, load):
        '''
        Receive a syndic minion return and format it to look like returns from
        individual minions.

        :param dict load: The minion payload
        '''
        loads = load.get('load')
        if not isinstance(loads, list):
            loads = [load]  # support old syndics not aggregating returns
        for load in loads:
            # Verify the load
            if any(key not in load for key in ('return', 'jid', 'id')):
                continue
            # if we have a load, save it
            if load.get('load'):
                fstr = '{0}.save_load'.format(self.opts['master_job_cache'])
                self.mminion.returners[fstr](load['jid'], load['load'])

            # Register the syndic
            syndic_cache_path = os.path.join(self.opts['cachedir'], 'syndics', load['id'])
            if not os.path.exists(syndic_cache_path):
                path_name = os.path.split(syndic_cache_path)[0]
                if not os.path.exists(path_name):
                    os.makedirs(path_name)
                with salt.utils.files.fopen(syndic_cache_path, 'w') as wfh:
                    wfh.write('')

            # Format individual return loads
            for key, item in six.iteritems(load['return']):
                ret = {'jid': load['jid'],
                       'id': key}
                ret.update(item)
                if 'master_id' in load:
                    ret['master_id'] = load['master_id']
                if 'fun' in load:
                    ret['fun'] = load['fun']
                if 'arg' in load:
                    ret['fun_args'] = load['arg']
                if 'out' in load:
                    ret['out'] = load['out']
                if 'sig' in load:
                    ret['sig'] = load['sig']
                self._return(ret)


Instruction:
Create a Python 3 function to
execute a runner from a minion return the runner s function data

Response:
def minion_runner(self, clear_load):
        '''
        Execute a runner from a minion, return the runner's function data

        :param dict clear_load: The minion payload

        :rtype: dict
        :return: The runner function data
        '''
        load = self.__verify_load(clear_load, ('fun', 'arg', 'id', 'tok'))
        if load is False:
            return {}
        else:
            return self.masterapi.minion_runner(clear_load)


Instruction:
Given the following Python 3 function, write the documentation
def pub_ret(self, load):
        '''
        Request the return data from a specific jid, only allowed
        if the requesting minion also initialted the execution.

        :param dict load: The minion payload

        :rtype: dict
        :return: Return data corresponding to a given JID
        '''
        load = self.__verify_load(load, ('jid', 'id', 'tok'))
        if load is False:
            return {}
        # Check that this minion can access this data
        auth_cache = os.path.join(
            self.opts['cachedir'],
            'publish_auth')
        if not os.path.isdir(auth_cache):
            os.makedirs(auth_cache)
        jid_fn = os.path.join(auth_cache, six.text_type(load['jid']))
        with salt.utils.files.fopen(jid_fn, 'r') as fp_:
            if not load['id'] == fp_.read():
                return {}
        # Grab the latest and return
        return self.local.get_cache_returns(load['jid'])

Response: Request the return data from a specific jid only allowed


Instruction:
How would you explain what the following Python 3 function does
def minion_pub(self, clear_load):
        '''
        Publish a command initiated from a minion, this method executes minion
        restrictions so that the minion publication will only work if it is
        enabled in the config.

        The configuration on the master allows minions to be matched to
        salt functions, so the minions can only publish allowed salt functions

        The config will look like this:

        .. code-block:: bash

            peer:
                .*:
                    - .*

        This configuration will enable all minions to execute all commands:

        .. code-block:: bash

            peer:
                foo.example.com:
                    - test.*

        The above configuration will only allow the minion foo.example.com to
        execute commands from the test module.

        :param dict clear_load: The minion pay
        '''
        if not self.__verify_minion_publish(clear_load):
            return {}
        else:
            return self.masterapi.minion_pub(clear_load)

Response: Publish a command initiated from a minion


Instruction:
Can you tell what is the following Python 3 function doing
def minion_publish(self, clear_load):
        '''
        Publish a command initiated from a minion, this method executes minion
        restrictions so that the minion publication will only work if it is
        enabled in the config.

        The configuration on the master allows minions to be matched to
        salt functions, so the minions can only publish allowed salt functions

        The config will look like this:

        .. code-block:: bash

            peer:
                .*:
                    - .*

        This configuration will enable all minions to execute all commands.
        peer:

        .. code-block:: bash

            foo.example.com:
                - test.*

        The above configuration will only allow the minion foo.example.com to
        execute commands from the test module.

        :param dict clear_load: The minion payload
        '''
        if not self.__verify_minion_publish(clear_load):
            return {}
        else:
            return self.masterapi.minion_publish(clear_load)

Response: Publish a command initiated from a minion


Instruction:
Make a summary of the following Python 3 code
def revoke_auth(self, load):
        '''
        Allow a minion to request revocation of its own key

        :param dict load: The minion payload

        :rtype: dict
        :return: If the load is invalid, it may be returned. No key operation is performed.

        :rtype: bool
        :return: True if key was revoked, False if not
        '''
        load = self.__verify_load(load, ('id', 'tok'))

        if not self.opts.get('allow_minion_key_revoke', False):
            log.warning(
                'Minion %s requested key revoke, but allow_minion_key_revoke '
                'is set to False', load['id']
            )
            return load

        if load is False:
            return load
        else:
            return self.masterapi.revoke_auth(load)

Response: Allow a minion to request revocation of its own key


Instruction:
Can you write a function in Python 3 where it
wraps for running functions that are not encrypted with AES encryption

Response:
def run_func(self, func, load):
        '''
        Wrapper for running functions executed with AES encryption

        :param function func: The function to run
        :return: The result of the master function that was called
        '''
        # Don't honor private functions
        if func.startswith('__'):
            # TODO: return some error? Seems odd to return {}
            return {}, {'fun': 'send'}
        # Run the func
        if hasattr(self, func):
            try:
                start = time.time()
                ret = getattr(self, func)(load)
                log.trace(
                    'Master function call %s took %s seconds',
                    func, time.time() - start
                )
            except Exception:
                ret = ''
                log.error('Error in function %s:\n', func, exc_info=True)
        else:
            log.error(
                'Received function %s which is unavailable on the master, '
                'returning False', func
            )
            return False, {'fun': 'send'}
        # Don't encrypt the return value for the _return func
        # (we don't care about the return value, so why encrypt it?)
        if func == '_return':
            return ret, {'fun': 'send'}
        if func == '_pillar' and 'id' in load:
            if load.get('ver') != '2' and self.opts['pillar_version'] == 1:
                # Authorized to return old pillar proto
                return ret, {'fun': 'send'}
            return ret, {'fun': 'send_private', 'key': 'pillar', 'tgt': load['id']}
        # Encrypt the return
        return ret, {'fun': 'send'}


Instruction:
Here you have a function in Python 3, explain what it does
def runner(self, clear_load):
        '''
        Send a master control function back to the runner system
        '''
        # All runner ops pass through eauth
        auth_type, err_name, key, sensitive_load_keys = self._prep_auth_info(clear_load)

        # Authenticate
        auth_check = self.loadauth.check_authentication(clear_load, auth_type, key=key)
        error = auth_check.get('error')

        if error:
            # Authentication error occurred: do not continue.
            return {'error': error}

        # Authorize
        username = auth_check.get('username')
        if auth_type != 'user':
            runner_check = self.ckminions.runner_check(
                auth_check.get('auth_list', []),
                clear_load['fun'],
                clear_load.get('kwarg', {})
            )
            if not runner_check:
                return {'error': {'name': err_name,
                                   'message': 'Authentication failure of type "{0}" occurred for '
                                             'user {1}.'.format(auth_type, username)}}
            elif isinstance(runner_check, dict) and 'error' in runner_check:
                # A dictionary with an error name/message was handled by ckminions.runner_check
                return runner_check

            # No error occurred, consume sensitive settings from the clear_load if passed.
            for item in sensitive_load_keys:
                clear_load.pop(item, None)
        else:
            if 'user' in clear_load:
                username = clear_load['user']
                if salt.auth.AuthUser(username).is_sudo():
                    username = self.opts.get('user', 'root')
            else:
                username = salt.utils.user.get_user()

        # Authorized. Do the job!
        try:
            fun = clear_load.pop('fun')
            runner_client = salt.runner.RunnerClient(self.opts)
            return runner_client.asynchronous(fun,
                                              clear_load.get('kwarg', {}),
                                              username)
        except Exception as exc:
            log.error('Exception occurred while introspecting %s: %s', fun, exc)
            return {'error': {'name': exc.__class__.__name__,
                              'args': exc.args,
                              'message': six.text_type(exc)}}

Response: Send a master control function back to the runner system


Instruction:
Can you implement a function in Python 3 that
sends a master control function back to the wheel system

Response:
def wheel(self, clear_load):
        '''
        Send a master control function back to the wheel system
        '''
        # All wheel ops pass through eauth
        auth_type, err_name, key, sensitive_load_keys = self._prep_auth_info(clear_load)

        # Authenticate
        auth_check = self.loadauth.check_authentication(clear_load, auth_type, key=key)
        error = auth_check.get('error')

        if error:
            # Authentication error occurred: do not continue.
            return {'error': error}

        # Authorize
        username = auth_check.get('username')
        if auth_type != 'user':
            wheel_check = self.ckminions.wheel_check(
                auth_check.get('auth_list', []),
                clear_load['fun'],
                clear_load.get('kwarg', {})
            )
            if not wheel_check:
                return {'error': {'name': err_name,
                                  'message': 'Authentication failure of type "{0}" occurred for '
                                             'user {1}.'.format(auth_type, username)}}
            elif isinstance(wheel_check, dict) and 'error' in wheel_check:
                # A dictionary with an error name/message was handled by ckminions.wheel_check
                return wheel_check

            # No error occurred, consume sensitive settings from the clear_load if passed.
            for item in sensitive_load_keys:
                clear_load.pop(item, None)
        else:
            if 'user' in clear_load:
                username = clear_load['user']
                if salt.auth.AuthUser(username).is_sudo():
                    username = self.opts.get('user', 'root')
            else:
                username = salt.utils.user.get_user()

        # Authorized. Do the job!
        try:
            jid = salt.utils.jid.gen_jid(self.opts)
            fun = clear_load.pop('fun')
            tag = tagify(jid, prefix='wheel')
            data = {'fun': "wheel.{0}".format(fun),
                    'jid': jid,
                    'tag': tag,
                    'user': username}

            self.event.fire_event(data, tagify([jid, 'new'], 'wheel'))
            ret = self.wheel_.call_func(fun, full_return=True, **clear_load)
            data['return'] = ret['return']
            data['success'] = ret['success']
            self.event.fire_event(data, tagify([jid, 'ret'], 'wheel'))
            return {'tag': tag,
                    'data': data}
        except Exception as exc:
            log.error('Exception occurred while introspecting %s: %s', fun, exc)
            data['return'] = 'Exception occurred in wheel {0}: {1}: {2}'.format(
                             fun,
                             exc.__class__.__name__,
                             exc,
            )
            data['success'] = False
            self.event.fire_event(data, tagify([jid, 'ret'], 'wheel'))
            return {'tag': tag,
                    'data': data}


Instruction:
Can you generate the documentation for the following Python 3 function
def mk_token(self, clear_load):
        '''
        Create and return an authentication token, the clear load needs to
        contain the eauth key and the needed authentication creds.
        '''
        token = self.loadauth.mk_token(clear_load)
        if not token:
            log.warning('Authentication failure of type "eauth" occurred.')
            return ''
        return token

Response: Create and return an authentication token.


Instruction:
How would you explain what the following Python 3 function does
def publish(self, clear_load):
        '''
        This method sends out publications to the minions, it can only be used
        by the LocalClient.
        '''
        extra = clear_load.get('kwargs', {})

        publisher_acl = salt.acl.PublisherACL(self.opts['publisher_acl_blacklist'])

        if publisher_acl.user_is_blacklisted(clear_load['user']) or \
                publisher_acl.cmd_is_blacklisted(clear_load['fun']):
            log.error(
                '%s does not have permissions to run %s. Please contact '
                'your local administrator if you believe this is in '
                'error.\n', clear_load['user'], clear_load['fun']
            )
            return {'error': {'name': 'AuthorizationError',
                              'message': 'Authorization error occurred.'}}

        # Retrieve the minions list
        delimiter = clear_load.get('kwargs', {}).get('delimiter', DEFAULT_TARGET_DELIM)
        _res = self.ckminions.check_minions(
            clear_load['tgt'],
            clear_load.get('tgt_type', 'glob'),
            delimiter
        )
        minions = _res.get('minions', list())
        missing = _res.get('missing', list())
        ssh_minions = _res.get('ssh_minions', False)

        # Check for external auth calls and authenticate
        auth_type, err_name, key, sensitive_load_keys = self._prep_auth_info(extra)
        if auth_type == 'user':
            auth_check = self.loadauth.check_authentication(clear_load, auth_type, key=key)
        else:
            auth_check = self.loadauth.check_authentication(extra, auth_type)

        # Setup authorization list variable and error information
        auth_list = auth_check.get('auth_list', [])
        err_msg = 'Authentication failure of type "{0}" occurred.'.format(auth_type)

        if auth_check.get('error'):
            # Authentication error occurred: do not continue.
            log.warning(err_msg)
            return {'error': {'name': 'AuthenticationError',
                              'message': 'Authentication error occurred.'}}

        # All Token, Eauth, and non-root users must pass the authorization check
        if auth_type != 'user' or (auth_type == 'user' and auth_list):
            # Authorize the request
            authorized = self.ckminions.auth_check(
                auth_list,
                clear_load['fun'],
                clear_load['arg'],
                clear_load['tgt'],
                clear_load.get('tgt_type', 'glob'),
                minions=minions,
                # always accept find_job
                whitelist=['saltutil.find_job'],
            )

            if not authorized:
                # Authorization error occurred. Do not continue.
                if auth_type == 'eauth' and not auth_list and 'username' in extra and 'eauth' in extra:
                    log.debug('Auth configuration for eauth "%s" and user "%s" is empty', extra['eauth'], extra['username'])
                log.warning(err_msg)
                return {'error': {'name': 'AuthorizationError',
                                  'message': 'Authorization error occurred.'}}

            # Perform some specific auth_type tasks after the authorization check
            if auth_type == 'token':
                username = auth_check.get('username')
                clear_load['user'] = username
                log.debug('Minion tokenized user = "%s"', username)
            elif auth_type == 'eauth':
                # The username we are attempting to auth with
                clear_load['user'] = self.loadauth.load_name(extra)

        # If we order masters (via a syndic), don't short circuit if no minions
        # are found
        if not self.opts.get('order_masters'):
            # Check for no minions
            if not minions:
                return {
                    'enc': 'clear',
                    'load': {
                        'jid': None,
                        'minions': minions,
                        'error': 'Master could not resolve minions for target {0}'.format(clear_load['tgt'])
                    }
                }
        if extra.get('batch', None):
            return self.publish_batch(clear_load, extra, minions, missing)

        jid = self._prep_jid(clear_load, extra)
        if jid is None:
            return {'enc': 'clear',
                    'load': {'error': 'Master failed to assign jid'}}
        payload = self._prep_pub(minions, jid, clear_load, extra, missing)

        # Send it!
        self._send_ssh_pub(payload, ssh_minions=ssh_minions)
        self._send_pub(payload)

        return {
            'enc': 'clear',
            'load': {
                'jid': clear_load['jid'],
                'minions': minions,
                'missing': missing
            }
        }

Response: Sends out publications to the minions


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _prep_jid(self, clear_load, extra):
        '''
        Return a jid for this publication
        '''
        # the jid in clear_load can be None, '', or something else. this is an
        # attempt to clean up the value before passing to plugins
        passed_jid = clear_load['jid'] if clear_load.get('jid') else None
        nocache = extra.get('nocache', False)

        # Retrieve the jid
        fstr = '{0}.prep_jid'.format(self.opts['master_job_cache'])
        try:
            # Retrieve the jid
            jid = self.mminion.returners[fstr](nocache=nocache,
                                               passed_jid=passed_jid)
        except (KeyError, TypeError):
            # The returner is not present
            msg = (
                'Failed to allocate a jid. The requested returner \'{0}\' '
                'could not be loaded.'.format(fstr.split('.')[0])
            )
            log.error(msg)
            return {'error': msg}
        return jid

Response: Prepare a jid for this publication


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _send_pub(self, load):
        '''
        Take a load and send it across the network to connected minions
        '''
        for transport, opts in iter_transport_opts(self.opts):
            chan = salt.transport.server.PubServerChannel.factory(opts)
            chan.publish(load)

Response: Send a load to all the minions that have been connected to


Instruction:
Write a Python 3 script to
send a load to the ssh server

Response:
def _send_ssh_pub(self, load, ssh_minions=False):
        '''
        Take a load and send it across the network to ssh minions
        '''
        if self.opts['enable_ssh_minions'] is True and ssh_minions is True:
            log.debug('Send payload to ssh minions')
            threading.Thread(target=self.ssh_client.cmd, kwargs=load).start()


Instruction:
Write a Python 3 script for
preparing a publication for a given load and perform the necessary steps to prepare a publication.

Response:
def _prep_pub(self, minions, jid, clear_load, extra, missing):
        '''
        Take a given load and perform the necessary steps
        to prepare a publication.

        TODO: This is really only bound by temporal cohesion
        and thus should be refactored even further.
        '''
        clear_load['jid'] = jid
        delimiter = clear_load.get('kwargs', {}).get('delimiter', DEFAULT_TARGET_DELIM)

        # TODO Error reporting over the master event bus
        self.event.fire_event({'minions': minions}, clear_load['jid'])
        new_job_load = {
            'jid': clear_load['jid'],
            'tgt_type': clear_load['tgt_type'],
            'tgt': clear_load['tgt'],
            'user': clear_load['user'],
            'fun': clear_load['fun'],
            'arg': clear_load['arg'],
            'minions': minions,
            'missing': missing,
            }

        # Announce the job on the event bus
        self.event.fire_event(new_job_load, tagify([clear_load['jid'], 'new'], 'job'))

        if self.opts['ext_job_cache']:
            fstr = '{0}.save_load'.format(self.opts['ext_job_cache'])
            save_load_func = True

            # Get the returner's save_load arg_spec.
            try:
                arg_spec = salt.utils.args.get_function_argspec(self.mminion.returners[fstr])

                # Check if 'minions' is included in returner's save_load arg_spec.
                # This may be missing in custom returners, which we should warn about.
                if 'minions' not in arg_spec.args:
                    log.critical(
                        'The specified returner used for the external job cache '
                        '\'%s\' does not have a \'minions\' kwarg in the returner\'s '
                        'save_load function.', self.opts['ext_job_cache']
                    )
            except (AttributeError, KeyError):
                save_load_func = False
                log.critical(
                    'The specified returner used for the external job cache '
                    '"%s" does not have a save_load function!',
                    self.opts['ext_job_cache']
                )

            if save_load_func:
                try:
                    self.mminion.returners[fstr](clear_load['jid'], clear_load, minions=minions)
                except Exception:
                    log.critical(
                        'The specified returner threw a stack trace:\n',
                        exc_info=True
                    )

        # always write out to the master job caches
        try:
            fstr = '{0}.save_load'.format(self.opts['master_job_cache'])
            self.mminion.returners[fstr](clear_load['jid'], clear_load, minions)
        except KeyError:
            log.critical(
                'The specified returner used for the master job cache '
                '"%s" does not have a save_load function!',
                self.opts['master_job_cache']
            )
        except Exception:
            log.critical(
                'The specified returner threw a stack trace:\n',
                exc_info=True
            )
        # Set up the payload
        payload = {'enc': 'aes'}
        # Altering the contents of the publish load is serious!! Changes here
        # break compatibility with minion/master versions and even tiny
        # additions can have serious implications on the performance of the
        # publish commands.
        #
        # In short, check with Thomas Hatch before you even think about
        # touching this stuff, we can probably do what you want to do another
        # way that won't have a negative impact.
        load = {
            'fun': clear_load['fun'],
            'arg': clear_load['arg'],
            'tgt': clear_load['tgt'],
            'jid': clear_load['jid'],
            'ret': clear_load['ret'],
        }
        # if you specified a master id, lets put that in the load
        if 'master_id' in self.opts:
            load['master_id'] = self.opts['master_id']
        # if someone passed us one, use that
        if 'master_id' in extra:
            load['master_id'] = extra['master_id']
        # Only add the delimiter to the pub data if it is non-default
        if delimiter != DEFAULT_TARGET_DELIM:
            load['delimiter'] = delimiter

        if 'id' in extra:
            load['id'] = extra['id']
        if 'tgt_type' in clear_load:
            load['tgt_type'] = clear_load['tgt_type']
        if 'to' in clear_load:
            load['to'] = clear_load['to']

        if 'kwargs' in clear_load:
            if 'ret_config' in clear_load['kwargs']:
                load['ret_config'] = clear_load['kwargs'].get('ret_config')

            if 'metadata' in clear_load['kwargs']:
                load['metadata'] = clear_load['kwargs'].get('metadata')

            if 'module_executors' in clear_load['kwargs']:
                load['module_executors'] = clear_load['kwargs'].get('module_executors')

            if 'executor_opts' in clear_load['kwargs']:
                load['executor_opts'] = clear_load['kwargs'].get('executor_opts')

            if 'ret_kwargs' in clear_load['kwargs']:
                load['ret_kwargs'] = clear_load['kwargs'].get('ret_kwargs')

        if 'user' in clear_load:
            log.info(
                'User %s Published command %s with jid %s',
                clear_load['user'], clear_load['fun'], clear_load['jid']
            )
            load['user'] = clear_load['user']
        else:
            log.info(
                'Published command %s with jid %s',
                clear_load['fun'], clear_load['jid']
            )
        log.debug('Published command details %s', load)
        return load


Instruction:
Can you generate a brief explanation for the following Python 3 code
def row_absent(name, db, table, where_sql, where_args=None):
    '''
    Makes sure the specified row is absent in db.  If multiple rows
    match where_sql, then the state will fail.

    name
        Only used as the unique ID

    db
        The database file name

    table
        The table name to check

    where_sql
        The sql to select the row to check

    where_args
        The list parameters to substitute in where_sql
    '''
    changes = {'name': name,
               'changes': {},
               'result': None,
               'comment': ''}
    conn = None
    try:
        conn = sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)
        conn.row_factory = _dict_factory
        rows = None
        if where_args is None:
            rows = _query(conn,
                          "SELECT * FROM `" + table + "` WHERE " + where_sql)
        else:
            rows = _query(conn,
                          "SELECT * FROM `" + table + "` WHERE " + where_sql,
                          where_args)

        if len(rows) > 1:
            changes['result'] = False
            changes['comment'] = "More than one row matched the specified query"
        elif len(rows) == 1:
            if __opts__['test']:
                changes['result'] = True
                changes['comment'] = "Row will be removed in " + table
                changes['changes']['old'] = rows[0]

            else:
                if where_args is None:
                    cursor = conn.execute("DELETE FROM `" +
                                          table + "` WHERE " + where_sql)
                else:
                    cursor = conn.execute("DELETE FROM `" +
                                          table + "` WHERE " + where_sql,
                                          where_args)
                conn.commit()
                if cursor.rowcount == 1:
                    changes['result'] = True
                    changes['comment'] = "Row removed"
                    changes['changes']['old'] = rows[0]
                else:
                    changes['result'] = False
                    changes['comment'] = "Unable to remove row"
        else:
            changes['result'] = True
            changes['comment'] = 'Row is absent'

    except Exception as e:
        changes['result'] = False
        changes['comment'] = six.text_type(e)

    finally:
        if conn:
            conn.close()

    return changes

Response: Make sure the specified row is absent in db.


Instruction:
Can you create a Python 3 function that
checks to make sure the given row exists and is present in the given database table and data. If update is True the row will be updated with data. Otherwise it will fail.

Response:
def row_present(name,
                db,
                table,
                data,
                where_sql,
                where_args=None,
                update=False):
    '''
    Checks to make sure the given row exists. If row exists and update is True
    then row will be updated with data. Otherwise it will leave existing
    row unmodified and check it against data. If the existing data
    doesn't match data_check the state will fail.  If the row doesn't
    exist then it will insert data into the table. If more than one
    row matches, then the state will fail.

    name
        Only used as the unique ID

    db
        The database file name

    table
        The table name to check the data

    data
        The dictionary of key/value pairs to check against if
        row exists, insert into the table if it doesn't

    where_sql
        The sql to select the row to check

    where_args
        The list parameters to substitute in where_sql

    update
        True will replace the existing row with data
        When False and the row exists and data does not equal
        the row data then the state will fail
    '''
    changes = {'name': name,
               'changes': {},
               'result': None,
               'comment': ''}
    conn = None
    try:
        conn = sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)
        conn.row_factory = _dict_factory
        rows = None
        if where_args is None:
            rows = _query(conn, "SELECT * FROM `" +
                                table + "` WHERE " + where_sql)
        else:
            rows = _query(conn, "SELECT * FROM `" +
                                table + "` WHERE " + where_sql,
                                where_args)

        if len(rows) > 1:
            changes['result'] = False
            changes['comment'] = 'More than one row matched the specified query'
        elif len(rows) == 1:
            for key, value in six.iteritems(data):
                if key in rows[0] and rows[0][key] != value:
                    if update:
                        if __opts__['test']:
                            changes['result'] = True
                            changes['comment'] = "Row will be update in " + table

                        else:
                            columns = []
                            params = []
                            for key, value in six.iteritems(data):
                                columns.append("`" + key + "`=?")
                                params.append(value)

                            if where_args is not None:
                                params += where_args

                            sql = "UPDATE `" + table + "` SET "
                            sql += ",".join(columns)
                            sql += " WHERE "
                            sql += where_sql
                            cursor = conn.execute(sql, params)
                            conn.commit()
                            if cursor.rowcount == 1:
                                changes['result'] = True
                                changes['comment'] = "Row updated"
                                changes['changes']['old'] = rows[0]
                                changes['changes']['new'] = data
                            else:
                                changes['result'] = False
                                changes['comment'] = "Row update failed"
                    else:
                        changes['result'] = False
                        changes['comment'] = "Existing data does" + \
                                             "not match desired state"
                        break

            if changes['result'] is None:
                changes['result'] = True
                changes['comment'] = "Row exists"
        else:
            if __opts__['test']:
                changes['result'] = True
                changes['changes']['new'] = data
                changes['comment'] = "Row will be inserted into " + table
            else:
                columns = []
                value_stmt = []
                values = []
                for key, value in six.iteritems(data):
                    value_stmt.append('?')
                    values.append(value)
                    columns.append("`" + key + "`")

                sql = "INSERT INTO `" + table + "` ("
                sql += ",".join(columns)
                sql += ") VALUES ("
                sql += ",".join(value_stmt)
                sql += ")"
                cursor = conn.execute(sql, values)
                conn.commit()
                if cursor.rowcount == 1:
                    changes['result'] = True
                    changes['changes']['new'] = data
                    changes['comment'] = 'Inserted row'
                else:
                    changes['result'] = False
                    changes['comment'] = "Unable to insert data"

    except Exception as e:
        changes['result'] = False
        changes['comment'] = six.text_type(e)

    finally:
        if conn:
            conn.close()

    return changes


Instruction:
Given the following Python 3 function, write the documentation
def table_absent(name, db):
    '''
    Make sure the specified table does not exist

    name
        The name of the table

    db
        The name of the database file
    '''
    changes = {'name': name,
               'changes': {},
               'result': None,
               'comment': ''}
    conn = None
    try:
        conn = sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)
        tables = _query(conn, "SELECT sql FROM sqlite_master " +
                              " WHERE type='table' AND name=?", [name])

        if len(tables) == 1:
            if __opts__['test']:
                changes['result'] = True
                changes['comment'] = "'" + name + "' will be dropped"
            else:
                conn.execute("DROP TABLE " + name)
                conn.commit()
                changes['changes']['old'] = tables[0][0]
                changes['result'] = True
                changes['comment'] = "'" + name + "' was dropped"
        elif not tables:
            changes['result'] = True
            changes['comment'] = "'" + name + "' is already absent"
        else:
            changes['result'] = False
            changes['comment'] = "Multiple tables with the same name='" + \
                                 name + "'"

    except Exception as e:
        changes['result'] = False
        changes['comment'] = six.text_type(e)

    finally:
        if conn:
            conn.close()

    return changes

Response: Make sure the specified table does not exist in the specified database


Instruction:
Can you generate the documentation for the following Python 3 function
def table_present(name, db, schema, force=False):
    '''
    Make sure the specified table exists with the specified schema

    name
        The name of the table

    db
        The name of the database file

    schema
        The dictionary containing the schema information

    force
        If the name of the table exists and force is set to False,
        the state will fail.  If force is set to True, the existing
        table will be replaced with the new table
    '''
    changes = {'name': name,
               'changes': {},
               'result': None,
               'comment': ''}
    conn = None
    try:
        conn = sqlite3.connect(db, detect_types=sqlite3.PARSE_DECLTYPES)
        tables = _query(conn,
                        "SELECT sql FROM sqlite_master " +
                        "WHERE type='table' AND name=?", [name])

        if len(tables) == 1:
            sql = None
            if isinstance(schema, six.string_types):
                sql = schema.strip()
            else:
                sql = _get_sql_from_schema(name, schema)

            if sql != tables[0][0]:
                if force:
                    if __opts__['test']:
                        changes['result'] = True
                        changes['changes']['old'] = tables[0][0]
                        changes['changes']['new'] = sql
                        changes['comment'] = "'" + name + "' will be replaced"
                    else:
                        conn.execute("DROP TABLE `" + name + "`")
                        conn.execute(sql)
                        conn.commit()
                        changes['result'] = True
                        changes['changes']['old'] = tables[0][0]
                        changes['changes']['new'] = sql
                        changes['comment'] = "Replaced '" + name + "'"
                else:
                    changes['result'] = False
                    changes['comment'] = "Expected schema=" + sql + \
                                         "\nactual schema=" + tables[0][0]
            else:
                changes['result'] = True
                changes['comment'] = "'" + name + \
                                     "' exists with matching schema"
        elif not tables:
            # Create the table
            sql = None
            if isinstance(schema, six.string_types):
                sql = schema
            else:
                sql = _get_sql_from_schema(name, schema)
            if __opts__['test']:
                changes['result'] = True
                changes['changes']['new'] = sql
                changes['comment'] = "'" + name + "' will be created"
            else:
                conn.execute(sql)
                conn.commit()
                changes['result'] = True
                changes['changes']['new'] = sql
                changes['comment'] = "Created table '" + name + "'"
        else:
            changes['result'] = False
            changes['comment'] = 'Multiple tables with the same name=' + name

    except Exception as e:
        changes['result'] = False
        changes['comment'] = str(e)

    finally:
        if conn:
            conn.close()

    return changes

Response: Ensure the specified table exists with the specified schema.


Instruction:
Can you write a function in Python 3 where it
matches based on range cluster Matches based on range cluster

Response:
def match(tgt, opts=None):
    '''
    Matches based on range cluster
    '''
    if not opts:
        opts = __opts__
    if HAS_RANGE:
        range_ = seco.range.Range(opts['range_server'])
        try:
            return opts['grains']['fqdn'] in range_.expand(tgt)
        except seco.range.RangeException as exc:
            log.debug('Range exception in compound match: %s', exc)
            return False
    return False


Instruction:
Can you generate a brief explanation for the following Python 3 code
def returner(ret):
    '''
    Return data to a Cassandra ColumnFamily
    '''

    consistency_level = getattr(pycassa.ConsistencyLevel,
                                __opts__['cassandra.consistency_level'])

    pool = pycassa.ConnectionPool(__opts__['cassandra.keyspace'],
                                  __opts__['cassandra.servers'])
    ccf = pycassa.ColumnFamily(pool, __opts__['cassandra.column_family'],
                               write_consistency_level=consistency_level)

    columns = {'fun': ret['fun'],
               'id': ret['id']}
    if isinstance(ret['return'], dict):
        for key, value in six.iteritems(ret['return']):
            columns['return.{0}'.format(key)] = six.text_type(value)
    else:
        columns['return'] = six.text_type(ret['return'])

    log.debug(columns)
    ccf.insert(ret['jid'], columns)

Response: Return data to a Cassandra ColumnFamily


Instruction:
Here you have a function in Python 3, explain what it does
def prep_bootstrap(mpt):
    '''
    Update and get the random script to a random place

    CLI Example:

    .. code-block:: bash

        salt '*' seed.prep_bootstrap /tmp

    '''
    # Verify that the boostrap script is downloaded
    bs_ = __salt__['config.gather_bootstrap_script']()
    fpd_ = os.path.join(mpt, 'tmp', "{0}".format(
        uuid.uuid4()))
    if not os.path.exists(fpd_):
        os.makedirs(fpd_)
    os.chmod(fpd_, 0o700)
    fp_ = os.path.join(fpd_, os.path.basename(bs_))
    # Copy script into tmp
    shutil.copy(bs_, fp_)
    tmppath = fpd_.replace(mpt, '')
    return fp_, tmppath

Response:
Update and get the random script to a random place
    CLI Example


Instruction:
Write a Python 3 function that can
generate a new minion key and a new minion key.

Response:
def apply_(path, id_=None, config=None, approve_key=True, install=True,
           prep_install=False, pub_key=None, priv_key=None, mount_point=None):
    '''
    Seed a location (disk image, directory, or block device) with the
    minion config, approve the minion's key, and/or install salt-minion.

    CLI Example:

    .. code-block:: bash

        salt 'minion' seed.apply path id [config=config_data] \\
                [gen_key=(true|false)] [approve_key=(true|false)] \\
                [install=(true|false)]

    path
        Full path to the directory, device, or disk image  on the target
        minion's file system.

    id
        Minion id with which to seed the path.

    config
        Minion configuration options. By default, the 'master' option is set to
        the target host's 'master'.

    approve_key
        Request a pre-approval of the generated minion key. Requires
        that the salt-master be configured to either auto-accept all keys or
        expect a signing request from the target host. Default: true.

    install
        Install salt-minion, if absent. Default: true.

    prep_install
        Prepare the bootstrap script, but don't run it. Default: false
    '''
    stats = __salt__['file.stats'](path, follow_symlinks=True)
    if not stats:
        return '{0} does not exist'.format(path)
    ftype = stats['type']
    path = stats['target']
    log.debug('Mounting %s at %s', ftype, path)
    try:
        os.makedirs(path)
    except OSError:
        # The directory already exists
        pass

    mpt = _mount(path, ftype, mount_point)

    if not mpt:
        return '{0} could not be mounted'.format(path)

    tmp = os.path.join(mpt, 'tmp')
    log.debug('Attempting to create directory %s', tmp)
    try:
        os.makedirs(tmp)
    except OSError:
        if not os.path.isdir(tmp):
            raise
    cfg_files = mkconfig(config, tmp=tmp, id_=id_, approve_key=approve_key,
                         pub_key=pub_key, priv_key=priv_key)

    if _check_install(mpt):
        # salt-minion is already installed, just move the config and keys
        # into place
        log.info('salt-minion pre-installed on image, '
                 'configuring as %s', id_)
        minion_config = salt.config.minion_config(cfg_files['config'])
        pki_dir = minion_config['pki_dir']
        if not os.path.isdir(os.path.join(mpt, pki_dir.lstrip('/'))):
            __salt__['file.makedirs'](
                os.path.join(mpt, pki_dir.lstrip('/'), '')
            )
        os.rename(cfg_files['privkey'], os.path.join(
            mpt, pki_dir.lstrip('/'), 'minion.pem'))
        os.rename(cfg_files['pubkey'], os.path.join(
            mpt, pki_dir.lstrip('/'), 'minion.pub'))
        os.rename(cfg_files['config'], os.path.join(mpt, 'etc/salt/minion'))
        res = True
    elif install:
        log.info('Attempting to install salt-minion to %s', mpt)
        res = _install(mpt)
    elif prep_install:
        log.error('The prep_install option is no longer supported. Please use '
                  'the bootstrap script installed with Salt, located at %s.',
                  salt.syspaths.BOOTSTRAP)
        res = False
    else:
        log.warning('No useful action performed on %s', mpt)
        res = False

    _umount(mpt, ftype)
    return res


Instruction:
Can you write a function in Python 3 where it
generates keys and config and put them in a tmp directory.

Response:
def mkconfig(config=None,
             tmp=None,
             id_=None,
             approve_key=True,
             pub_key=None,
             priv_key=None):
    '''
    Generate keys and config and put them in a tmp directory.

    pub_key
        absolute path or file content of an optional preseeded salt key

    priv_key
        absolute path or file content of an optional preseeded salt key

    CLI Example:

    .. code-block:: bash

        salt 'minion' seed.mkconfig [config=config_data] [tmp=tmp_dir] \\
                [id_=minion_id] [approve_key=(true|false)]
    '''
    if tmp is None:
        tmp = tempfile.mkdtemp()
    if config is None:
        config = {}
    if 'master' not in config and __opts__['master'] != 'salt':
        config['master'] = __opts__['master']
    if id_:
        config['id'] = id_

    # Write the new minion's config to a tmp file
    tmp_config = os.path.join(tmp, 'minion')
    with salt.utils.files.fopen(tmp_config, 'w+') as fp_:
        fp_.write(salt.utils.cloud.salt_config_to_yaml(config))

    # Generate keys for the minion
    pubkeyfn = os.path.join(tmp, 'minion.pub')
    privkeyfn = os.path.join(tmp, 'minion.pem')
    preseeded = pub_key and priv_key
    if preseeded:
        log.debug('Writing minion.pub to %s', pubkeyfn)
        log.debug('Writing minion.pem to %s', privkeyfn)
        with salt.utils.files.fopen(pubkeyfn, 'w') as fic:
            fic.write(salt.utils.stringutils.to_str(_file_or_content(pub_key)))
        with salt.utils.files.fopen(privkeyfn, 'w') as fic:
            fic.write(salt.utils.stringutils.to_str(_file_or_content(priv_key)))
        os.chmod(pubkeyfn, 0o600)
        os.chmod(privkeyfn, 0o600)
    else:
        salt.crypt.gen_keys(tmp, 'minion', 2048)
    if approve_key and not preseeded:
        with salt.utils.files.fopen(pubkeyfn) as fp_:
            pubkey = salt.utils.stringutils.to_unicode(fp_.read())
            __salt__['pillar.ext']({'virtkey': [id_, pubkey]})

    return {'config': tmp_config, 'pubkey': pubkeyfn, 'privkey': privkeyfn}


Instruction:
Explain what the following Python 3 code does
def _install(mpt):
    '''
    Determine whether salt-minion is installed and, if not,
    install it.
    Return True if install is successful or already installed.
    '''
    _check_resolv(mpt)
    boot_, tmppath = (prep_bootstrap(mpt)
             or salt.syspaths.BOOTSTRAP)
    # Exec the chroot command
    cmd = 'if type salt-minion; then exit 0; '
    cmd += 'else sh {0} -c /tmp; fi'.format(os.path.join(tmppath, 'bootstrap-salt.sh'))
    return not __salt__['cmd.run_chroot'](mpt, cmd, python_shell=True)['retcode']

Response: Install salt - minion if not installed.


Instruction:
Explain what the following Python 3 code does
def _check_resolv(mpt):
    '''
    Check that the resolv.conf is present and populated
    '''
    resolv = os.path.join(mpt, 'etc/resolv.conf')
    replace = False
    if os.path.islink(resolv):
        resolv = os.path.realpath(resolv)
        if not os.path.isdir(os.path.dirname(resolv)):
            os.makedirs(os.path.dirname(resolv))
    if not os.path.isfile(resolv):
        replace = True
    if not replace:
        with salt.utils.files.fopen(resolv, 'rb') as fp_:
            conts = salt.utils.stringutils.to_unicode(fp_.read())
            if 'nameserver' not in conts:
                replace = True
            if 'nameserver 127.0.0.1' in conts:
                replace = True
    if replace:
        shutil.copy('/etc/resolv.conf', resolv)

Response: Check that the resolv. conf is present and populated


Instruction:
Make a summary of the following Python 3 code
def _netsh_file(content):
    '''
    helper function to get the results of ``netsh -f content.txt``

    Running ``netsh`` will drop you into a ``netsh`` prompt where you can issue
    ``netsh`` commands. You can put a series of commands in an external file and
    run them as if from a ``netsh`` prompt using the ``-f`` switch. That's what
    this function does.

    Args:

        content (str):
            The contents of the file that will be run by the ``netsh -f``
            command

    Returns:
        str: The text returned by the netsh command
    '''
    with tempfile.NamedTemporaryFile(mode='w',
                                     prefix='salt-',
                                     suffix='.netsh',
                                     delete=False) as fp:
        fp.write(content)
    try:
        log.debug('%s:\n%s', fp.name, content)
        return salt.modules.cmdmod.run('netsh -f {0}'.format(fp.name), python_shell=True)
    finally:
        os.remove(fp.name)

Response: helper function to get the results of netsh - f content. txt


Instruction:
Can you tell what is the following Python 3 function doing
def get_settings(profile, section, store='local'):
    '''
    Get the firewall property from the specified profile in the specified store
    as returned by ``netsh advfirewall``.

    Args:

        profile (str):
            The firewall profile to query. Valid options are:

            - domain
            - public
            - private

        section (str):
            The property to query within the selected profile. Valid options
            are:

            - firewallpolicy : inbound/outbound behavior
            - logging : firewall logging settings
            - settings : firewall properties
            - state : firewalls state (on | off)

        store (str):
            The store to use. This is either the local firewall policy or the
            policy defined by local group policy. Valid options are:

            - lgpo
            - local

            Default is ``local``

    Returns:
        dict: A dictionary containing the properties for the specified profile

    Raises:
        CommandExecutionError: If an error occurs
        ValueError: If the parameters are incorrect
    '''
    # validate input
    if profile.lower() not in ('domain', 'public', 'private'):
        raise ValueError('Incorrect profile: {0}'.format(profile))
    if section.lower() not in ('state', 'firewallpolicy', 'settings', 'logging'):
        raise ValueError('Incorrect section: {0}'.format(section))
    if store.lower() not in ('local', 'lgpo'):
        raise ValueError('Incorrect store: {0}'.format(store))
    command = 'show {0}profile {1}'.format(profile, section)
    # run it
    results = _netsh_command(command=command, store=store)
    # sample output:
    # Domain Profile Settings:
    # ----------------------------------------------------------------------
    # LocalFirewallRules                    N/A (GPO-store only)
    # LocalConSecRules                      N/A (GPO-store only)
    # InboundUserNotification               Disable
    # RemoteManagement                      Disable
    # UnicastResponseToMulticast            Enable

    # if it's less than 3 lines it failed
    if len(results) < 3:
        raise CommandExecutionError('Invalid results: {0}'.format(results))
    ret = {}
    # Skip the first 2 lines. Add everything else to a dictionary
    for line in results[3:]:
        ret.update(dict(list(zip(*[iter(re.split(r"\s{2,}", line))]*2))))

    # Remove spaces from the values so that `Not Configured` is detected
    # correctly
    for item in ret:
        ret[item] = ret[item].replace(' ', '')

    # special handling for firewallpolicy
    if section == 'firewallpolicy':
        inbound, outbound = ret['Firewall Policy'].split(',')
        return {'Inbound': inbound, 'Outbound': outbound}

    return ret

Response: Get the firewall properties for the specified profile in the specified store.


Instruction:
Here you have a function in Python 3, explain what it does
def get_all_settings(profile, store='local'):
    '''
    Gets all the properties for the specified profile in the specified store

    Args:

        profile (str):
            The firewall profile to query. Valid options are:

            - domain
            - public
            - private

        store (str):
            The store to use. This is either the local firewall policy or the
            policy defined by local group policy. Valid options are:

            - lgpo
            - local

            Default is ``local``

    Returns:
        dict: A dictionary containing the specified settings
    '''
    ret = dict()
    ret.update(get_settings(profile=profile, section='state', store=store))
    ret.update(get_settings(profile=profile, section='firewallpolicy', store=store))
    ret.update(get_settings(profile=profile, section='settings', store=store))
    ret.update(get_settings(profile=profile, section='logging', store=store))
    return ret

Response: Gets all the properties for the specified profile in the specified store


Instruction:
Can you write a function in Python 3 where it
gets all properties for all profiles in the specified store

Response:
def get_all_profiles(store='local'):
    '''
    Gets all properties for all profiles in the specified store

    Args:

        store (str):
            The store to use. This is either the local firewall policy or the
            policy defined by local group policy. Valid options are:

            - lgpo
            - local

            Default is ``local``

    Returns:
        dict: A dictionary containing the specified settings for each profile
    '''
    return {
        'Domain Profile': get_all_settings(profile='domain', store=store),
        'Private Profile': get_all_settings(profile='private', store=store),
        'Public Profile': get_all_settings(profile='public', store=store)
    }


Instruction:
Write a Python 3 script for
setting the firewall settings for the specified profile and store.

Response:
def set_firewall_settings(profile,
                          inbound=None,
                          outbound=None,
                          store='local'):
    '''
    Set the firewall inbound/outbound settings for the specified profile and
    store

    Args:

        profile (str):
            The firewall profile to configure. Valid options are:

            - domain
            - public
            - private

        inbound (str):
            The inbound setting. If ``None`` is passed, the setting will remain
            unchanged. Valid values are:

            - blockinbound
            - blockinboundalways
            - allowinbound
            - notconfigured

            Default is ``None``

        outbound (str):
            The outbound setting. If ``None`` is passed, the setting will remain
            unchanged. Valid values are:

            - allowoutbound
            - blockoutbound
            - notconfigured

            Default is ``None``

        store (str):
            The store to use. This is either the local firewall policy or the
            policy defined by local group policy. Valid options are:

            - lgpo
            - local

            Default is ``local``

    Returns:
        bool: ``True`` if successful

    Raises:
        CommandExecutionError: If an error occurs
        ValueError: If the parameters are incorrect
    '''
    # Input validation
    if profile.lower() not in ('domain', 'public', 'private'):
        raise ValueError('Incorrect profile: {0}'.format(profile))
    if inbound and inbound.lower() not in ('blockinbound',
                                           'blockinboundalways',
                                           'allowinbound',
                                           'notconfigured'):
        raise ValueError('Incorrect inbound value: {0}'.format(inbound))
    if outbound and outbound.lower() not in ('allowoutbound',
                                             'blockoutbound',
                                             'notconfigured'):
        raise ValueError('Incorrect outbound value: {0}'.format(outbound))
    if not inbound and not outbound:
        raise ValueError('Must set inbound or outbound')

    # You have to specify inbound and outbound setting at the same time
    # If you're only specifying one, you have to get the current setting for the
    # other
    if not inbound or not outbound:
        ret = get_settings(profile=profile,
                           section='firewallpolicy',
                           store=store)
        if not inbound:
            inbound = ret['Inbound']
        if not outbound:
            outbound = ret['Outbound']

    command = 'set {0}profile firewallpolicy {1},{2}' \
              ''.format(profile, inbound, outbound)

    results = _netsh_command(command=command, store=store)

    if results:
        raise CommandExecutionError('An error occurred: {0}'.format(results))

    return True


Instruction:
Explain what the following Python 3 code does
def set_logging_settings(profile, setting, value, store='local'):
    '''
    Configure logging settings for the Windows firewall.

    Args:

        profile (str):
            The firewall profile to configure. Valid options are:

            - domain
            - public
            - private

        setting (str):
            The logging setting to configure. Valid options are:

            - allowedconnections
            - droppedconnections
            - filename
            - maxfilesize

        value (str):
            The value to apply to the setting. Valid values are dependent upon
            the setting being configured. Valid options are:

            allowedconnections:

                - enable
                - disable
                - notconfigured

            droppedconnections:

                - enable
                - disable
                - notconfigured

            filename:

                - Full path and name of the firewall log file
                - notconfigured

            maxfilesize:

                - 1 - 32767 (Kb)
                - notconfigured

        store (str):
            The store to use. This is either the local firewall policy or the
            policy defined by local group policy. Valid options are:

            - lgpo
            - local

            Default is ``local``

    Returns:
        bool: ``True`` if successful

    Raises:
        CommandExecutionError: If an error occurs
        ValueError: If the parameters are incorrect
    '''
    # Input validation
    if profile.lower() not in ('domain', 'public', 'private'):
        raise ValueError('Incorrect profile: {0}'.format(profile))
    if setting.lower() not in ('allowedconnections',
                               'droppedconnections',
                               'filename',
                               'maxfilesize'):
        raise ValueError('Incorrect setting: {0}'.format(setting))
    if setting.lower() in ('allowedconnections', 'droppedconnections'):
        if value.lower() not in ('enable', 'disable', 'notconfigured'):
            raise ValueError('Incorrect value: {0}'.format(value))
    # TODO: Consider adding something like the following to validate filename
    # https://stackoverflow.com/questions/9532499/check-whether-a-path-is-valid-in-python-without-creating-a-file-at-the-paths-ta
    if setting.lower() == 'maxfilesize':
        if value.lower() != 'notconfigured':
            # Must be a number between 1 and 32767
            try:
                int(value)
            except ValueError:
                raise ValueError('Incorrect value: {0}'.format(value))
            if not 1 <= int(value) <= 32767:
                raise ValueError('Incorrect value: {0}'.format(value))
    # Run the command
    command = 'set {0}profile logging {1} {2}'.format(profile, setting, value)
    results = _netsh_command(command=command, store=store)

    # A successful run should return an empty list
    if results:
        raise CommandExecutionError('An error occurred: {0}'.format(results))

    return True

Response: Configure the logging settings for the Windows firewall.


Instruction:
Implement a Python 3 function for
configuring firewall settings. Args: profile (str): The firewall profile to configure. Valid options are: - domain - public - private setting (str): The firewall setting to configure. Valid options are: - localfirewallrules - localconsecrules - inboundusernotification - remotemanagement - unicastresponsetomulticast value (str): The value to apply to the setting. Valid options are - enable - disable - notconfigured store (str): The store to use. This is either the local firewall policy or the policy defined by local group policy. Valid options are: - lgpo - local Default is ``local`` Returns: bool: ``True`` if successful Raises: CommandExecutionError: If an error occurs ValueError: If the parameters are incorrect

Response:
def set_settings(profile, setting, value, store='local'):
    '''
    Configure firewall settings.

    Args:

        profile (str):
            The firewall profile to configure. Valid options are:

            - domain
            - public
            - private

        setting (str):
            The firewall setting to configure. Valid options are:

            - localfirewallrules
            - localconsecrules
            - inboundusernotification
            - remotemanagement
            - unicastresponsetomulticast

        value (str):
            The value to apply to the setting. Valid options are

            - enable
            - disable
            - notconfigured

        store (str):
            The store to use. This is either the local firewall policy or the
            policy defined by local group policy. Valid options are:

            - lgpo
            - local

            Default is ``local``

    Returns:
        bool: ``True`` if successful

    Raises:
        CommandExecutionError: If an error occurs
        ValueError: If the parameters are incorrect
    '''
    # Input validation
    if profile.lower() not in ('domain', 'public', 'private'):
        raise ValueError('Incorrect profile: {0}'.format(profile))
    if setting.lower() not in ('localfirewallrules',
                               'localconsecrules',
                               'inboundusernotification',
                               'remotemanagement',
                               'unicastresponsetomulticast'):
        raise ValueError('Incorrect setting: {0}'.format(setting))
    if value.lower() not in ('enable', 'disable', 'notconfigured'):
        raise ValueError('Incorrect value: {0}'.format(value))

    # Run the command
    command = 'set {0}profile settings {1} {2}'.format(profile, setting, value)
    results = _netsh_command(command=command, store=store)

    # A successful run should return an empty list
    if results:
        raise CommandExecutionError('An error occurred: {0}'.format(results))

    return True


Instruction:
Write a Python 3 script for
configuring the firewall state of the current node.

Response:
def set_state(profile, state, store='local'):
    '''
    Configure the firewall state.

    Args:

        profile (str):
            The firewall profile to configure. Valid options are:

            - domain
            - public
            - private

        state (str):
            The firewall state. Valid options are:

            - on
            - off
            - notconfigured

        store (str):
            The store to use. This is either the local firewall policy or the
            policy defined by local group policy. Valid options are:

            - lgpo
            - local

            Default is ``local``

    Returns:
        bool: ``True`` if successful

    Raises:
        CommandExecutionError: If an error occurs
        ValueError: If the parameters are incorrect
    '''
    # Input validation
    if profile.lower() not in ('domain', 'public', 'private'):
        raise ValueError('Incorrect profile: {0}'.format(profile))
    if state.lower() not in ('on', 'off', 'notconfigured'):
        raise ValueError('Incorrect state: {0}'.format(state))

    # Run the command
    command = 'set {0}profile state {1}'.format(profile, state)
    results = _netsh_command(command=command, store=store)

    # A successful run should return an empty list
    if results:
        raise CommandExecutionError('An error occurred: {0}'.format(results))

    return True


Instruction:
Create a Python 3 function for
parsing the aliases file and return a list of line components

Response:
def __parse_aliases():
    '''
    Parse the aliases file, and return a list of line components:

    [
      (alias1, target1, comment1),
      (alias2, target2, comment2),
    ]
    '''
    afn = __get_aliases_filename()
    ret = []
    if not os.path.isfile(afn):
        return ret
    with salt.utils.files.fopen(afn, 'r') as ifile:
        for line in ifile:
            line = salt.utils.stringutils.to_unicode(line)
            match = __ALIAS_RE.match(line)
            if match:
                ret.append(match.groups())
            else:
                ret.append((None, None, line.strip()))
    return ret


Instruction:
How would you code a function in Python 3 to
write a new copy of the aliases file.

Response:
def __write_aliases_file(lines):
    '''
    Write a new copy of the aliases file.  Lines is a list of lines
    as returned by __parse_aliases.
    '''
    afn = __get_aliases_filename()
    adir = os.path.dirname(afn)

    out = tempfile.NamedTemporaryFile(dir=adir, delete=False)

    if not __opts__.get('integration.test', False):
        if os.path.isfile(afn):
            afn_st = os.stat(afn)
            os.chmod(out.name, stat.S_IMODE(afn_st.st_mode))
            os.chown(out.name, afn_st.st_uid, afn_st.st_gid)
        else:
            os.chmod(out.name, 0o644)
            os.chown(out.name, 0, 0)

    for (line_alias, line_target, line_comment) in lines:
        if isinstance(line_target, list):
            line_target = ', '.join(line_target)
        if not line_comment:
            line_comment = ''
        if line_alias and line_target:
            write_line = '{0}: {1}{2}\n'.format(
                line_alias, line_target, line_comment
            )
        else:
            write_line = '{0}\n'.format(line_comment)
        if six.PY3:
            write_line = write_line.encode(__salt_system_encoding__)
        out.write(write_line)

    out.close()
    os.rename(out.name, afn)

    # Search $PATH for the newalises command
    newaliases = salt.utils.path.which('newaliases')
    if newaliases is not None:
        __salt__['cmd.run'](newaliases)

    return True


Instruction:
How would you code a function in Python 3 to
return the aliases found in the aliases file in this format

Response:
def list_aliases():
    '''
    Return the aliases found in the aliases file in this format::

        {'alias': 'target'}

    CLI Example:

    .. code-block:: bash

        salt '*' aliases.list_aliases
    '''
    ret = dict((alias, target) for alias, target, comment in __parse_aliases() if alias)
    return ret


Instruction:
Make a summary of the following Python 3 code
def has_target(alias, target):
    '''
    Return true if the alias/target is set

    CLI Example:

    .. code-block:: bash

        salt '*' aliases.has_target alias target
    '''
    if target == '':
        raise SaltInvocationError('target can not be an empty string')
    aliases = list_aliases()
    if alias not in aliases:
        return False
    if isinstance(target, list):
        target = ', '.join(target)
    return target == aliases[alias]

Response: Return true if the alias is set otherwise False


Instruction:
Implement a Python 3 function for
setting the entry in the aliases file for the given alias to the given target.

Response:
def set_target(alias, target):
    '''
    Set the entry in the aliases file for the given alias, this will overwrite
    any previous entry for the given alias or create a new one if it does not
    exist.

    CLI Example:

    .. code-block:: bash

        salt '*' aliases.set_target alias target
    '''

    if alias == '':
        raise SaltInvocationError('alias can not be an empty string')

    if target == '':
        raise SaltInvocationError('target can not be an empty string')

    if get_target(alias) == target:
        return True

    lines = __parse_aliases()
    out = []
    ovr = False
    for (line_alias, line_target, line_comment) in lines:
        if line_alias == alias:
            if not ovr:
                out.append((alias, target, line_comment))
                ovr = True
        else:
            out.append((line_alias, line_target, line_comment))
    if not ovr:
        out.append((alias, target, ''))

    __write_aliases_file(out)
    return True


Instruction:
How would you explain what the following Python 3 function does
def rm_alias(alias):
    '''
    Remove an entry from the aliases file

    CLI Example:

    .. code-block:: bash

        salt '*' aliases.rm_alias alias
    '''
    if not get_target(alias):
        return True

    lines = __parse_aliases()
    out = []
    for (line_alias, line_target, line_comment) in lines:
        if line_alias != alias:
            out.append((line_alias, line_target, line_comment))

    __write_aliases_file(out)
    return True

Response:
Remove an entry from the aliases file
    CLI Example : bash
CTYPE rm_alias alias


Instruction:
Write a Python 3 function for
gathering the specified data from the minion data cache

Response:
def gather_cache(self):
        '''
        Gather the specified data from the minion data cache
        '''
        cache = {'grains': {}, 'pillar': {}}
        if self.grains or self.pillar:
            if self.opts.get('minion_data_cache'):
                minions = self.cache.list('minions')
                if not minions:
                    return cache
                for minion in minions:
                    total = self.cache.fetch('minions/{0}'.format(minion), 'data')

                    if 'pillar' in total:
                        if self.pillar_keys:
                            for key in self.pillar_keys:
                                if key in total['pillar']:
                                    cache['pillar'][minion][key] = total['pillar'][key]
                        else:
                            cache['pillar'][minion] = total['pillar']
                    else:
                        cache['pillar'][minion] = {}

                    if 'grains' in total:
                        if self.grain_keys:
                            for key in self.grain_keys:
                                if key in total['grains']:
                                    cache['grains'][minion][key] = total['grains'][key]
                        else:
                            cache['grains'][minion] = total['grains']
                    else:
                        cache['grains'][minion] = {}
        return cache


Instruction:
Can you create a Python 3 function that
compiles the top file and return the lowstate for the thorium runtime to iterate over

Response:
def get_chunks(self, exclude=None, whitelist=None):
        '''
        Compile the top file and return the lowstate for the thorium runtime
        to iterate over
        '''
        ret = {}
        err = []
        try:
            top = self.get_top()
        except SaltRenderError as err:
            return ret
        except Exception:
            trb = traceback.format_exc()
            err.append(trb)
            return err
        err += self.verify_tops(top)
        matches = self.top_matches(top)
        if not matches:
            msg = 'No Top file found!'
            raise SaltRenderError(msg)
        matches = self.matches_whitelist(matches, whitelist)
        high, errors = self.render_highstate(matches)
        if exclude:
            if isinstance(exclude, six.string_types):
                exclude = exclude.split(',')
            if '__exclude__' in high:
                high['__exclude__'].extend(exclude)
            else:
                high['__exclude__'] = exclude
            err += errors
        high, ext_errors = self.state.reconcile_extend(high)
        err += ext_errors
        err += self.state.verify_high(high)
        if err:
            raise SaltRenderError(err)
        return self.state.compile_high_data(high)


Instruction:
Implement a function in Python 3 to
iterate over the available events and return a list of events

Response:
def get_events(self):
        '''
        iterate over the available events and return a list of events
        '''
        ret = []
        while True:
            event = self.event.get_event(wait=1, full=True)
            if event is None:
                return ret
            ret.append(event)


Instruction:
Given the following Python 3 function, write the documentation
def cache_file(source):
    '''
    Wrapper for cp.cache_file which raises an error if the file was unable to
    be cached.

    CLI Example:

    .. code-block:: bash

        salt myminion container_resource.cache_file salt://foo/bar/baz.txt
    '''
    try:
        # Don't just use cp.cache_file for this. Docker has its own code to
        # pull down images from the web.
        if source.startswith('salt://'):
            cached_source = __salt__['cp.cache_file'](source)
            if not cached_source:
                raise CommandExecutionError(
                    'Unable to cache {0}'.format(source)
                )
            return cached_source
    except AttributeError:
        raise SaltInvocationError('Invalid source file {0}'.format(source))
    return source

Response: Wrapper for cp. cache_file which raises an error if the file was unable to be cached.


Instruction:
Here you have a function in Python 3, explain what it does
def run(name,
        cmd,
        container_type=None,
        exec_driver=None,
        output=None,
        no_start=False,
        stdin=None,
        python_shell=True,
        output_loglevel='debug',
        ignore_retcode=False,
        path=None,
        use_vt=False,
        keep_env=None):
    '''
    Common logic for running shell commands in containers

    path
        path to the container parent (for LXC only)
        default: /var/lib/lxc (system default)

    CLI Example:

    .. code-block:: bash

        salt myminion container_resource.run mycontainer 'ps aux' container_type=docker exec_driver=nsenter output=stdout
    '''
    valid_output = ('stdout', 'stderr', 'retcode', 'all')
    if output is None:
        cmd_func = 'cmd.run'
    elif output not in valid_output:
        raise SaltInvocationError(
            '\'output\' param must be one of the following: {0}'
            .format(', '.join(valid_output))
        )
    else:
        cmd_func = 'cmd.run_all'

    if keep_env is None or isinstance(keep_env, bool):
        to_keep = []
    elif not isinstance(keep_env, (list, tuple)):
        try:
            to_keep = keep_env.split(',')
        except AttributeError:
            log.warning('Invalid keep_env value, ignoring')
            to_keep = []
    else:
        to_keep = keep_env

    if exec_driver == 'lxc-attach':
        full_cmd = 'lxc-attach '
        if path:
            full_cmd += '-P {0} '.format(pipes.quote(path))
        if keep_env is not True:
            full_cmd += '--clear-env '
            if 'PATH' not in to_keep:
                full_cmd += '--set-var {0} '.format(PATH)
                # --clear-env results in a very restrictive PATH
                # (/bin:/usr/bin), use a good fallback.
        full_cmd += ' '.join(
            ['--set-var {0}={1}'.format(x, pipes.quote(os.environ[x]))
                for x in to_keep
                if x in os.environ]
        )
        full_cmd += ' -n {0} -- {1}'.format(pipes.quote(name), cmd)
    elif exec_driver == 'nsenter':
        pid = __salt__['{0}.pid'.format(container_type)](name)
        full_cmd = (
            'nsenter --target {0} --mount --uts --ipc --net --pid -- '
            .format(pid)
        )
        if keep_env is not True:
            full_cmd += 'env -i '
            if 'PATH' not in to_keep:
                full_cmd += '{0} '.format(PATH)
        full_cmd += ' '.join(
            ['{0}={1}'.format(x, pipes.quote(os.environ[x]))
                for x in to_keep
                if x in os.environ]
        )
        full_cmd += ' {0}'.format(cmd)
    elif exec_driver == 'docker-exec':
        # We're using docker exec on the CLI as opposed to via docker-py, since
        # the Docker API doesn't return stdout and stderr separately.
        full_cmd = 'docker exec '
        if stdin:
            full_cmd += '-i '
        full_cmd += '{0} '.format(name)
        if keep_env is not True:
            full_cmd += 'env -i '
            if 'PATH' not in to_keep:
                full_cmd += '{0} '.format(PATH)
        full_cmd += ' '.join(
            ['{0}={1}'.format(x, pipes.quote(os.environ[x]))
                for x in to_keep
                if x in os.environ]
        )
        full_cmd += ' {0}'.format(cmd)

    if not use_vt:
        ret = __salt__[cmd_func](full_cmd,
                                 stdin=stdin,
                                 python_shell=python_shell,
                                 output_loglevel=output_loglevel,
                                 ignore_retcode=ignore_retcode)
    else:
        stdout, stderr = '', ''
        proc = salt.utils.vt.Terminal(
            full_cmd,
            shell=python_shell,
            log_stdin_level='quiet' if output_loglevel == 'quiet' else 'info',
            log_stdout_level=output_loglevel,
            log_stderr_level=output_loglevel,
            log_stdout=True,
            log_stderr=True,
            stream_stdout=False,
            stream_stderr=False
        )
        # Consume output
        try:
            while proc.has_unread_data:
                try:
                    cstdout, cstderr = proc.recv()
                    if cstdout:
                        stdout += cstdout
                    if cstderr:
                        if output is None:
                            stdout += cstderr
                        else:
                            stderr += cstderr
                    time.sleep(0.5)
                except KeyboardInterrupt:
                    break
            ret = stdout if output is None \
                else {'retcode': proc.exitstatus,
                      'pid': 2,
                      'stdout': stdout,
                      'stderr': stderr}
        except salt.utils.vt.TerminalException:
            trace = traceback.format_exc()
            log.error(trace)
            ret = stdout if output is None \
                else {'retcode': 127,
                      'pid': 2,
                      'stdout': stdout,
                      'stderr': stderr}
        finally:
            proc.terminate()

    return ret

Response: Run a command in a container