summaryrefslogtreecommitdiffstats
path: root/ansible_collections/community/general/plugins/modules/pkgin.py
blob: 8b29655d379f3e5818fb05e979ff03266e5e0508 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2013 Shaun Zinck <shaun.zinck at gmail.com>
# Copyright (c) 2015 Lawrence Leonard Gilbert <larry@L2G.to>
# Copyright (c) 2016 Jasper Lievisse Adriaanse <j at jasper.la>
#
# Written by Shaun Zinck
# Based on pacman module written by Afterburn <http://github.com/afterburn>
#  that was based on apt module written by Matthew Williams <matthew@flowroute.com>
#
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

from __future__ import absolute_import, division, print_function
__metaclass__ = type


DOCUMENTATION = '''
---
module: pkgin
short_description: Package manager for SmartOS, NetBSD, et al
description:
    - "The standard package manager for SmartOS, but also usable on NetBSD
      or any OS that uses C(pkgsrc).  (Home: U(http://pkgin.net/))"
author:
    - "Larry Gilbert (@L2G)"
    - "Shaun Zinck (@szinck)"
    - "Jasper Lievisse Adriaanse (@jasperla)"
notes:
    - "Known bug with pkgin < 0.8.0: if a package is removed and another
      package depends on it, the other package will be silently removed as
      well."
extends_documentation_fragment:
    - community.general.attributes
attributes:
    check_mode:
        support: full
    diff_mode:
        support: none
options:
    name:
        description:
            - Name of package to install/remove;
            - multiple names may be given, separated by commas
        aliases: [pkg]
        type: list
        elements: str
    state:
        description:
            - Intended state of the package
        choices: [ 'present', 'absent' ]
        default: present
        type: str
    update_cache:
        description:
          - Update repository database. Can be run with other steps or on it's own.
        type: bool
        default: false
    upgrade:
        description:
          - Upgrade main packages to their newer versions
        type: bool
        default: false
    full_upgrade:
        description:
          - Upgrade all packages to their newer versions
        type: bool
        default: false
    clean:
        description:
          - Clean packages cache
        type: bool
        default: false
    force:
        description:
          - Force package reinstall
        type: bool
        default: false
'''

EXAMPLES = '''
- name: Install package foo
  community.general.pkgin:
    name: foo
    state: present

- name: Install specific version of foo package
  community.general.pkgin:
    name: foo-2.0.1
    state: present

- name: Update cache and install foo package
  community.general.pkgin:
    name: foo
    update_cache: true

- name: Remove package foo
  community.general.pkgin:
    name: foo
    state: absent

- name: Remove packages foo and bar
  community.general.pkgin:
    name: foo,bar
    state: absent

- name: Update repositories as a separate step
  community.general.pkgin:
    update_cache: true

- name: Upgrade main packages (equivalent to pkgin upgrade)
  community.general.pkgin:
    upgrade: true

- name: Upgrade all packages (equivalent to pkgin full-upgrade)
  community.general.pkgin:
    full_upgrade: true

- name: Force-upgrade all packages (equivalent to pkgin -F full-upgrade)
  community.general.pkgin:
    full_upgrade: true
    force: true

- name: Clean packages cache (equivalent to pkgin clean)
  community.general.pkgin:
    clean: true
'''


import re

from ansible.module_utils.basic import AnsibleModule


class PackageState(object):
    PRESENT = 1
    NOT_INSTALLED = 2
    OUTDATED = 4
    NOT_FOUND = 8


def query_package(module, name):
    """Search for the package by name and return state of the package.
    """

    # test whether '-p' (parsable) flag is supported.
    rc, out, err = module.run_command([PKGIN_PATH, "-p", "-v"])

    if rc == 0:
        pflag = ['-p']
        splitchar = ';'
    else:
        pflag = []
        splitchar = ' '

    # Use "pkgin search" to find the package. The regular expression will
    # only match on the complete name.
    rc, out, err = module.run_command([PKGIN_PATH] + pflag + ["search", "^%s$" % name])

    # rc will not be 0 unless the search was a success
    if rc == 0:

        # Search results may contain more than one line (e.g., 'emacs'), so iterate
        # through each line to see if we have a match.
        packages = out.split('\n')

        for package in packages:

            # Break up line at spaces.  The first part will be the package with its
            # version (e.g. 'gcc47-libs-4.7.2nb4'), and the second will be the state
            # of the package:
            #     ''  - not installed
            #     '<' - installed but out of date
            #     '=' - installed and up to date
            #     '>' - installed but newer than the repository version

            if (package in ('reading local summary...',
                            'processing local summary...',
                            'downloading pkg_summary.xz done.')) or \
               (package.startswith('processing remote summary (')):
                continue

            pkgname_with_version, raw_state = package.split(splitchar)[0:2]

            # Search for package, stripping version
            # (results in sth like 'gcc47-libs' or 'emacs24-nox11')
            pkg_search_obj = re.search(r'^(.*?)\-[0-9][0-9.]*(nb[0-9]+)*', pkgname_with_version, re.M)

            # Do not proceed unless we have a match
            if not pkg_search_obj:
                continue

            # Grab matched string
            pkgname_without_version = pkg_search_obj.group(1)

            if name not in (pkgname_with_version, pkgname_without_version):
                continue

            # The package was found; now return its state
            if raw_state == '<':
                return PackageState.OUTDATED
            elif raw_state == '=' or raw_state == '>':
                return PackageState.PRESENT
            else:
                # Package found but not installed
                return PackageState.NOT_INSTALLED
            # no fall-through

        # No packages were matched
        return PackageState.NOT_FOUND

    # Search failed
    return PackageState.NOT_FOUND


