2013-03-03 00:01:02 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2015-06-29 09:27:44 +02:00
|
|
|
# (c) 2013, Patrik Lundin <patrik@sigterm.se>
|
2013-03-03 00:01:02 +01:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2015-11-03 22:16:33 +01:00
|
|
|
import os
|
2016-06-29 23:27:31 +02:00
|
|
|
import platform
|
2013-03-03 00:01:02 +01:00
|
|
|
import re
|
2013-07-20 17:45:07 +02:00
|
|
|
import shlex
|
2015-11-03 22:16:33 +01:00
|
|
|
import sqlite3
|
2013-03-03 00:01:02 +01:00
|
|
|
|
2016-06-29 23:27:31 +02:00
|
|
|
from distutils.version import StrictVersion
|
|
|
|
|
2013-03-03 00:01:02 +01:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: openbsd_pkg
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Patrik Lundin (@eest)"
|
2013-03-30 20:44:34 +01:00
|
|
|
version_added: "1.1"
|
2013-03-03 00:01:02 +01:00
|
|
|
short_description: Manage packages on OpenBSD.
|
|
|
|
description:
|
|
|
|
- Manage packages on OpenBSD using the pkg tools.
|
2016-01-18 21:20:14 +01:00
|
|
|
requirements: [ "python >= 2.5" ]
|
2013-03-03 00:01:02 +01:00
|
|
|
options:
|
|
|
|
name:
|
|
|
|
required: true
|
|
|
|
description:
|
|
|
|
- Name of the package.
|
|
|
|
state:
|
|
|
|
required: true
|
|
|
|
choices: [ present, latest, absent ]
|
|
|
|
description:
|
2013-03-26 18:22:15 +01: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.
|
2015-11-03 22:16:33 +01:00
|
|
|
build:
|
|
|
|
required: false
|
|
|
|
choices: [ yes, no ]
|
|
|
|
default: no
|
|
|
|
description:
|
|
|
|
- Build the package from source instead of downloading and installing
|
|
|
|
a binary. Requires that the port source tree is already installed.
|
|
|
|
Automatically builds and installs the 'sqlports' package, if it is
|
|
|
|
not already installed.
|
2016-01-18 21:20:14 +01:00
|
|
|
version_added: "2.1"
|
2015-11-03 22:16:33 +01:00
|
|
|
ports_dir:
|
|
|
|
required: false
|
|
|
|
default: /usr/ports
|
|
|
|
description:
|
|
|
|
- When used in combination with the 'build' option, allows overriding
|
|
|
|
the default ports source directory.
|
2016-01-18 21:20:14 +01:00
|
|
|
version_added: "2.1"
|
2013-06-14 11:53:43 +02: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 02:31:45 +02:00
|
|
|
|
2015-11-03 22:16:33 +01:00
|
|
|
# Make sure nmap is installed, build it from source if it is not
|
|
|
|
- openbsd_pkg: name=nmap state=present build=yes
|
|
|
|
|
2014-06-15 02:31:45 +02:00
|
|
|
# Specify a pkg flavour with '--'
|
2016-01-28 21:54:25 +01:00
|
|
|
- openbsd_pkg: name=vim--no_x11 state=present
|
2014-06-15 02:31:45 +02:00
|
|
|
|
2014-06-16 18:35:57 +02:00
|
|
|
# Specify the default flavour to avoid ambiguity errors
|
2014-06-15 02:31:45 +02:00
|
|
|
- openbsd_pkg: name=vim-- state=present
|
2013-12-30 16:58:57 +01:00
|
|
|
|
2016-06-29 23:27:31 +02:00
|
|
|
# Specify a package branch (requires at least OpenBSD 6.0)
|
|
|
|
- openbsd_pkg: name=python%3.5 state=present
|
|
|
|
|
2014-03-26 05:59:37 +01:00
|
|
|
# Update all packages on the system
|
|
|
|
- openbsd_pkg: name=* state=latest
|
2013-03-03 00:01:02 +01:00
|
|
|
'''
|
|
|
|
|
2013-07-10 21:23:10 +02:00
|
|
|
# Function used for executing commands.
|
2013-07-20 17:45:07 +02:00
|
|
|
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)
|
2013-03-03 00:01:02 +01:00
|
|
|
|
|
|
|
# Function used to find out if a package is currently installed.
|
2013-11-18 20:24:10 +01:00
|
|
|
def get_package_state(name, pkg_spec, module):
|
2016-06-29 23:27:31 +02:00
|
|
|
info_cmd = 'pkg_info -Iq'
|
2013-03-03 00:01:02 +01:00
|
|
|
|
2016-06-29 23:27:31 +02:00
|
|
|
command = "%s inst:%s" % (info_cmd, name)
|
2013-03-03 00:01:02 +01:00
|
|
|
|
2013-11-18 20:24:10 +01:00
|
|
|
rc, stdout, stderr = execute_command(command, module)
|
|
|
|
|
2016-06-29 23:27:31 +02:00
|
|
|
if stderr:
|
2013-11-18 20:24:10 +01:00
|
|
|
module.fail_json(msg="failed in get_package_state(): " + stderr)
|
2013-03-03 00:01:02 +01:00
|
|
|
|
2016-06-29 23:27:31 +02:00
|
|
|
if stdout:
|
|
|
|
# If the requested package name is just a stem, like "python", we may
|
|
|
|
# find multiple packages with that name.
|
2016-06-30 00:38:57 +02:00
|
|
|
pkg_spec['installed_names'] = [name for name in stdout.splitlines()]
|
2016-06-29 23:27:31 +02:00
|
|
|
module.debug("get_package_state(): installed_names = %s" % pkg_spec['installed_names'])
|
2013-03-03 00:01:02 +01:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Function used to make sure a package is present.
|
2013-11-18 20:24:10 +01:00
|
|
|
def package_present(name, installed_state, pkg_spec, module):
|
2015-11-03 22:16:33 +01:00
|
|
|
build = module.params['build']
|
|
|
|
|
2013-06-15 16:27:45 +02:00
|
|
|
if module.check_mode:
|
2013-07-10 18:19:01 +02:00
|
|
|
install_cmd = 'pkg_add -Imn'
|
|
|
|
else:
|
2015-11-03 22:16:33 +01:00
|
|
|
if build is True:
|
|
|
|
port_dir = "%s/%s" % (module.params['ports_dir'], get_package_source_path(name, pkg_spec, module))
|
|
|
|
if os.path.isdir(port_dir):
|
|
|
|
if pkg_spec['flavor']:
|
|
|
|
flavors = pkg_spec['flavor'].replace('-', ' ')
|
|
|
|
install_cmd = "cd %s && make clean=depends && FLAVOR=\"%s\" make install && make clean=depends" % (port_dir, flavors)
|
|
|
|
elif pkg_spec['subpackage']:
|
|
|
|
install_cmd = "cd %s && make clean=depends && SUBPACKAGE=\"%s\" make install && make clean=depends" % (port_dir, pkg_spec['subpackage'])
|
|
|
|
else:
|
|
|
|
install_cmd = "cd %s && make install && make clean=depends" % (port_dir)
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="the port source directory %s does not exist" % (port_dir))
|
|
|
|
else:
|
|
|
|
install_cmd = 'pkg_add -Im'
|
2013-06-15 16:27:45 +02:00
|
|
|
|
2013-03-03 00:01:02 +01:00
|
|
|
if installed_state is False:
|
2013-06-15 16:27:45 +02:00
|
|
|
|
|
|
|
# Attempt to install the package
|
2015-11-03 22:16:33 +01:00
|
|
|
if build is True and not module.check_mode:
|
|
|
|
(rc, stdout, stderr) = module.run_command(install_cmd, module, use_unsafe_shell=True)
|
|
|
|
else:
|
|
|
|
(rc, stdout, stderr) = execute_command("%s %s" % (install_cmd, name), module)
|
2013-06-15 16:27:45 +02:00
|
|
|
|
2013-07-09 20:56:22 +02:00
|
|
|
# The behaviour of pkg_add is a bit different depending on if a
|
|
|
|
# specific version is supplied or not.
|
2013-07-10 21:23:10 +02:00
|
|
|
#
|
2013-07-09 20:56:22 +02:00
|
|
|
# When a specific version is supplied the return code will be 0 when
|
2016-06-29 23:27:31 +02:00
|
|
|
# a package is found and 1 when it is not. If a version is not
|
|
|
|
# supplied the tool will exit 0 in both cases.
|
|
|
|
#
|
|
|
|
# It is important to note that "version" relates to the
|
|
|
|
# packages-specs(7) notion of a version. If using the branch syntax
|
|
|
|
# (like "python%3.5") the version number is considered part of the
|
|
|
|
# stem, and the pkg_add behavior behaves the same as if the name did
|
|
|
|
# not contain a version (which it strictly speaking does not).
|
2015-11-03 22:16:33 +01:00
|
|
|
if pkg_spec['version'] or build is True:
|
2013-07-09 20:56:22 +02:00
|
|
|
# Depend on the return code.
|
2015-10-01 06:29:57 +02:00
|
|
|
module.debug("package_present(): depending on return code")
|
2013-07-10 21:23:10 +02:00
|
|
|
if rc:
|
2013-07-09 20:56:22 +02:00
|
|
|
changed=False
|
2013-03-03 00:01:02 +01:00
|
|
|
else:
|
2013-07-10 18:19:01 +02:00
|
|
|
# Depend on stderr instead.
|
2015-10-01 06:29:57 +02:00
|
|
|
module.debug("package_present(): depending on stderr")
|
2013-07-09 20:56:22 +02:00
|
|
|
if stderr:
|
2013-07-10 18:19:01 +02: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.
|
2015-10-01 06:29:57 +02:00
|
|
|
module.debug("package_present(): we were able to install package")
|
2013-07-10 18:19:01 +02:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# We really did fail, fake the return code.
|
2015-10-01 06:29:57 +02:00
|
|
|
module.debug("package_present(): we really did fail")
|
2013-07-10 18:19:01 +02:00
|
|
|
rc = 1
|
|
|
|
changed=False
|
2013-11-18 20:24:10 +01:00
|
|
|
else:
|
2015-10-01 06:29:57 +02:00
|
|
|
module.debug("package_present(): stderr was not set")
|
2013-07-09 20:56:22 +02:00
|
|
|
|
|
|
|
if rc == 0:
|
2013-06-15 16:27:45 +02:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
2013-03-03 00:01:02 +01:00
|
|
|
changed=True
|
2013-07-09 20:56:22 +02:00
|
|
|
|
2013-03-03 00:01:02 +01: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 20:24:10 +01:00
|
|
|
def package_latest(name, installed_state, pkg_spec, module):
|
2015-11-03 22:16:33 +01:00
|
|
|
|
|
|
|
if module.params['build'] is True:
|
|
|
|
module.fail_json(msg="the combination of build=%s and state=latest is not supported" % module.params['build'])
|
|
|
|
|
2013-06-15 16:27:45 +02:00
|
|
|
if module.check_mode:
|
|
|
|
upgrade_cmd = 'pkg_add -umn'
|
2013-07-10 21:23:10 +02:00
|
|
|
else:
|
2013-06-15 16:27:45 +02:00
|
|
|
upgrade_cmd = 'pkg_add -um'
|
2013-03-03 00:01:02 +01:00
|
|
|
|
|
|
|
pre_upgrade_name = ''
|
2013-06-15 16:27:45 +02:00
|
|
|
|
2013-03-03 00:01:02 +01:00
|
|
|
if installed_state is True:
|
|
|
|
|
2013-11-18 20:24:10 +01:00
|
|
|
# Attempt to upgrade the package.
|
2013-07-20 17:45:07 +02:00
|
|
|
(rc, stdout, stderr) = execute_command("%s %s" % (upgrade_cmd, name), module)
|
2013-03-03 00:01:02 +01:00
|
|
|
|
2013-06-15 16:27:45 +02: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.
|
2016-06-29 23:27:31 +02:00
|
|
|
changed = False
|
|
|
|
for installed_name in pkg_spec['installed_names']:
|
|
|
|
module.debug("package_latest(): checking for pre-upgrade package name: %s" % installed_name)
|
|
|
|
match = re.search("\W%s->.+: ok\W" % installed_name, stdout)
|
|
|
|
if match:
|
2016-06-30 18:51:35 +02:00
|
|
|
module.debug("package_latest(): pre-upgrade package name match: %s" % installed_name)
|
2016-06-29 23:27:31 +02:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2013-03-03 00:01:02 +01:00
|
|
|
|
2016-06-29 23:27:31 +02:00
|
|
|
changed = True
|
2016-06-30 17:30:28 +02:00
|
|
|
break
|
2013-06-15 16:27:45 +02:00
|
|
|
|
2013-07-10 21:06:35 +02: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-03 00:01:02 +01:00
|
|
|
|
|
|
|
return (rc, stdout, stderr, changed)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# If package was not installed at all just make it present.
|
2015-10-01 06:29:57 +02:00
|
|
|
module.debug("package_latest(): package is not installed, calling package_present()")
|
2013-11-18 20:24:10 +01:00
|
|
|
return package_present(name, installed_state, pkg_spec, module)
|
2013-03-03 00:01:02 +01:00
|
|
|
|
|
|
|
# Function used to make sure a package is not installed.
|
2013-06-15 16:27:45 +02: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-03 00:01:02 +01:00
|
|
|
if installed_state is True:
|
2013-06-15 16:27:45 +02:00
|
|
|
|
2013-11-18 20:24:10 +01:00
|
|
|
# Attempt to remove the package.
|
2013-07-20 17:45:07 +02:00
|
|
|
rc, stdout, stderr = execute_command("%s %s" % (remove_cmd, name), module)
|
2013-03-03 00:01:02 +01:00
|
|
|
|
|
|
|
if rc == 0:
|
2013-06-15 16:27:45 +02:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
2013-03-03 00:01:02 +01:00
|
|
|
changed=True
|
|
|
|
else:
|
|
|
|
changed=False
|
2013-06-15 16:27:45 +02:00
|
|
|
|
2013-03-03 00:01:02 +01:00
|
|
|
else:
|
|
|
|
rc = 0
|
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
changed=False
|
|
|
|
|
|
|
|
return (rc, stdout, stderr, changed)
|
|
|
|
|
2013-12-30 16:58:57 +01:00
|
|
|
# Function used to parse the package name based on packages-specs(7).
|
|
|
|
# The general name structure is "stem-version[-flavors]".
|
2016-06-29 23:27:31 +02:00
|
|
|
#
|
|
|
|
# Names containing "%" are a special variation not part of the
|
|
|
|
# packages-specs(7) syntax. See pkg_add(1) on OpenBSD 6.0 or later for a
|
|
|
|
# description.
|
2013-11-18 20:24:10 +01:00
|
|
|
def parse_package_name(name, pkg_spec, module):
|
2016-06-29 23:27:31 +02:00
|
|
|
module.debug("parse_package_name(): parsing name: %s" % name)
|
2013-11-18 20:24:10 +01:00
|
|
|
# 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:
|
2016-06-30 18:55:43 +02:00
|
|
|
module.fail_json(msg="package name both has a version and is version-less: " + name)
|
2013-11-18 20:24:10 +01:00
|
|
|
|
|
|
|
# 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')
|
2015-11-03 22:16:33 +01:00
|
|
|
pkg_spec['style'] = 'version'
|
2013-11-18 20:24:10 +01:00
|
|
|
else:
|
2016-06-30 18:55:43 +02:00
|
|
|
module.fail_json(msg="unable to parse package name at version_match: " + name)
|
2013-11-18 20:24:10 +01:00
|
|
|
|
|
|
|
# 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')
|
2015-11-03 22:16:33 +01:00
|
|
|
pkg_spec['style'] = 'versionless'
|
2013-11-18 20:24:10 +01:00
|
|
|
else:
|
2016-06-30 18:55:43 +02:00
|
|
|
module.fail_json(msg="unable to parse package name at versionless_match: " + name)
|
2013-11-18 20:24:10 +01:00
|
|
|
|
|
|
|
# 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
|
2015-11-03 22:16:33 +01:00
|
|
|
pkg_spec['style'] = 'stem'
|
2013-11-18 20:24:10 +01:00
|
|
|
else:
|
2016-06-30 18:55:43 +02:00
|
|
|
module.fail_json(msg="unable to parse package name at else: " + name)
|
2013-11-18 20:24:10 +01:00
|
|
|
|
2016-06-29 23:27:31 +02:00
|
|
|
# If the stem contains an "%" then it needs special treatment.
|
|
|
|
branch_match = re.search("%", pkg_spec['stem'])
|
|
|
|
if branch_match:
|
|
|
|
|
|
|
|
branch_release = "6.0"
|
|
|
|
|
|
|
|
if version_match or versionless_match:
|
2016-06-30 18:55:43 +02:00
|
|
|
module.fail_json(msg="package name using 'branch' syntax also has a version or is version-less: " + name)
|
2016-06-29 23:27:31 +02:00
|
|
|
if StrictVersion(platform.release()) < StrictVersion(branch_release):
|
2016-06-30 18:55:43 +02:00
|
|
|
module.fail_json(msg="package name using 'branch' syntax requires at least OpenBSD %s: %s" % (branch_release, name))
|
2016-06-29 23:27:31 +02:00
|
|
|
|
|
|
|
pkg_spec['style'] = 'branch'
|
|
|
|
|
2013-11-18 20:24:10 +01:00
|
|
|
# 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:
|
2016-06-30 18:55:43 +02:00
|
|
|
module.fail_json(msg="trailing dash in flavor: " + pkg_spec['flavor'])
|
2013-11-18 20:24:10 +01:00
|
|
|
|
2015-11-03 22:16:33 +01:00
|
|
|
# Function used for figuring out the port path.
|
|
|
|
def get_package_source_path(name, pkg_spec, module):
|
|
|
|
pkg_spec['subpackage'] = None
|
|
|
|
if pkg_spec['stem'] == 'sqlports':
|
|
|
|
return 'databases/sqlports'
|
|
|
|
else:
|
|
|
|
# try for an exact match first
|
2016-06-29 23:57:31 +02:00
|
|
|
sqlports_db_file = '/usr/local/share/sqlports'
|
|
|
|
if not os.path.isfile(sqlports_db_file):
|
|
|
|
module.fail_json(msg="sqlports file '%s' is missing" % sqlports_db_file)
|
|
|
|
|
|
|
|
conn = sqlite3.connect(sqlports_db_file)
|
2015-11-03 22:16:33 +01:00
|
|
|
first_part_of_query = 'SELECT fullpkgpath, fullpkgname FROM ports WHERE fullpkgname'
|
|
|
|
query = first_part_of_query + ' = ?'
|
2016-06-30 17:41:17 +02:00
|
|
|
module.debug("package_package_source_path(): exact query: %s" % query)
|
2015-11-03 22:16:33 +01:00
|
|
|
cursor = conn.execute(query, (name,))
|
|
|
|
results = cursor.fetchall()
|
|
|
|
|
|
|
|
# next, try for a fuzzier match
|
|
|
|
if len(results) < 1:
|
|
|
|
looking_for = pkg_spec['stem'] + (pkg_spec['version_separator'] or '-') + (pkg_spec['version'] or '%')
|
|
|
|
query = first_part_of_query + ' LIKE ?'
|
|
|
|
if pkg_spec['flavor']:
|
|
|
|
looking_for += pkg_spec['flavor_separator'] + pkg_spec['flavor']
|
2016-06-30 17:41:17 +02:00
|
|
|
module.debug("package_package_source_path(): fuzzy flavor query: %s" % query)
|
2015-11-03 22:16:33 +01:00
|
|
|
cursor = conn.execute(query, (looking_for,))
|
|
|
|
elif pkg_spec['style'] == 'versionless':
|
|
|
|
query += ' AND fullpkgname NOT LIKE ?'
|
2016-06-30 17:41:17 +02:00
|
|
|
module.debug("package_package_source_path(): fuzzy versionless query: %s" % query)
|
2015-11-03 22:16:33 +01:00
|
|
|
cursor = conn.execute(query, (looking_for, "%s-%%" % looking_for,))
|
|
|
|
else:
|
2016-06-30 17:41:17 +02:00
|
|
|
module.debug("package_package_source_path(): fuzzy query: %s" % query)
|
2015-11-03 22:16:33 +01:00
|
|
|
cursor = conn.execute(query, (looking_for,))
|
|
|
|
results = cursor.fetchall()
|
|
|
|
|
|
|
|
# error if we don't find exactly 1 match
|
|
|
|
conn.close()
|
|
|
|
if len(results) < 1:
|
|
|
|
module.fail_json(msg="could not find a port by the name '%s'" % name)
|
|
|
|
if len(results) > 1:
|
|
|
|
matches = map(lambda x:x[1], results)
|
|
|
|
module.fail_json(msg="too many matches, unsure which to build: %s" % ' OR '.join(matches))
|
|
|
|
|
|
|
|
# there's exactly 1 match, so figure out the subpackage, if any, then return
|
|
|
|
fullpkgpath = results[0][0]
|
|
|
|
parts = fullpkgpath.split(',')
|
|
|
|
if len(parts) > 1 and parts[1][0] == '-':
|
|
|
|
pkg_spec['subpackage'] = parts[1]
|
|
|
|
return parts[0]
|
|
|
|
|
2013-12-30 16:58:57 +01:00
|
|
|
# Function used for upgrading all installed packages.
|
|
|
|
def upgrade_packages(module):
|
|
|
|
if module.check_mode:
|
|
|
|
upgrade_cmd = 'pkg_add -Imnu'
|
|
|
|
else:
|
|
|
|
upgrade_cmd = 'pkg_add -Imu'
|
|
|
|
|
|
|
|
# Attempt to upgrade all packages.
|
|
|
|
rc, stdout, stderr = execute_command("%s" % upgrade_cmd, module)
|
|
|
|
|
|
|
|
# Try to find any occurance of a package changing version like:
|
|
|
|
# "bzip2-1.0.6->1.0.6p0: ok".
|
2014-05-13 19:03:18 +02:00
|
|
|
match = re.search("\W\w.+->.+: ok\W", stdout)
|
|
|
|
if match:
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
|
|
|
|
|
|
|
changed=True
|
|
|
|
|
|
|
|
else:
|
|
|
|
changed=False
|
2013-12-30 16:58:57 +01:00
|
|
|
|
|
|
|
# It seems we can not trust the return value, so depend on the presence of
|
|
|
|
# stderr to know if something failed.
|
|
|
|
if stderr:
|
|
|
|
rc = 1
|
|
|
|
else:
|
|
|
|
rc = 0
|
|
|
|
|
|
|
|
return (rc, stdout, stderr, changed)
|
|
|
|
|
2013-03-03 00:01:02 +01:00
|
|
|
# ===========================================
|
2013-12-30 16:58:57 +01:00
|
|
|
# Main control flow.
|
2013-03-03 00:01:02 +01:00
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
2014-03-27 19:44:18 +01:00
|
|
|
name = dict(required=True),
|
|
|
|
state = dict(required=True, choices=['absent', 'installed', 'latest', 'present', 'removed']),
|
2015-11-03 22:16:33 +01:00
|
|
|
build = dict(default='no', type='bool'),
|
|
|
|
ports_dir = dict(default='/usr/ports'),
|
2013-06-15 16:27:45 +02:00
|
|
|
),
|
|
|
|
supports_check_mode = True
|
2013-03-03 00:01:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
name = module.params['name']
|
|
|
|
state = module.params['state']
|
2015-11-03 22:16:33 +01:00
|
|
|
build = module.params['build']
|
|
|
|
ports_dir = module.params['ports_dir']
|
2013-03-03 00:01:02 +01:00
|
|
|
|
|
|
|
rc = 0
|
|
|
|
stdout = ''
|
|
|
|
stderr = ''
|
|
|
|
result = {}
|
|
|
|
result['name'] = name
|
|
|
|
result['state'] = state
|
2015-11-03 22:16:33 +01:00
|
|
|
result['build'] = build
|
|
|
|
|
|
|
|
if build is True:
|
|
|
|
if not os.path.isdir(ports_dir):
|
|
|
|
module.fail_json(msg="the ports source directory %s does not exist" % (ports_dir))
|
|
|
|
|
|
|
|
# build sqlports if its not installed yet
|
|
|
|
pkg_spec = {}
|
|
|
|
parse_package_name('sqlports', pkg_spec, module)
|
2016-06-29 23:57:31 +02:00
|
|
|
installed_state = get_package_state('sqlports', pkg_spec, module)
|
2015-11-03 22:16:33 +01:00
|
|
|
if not installed_state:
|
2016-07-01 18:38:19 +02:00
|
|
|
module.debug("main(): installing 'sqlports' because build=%s" % module.params['build'])
|
2015-11-03 22:16:33 +01:00
|
|
|
package_present('sqlports', installed_state, pkg_spec, module)
|
2013-12-30 16:58:57 +01:00
|
|
|
|
2014-03-27 19:44:18 +01:00
|
|
|
if name == '*':
|
|
|
|
if state != 'latest':
|
|
|
|
module.fail_json(msg="the package name '*' is only valid when using state=latest")
|
2014-03-26 05:59:37 +01:00
|
|
|
else:
|
2014-03-27 19:44:18 +01:00
|
|
|
# Perform an upgrade of all installed packages.
|
|
|
|
(rc, stdout, stderr, changed) = upgrade_packages(module)
|
2013-12-30 16:58:57 +01:00
|
|
|
else:
|
2014-03-27 19:44:18 +01:00
|
|
|
# Parse package name and put results in the pkg_spec dictionary.
|
|
|
|
pkg_spec = {}
|
|
|
|
parse_package_name(name, pkg_spec, module)
|
|
|
|
|
2016-06-30 18:13:40 +02:00
|
|
|
# Not sure how the branch syntax is supposed to play together
|
|
|
|
# with build mode. Disable it for now.
|
|
|
|
if pkg_spec['style'] == 'branch' and module.params['build'] is True:
|
|
|
|
module.fail_json(msg="the combination of 'branch' syntax and build=%s is not supported: %s" % (module.params['build'], name))
|
|
|
|
|
2014-03-27 19:44:18 +01:00
|
|
|
# Get package state.
|
|
|
|
installed_state = get_package_state(name, pkg_spec, module)
|
|
|
|
|
|
|
|
# Perform requested action.
|
|
|
|
if state in ['installed', 'present']:
|
|
|
|
(rc, stdout, stderr, changed) = package_present(name, installed_state, pkg_spec, module)
|
|
|
|
elif state in ['absent', 'removed']:
|
|
|
|
(rc, stdout, stderr, changed) = package_absent(name, installed_state, module)
|
|
|
|
elif state == 'latest':
|
|
|
|
(rc, stdout, stderr, changed) = package_latest(name, installed_state, pkg_spec, module)
|
2013-03-03 00:01:02 +01: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-30 16:58:57 +01:00
|
|
|
# Import module snippets.
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2013-03-03 00:01:02 +01:00
|
|
|
main()
|