apt module: part rewrite of the upgrade logic, consistently checking
the executable paths, adding 'safe' as an alias to yes, and adding 'full' to allow aptitude full-upgrade as an alternative to apt-get dist-upgrade
This commit is contained in:
parent
290780d13f
commit
4a729ebf3a
1 changed files with 36 additions and 25 deletions
61
library/apt
61
library/apt
|
@ -69,11 +69,14 @@ options:
|
||||||
choices: [ "yes", "no" ]
|
choices: [ "yes", "no" ]
|
||||||
upgrade:
|
upgrade:
|
||||||
description:
|
description:
|
||||||
- 'If yes, performs an aptitude safe-upgrade. If dist, performs an apt-get dist-upgrade. Note: This does not upgrade a specific package, use state=latest for that.'
|
- 'If yes or safe, performs an aptitude safe-upgrade.'
|
||||||
|
- 'If full, performs an aptitude full-upgrade.'
|
||||||
|
- 'If dist, performs an apt-get dist-upgrade.'
|
||||||
|
- 'Note: This does not upgrade a specific package, use state=latest for that.'
|
||||||
version_added: "1.1"
|
version_added: "1.1"
|
||||||
required: false
|
required: false
|
||||||
default: no
|
default: "yes"
|
||||||
choices: [ "yes", "dist"]
|
choices: [ "yes", "safe", "full", "dist"]
|
||||||
author: Matthew Williams
|
author: Matthew Williams
|
||||||
notes: []
|
notes: []
|
||||||
examples:
|
examples:
|
||||||
|
@ -102,8 +105,12 @@ import warnings
|
||||||
warnings.filterwarnings('ignore', "apt API not stable yet", FutureWarning)
|
warnings.filterwarnings('ignore', "apt API not stable yet", FutureWarning)
|
||||||
|
|
||||||
# APT related constants
|
# APT related constants
|
||||||
APT_PATH = "/usr/bin/apt-get"
|
APTITUDE_CMD = "aptitude"
|
||||||
APT = "DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical %s" % APT_PATH
|
APT_GET_CMD = "apt-get"
|
||||||
|
APT_ENVVARS = "DEBIAN_FRONTEND=noninteractive DEBIAN_PRIORITY=critical"
|
||||||
|
DPKG_OPTIONS = '-o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold"'
|
||||||
|
APT_GET_ZERO = "0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded."
|
||||||
|
APTITUDE_ZERO = "0 packages upgraded, 0 newly installed, 0 to remove and 0 not upgraded."
|
||||||
|
|
||||||
def package_split(pkgspec):
|
def package_split(pkgspec):
|
||||||
parts = pkgspec.split('=')
|
parts = pkgspec.split('=')
|
||||||
|
@ -147,7 +154,7 @@ def install(m, pkgspec, cache, upgrade=False, default_release=None, install_reco
|
||||||
else:
|
else:
|
||||||
force_yes = ''
|
force_yes = ''
|
||||||
|
|
||||||
cmd = "%s --option Dpkg::Options::=--force-confold -q -y %s install %s" % (APT, force_yes,packages)
|
cmd = "%s --option Dpkg::Options::=--force-confold -q -y %s install %s" % (APT_GET, force_yes, packages)
|
||||||
if default_release:
|
if default_release:
|
||||||
cmd += " -t '%s'" % (default_release,)
|
cmd += " -t '%s'" % (default_release,)
|
||||||
if not install_recommends:
|
if not install_recommends:
|
||||||
|
@ -178,7 +185,7 @@ def remove(m, pkgspec, cache, purge=False):
|
||||||
purge = ''
|
purge = ''
|
||||||
if purge:
|
if purge:
|
||||||
purge = '--purge'
|
purge = '--purge'
|
||||||
cmd = "%s -q -y %s remove %s" % (APT, purge,packages)
|
cmd = "%s -q -y %s remove %s" % (APT_GET, purge,packages)
|
||||||
|
|
||||||
if m.check_mode:
|
if m.check_mode:
|
||||||
m.exit_json(changed=True)
|
m.exit_json(changed=True)
|
||||||
|
@ -193,22 +200,29 @@ def upgrade(m, mode="yes"):
|
||||||
check_arg = '--simulate'
|
check_arg = '--simulate'
|
||||||
else:
|
else:
|
||||||
check_arg = ''
|
check_arg = ''
|
||||||
upgrade_command = 'upgrade'
|
|
||||||
if mode == "dist":
|
if mode == "dist":
|
||||||
cmd = '%s %s -q -y -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold" dist-upgrade' % (APT, check_arg)
|
# apt-get dist-upgrade
|
||||||
rc, out, err = m.run_command(cmd)
|
apt_cmd = APT_GET_CMD
|
||||||
if rc:
|
upgrade_command = "dist-upgrade"
|
||||||
m.fail_json(msg="'apt-get %s' failed: %s" % (upgrade_command, err))
|
elif mode == "full":
|
||||||
if "0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded." in out :
|
# aptitude full-upgrade
|
||||||
m.exit_json(changed=False)
|
apt_cmd = APTITUDE_CMD
|
||||||
|
upgrade_command = "full-upgrade"
|
||||||
else:
|
else:
|
||||||
cmd = "/usr/bin/aptitude safe-upgrade %s -y" % check_arg
|
# aptitude safe-upgrade # mode=yes # default
|
||||||
rc, out, err = m.run_command(cmd)
|
apt_cmd = APTITUDE_CMD
|
||||||
if rc:
|
upgrade_command = "safe-upgrade"
|
||||||
m.fail_json(msg="'aptitude safe-upgrade' failed: %s" % err)
|
|
||||||
if "No packages will be installed, upgraded, or removed." in out:
|
apt_cmd_path = m.get_bin_path(apt_cmd, required=True)
|
||||||
m.exit_json(changed=False)
|
cmd = '%s %s -y %s %s %s' % (APT_ENVVARS, apt_cmd_path, DPKG_OPTIONS,
|
||||||
m.exit_json(changed=True)
|
check_arg, upgrade_command)
|
||||||
|
rc, out, err = m.run_command(cmd)
|
||||||
|
if rc:
|
||||||
|
m.fail_json(msg="'%s %s' failed: %s" % (apt_cmd, upgrade_command, err))
|
||||||
|
m.fail_json(msg="'aptitude safe-upgrade' failed: %s" % err)
|
||||||
|
if (apt_cmd == APT_GET_CMD and APT_GET_ZERO in out) or (apt_cmd == APTITUDE_CMD and APTITUDE_ZERO in out):
|
||||||
|
m.exit_json(changed=False, msg=out)
|
||||||
|
m.exit_json(changed=True, msg=out)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
|
@ -220,7 +234,7 @@ def main():
|
||||||
default_release = dict(default=None, aliases=['default-release']),
|
default_release = dict(default=None, aliases=['default-release']),
|
||||||
install_recommends = dict(default='yes', aliases=['install-recommends'], type='bool'),
|
install_recommends = dict(default='yes', aliases=['install-recommends'], type='bool'),
|
||||||
force = dict(default='no', type='bool'),
|
force = dict(default='no', type='bool'),
|
||||||
upgrade = dict(choices=['yes', 'dist'])
|
upgrade = dict(choices=['yes', 'safe', 'full', 'dist'])
|
||||||
),
|
),
|
||||||
mutually_exclusive = [['package', 'upgrade']],
|
mutually_exclusive = [['package', 'upgrade']],
|
||||||
required_one_of = [['package', 'upgrade', 'update_cache']],
|
required_one_of = [['package', 'upgrade', 'update_cache']],
|
||||||
|
@ -233,9 +247,6 @@ def main():
|
||||||
except:
|
except:
|
||||||
module.fail_json(msg="Could not import python modules: apt, apt_pkg. Please install python-apt package.")
|
module.fail_json(msg="Could not import python modules: apt, apt_pkg. Please install python-apt package.")
|
||||||
|
|
||||||
if not os.path.exists(APT_PATH):
|
|
||||||
module.fail_json(msg="Cannot find apt-get")
|
|
||||||
|
|
||||||
p = module.params
|
p = module.params
|
||||||
install_recommends = p['install_recommends']
|
install_recommends = p['install_recommends']
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue