1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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 <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2020-03-27 15:41:27 +05:30 committed by GitHub
parent 4fba692c1b
commit c874089d1d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 122 additions and 7 deletions

View file

@ -69,6 +69,11 @@ options:
aliases: ['options'] aliases: ['options']
type: list type: list
elements: str elements: str
upgrade_options:
description:
- Option flags to upgrade.
type: list
elements: str
notes: notes:
- When used with a `loop:` each package will be processed individually, - 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. it is much more efficient to pass the list directly to the `name` option.
@ -123,6 +128,11 @@ EXAMPLES = '''
name: foo name: foo
state: present state: present
install_options: with-baz,enable-debug 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 import os.path
@ -359,14 +369,17 @@ class Homebrew(object):
def __init__(self, module, path, packages=None, state=None, def __init__(self, module, path, packages=None, state=None,
update_homebrew=False, upgrade_all=False, update_homebrew=False, upgrade_all=False,
install_options=None): install_options=None, upgrade_options=None):
if not install_options: if not install_options:
install_options = list() install_options = list()
if not upgrade_options:
upgrade_options = list()
self._setup_status_vars() self._setup_status_vars()
self._setup_instance_vars(module=module, path=path, packages=packages, self._setup_instance_vars(module=module, path=path, packages=packages,
state=state, update_homebrew=update_homebrew, state=state, update_homebrew=update_homebrew,
upgrade_all=upgrade_all, upgrade_all=upgrade_all,
install_options=install_options, ) install_options=install_options,
upgrade_options=upgrade_options,)
self._prep() self._prep()
@ -536,10 +549,9 @@ class Homebrew(object):
self.changed = True self.changed = True
self.message = 'Homebrew packages would be upgraded.' self.message = 'Homebrew packages would be upgraded.'
raise HomebrewException(self.message) raise HomebrewException(self.message)
rc, out, err = self.module.run_command([ cmd = [self.brew_path, 'upgrade'] + self.upgrade_options
self.brew_path,
'upgrade', rc, out, err = self.module.run_command(cmd)
])
if rc == 0: if rc == 0:
if not out: if not out:
self.message = 'Homebrew packages already upgraded.' self.message = 'Homebrew packages already upgraded.'
@ -857,6 +869,11 @@ def main():
aliases=['options'], aliases=['options'],
type='list', type='list',
elements='str', elements='str',
),
upgrade_options=dict(
default=None,
type='list',
elements='str',
) )
), ),
supports_check_mode=True, supports_check_mode=True,
@ -895,9 +912,13 @@ def main():
install_options = ['--{0}'.format(install_option) install_options = ['--{0}'.format(install_option)
for install_option in p['install_options']] 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, brew = Homebrew(module=module, path=path, packages=packages,
state=state, update_homebrew=update_homebrew, 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() (failed, changed, message) = brew.run()
if failed: if failed:
module.fail_json(msg=message) module.fail_json(msg=message)

View file

@ -0,0 +1,6 @@
shippable/posix/group1
skip/aix
skip/freebsd
skip/rhel
skip/docker
skip/python2.6

View file

@ -0,0 +1,88 @@
# Test code for the homebrew module.
# Copyright: (c) 2020, Abhijeet Kasurde <akasurde@redhat.com>
# 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