def mod_repo(repo, basedir=None, **kwargs):
    '''
    Modify one or more values for a repo. If the repo does not exist, it will
    be created, so long as the following values are specified:

    repo
        name by which the yum refers to the repo
    name
        a human-readable name for the repo
    baseurl
        the URL for yum to reference
    mirrorlist
        the URL for yum to reference
    key_url
        the URL to gather the repo key from (salt:// or any other scheme supported by cp.cache_file)

    Key/Value pairs may also be removed from a repo's configuration by setting
    a key to a blank value. Bear in mind that a name cannot be deleted, and a
    baseurl can only be deleted if a mirrorlist is specified (or vice versa).

    CLI Examples:

    .. code-block:: bash

        salt '*' pkg.mod_repo reponame enabled=1 gpgcheck=1
        salt '*' pkg.mod_repo reponame basedir=/path/to/dir enabled=1
        salt '*' pkg.mod_repo reponame baseurl= mirrorlist=http://host.com/
    '''
    # Filter out '__pub' arguments, as well as saltenv
    repo_opts = dict(
        (x, kwargs[x]) for x in kwargs
        if not x.startswith('__') and x not in ('saltenv',)
    )

    if all(x in repo_opts for x in ('mirrorlist', 'baseurl')):
        raise SaltInvocationError(
            'Only one of \'mirrorlist\' and \'baseurl\' can be specified'
        )

    # Build a list of keys to be deleted
    todelete = []
    # list() of keys because the dict could be shrinking in the for loop.
    for key in list(repo_opts):
        if repo_opts[key] != 0 and not repo_opts[key]:
            del repo_opts[key]
            todelete.append(key)

    # Add baseurl or mirrorlist to the 'todelete' list if the other was
    # specified in the repo_opts
    if 'mirrorlist' in repo_opts:
        todelete.append('baseurl')
    elif 'baseurl' in repo_opts:
        todelete.append('mirrorlist')

    # Fail if the user tried to delete the name
    if 'name' in todelete:
        raise SaltInvocationError('The repo name cannot be deleted')

    # Give the user the ability to change the basedir
    repos = {}
    basedirs = _normalize_basedir(basedir)
    repos = list_repos(basedirs)

    repofile = ''
    header = ''
    filerepos = {}
    if repo not in repos:
        # If the repo doesn't exist, create it in a new file in the first
        # repo directory that exists
        newdir = None
        for d in basedirs:
            if os.path.exists(d):
                newdir = d
                break
        if not newdir:
            raise SaltInvocationError(
                'The repo does not exist and needs to be created, but none '
                'of the following basedir directories exist: {0}'.format(basedirs)
            )

        repofile = '{0}/{1}.repo'.format(newdir, repo)

        if 'name' not in repo_opts:
            raise SaltInvocationError(
                'The repo does not exist and needs to be created, but a name '
                'was not given'
            )

        if 'baseurl' not in repo_opts and 'mirrorlist' not in repo_opts:
            raise SaltInvocationError(
                'The repo does not exist and needs to be created, but either '
                'a baseurl or a mirrorlist needs to be given'
            )
        filerepos[repo] = {}
    else:
        # The repo does exist, open its file
        repofile = repos[repo]['file']
        header, filerepos = _parse_repo_file(repofile)

    # Error out if they tried to delete baseurl or mirrorlist improperly
    if 'baseurl' in todelete:
        if 'mirrorlist' not in repo_opts and 'mirrorlist' \
                not in filerepos[repo]:
            raise SaltInvocationError(
                'Cannot delete baseurl without specifying mirrorlist'
            )
    if 'mirrorlist' in todelete:
        if 'baseurl' not in repo_opts and 'baseurl' \
                not in filerepos[repo]:
            raise SaltInvocationError(
                'Cannot delete mirrorlist without specifying baseurl'
            )

    # Import repository gpg key
    if 'key_url' in repo_opts:
        key_url = kwargs['key_url']
        fn_ = __salt__['cp.cache_file'](key_url, saltenv=(kwargs['saltenv'] if 'saltenv' in kwargs else 'base'))
        if not fn_:
            raise CommandExecutionError(
                'Error: Unable to copy key from URL {0} for repository {1}'.format(key_url, repo_opts['name'])
            )
        cmd = ['rpm', '--import', fn_]
        out = __salt__['cmd.retcode'](cmd, python_shell=False, **kwargs)
        if out != salt.defaults.exitcodes.EX_OK:
            raise CommandExecutionError(
                'Error: Unable to import key from URL {0} for repository {1}'.format(key_url, repo_opts['name'])
            )
        del repo_opts['key_url']

    # Delete anything in the todelete list
    for key in todelete:
        if key in six.iterkeys(filerepos[repo].copy()):
            del filerepos[repo][key]

    _bool_to_str = lambda x: '1' if x else '0'
    # Old file or new, write out the repos(s)
    filerepos[repo].update(repo_opts)
    content = header
    for stanza in six.iterkeys(filerepos):
        comments = salt.utils.pkg.rpm.combine_comments(
            filerepos[stanza].pop('comments', [])
        )
        content += '[{0}]\n'.format(stanza)
        for line in six.iterkeys(filerepos[stanza]):
            content += '{0}={1}\n'.format(
                line,
                filerepos[stanza][line]
                    if not isinstance(filerepos[stanza][line], bool)
                    else _bool_to_str(filerepos[stanza][line])
            )
        content += comments + '\n'

    with salt.utils.files.fopen(repofile, 'w') as fileout:
        fileout.write(salt.utils.stringutils.to_str(content))

    return {repofile: filerepos}
