From c874089d1dce61ecaf91d499f9519e16c3d404f2 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Fri, 27 Mar 2020 15:41:27 +0530 Subject: [PATCH] homebrew: Add upgrade_options in upgrade_all (#24) Handle upgrade options in upgrade_all state in homebrew module. Fixes: #54541 Signed-off-by: Abhijeet Kasurde --- plugins/modules/packaging/os/homebrew.py | 35 ++++++-- tests/integration/targets/homebrew/aliases | 6 ++ .../targets/homebrew/tasks/main.yml | 88 +++++++++++++++++++ 3 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 tests/integration/targets/homebrew/aliases create mode 100644 tests/integration/targets/homebrew/tasks/main.yml diff --git a/plugins/modules/packaging/os/homebrew.py b/plugins/modules/packaging/os/homebrew.py index 174a7a7547..887903be8b 100644 --- a/plugins/modules/packaging/os/homebrew.py +++ b/plugins/modules/packaging/os/homebrew.py @@ -69,6 +69,11 @@ options: aliases: ['options'] type: list elements: str + upgrade_options: + description: + - Option flags to upgrade. + type: list + elements: str 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. @@ -123,6 +128,11 @@ EXAMPLES = ''' name: foo state: present install_options: with-baz,enable-debug + +- name: Use ignored-pinned option while upgrading all + homebrew: + upgrade_all: yes + upgrade_options: ignored-pinned ''' import os.path @@ -359,14 +369,17 @@ class Homebrew(object): def __init__(self, module, path, packages=None, state=None, update_homebrew=False, upgrade_all=False, - install_options=None): + install_options=None, upgrade_options=None): if not install_options: install_options = list() + if not upgrade_options: + upgrade_options = list() self._setup_status_vars() self._setup_instance_vars(module=module, path=path, packages=packages, state=state, update_homebrew=update_homebrew, upgrade_all=upgrade_all, - install_options=install_options, ) + install_options=install_options, + upgrade_options=upgrade_options,) self._prep() @@ -536,10 +549,9 @@ class Homebrew(object): self.changed = True self.message = 'Homebrew packages would be upgraded.' raise HomebrewException(self.message) - rc, out, err = self.module.run_command([ - self.brew_path, - 'upgrade', - ]) + cmd = [self.brew_path, 'upgrade'] + self.upgrade_options + + rc, out, err = self.module.run_command(cmd) if rc == 0: if not out: self.message = 'Homebrew packages already upgraded.' @@ -857,6 +869,11 @@ def main(): aliases=['options'], type='list', elements='str', + ), + upgrade_options=dict( + default=None, + type='list', + elements='str', ) ), supports_check_mode=True, @@ -895,9 +912,13 @@ def main(): install_options = ['--{0}'.format(install_option) for install_option in p['install_options']] + p['upgrade_options'] = p['upgrade_options'] or [] + upgrade_options = ['--{0}'.format(upgrade_option) + for upgrade_option in p['upgrade_options']] brew = Homebrew(module=module, path=path, packages=packages, state=state, update_homebrew=update_homebrew, - upgrade_all=upgrade_all, install_options=install_options) + upgrade_all=upgrade_all, install_options=install_options, + upgrade_options=upgrade_options) (failed, changed, message) = brew.run() if failed: module.fail_json(msg=message) diff --git a/tests/integration/targets/homebrew/aliases b/tests/integration/targets/homebrew/aliases new file mode 100644 index 0000000000..7361196090 --- /dev/null +++ b/tests/integration/targets/homebrew/aliases @@ -0,0 +1,6 @@ +shippable/posix/group1 +skip/aix +skip/freebsd +skip/rhel +skip/docker +skip/python2.6 diff --git a/tests/integration/targets/homebrew/tasks/main.yml b/tests/integration/targets/homebrew/tasks/main.yml new file mode 100644 index 0000000000..0db5096138 --- /dev/null +++ b/tests/integration/targets/homebrew/tasks/main.yml @@ -0,0 +1,88 @@ +# Test code for the homebrew module. +# Copyright: (c) 2020, Abhijeet Kasurde +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +--- +- name: Find brew binary + command: which brew + register: brew_which + when: ansible_distribution in ['MacOSX'] + +- name: Get owner of brew binary + stat: + path: "{{ brew_which.stdout }}" + register: brew_stat + when: ansible_distribution in ['MacOSX'] + +#- name: Use ignored-pinned option while upgrading all +# homebrew: +# upgrade_all: yes +# upgrade_options: ignore-pinned +# become: yes +# become_user: "{{ brew_stat.stat.pw_name }}" +# register: upgrade_option_result +# environment: +# HOMEBREW_NO_AUTO_UPDATE: True + +#- assert: +# that: +# - upgrade_option_result.changed + +- name: Install xz package using homebrew + homebrew: + name: xz + state: present + update_homebrew: no + become: yes + become_user: "{{ brew_stat.stat.pw_name }}" + register: xz_result + environment: + HOMEBREW_NO_AUTO_UPDATE: True + +- assert: + that: + - xz_result.changed + +- name: Again install xz package using homebrew + homebrew: + name: xz + state: present + update_homebrew: no + become: yes + become_user: "{{ brew_stat.stat.pw_name }}" + register: xz_result + environment: + HOMEBREW_NO_AUTO_UPDATE: True + +- assert: + that: + - not xz_result.changed + +- name: Uninstall xz package using homebrew + homebrew: + name: xz + state: absent + update_homebrew: no + become: yes + become_user: "{{ brew_stat.stat.pw_name }}" + register: xz_result + environment: + HOMEBREW_NO_AUTO_UPDATE: True + +- assert: + that: + - xz_result.changed + +- name: Again uninstall xz package using homebrew + homebrew: + name: xz + state: absent + update_homebrew: no + become: yes + become_user: "{{ brew_stat.stat.pw_name }}" + register: xz_result + environment: + HOMEBREW_NO_AUTO_UPDATE: True + +- assert: + that: + - not xz_result.changed