Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yum/dnf: Add download_dir param #53171

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/24004-yum-dnf-add-download_dir-param.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- yum/dnf - Add download_dir param (https://github.com/ansible/ansible/issues/24004)
2 changes: 2 additions & 0 deletions lib/ansible/module_utils/yumdnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
disable_plugin=dict(type='list', default=[]),
disablerepo=dict(type='list', default=[]),
download_only=dict(type='bool', default=False),
download_dir=dict(type='str', default=None),
enable_plugin=dict(type='list', default=[]),
enablerepo=dict(type='list', default=[]),
exclude=dict(type='list', default=[]),
Expand Down Expand Up @@ -73,6 +74,7 @@ def __init__(self, module):
self.disable_plugin = self.module.params['disable_plugin']
self.disablerepo = self.module.params.get('disablerepo', [])
self.download_only = self.module.params['download_only']
self.download_dir = self.module.params['download_dir']
self.enable_plugin = self.module.params['enable_plugin']
self.enablerepo = self.module.params.get('enablerepo', [])
self.exclude = self.module.params['exclude']
Expand Down
20 changes: 20 additions & 0 deletions lib/ansible/modules/packaging/os/dnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@
type: bool
default: "yes"
version_added: "2.8"
download_dir:
description:
- Specifies an alternate directory to store packages.
- Has an effect only if I(download_only) is specified.
type: str
version_added: "2.8"
notes:
- When used with a `loop:` each package will be processed individually, it is much more efficient to pass the list directly to the `name` option.
- Group removal doesn't work if the group was installed with Ansible because
Expand Down Expand Up @@ -564,6 +570,8 @@ def _configure_base(self, base, conf_file, disable_gpg_check, installroot='/'):

if self.download_only:
conf.downloadonly = True
if self.download_dir:
conf.destdir = self.download_dir

# Default in dnf upstream is true
conf.clean_requirements_on_remove = self.autoremove
Expand Down Expand Up @@ -1122,6 +1130,10 @@ def ensure(self):
self.module.exit_json(**response)

try:
if self.download_only and self.download_dir and self.base.conf.destdir:
dnf.util.ensure_dir(self.base.conf.destdir)
self.base.repos.all().pkgdir = self.base.conf.destdir

self.base.download_packages(self.base.transaction.install_set)
except dnf.exceptions.DownloadError as e:
self.module.fail_json(
Expand Down Expand Up @@ -1171,6 +1183,14 @@ def run(self):
results=[],
)

# Check if download_dir is called correctly
if self.download_dir:
if LooseVersion(dnf.__version__) < LooseVersion('2.6.2'):
self.module.fail_json(
msg="download_dir requires dnf>=2.6.2. Current dnf version is %s" % dnf.__version__,
results=[],
)

if self.update_cache and not self.names and not self.list:
self.base = self._base(
self.conf_file, self.disable_gpg_check, self.disablerepo,
Expand Down
9 changes: 9 additions & 0 deletions lib/ansible/modules/packaging/os/yum.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@
type: bool
default: "yes"
version_added: "2.8"
download_dir:
description:
- Specifies an alternate directory to store packages.
- Has an effect only if I(download_only) is specified.
type: str
version_added: "2.8"
notes:
- When used with a `loop:` each package will be processed individually,
it is much more efficient to pass the list directly to the `name` option.
Expand Down Expand Up @@ -1385,6 +1391,9 @@ def ensure(self, repoq):
if self.download_only:
self.yum_basecmd.extend(['--downloadonly'])

if self.download_dir:
self.yum_basecmd.extend(['--downloaddir=%s' % self.download_dir])

if self.installroot != '/':
# do not setup installroot by default, because of error
# CRITICAL:yum.cli:Config Error: Error accessing file for config file:////etc/yum.conf
Expand Down
30 changes: 28 additions & 2 deletions test/integration/targets/dnf/tasks/dnf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,12 @@
dnf: name=sos installroot='/'
register: dnf_result

# Test download_only
- name: uninstall sos for downloadonly test
dnf:
name: sos
state: absent

- name: install sos
- name: Test download_only
dnf:
name: sos
state: latest
Expand All @@ -264,6 +263,33 @@
- "dnf_result is success"
- "not dnf_result is changed"

- name: uninstall sos for downloadonly/downloaddir test
dnf:
name: sos
state: absent

- name: Test download_only/download_dir
dnf:
name: sos
state: latest
download_only: true
download_dir: "/var/tmp/packages"
register: dnf_result

- name: verify dnf output
assert:
that:
- "dnf_result is success"
- "dnf_result is changed"

- command: "ls /var/tmp/packages"
register: ls_out

- name: Verify specified download_dir was used
assert:
that:
- "'sos' in ls_out.stdout"

# GROUP INSTALL
- name: install Custom Group group
dnf:
Expand Down
30 changes: 28 additions & 2 deletions test/integration/targets/yum/tasks/yum.yml
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@
state: removed
register: yum_result

# Test download_only
- name: install sos
- name: Test download_only
yum:
name: sos
state: latest
Expand All @@ -329,6 +328,33 @@
- "yum_result is success"
- "not yum_result is changed"

- name: uninstall sos for downloadonly/downloaddir test
yum:
name: sos
state: absent

- name: Test download_only/download_dir
yum:
name: sos
state: latest
download_only: true
download_dir: "/var/tmp/packages"
register: yum_result

- name: verify yum output
assert:
that:
- "yum_result is success"
- "yum_result is changed"

- command: "ls /var/tmp/packages"
register: ls_out

- name: Verify specified download_dir was used
assert:
that:
- "'sos' in ls_out.stdout"

- name: install group
yum:
name: "@Development Tools"
Expand Down