def mod_repo ( repo , basedir = None , ** kwargs ) : repo_opts = dict ( ( x , kwargs [ x ] ) for x in kwargs if not x . startswith ( '__' ) and x not in ( 'saltenv' , ) ) if all ( x in repo_opts for x in ( 'mirrorlist' , 'baseurl' ) ) : raise SaltInvocationError ( 'Only one of \'mirrorlist\' and \'baseurl\' can be specified' ) todelete = [ ] for key in list ( repo_opts ) : if repo_opts [ key ] != 0 and not repo_opts [ key ] : del repo_opts [ key ] todelete . append ( key ) if 'mirrorlist' in repo_opts : todelete . append ( 'baseurl' ) elif 'baseurl' in repo_opts : todelete . append ( 'mirrorlist' ) if 'name' in todelete : raise SaltInvocationError ( 'The repo name cannot be deleted' ) repos = { } basedirs = _normalize_basedir ( basedir ) repos = list_repos ( basedirs ) repofile = '' header = '' filerepos = { } if repo not in repos : newdir = None for d in basedirs : if os . path . exists ( d ) : newdir = d break if not newdir : raise SaltInvocationError ( 'The repo does not exist and needs to be created, but none ' 'of the following basedir directories exist: {0}' . format ( basedirs ) ) repofile = '{0}/{1}.repo' . format ( newdir , repo ) if 'name' not in repo_opts : raise SaltInvocationError ( 'The repo does not exist and needs to be created, but a name ' 'was not given' ) if 'baseurl' not in repo_opts and 'mirrorlist' not in repo_opts : raise SaltInvocationError ( 'The repo does not exist and needs to be created, but either ' 'a baseurl or a mirrorlist needs to be given' ) filerepos [ repo ] = { } else : repofile = repos [ repo ] [ 'file' ] header , filerepos = _parse_repo_file ( repofile ) if 'baseurl' in todelete : if 'mirrorlist' not in repo_opts and 'mirrorlist' not in filerepos [ repo ] : raise SaltInvocationError ( 'Cannot delete baseurl without specifying mirrorlist' ) if 'mirrorlist' in todelete : if 'baseurl' not in repo_opts and 'baseurl' not in filerepos [ repo ] : raise SaltInvocationError ( 'Cannot delete mirrorlist without specifying baseurl' ) if 'key_url' in repo_opts : key_url = kwargs [ 'key_url' ] fn_ = __salt__ [ 'cp.cache_file' ] ( key_url , saltenv = ( kwargs [ 'saltenv' ] if 'saltenv' in kwargs else 'base' ) ) if not fn_ : raise CommandExecutionError ( 'Error: Unable to copy key from URL {0} for repository {1}' . format ( key_url , repo_opts [ 'name' ] ) ) cmd = [ 'rpm' , '--import' , fn_ ] out = __salt__ [ 'cmd.retcode' ] ( cmd , python_shell = False , ** kwargs ) if out != salt . defaults . exitcodes . EX_OK : raise CommandExecutionError ( 'Error: Unable to import key from URL {0} for repository {1}' . format ( key_url , repo_opts [ 'name' ] ) ) del repo_opts [ 'key_url' ] for key in todelete : if key in six . iterkeys ( filerepos [ repo ] . copy ( ) ) : del filerepos [ repo ] [ key ] _bool_to_str = lambda x : '1' if x else '0' filerepos [ repo ] . update ( repo_opts ) content = header for stanza in six . iterkeys ( filerepos ) : comments = salt . utils . pkg . rpm . combine_comments ( filerepos [ stanza ] . pop ( 'comments' , [ ] ) ) content += '[{0}]\n' . format ( stanza ) for line in six . iterkeys ( filerepos [ stanza ] ) : content += '{0}={1}\n' . format ( line , filerepos [ stanza ] [ line ] if not isinstance ( filerepos [ stanza ] [ line ] , bool ) else _bool_to_str ( filerepos [ stanza ] [ line ] ) ) content += comments + '\n' with salt . utils . files . fopen ( repofile , 'w' ) as fileout : fileout . write ( salt . utils . stringutils . to_str ( content ) ) return { repofile : filerepos }
Modify one or more values for a repo. If the repo does not exist, it will
    be created, so long as the following values are specified:

    repo
        name by which the yum refers to the repo
    name
        a human-readable name for the repo
    baseurl
        the URL for yum to reference
    mirrorlist
        the URL for yum to reference
    key_url
        the URL to gather the repo key from (salt:// or any other scheme supported by cp.cache_file)

    Key/Value pairs may also be removed from a repo's configuration by setting
    a key to a blank value. Bear in mind that a name cannot be deleted, and a
    baseurl can only be deleted if a mirrorlist is specified (or vice versa).

    CLI Examples:

    .. code-block:: bash

        salt '*' pkg.mod_repo reponame enabled=1 gpgcheck=1
        salt '*' pkg.mod_repo reponame basedir=/path/to/dir enabled=1
        salt '*' pkg.mod_repo reponame baseurl= mirrorlist=http://host.com/