Add a user flag to every function in the hg module

This way salt users can run hg as a user != the minion's user
This commit is contained in:
Jeff Schroeder 2012-03-18 12:09:22 -07:00
parent 831a1c58f6
commit 5d6560ea4d

View File

@ -2,7 +2,7 @@
Support for the Mercurial SCM
'''
def revision(cwd, rev='tip', short=False):
def revision(cwd, rev='tip', short=False, user=None):
'''
Returns the long hash of a given identifier (hash, branch, tag, HEAD, etc)
@ -14,14 +14,14 @@ def revision(cwd, rev='tip', short=False):
short = ' --debug' if not short else '',
rev = ' -r {0}'.format(rev))
result = __salt__['cmd.run_all'](cmd, cwd=cwd)
result = __salt__['cmd.run_all'](cmd, cwd=cwd, runas=user)
if result['retcode'] == 0:
return result['stdout'].strip('\n')
return result['stdout']
else:
return ''
def describe(cwd, rev='tip'):
def describe(cwd, rev='tip', user=None):
'''
Mimick git describe and return an identifier for the given revision
@ -31,11 +31,11 @@ def describe(cwd, rev='tip'):
'''
cmd = "hg log -r {0} --template"\
" '{{latesttag}}-{{latesttagdistance}}-{{node|short}}'".format(rev)
desc = __salt__['cmd.run_stdout'](cmd, cwd=cwd)
desc = __salt__['cmd.run_stdout'](cmd, cwd=cwd, runas=user)
return desc or revision(cwd, rev, short=True)
def archive(cwd, output, rev='tip', fmt='', prefix=''):
def archive(cwd, output, rev='tip', fmt='', prefix='', user=None):
'''
Export a tarball from the repository
@ -52,9 +52,9 @@ def archive(cwd, output, rev='tip', fmt='', prefix=''):
fmt = ' --type {0}'.format(fmt) if fmt else '',
prefix = ' --prefix "{0}"'.format(prefix if prefix else ''))
return __salt__['cmd.run'](cmd, cwd=cwd)
return __salt__['cmd.run'](cmd, cwd=cwd, runas=user)
def pull(cwd, opts=''):
def pull(cwd, opts='', user=None):
'''
Perform a pull on the given repository
@ -62,9 +62,9 @@ def pull(cwd, opts=''):
salt '*' hg.pull /path/to/repo '-u'
'''
return __salt__['cmd.run']('hg pull {0}'.format(opts), cwd=cwd)
return __salt__['cmd.run']('hg pull {0}'.format(opts), cwd=cwd, runas=user)
def update(cwd, rev, force=False):
def update(cwd, rev, force=False, user=None):
'''
Checkout a given revision
@ -73,9 +73,9 @@ def update(cwd, rev, force=False):
salt '*' hg.update /path/to/repo somebranch
'''
cmd = 'hg update {0}{1}'.format(rev, ' -C' if force else '')
return __salt__['cmd.run'](cmd, cwd=cwd)
return __salt__['cmd.run'](cmd, cwd=cwd, runas=user)
def clone(cwd, repository, opts=''):
def clone(cwd, repository, opts='', user=None):
'''
Clone a new repository
@ -84,4 +84,4 @@ def clone(cwd, repository, opts=''):
salt '*' hg.clone /path/to/repo https://bitbucket.org/birkenfeld/sphinx
'''
cmd = 'hg clone {0} {1} {2}'.format(repository, cwd, opts)
return __salt__['cmd.run'](cmd)
return __salt__['cmd.run'](cmd, runas=user)