Enable to use greedy checks for outdated casks (#40799)

* Enable to use greedy checks for outdated casks

When using brew cask outdated to check if an installed cask is outdated
or not, brew cask will skip casks that have auto_updates set to true or
version: latest. This means that Ansible tasks using the homebrew_cask
module to upgrade packages installed by brew cask will miss upgrading
such packages. However such packages can still be managed by brew cask
so we need to be able detect such packages. This can be done with the
--greedy flag passed to brew cask outdated as this will also include
such packages that are outdated. This commit adds a greedy parameter to
the homebrew_cask module to enable upgrading such packages using Ansible
tasks with the homebrew_cask module. The default behavior preserves the
same behavior as today. Example usage would be:

- homebrew_cask:
  name: 1password
  state: upgraded
  update_homebrew: yes
  greedy: yes

* Fix test issues

* Add extra comma to match style
This commit is contained in:
Jason Tedor 2018-06-18 17:30:32 -04:00 committed by Brian Coca
parent 16fb74ffb3
commit f71ad4e315

View file

@ -74,6 +74,14 @@ options:
type: bool
default: 'no'
version_added: "2.5.0"
greedy:
description:
- upgrade casks that auto update; passes --greedy to brew cask
outdated when checking if an installed cask has a newer version
available
type: bool
default: 'no'
version_added: "2.7.0"
'''
EXAMPLES = '''
- homebrew_cask:
@ -112,6 +120,11 @@ EXAMPLES = '''
state: upgraded
install_options: force
- homebrew_cask:
name: 1password
state: upgraded
greedy: True
'''
import os.path
@ -335,7 +348,8 @@ class HomebrewCask(object):
def __init__(self, module, path=path, casks=None, state=None,
update_homebrew=False, install_options=None,
accept_external_apps=False, upgrade_all=False):
accept_external_apps=False, upgrade_all=False,
greedy=False):
if not install_options:
install_options = list()
self._setup_status_vars()
@ -343,7 +357,8 @@ class HomebrewCask(object):
state=state, update_homebrew=update_homebrew,
install_options=install_options,
accept_external_apps=accept_external_apps,
upgrade_all=upgrade_all, )
upgrade_all=upgrade_all,
greedy=greedy, )
self._prep()
@ -406,12 +421,17 @@ class HomebrewCask(object):
if not self.valid_cask(self.current_cask):
return False
rc, out, err = self.module.run_command([
self.brew_path,
'cask',
'outdated',
self.current_cask,
])
cask_is_outdated_command = (
[
self.brew_path,
'cask',
'outdated',
]
+ (['--greedy'] if self.greedy else [])
+ [self.current_cask]
)
rc, out, err = self.module.run_command(cask_is_outdated_command)
return out != ""
@ -697,6 +717,10 @@ def main():
aliases=["upgrade"],
type='bool',
),
greedy=dict(
default=False,
type='bool',
),
),
supports_check_mode=True,
)
@ -724,6 +748,7 @@ def main():
update_homebrew = p['update_homebrew']
upgrade_all = p['upgrade_all']
greedy = p['greedy']
p['install_options'] = p['install_options'] or []
install_options = ['--{0}'.format(install_option)
for install_option in p['install_options']]
@ -735,6 +760,7 @@ def main():
install_options=install_options,
accept_external_apps=accept_external_apps,
upgrade_all=upgrade_all,
greedy=greedy,
)
(failed, changed, message) = brew_cask.run()
if failed: