Update modules to use run_command in module_common.py

This updates apt, apt_repository, command, cron, easy_install, facter,
fireball, git, group, mount, ohai, pip, service, setup, subversion,
supervisorctl, svr4pkg, user, and yum to take advantage of run_command
in module_common.py.
This commit is contained in:
Stephen Fromm
2013-01-11 22:10:21 -08:00
parent 300531507b
commit 3fb21a5281
19 changed files with 111 additions and 275 deletions

View File

@@ -64,12 +64,6 @@ examples:
import re
import tempfile
def _run(args):
cmd = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = cmd.communicate()
rc = cmd.returncode
return (rc, out, err)
def get_version(dest):
''' samples the version of the git repo '''
os.chdir(dest)
@@ -78,7 +72,7 @@ def get_version(dest):
sha = sha[0].split()[1]
return sha
def clone(repo, dest, remote):
def clone(module, repo, dest, remote):
''' makes a new git repo if it does not already exist '''
dest_dirname = os.path.dirname(dest)
try:
@@ -86,7 +80,8 @@ def clone(repo, dest, remote):
except:
pass
os.chdir(dest_dirname)
return _run("git clone -o %s %s %s" % (remote, repo, dest))
return module.run_command("git clone -o %s %s %s" % (remote, repo, dest),
fail_on_rc_non_zero=True)
def has_local_mods(dest):
os.chdir(dest)
@@ -104,12 +99,12 @@ def reset(module,dest,force):
os.chdir(dest)
if not force and has_local_mods(dest):
module.fail_json(msg="Local modifications exist in repository (force=no).")
return _run("git reset --hard HEAD")
return module.run_command("git reset --hard HEAD", fail_on_rc_non_zero=True)
def get_branches(module, dest):
os.chdir(dest)
branches = []
(rc, out, err) = _run("git branch -a")
(rc, out, err) = module.run_command("git branch -a")
if rc != 0:
module.fail_json(msg="Could not determine branch data - received %s" % out)
for line in out.split('\n'):
@@ -185,11 +180,11 @@ def get_head_branch(module, dest, remote):
def fetch(module, repo, dest, version, remote):
''' updates repo from remote sources '''
os.chdir(dest)
(rc, out1, err1) = _run("git fetch %s" % remote)
(rc, out1, err1) = module.run_command("git fetch %s" % remote)
if rc != 0:
module.fail_json(msg="Failed to download remote objects and refs")
(rc, out2, err2) = _run("git fetch --tags %s" % remote)
(rc, out2, err2) = module.run_command("git fetch --tags %s" % remote)
if rc != 0:
module.fail_json(msg="Failed to download remote objects and refs")
return (rc, out1 + out2, err1 + err2)
@@ -203,7 +198,7 @@ def switch_version(module, dest, remote, version):
if not is_local_branch(module, dest, version):
cmd = "git checkout --track -b %s %s/%s" % (version, remote, version)
else:
(rc, out, err) = _run("git checkout --force %s" % version)
(rc, out, err) = module.run_command("git checkout --force %s" % version)
if rc != 0:
module.fail_json(msg="Failed to checkout branch %s" % version)
cmd = "git reset --hard %s/%s" % (remote, version)
@@ -211,11 +206,11 @@ def switch_version(module, dest, remote, version):
cmd = "git checkout --force %s" % version
else:
branch = get_head_branch(module, dest, remote)
(rc, out, err) = _run("git checkout --force %s" % branch)
(rc, out, err) = module.run_command("git checkout --force %s" % branch)
if rc != 0:
module.fail_json(msg="Failed to checkout branch %s" % branch)
cmd = "git reset --hard %s" % remote
return _run(cmd)
return module.run_command(cmd, fail_on_rc_non_zero=True)
# ===========================================
@@ -245,9 +240,7 @@ def main():
before = None
local_mods = False
if not os.path.exists(gitconfig):
(rc, out, err) = clone(repo, dest, remote)
if rc != 0:
module.fail_json(msg=err)
(rc, out, err) = clone(module, repo, dest, remote)
else:
# else do a pull
local_mods = has_local_mods(dest)
@@ -262,8 +255,6 @@ def main():
# switch to version specified regardless of whether
# we cloned or pulled
(rc, out, err) = switch_version(module, dest, remote, version)
if rc != 0:
module.fail_json(msg=err)
# determine if we changed anything
after = get_version(dest)