2013-03-02 23:01:02 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# (c) 2013, Patrik Lundin <patrik.lundin.swe@gmail.com>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import re
|
2013-07-20 15:45:07 +00:00
|
|
|
import shlex
|
2013-11-18 19:24:10 +00:00
|
|
|
import syslog
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: openbsd_pkg
|
|
|
|
author: Patrik Lundin
|
2013-03-30 19:44:34 +00:00
|
|
|
version_added: "1.1"
|
2013-03-02 23:01:02 +00:00
|
|
|
short_description: Manage packages on OpenBSD.
|
|
|
|
description:
|
|
|
|
- Manage packages on OpenBSD using the pkg tools.
|
|
|
|
options:
|
|
|
|
name:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Name of the package.
|
|
|
|
state:
|
|
|
|
required: true
|
|
|
|
choices: [ present, latest, absent ]
|
|
|
|
description:
|
2013-03-26 17:22:15 +00:00
|
|
|
- C(present) will make sure the package is installed.
|
|
|
|
C(latest) will make sure the latest version of the package is installed.
|
|
|
|
C(absent) will make sure the specified package is not installed.
|
2013-06-14 09:53:43 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Make sure nmap is installed
|
|
|
|
- openbsd_pkg: name=nmap state=present
|
|
|
|
|
|
|
|
# Make sure nmap is the latest version
|
|
|
|
- openbsd_pkg: name=nmap state=latest
|
|
|
|
|
|
|
|
# Make sure nmap is not installed
|
|
|
|
- openbsd_pkg: name=nmap state=absent
|
2014-06-15 00:31:45 +00:00
|
|
|
|
|
|
|
# Specify a pkg flavour with '--'
|
|
|
|
- openbsd_pkg: name=vim--nox11 state=present
|
|
|
|
|
2014-06-16 16:35:57 +00:00
|
|
|
# Specify the default flavour to avoid ambiguity errors
|
2014-06-15 00:31:45 +00:00
|
|
|
- openbsd_pkg: name=vim-- state=present
|
2013-03-02 23:01:02 +00:00
|
|
|
'''
|
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
# Control if we write debug information to syslog.
|
|
|
|
debug = False
|
|
|
|
|
2013-07-10 19:23:10 +00:00
|
|
|
# Function used for executing commands.
|
2013-07-20 15:45:07 +00:00
|
|
|
def execute_command(cmd, module):
|
2013-11-18 19:24:10 +00:00
|
|
|
if debug:
|
|
|
|
syslog.syslog("execute_command(): cmd = %s" % cmd)
|
2013-07-20 15:45:07 +00:00
|
|
|
# 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)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
# Function used for getting the name of a currently installed package.
|
2013-11-18 19:24:10 +00:00
|
|
|
def get_current_name(name, pkg_spec, module):
|
2013-03-02 23:01:02 +00:00
|
|
|
info_cmd = 'pkg_info'
|
2013-07-20 15:45:07 +00:00
|
|
|
(rc, stdout, stderr) = execute_command("%s" % (info_cmd), module)
|
2013-03-02 23:01:02 +00:00
|
|
|
if rc != 0:
|
|
|
|
return (rc, stdout, stderr)
|
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
if pkg_spec['version']:
|
|
|
|
pattern = "^%s" % name
|
|
|
|
elif pkg_spec['flavor']:
|
|
|
|
pattern = "^%s-.*-%s\s" % (pkg_spec['stem'], pkg_spec['flavor'])
|
2013-03-02 23:01:02 +00:00
|
|
|
else:
|
2013-11-18 19:24:10 +00:00
|
|
|
pattern = "^%s-" % pkg_spec['stem']
|
|
|
|
|
|
|
|
if debug:
|
|
|
|
syslog.syslog("get_current_name(): pattern = %s" % pattern)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
for line in stdout.splitlines():
|
2013-11-18 19:24:10 +00:00
|
|
|
if debug:
|
|
|
|
syslog.syslog("get_current_name: line = %s" % line)
|
|
|
|
match = re.search(pattern, line)
|
|
|
|
if match:
|
2013-04-10 20:37:49 +00:00
|
|
|
current_name = line.split()[0]
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
return current_name
|
|
|
|
|
|
|
|
# Function used to find out if a package is currently installed.
|
2013-11-18 19:24:10 +00:00
|
|
|
def get_package_state(name, pkg_spec, module):
|
2013-03-02 23:01:02 +00:00
|
|
|
info_cmd = 'pkg_info -e'
|
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
if pkg_spec['version']:
|
|
|
|
command = "%s %s" % (info_cmd, name)
|
|
|
|
elif pkg_spec['flavor']:
|
|
|
|
command = "%s %s-*-%s" % (info_cmd, pkg_spec['stem'], pkg_spec['flavor'])
|
2013-03-02 23:01:02 +00:00
|
|
|
else:
|
2013-11-18 19:24:10 +00:00
|
|
|
command = "%s %s-*" % (info_cmd, pkg_spec['stem'])
|
2013-03-02 23:01:02 +00:00
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
rc, stdout, stderr = execute_command(command, module)
|
|
|
|
|
|
|
|
if (stderr):
|
|
|
|
module.fail_json(msg="failed in get_package_state(): " + stderr)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
if rc == 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Function used to make sure a package is present.
|
2013-11-18 19:24:10 +00:00
|
|
|
def package_present(name, installed_state, pkg_spec, module):
|
2013-06-15 14:27:45 +00:00
|
|
|
if module.check_mode:
|
2013-07-10 16:19:01 +00:00
|
|
|
install_cmd = 'pkg_add -Imn'
|
|
|
|
else:
|
|
|
|
install_cmd = 'pkg_add -Im'
|
2013-06-15 14:27:45 +00:00
|
|
|
|
2013-03-02 23:01:02 +00:00
|
|
|
if installed_state is False:
|
2013-06-15 14:27:45 +00:00
|
|
|
|
|
|
|
# Attempt to install the package
|
2013-07-20 15:45:07 +00:00
|
|
|
(rc, stdout, stderr) = execute_command("%s %s" % (install_cmd, name), module)
|
2013-06-15 14:27:45 +00:00
|
|
|
|
2013-07-09 18:56:22 +00:00
|
|
|
# The behaviour of pkg_add is a bit different depending on if a
|
|
|
|
# specific version is supplied or not.
|
2013-07-10 19:23:10 +00:00
|
|
|
#
|
2013-07-09 18:56:22 +00:00
|
|
|
# When a specific version is supplied the return code will be 0 when
|
|
|
|
# a package is found and 1 when it is not, if a version is not
|
|
|
|
# supplied the tool will exit 0 in both cases:
|
2013-11-18 19:24:10 +00:00
|
|
|
if pkg_spec['version']:
|
2013-07-09 18:56:22 +00:00
|
|
|
# Depend on the return code.
|
2013-11-18 19:24:10 +00:00
|
|
|
if debug:
|
|
|
|
syslog.syslog("package_present(): depending on return code")
|
2013-07-10 19:23:10 +00:00
|
|
|
if rc:
|
2013-07-09 18:56:22 +00:00
|
|
|
changed=False
|
2013-03-02 23:01:02 +00:00
|
|
|
else:
|
2013-07-10 16:19:01 +00:00
|
|
|
# Depend on stderr instead.
|
2013-11-18 19:24:10 +00:00
|
|
|
if debug:
|
|
|
|
syslog.syslog("package_present(): depending on stderr")
|
2013-07-09 18:56:22 +00:00
|
|
|
if stderr:
|
2013-07-10 16:19:01 +00:00
|
|
|
# There is a corner case where having an empty directory in
|
|
|
|
# installpath prior to the right location will result in a
|
|
|
|
# "file:/local/package/directory/ is empty" message on stderr
|
|
|
|
# while still installing the package, so we need to look for
|
|
|
|
# for a message like "packagename-1.0: ok" just in case.
|
|
|
|
match = re.search("\W%s-[^:]+: ok\W" % name, stdout)
|
|
|
|
if match:
|
|
|
|
# It turns out we were able to install the package.
|
2013-11-18 19:24:10 +00:00
|
|
|
if debug:
|
|
|
|
syslog.syslog("package_present(): we were able to install package")
|
2013-07-10 16:19:01 +00:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# We really did fail, fake the return code.
|
2013-11-18 19:24:10 +00:00
|
|
|
if debug:
|
|
|
|
syslog.syslog("package_present(): we really did fail")
|
2013-07-10 16:19:01 +00:00
|
|
|
rc = 1
|
|
|
|
changed=False
|
2013-11-18 19:24:10 +00:00
|
|
|
else:
|
|
|
|
if debug:
|
|
|
|
syslog.syslog("package_present(): stderr was not set")
|
2013-07-09 18:56:22 +00:00
|
|
|
|
|
|
|
if rc == 0:
|
2013-06-15 14:27:45 +00:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
2013-03-02 23:01:02 +00:00
|
|
|
changed=True
|
2013-07-09 18:56:22 +00:00
|
|
|
|
2013-03-02 23:01:02 +00:00
|
|
|
else:
|
|
|
|
rc = 0
|
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
changed=False
|
|
|
|
|
|
|
|
return (rc, stdout, stderr, changed)
|
|
|
|
|
|
|
|
# Function used to make sure a package is the latest available version.
|
2013-11-18 19:24:10 +00:00
|
|
|
def package_latest(name, installed_state, pkg_spec, module):
|
2013-06-15 14:27:45 +00:00
|
|
|
if module.check_mode:
|
|
|
|
upgrade_cmd = 'pkg_add -umn'
|
2013-07-10 19:23:10 +00:00
|
|
|
else:
|
2013-06-15 14:27:45 +00:00
|
|
|
upgrade_cmd = 'pkg_add -um'
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
pre_upgrade_name = ''
|
2013-06-15 14:27:45 +00:00
|
|
|
|
2013-03-02 23:01:02 +00:00
|
|
|
if installed_state is True:
|
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
# Fetch name of currently installed package.
|
|
|
|
pre_upgrade_name = get_current_name(name, pkg_spec, module)
|
|
|
|
|
|
|
|
if debug:
|
|
|
|
syslog.syslog("package_latest(): pre_upgrade_name = %s" % pre_upgrade_name)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
# Attempt to upgrade the package.
|
2013-07-20 15:45:07 +00:00
|
|
|
(rc, stdout, stderr) = execute_command("%s %s" % (upgrade_cmd, name), module)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
2013-06-15 14:27:45 +00:00
|
|
|
# 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
|
|
|
|
# from progress meter output.
|
|
|
|
match = re.search("\W%s->.+: ok\W" % pre_upgrade_name, stdout)
|
|
|
|
if match:
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
changed = True
|
2013-06-15 14:27:45 +00:00
|
|
|
else:
|
|
|
|
changed = False
|
|
|
|
|
2013-07-10 19:06:35 +00:00
|
|
|
# FIXME: This part is problematic. Based on the issues mentioned (and
|
|
|
|
# handled) in package_present() it is not safe to blindly trust stderr
|
|
|
|
# as an indicator that the command failed, and in the case with
|
|
|
|
# empty installpath directories this will break.
|
|
|
|
#
|
|
|
|
# For now keep this safeguard here, but ignore it if we managed to
|
|
|
|
# parse out a successful update above. This way we will report a
|
|
|
|
# successful run when we actually modify something but fail
|
|
|
|
# otherwise.
|
|
|
|
if changed != True:
|
|
|
|
if stderr:
|
|
|
|
rc=1
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
return (rc, stdout, stderr, changed)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# If package was not installed at all just make it present.
|
2013-11-18 19:24:10 +00:00
|
|
|
if debug:
|
|
|
|
syslog.syslog("package_latest(): package is not installed, calling package_present()")
|
|
|
|
return package_present(name, installed_state, pkg_spec, module)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
# Function used to make sure a package is not installed.
|
2013-06-15 14:27:45 +00:00
|
|
|
def package_absent(name, installed_state, module):
|
|
|
|
if module.check_mode:
|
|
|
|
remove_cmd = 'pkg_delete -In'
|
|
|
|
else:
|
|
|
|
remove_cmd = 'pkg_delete -I'
|
|
|
|
|
2013-03-02 23:01:02 +00:00
|
|
|
if installed_state is True:
|
2013-06-15 14:27:45 +00:00
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
# Attempt to remove the package.
|
2013-07-20 15:45:07 +00:00
|
|
|
rc, stdout, stderr = execute_command("%s %s" % (remove_cmd, name), module)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
if rc == 0:
|
2013-06-15 14:27:45 +00:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
2013-03-02 23:01:02 +00:00
|
|
|
changed=True
|
|
|
|
else:
|
|
|
|
changed=False
|
2013-06-15 14:27:45 +00:00
|
|
|
|
2013-03-02 23:01:02 +00:00
|
|
|
else:
|
|
|
|
rc = 0
|
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
changed=False
|
|
|
|
|
|
|
|
return (rc, stdout, stderr, changed)
|
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
# Function used to parse the package name based on packages-specs(7)
|
|
|
|
# The general name structure is "stem-version[-flavors]"
|
|
|
|
def parse_package_name(name, pkg_spec, module):
|
|
|
|
# Do some initial matches so we can base the more advanced regex on that.
|
|
|
|
version_match = re.search("-[0-9]", name)
|
|
|
|
versionless_match = re.search("--", name)
|
|
|
|
|
|
|
|
# Stop if someone is giving us a name that both has a version and is
|
|
|
|
# version-less at the same time.
|
|
|
|
if version_match and versionless_match:
|
|
|
|
module.fail_json(msg="Package name both has a version and is version-less: " + name)
|
|
|
|
|
|
|
|
# If name includes a version.
|
|
|
|
if version_match:
|
|
|
|
match = re.search("^(?P<stem>.*)-(?P<version>[0-9][^-]*)(?P<flavor_separator>-)?(?P<flavor>[a-z].*)?$", name)
|
|
|
|
if match:
|
|
|
|
pkg_spec['stem'] = match.group('stem')
|
|
|
|
pkg_spec['version_separator'] = '-'
|
|
|
|
pkg_spec['version'] = match.group('version')
|
|
|
|
pkg_spec['flavor_separator'] = match.group('flavor_separator')
|
|
|
|
pkg_spec['flavor'] = match.group('flavor')
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Unable to parse package name at version_match: " + name)
|
|
|
|
|
|
|
|
# If name includes no version but is version-less ("--").
|
|
|
|
elif versionless_match:
|
|
|
|
match = re.search("^(?P<stem>.*)--(?P<flavor>[a-z].*)?$", name)
|
|
|
|
if match:
|
|
|
|
pkg_spec['stem'] = match.group('stem')
|
|
|
|
pkg_spec['version_separator'] = '-'
|
|
|
|
pkg_spec['version'] = None
|
|
|
|
pkg_spec['flavor_separator'] = '-'
|
|
|
|
pkg_spec['flavor'] = match.group('flavor')
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Unable to parse package name at versionless_match: " + name)
|
|
|
|
|
|
|
|
# If name includes no version, and is not version-less, it is all a stem.
|
|
|
|
else:
|
|
|
|
match = re.search("^(?P<stem>.*)$", name)
|
|
|
|
if match:
|
|
|
|
pkg_spec['stem'] = match.group('stem')
|
|
|
|
pkg_spec['version_separator'] = None
|
|
|
|
pkg_spec['version'] = None
|
|
|
|
pkg_spec['flavor_separator'] = None
|
|
|
|
pkg_spec['flavor'] = None
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="Unable to parse package name at else: " + name)
|
|
|
|
|
|
|
|
# Sanity check that there are no trailing dashes in flavor.
|
|
|
|
# Try to stop strange stuff early so we can be strict later.
|
|
|
|
if pkg_spec['flavor']:
|
|
|
|
match = re.search("-$", pkg_spec['flavor'])
|
|
|
|
if match:
|
|
|
|
module.fail_json(msg="Trailing dash in flavor: " + pkg_spec['flavor'])
|
|
|
|
|
2013-03-02 23:01:02 +00:00
|
|
|
# ===========================================
|
|
|
|
# Main control flow
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
name = dict(required=True),
|
|
|
|
state = dict(required=True, choices=['absent', 'installed', 'latest', 'present', 'removed']),
|
2013-06-15 14:27:45 +00:00
|
|
|
),
|
|
|
|
supports_check_mode = True
|
2013-03-02 23:01:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
name = module.params['name']
|
|
|
|
state = module.params['state']
|
|
|
|
|
|
|
|
rc = 0
|
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
result = {}
|
|
|
|
result['name'] = name
|
|
|
|
result['state'] = state
|
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
# Parse package name and put results in the pkg_spec dictionary.
|
|
|
|
pkg_spec = {}
|
|
|
|
parse_package_name(name, pkg_spec, module)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
# Get package state.
|
|
|
|
installed_state = get_package_state(name, pkg_spec, module)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
2013-11-18 19:24:10 +00:00
|
|
|
# Perform requested action.
|
2013-03-02 23:01:02 +00:00
|
|
|
if state in ['installed', 'present']:
|
2013-11-18 19:24:10 +00:00
|
|
|
(rc, stdout, stderr, changed) = package_present(name, installed_state, pkg_spec, module)
|
2013-03-02 23:01:02 +00:00
|
|
|
elif state in ['absent', 'removed']:
|
2013-06-15 14:27:45 +00:00
|
|
|
(rc, stdout, stderr, changed) = package_absent(name, installed_state, module)
|
2013-03-02 23:01:02 +00:00
|
|
|
elif state == 'latest':
|
2013-11-18 19:24:10 +00:00
|
|
|
(rc, stdout, stderr, changed) = package_latest(name, installed_state, pkg_spec, module)
|
2013-03-02 23:01:02 +00:00
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
if stderr:
|
|
|
|
module.fail_json(msg=stderr)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg=stdout)
|
|
|
|
|
|
|
|
result['changed'] = changed
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2013-12-02 20:13:49 +00:00
|
|
|
# import module snippets
|
2013-12-02 20:11:23 +00:00
|
|
|
from ansible.module_utils.basic import *
|
2013-03-02 23:01:02 +00:00
|
|
|
main()
|