From 37fff3dc3851276a71186bf77750eb053c00b90b Mon Sep 17 00:00:00 2001 From: Daniel Jaouen Date: Sun, 20 Apr 2014 16:12:17 -0400 Subject: [PATCH] Add `upgrade_all` option to homebrew module. --- library/packaging/homebrew | 46 ++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/library/packaging/homebrew b/library/packaging/homebrew index 1bf9a677c72..aafb7aaa872 100644 --- a/library/packaging/homebrew +++ b/library/packaging/homebrew @@ -44,6 +44,12 @@ options: required: false default: "no" choices: [ "yes", "no" ] + upgrade_all: + description: + - upgrade all homebrew packages + required: false + default: no + choices: [ "yes", "no" ] install_options: description: - options flags to install a package @@ -56,7 +62,7 @@ EXAMPLES = ''' - homebrew: name=foo state=present - homebrew: name=foo state=present update_homebrew=yes - homebrew: name=foo state=latest update_homebrew=yes -- homebrew: update_homebrew=yes upgrade=yes +- homebrew: update_homebrew=yes upgrade_all=yes - homebrew: name=foo state=head - homebrew: name=foo state=linked - homebrew: name=foo state=absent @@ -289,12 +295,14 @@ class Homebrew(object): # /class properties -------------------------------------------- }}} def __init__(self, module, path=None, packages=None, state=None, - update_homebrew=False, install_options=None): + update_homebrew=False, upgrade_all=False, + install_options=None): if not install_options: install_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, ) self._prep() @@ -418,6 +426,9 @@ class Homebrew(object): if self.update_homebrew: self._update_homebrew() + if self.upgrade_all: + self._upgrade_all() + if self.packages: if self.state == 'installed': return self._install_packages() @@ -458,6 +469,27 @@ class Homebrew(object): raise HomebrewException(self.message) # /updated ------------------------------- }}} + # _upgrade_all --------------------------- {{{ + def _upgrade_all(self): + rc, out, err = self.module.run_command([ + self.brew_path, + 'upgrade', + ]) + if rc == 0: + if not out: + self.message = 'Homebrew packages already upgraded.' + + else: + self.changed = True + self.message = 'Homebrew upgraded.' + + return True + else: + self.failed = True + self.message = err.strip() + raise HomebrewException(self.message) + # /_upgrade_all -------------------------- }}} + # installed ------------------------------ {{{ def _install_current_package(self): if not self.valid_package(self.current_package): @@ -741,6 +773,11 @@ def main(): aliases=["update-brew"], type='bool', ), + upgrade_all=dict( + default="no", + aliases=["upgrade"], + type='bool', + ), install_options=dict( default=None, aliases=['options'], @@ -765,7 +802,7 @@ def main(): state = p['state'] if state in ('present', 'installed'): state = 'installed' - if state in ('head'): + if state in ('head', ): state = 'head' if state in ('latest', 'upgraded'): state = 'upgraded' @@ -777,13 +814,14 @@ def main(): state = 'absent' update_homebrew = p['update_homebrew'] + upgrade_all = p['upgrade_all'] p['install_options'] = p['install_options'] or [] install_options = ['--{0}'.format(install_option) for install_option in p['install_options']] brew = Homebrew(module=module, path=path, packages=packages, state=state, update_homebrew=update_homebrew, - install_options=install_options) + upgrade_all=upgrade_all, install_options=install_options) (failed, changed, message) = brew.run() if failed: module.fail_json(msg=message)