Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

openbsd_pkg: Use ansible run_command(). #3603

Merged
merged 1 commit into from
Jul 20, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 16 additions & 21 deletions library/packaging/openbsd_pkg
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.

import re
import shlex

DOCUMENTATION = '''
---
Expand Down Expand Up @@ -53,24 +54,18 @@ EXAMPLES = '''
- openbsd_pkg: name=nmap state=absent
'''

# select whether we dump additional debug info through syslog
syslogging = False

# Function used for executing commands.
def execute_command(cmd, syslogging):
if syslogging:
syslog.openlog('ansible-%s' % os.path.basename(__file__))
syslog.syslog(syslog.LOG_NOTICE, 'Command %s' % '|'.join(cmd))

p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = p.communicate()
rc = p.returncode
return (rc, out, err)
def execute_command(cmd, module):
# Break command line into arguments.
# This makes run_command() use shell=False which we need to not cause shell
# expansion of special characters like '*'.
cmd_args = shlex.split(cmd)
return module.run_command(cmd_args)

# Function used for getting the name of a currently installed package.
def get_current_name(name, specific_version):
def get_current_name(name, specific_version, module):
info_cmd = 'pkg_info'
(rc, stdout, stderr) = execute_command("%s" % (info_cmd), syslogging)
(rc, stdout, stderr) = execute_command("%s" % (info_cmd), module)
if rc != 0:
return (rc, stdout, stderr)

Expand All @@ -86,15 +81,15 @@ def get_current_name(name, specific_version):
return current_name

# Function used to find out if a package is currently installed.
def get_package_state(name, specific_version):
def get_package_state(name, specific_version, module):
info_cmd = 'pkg_info -e'

if specific_version:
syntax = "%s %s"
else:
syntax = "%s %s-*"

rc, stdout, stderr = execute_command(syntax % (info_cmd, name), syslogging)
rc, stdout, stderr = execute_command(syntax % (info_cmd, name), module)

if rc == 0:
return True
Expand All @@ -111,7 +106,7 @@ def package_present(name, installed_state, specific_version, module):
if installed_state is False:

# Attempt to install the package
(rc, stdout, stderr) = execute_command("%s %s" % (install_cmd, name), syslogging)
(rc, stdout, stderr) = execute_command("%s %s" % (install_cmd, name), module)

# The behaviour of pkg_add is a bit different depending on if a
# specific version is supplied or not.
Expand Down Expand Up @@ -166,10 +161,10 @@ def package_latest(name, installed_state, specific_version, module):
if installed_state is True:

# Fetch name of currently installed package
pre_upgrade_name = get_current_name(name, specific_version)
pre_upgrade_name = get_current_name(name, specific_version, module)

# Attempt to upgrade the package
(rc, stdout, stderr) = execute_command("%s %s" % (upgrade_cmd, name), syslogging)
(rc, stdout, stderr) = execute_command("%s %s" % (upgrade_cmd, name), module)

# Look for output looking something like "nmap-6.01->6.25: ok" to see if
# something changed (or would have changed). Use \W to delimit the match
Expand Down Expand Up @@ -212,7 +207,7 @@ def package_absent(name, installed_state, module):
if installed_state is True:

# Attempt to remove the package
rc, stdout, stderr = execute_command("%s %s" % (remove_cmd, name), syslogging)
rc, stdout, stderr = execute_command("%s %s" % (remove_cmd, name), module)

if rc == 0:
if module.check_mode:
Expand Down Expand Up @@ -261,7 +256,7 @@ def main():
specific_version = False

# Get package state
installed_state = get_package_state(name, specific_version)
installed_state = get_package_state(name, specific_version, module)

# Perform requested action
if state in ['installed', 'present']:
Expand Down