def format_action_message(module, action, count):
    vars = {"actioned": action,
            "count": count}

    if module.check_mode:
        message = "would have %(actioned)s %(count)d package" % vars
    else:
        message = "%(actioned)s %(count)d package" % vars

    if count == 1:
        return message
    else:
        return message + "s"


def format_pkgin_command(module, command, package=None):
    # Not all commands take a package argument, so cover this up by passing
    # an empty string. Some commands (e.g. 'update') will ignore extra
    # arguments, however this behaviour cannot be relied on for others.
    if package is None:
        packages = []
    else:
        packages = [package]

    if module.params["force"]:
        force = ["-F"]
    else:
        force = []

    if module.check_mode:
        return [PKGIN_PATH, "-n", command] + packages
    else:
        return [PKGIN_PATH, "-y"] + force + [command] + packages


def remove_packages(module, packages):

    remove_c = 0

    # Using a for loop in case of error, we can report the package that failed
    for package in packages:
        # Query the package first, to see if we even need to remove
        if query_package(module, package) in [PackageState.NOT_INSTALLED, PackageState.NOT_FOUND]:
            continue

        rc, out, err = module.run_command(
            format_pkgin_command(module, "remove", package))

        if not module.check_mode and query_package(module, package) in [PackageState.PRESENT, PackageState.OUTDATED]:
            module.fail_json(msg="failed to remove %s: %s" % (package, out), stdout=out, stderr=err)

        remove_c += 1

    if remove_c > 0:
        module.exit_json(changed=True, msg=format_action_message(module, "removed", remove_c))

    module.exit_json(changed=False, msg="package(s) already absent")


def install_packages(module, packages):

    install_c = 0

    for package in packages:
        query_result = query_package(module, package)
        if query_result in [PackageState.PRESENT, PackageState.OUTDATED]:
            continue
        elif query_result is PackageState.NOT_FOUND:
            module.fail_json(msg="failed to find package %s for installation" % package)

        rc, out, err = module.run_command(
            format_pkgin_command(module, "install", package))

        if not module.check_mode and not query_package(module, package) in [PackageState.PRESENT, PackageState.OUTDATED]:
            module.fail_json(msg="failed to install %s: %s" % (package, out), stdout=out, stderr=err)

        install_c += 1

    if install_c > 0:
        module.exit_json(changed=True, msg=format_action_message(module, "installed", install_c), stdout=out, stderr=err)

    module.exit_json(changed=False, msg="package(s) already present")


def update_package_db(module):
    rc, out, err = module.run_command(
        format_pkgin_command(module, "update"))

    if rc == 0:
        if re.search('database for.*is up-to-date\n$', out):
            return False, "database is up-to-date"
        else:
            return True, "updated repository database"
    else:
        module.fail_json(msg="could not update package db", stdout=out, stderr=err)


def do_upgrade_packages(module, full=False):
    if full:
        cmd = "full-upgrade"
    else:
        cmd = "upgrade"

    rc, out, err = module.run_command(
        format_pkgin_command(module, cmd))

    if rc == 0:
        if re.search('^(.*\n|)nothing to do.\n$', out):
            module.exit_json(changed=False, msg="nothing left to upgrade")
    else:
        module.fail_json(msg="could not %s packages" % cmd, stdout=out, stderr=err)


def upgrade_packages(module):
    do_upgrade_packages(module)


def full_upgrade_packages(module):
    do_upgrade_packages(module, True)


def clean_cache(module):
    rc, out, err = module.run_command(
        format_pkgin_command(module, "clean"))

    if rc == 0:
        # There's no indication if 'clean' actually removed anything,
        # so assume it did.
        module.exit_json(changed=True, msg="cleaned caches")
    else:
        module.fail_json(msg="could not clean package cache", stdout=out, stderr=err)


def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(default="present", choices=["present", "absent"]),
            name=dict(aliases=["pkg"], type='list', elements='str'),
            update_cache=dict(default=False, type='bool'),
            upgrade=dict(default=False, type='bool'),
            full_upgrade=dict(default=False, type='bool'),
            clean=dict(default=False, type='bool'),
            force=dict(default=False, type='bool')),
        required_one_of=[['name', 'update_cache', 'upgrade', 'full_upgrade', 'clean']],
        supports_check_mode=True)

    global PKGIN_PATH
    PKGIN_PATH = module.get_bin_path('pkgin', True, ['/opt/local/bin'])

    module.run_command_environ_update = dict(LANG='C', LC_ALL='C', LC_MESSAGES='C', LC_CTYPE='C')

    p = module.params

    if p["update_cache"]:
        c, msg = update_package_db(module)
        if not (p['name'] or p["upgrade"] or p["full_upgrade"]):
            module.exit_json(changed=c, msg=msg)

    if p["upgrade"]:
        upgrade_packages(module)
        if not p['name']:
            module.exit_json(changed=True, msg='upgraded packages')

    if p["full_upgrade"]:
        full_upgrade_packages(module)
        if not p['name']:
            module.exit_json(changed=True, msg='upgraded all packages')

    if p["clean"]:
        clean_cache(module)
        if not p['name']:
            module.exit_json(changed=True, msg='cleaned caches')

    pkgs = p["name"]

    if p["state"] == "present":
        install_packages(module, pkgs)

    elif p["state"] == "absent":
        remove_packages(module, pkgs)


if __name__ == '__main__':
    main()