homebrew_cask - add sudo_password parameter(#46471)

* Fixes #29403: homebrew_cask: Add SUDO_ASKPASS to module.run_command

* homebrew_cask: refactor sudo_password code

* homebrew_cask: more refactoring

* homebrew_cask: use %s instead of string addition
pull/51136/head
Daniel Jaouen 6 years ago committed by Sam Doran
parent 94ee3712ae
commit c9bc47b354

@ -43,6 +43,11 @@ options:
- state of the cask - state of the cask
choices: [ 'present', 'absent', 'upgraded' ] choices: [ 'present', 'absent', 'upgraded' ]
default: present default: present
sudo_password:
description:
- The sudo password to be passed to SUDO_ASKPASS.
required: false
version_added: 2.8
update_homebrew: update_homebrew:
description: description:
- update homebrew itself first. Note that C(brew cask update) is - update homebrew itself first. Note that C(brew cask update) is
@ -125,11 +130,18 @@ EXAMPLES = '''
state: upgraded state: upgraded
greedy: True greedy: True
- homebrew_cask:
name: wireshark
state: present
sudo_password: "{{ ansible_become_pass }}"
''' '''
import os.path import os
import re import re
import tempfile
from ansible.module_utils._text import to_bytes
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import iteritems, string_types from ansible.module_utils.six import iteritems, string_types
@ -347,14 +359,15 @@ class HomebrewCask(object):
# /class properties -------------------------------------------- }}} # /class properties -------------------------------------------- }}}
def __init__(self, module, path=path, casks=None, state=None, def __init__(self, module, path=path, casks=None, state=None,
update_homebrew=False, install_options=None, sudo_password=None, update_homebrew=False,
accept_external_apps=False, upgrade_all=False, install_options=None, accept_external_apps=False,
greedy=False): upgrade_all=False, greedy=False):
if not install_options: if not install_options:
install_options = list() install_options = list()
self._setup_status_vars() self._setup_status_vars()
self._setup_instance_vars(module=module, path=path, casks=casks, self._setup_instance_vars(module=module, path=path, casks=casks,
state=state, update_homebrew=update_homebrew, state=state, sudo_password=sudo_password,
update_homebrew=update_homebrew,
install_options=install_options, install_options=install_options,
accept_external_apps=accept_external_apps, accept_external_apps=accept_external_apps,
upgrade_all=upgrade_all, upgrade_all=upgrade_all,
@ -472,6 +485,25 @@ class HomebrewCask(object):
self.message = "You must select a cask to install." self.message = "You must select a cask to install."
raise HomebrewCaskException(self.message) raise HomebrewCaskException(self.message)
# sudo_password fix ---------------------- {{{
def _run_command_with_sudo_password(self, cmd):
rc, out, err = '', '', ''
with tempfile.NamedTemporaryFile() as sudo_askpass_file:
sudo_askpass_file.write(b"#!/bin/sh\n\necho '%s'\n" % to_bytes(self.sudo_password))
os.chmod(sudo_askpass_file.name, 0o700)
sudo_askpass_file.file.close()
rc, out, err = self.module.run_command(
cmd,
environ_update={'SUDO_ASKPASS': sudo_askpass_file.name}
)
self.module.add_cleanup_file(sudo_askpass_file.name)
return (rc, out, err)
# /sudo_password fix --------------------- }}}
# updated -------------------------------- {{{ # updated -------------------------------- {{{
def _update_homebrew(self): def _update_homebrew(self):
rc, out, err = self.module.run_command([ rc, out, err = self.module.run_command([
@ -505,11 +537,19 @@ class HomebrewCask(object):
self.message = 'Casks would be upgraded.' self.message = 'Casks would be upgraded.'
raise HomebrewCaskException(self.message) raise HomebrewCaskException(self.message)
rc, out, err = self.module.run_command([ opts = (
self.brew_path, [self.brew_path, 'cask', 'upgrade']
'cask', )
'upgrade',
]) cmd = [opt for opt in opts if opt]
rc, out, err = '', '', ''
if self.sudo_password:
rc, out, err = self._run_command_with_sudo_password(cmd)
else:
rc, out, err = self.module.run_command(cmd)
if rc == 0: if rc == 0:
if re.search(r'==> No Casks to upgrade', out.strip(), re.IGNORECASE): if re.search(r'==> No Casks to upgrade', out.strip(), re.IGNORECASE):
self.message = 'Homebrew casks already upgraded.' self.message = 'Homebrew casks already upgraded.'
@ -552,6 +592,12 @@ class HomebrewCask(object):
) )
cmd = [opt for opt in opts if opt] cmd = [opt for opt in opts if opt]
rc, out, err = '', '', ''
if self.sudo_password:
rc, out, err = self._run_command_with_sudo_password(cmd)
else:
rc, out, err = self.module.run_command(cmd) rc, out, err = self.module.run_command(cmd)
if self._current_cask_is_installed(): if self._current_cask_is_installed():
@ -610,6 +656,12 @@ class HomebrewCask(object):
+ [self.current_cask] + [self.current_cask]
) )
cmd = [opt for opt in opts if opt] cmd = [opt for opt in opts if opt]
rc, out, err = '', '', ''
if self.sudo_password:
rc, out, err = self._run_command_with_sudo_password(cmd)
else:
rc, out, err = self.module.run_command(cmd) rc, out, err = self.module.run_command(cmd)
if self._current_cask_is_installed() and not self._current_cask_is_outdated(): if self._current_cask_is_installed() and not self._current_cask_is_outdated():
@ -657,6 +709,12 @@ class HomebrewCask(object):
) )
cmd = [opt for opt in opts if opt] cmd = [opt for opt in opts if opt]
rc, out, err = '', '', ''
if self.sudo_password:
rc, out, err = self._run_command_with_sudo_password(cmd)
else:
rc, out, err = self.module.run_command(cmd) rc, out, err = self.module.run_command(cmd)
if not self._current_cask_is_installed(): if not self._current_cask_is_installed():
@ -700,6 +758,11 @@ def main():
"absent", "removed", "uninstalled", "absent", "removed", "uninstalled",
], ],
), ),
sudo_password=dict(
type="str",
required=False,
no_log=True,
),
update_homebrew=dict( update_homebrew=dict(
default=False, default=False,
aliases=["update-brew"], aliases=["update-brew"],
@ -748,6 +811,8 @@ def main():
if state in ('absent', 'removed', 'uninstalled'): if state in ('absent', 'removed', 'uninstalled'):
state = 'absent' state = 'absent'
sudo_password = p['sudo_password']
update_homebrew = p['update_homebrew'] update_homebrew = p['update_homebrew']
upgrade_all = p['upgrade_all'] upgrade_all = p['upgrade_all']
greedy = p['greedy'] greedy = p['greedy']
@ -758,7 +823,8 @@ def main():
accept_external_apps = p['accept_external_apps'] accept_external_apps = p['accept_external_apps']
brew_cask = HomebrewCask(module=module, path=path, casks=casks, brew_cask = HomebrewCask(module=module, path=path, casks=casks,
state=state, update_homebrew=update_homebrew, state=state, sudo_password=sudo_password,
update_homebrew=update_homebrew,
install_options=install_options, install_options=install_options,
accept_external_apps=accept_external_apps, accept_external_apps=accept_external_apps,
upgrade_all=upgrade_all, upgrade_all=upgrade_all,

Loading…
Cancel
Save