summaryrefslogtreecommitdiff
path: root/packaging/os/zypper.py
blob: 43e8919d5a2cf2ac9a3fa466edb2491c941bdd9a (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
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-

# (c) 2013, Patrick Callahan <pmc@patrickcallahan.com>
# based on
#     openbsd_pkg
#         (c) 2013
#         Patrik Lundin <patrik.lundin.swe@gmail.com>
#
#     yum
#         (c) 2012, Red Hat, Inc
#         Written by Seth Vidal <skvidal at fedoraproject.org>
#
# 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

DOCUMENTATION = '''
---
module: zypper
author:
    - "Patrick Callahan (@dirtyharrycallahan)"
    - "Alexander Gubin (@alxgu)"
version_added: "1.2"
short_description: Manage packages on SUSE and openSUSE
description:
    - Manage packages on SUSE and openSUSE using the zypper and rpm tools.
options:
    name:
        description:
        - package name or package specifier with version C(name) or C(name-1.0). You can also pass a url or a local path to a rpm file.
        required: true
        aliases: [ 'pkg' ]
    state:
        description:
          - 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.
        required: false
        choices: [ present, latest, absent ]
        default: "present"
    type:
        description:
          - The type of package to be operated on.
        required: false
        choices: [ package, patch, pattern, product, srcpackage ]
        default: "package"
        version_added: "2.0"
    disable_gpg_check:
        description:
          - Whether to disable to GPG signature checking of the package
            signature being installed. Has an effect only if state is
            I(present) or I(latest).
        required: false
        default: "no"
        choices: [ "yes", "no" ]
        aliases: []
    disable_recommends:
        version_added: "1.8"
        description:
          - Corresponds to the C(--no-recommends) option for I(zypper). Default behavior (C(yes)) modifies zypper's default behavior; C(no) does install recommended packages. 
        required: false
        default: "yes"
        choices: [ "yes", "no" ]

notes: []
# informational: requirements for nodes
requirements: [ zypper, rpm ]
'''

EXAMPLES = '''
# Install "nmap"
- zypper: name=nmap state=present

# Install apache2 with recommended packages
- zypper: name=apache2 state=present disable_recommends=no

# Remove the "nmap" package
- zypper: name=nmap state=absent

# Install the nginx rpm from a remote repo
- zypper: name=http://nginx.org/packages/sles/12/x86_64/RPMS/nginx-1.8.0-1.sles12.ngx.x86_64.rpm state=present

# Install local rpm file
- zypper: name=/tmp/fancy-software.rpm state=present
'''

# Function used for getting zypper version
def zypper_version(module):
    """Return (rc, message) tuple"""
    cmd = ['/usr/bin/zypper', '-V']
    rc, stdout, stderr = module.run_command(cmd, check_rc=False)
    if rc == 0:
        return rc, stdout
    else:
        return rc, stderr

# Function used for getting versions of currently installed packages.
def get_current_version(m, packages):
    cmd = ['/bin/rpm', '-q', '--qf', '%{NAME} %{VERSION}-%{RELEASE}\n']
    cmd.extend(packages)

    rc, stdout, stderr = m.run_command(cmd, check_rc=False)

    current_version = {}
    rpmoutput_re = re.compile('^(\S+) (\S+)$')

    for stdoutline in stdout.splitlines():
        match = rpmoutput_re.match(stdoutline)
        if match == None:
            return None
        package = match.group(1)
        version = match.group(2)
        current_version[package] = version

    for package in packages:
        if package not in current_version:
            print package + ' was not returned by rpm \n'
            return None

    return current_version


# Function used to find out if a package is currently installed.
def get_package_state(m, packages):
    for i in range(0, len(packages)):
        # Check state of a local rpm-file
        if ".rpm" in packages[i]:
            # Check if rpm file is available
            package = packages[i]
            if not os.path.isfile(package) and not '://' in package:
                stderr = "No Package file matching '%s' found on system" % package
                m.fail_json(msg=stderr, rc=1)
            # Get packagename from rpm file
            cmd = ['/bin/rpm', '--query', '--qf', '%{NAME}', '--package']
            cmd.append(package)
            rc, stdout, stderr = m.run_command(cmd, check_rc=False)
            packages[i] = stdout

    cmd = ['/bin/rpm', '--query', '--qf', 'package %{NAME} is installed\n']
    cmd.extend(packages)

    rc, stdout, stderr = m.run_command(cmd, check_rc=False)

    installed_state = {}
    rpmoutput_re = re.compile('^package (\S+) (.*)$')
    for stdoutline in stdout.splitlines():
        match = rpmoutput_re.match(stdoutline)
        if match == None:
            continue
        package = match.group(1)
        result = match.group(2)
        if result == 'is installed':
            installed_state[package] = True
        else:
            installed_state[package] = False

    return installed_state

# Function used to make sure a package is present.
def package_present(m, name, installed_state, package_type, disable_gpg_check, disable_recommends, old_zypper):
    packages = []
    for package in name:
        if package not in installed_state or installed_state[package] is False:
            packages.append(package)
    if len(packages) != 0:
        cmd = ['/usr/bin/zypper', '--non-interactive']
        # add global options before zypper command
        if disable_gpg_check:
            cmd.append('--no-gpg-checks')
        cmd.extend(['install', '--auto-agree-with-licenses', '-t', package_type])
        # add install parameter
        if disable_recommends and not old_zypper:
            cmd.append('--no-recommends')
        cmd.extend(packages)
        rc, stdout, stderr = m.run_command(cmd, check_rc=False)

        if rc == 0:
            changed=True
        else:
            changed=False
    else:
        rc = 0
        stdout = ''
        stderr = ''
        changed=False

    return (rc, stdout, stderr, changed)

# Function used to make sure a package is the latest available version.
def package_latest(m, name, installed_state, package_type, disable_gpg_check, disable_recommends, old_zypper):

    # first of all, make sure all the packages are installed
    (rc, stdout, stderr, changed) = package_present(m, name, installed_state, package_type, disable_gpg_check, disable_recommends, old_zypper)

    # return if an error occured while installation
    # otherwise error messages will be lost and user doesn`t see any error
    if rc:
        return (rc, stdout, stderr, changed)

    # if we've already made a change, we don't have to check whether a version changed
    if not changed:
        pre_upgrade_versions = get_current_version(m, name)

    cmd = ['/usr/bin/zypper', '--non-interactive']

    if disable_gpg_check:
        cmd.append('--no-gpg-checks')

    if old_zypper:
        cmd.extend(['install', '--auto-agree-with-licenses', '-t', package_type])
    else:
        cmd.extend(['update', '--auto-agree-with-licenses', '-t', package_type])

    cmd.extend(name)
    rc, stdout, stderr = m.run_command(cmd, check_rc=False)

    # if we've already made a change, we don't have to check whether a version changed
    if not changed:
        post_upgrade_versions = get_current_version(m, name)
        if pre_upgrade_versions != post_upgrade_versions:
            changed = True

    return (rc, stdout, stderr, changed)

# Function used to make sure a package is not installed.
def package_absent(m, name, installed_state, package_type, old_zypper):
    packages = []
    for package in name:
        if package not in installed_state or installed_state[package] is True:
            packages.append(package)
    if len(packages) != 0:
        cmd = ['/usr/bin/zypper', '--non-interactive', 'remove', '-t', package_type]
        cmd.extend(packages)
        rc, stdout, stderr = m.run_command(cmd)

        if rc == 0:
            changed=True
        else:
            changed=False
    else:
        rc = 0
        stdout = ''
        stderr = ''
        changed=False

    return (rc, stdout, stderr, changed)

# ===========================================
# Main control flow

def main():
    module = AnsibleModule(
        argument_spec = dict(
            name = dict(required=True, aliases=['pkg'], type='list'),
            state = dict(required=False, default='present', choices=['absent', 'installed', 'latest', 'present', 'removed']),
            type = dict(required=False, default='package', choices=['package', 'patch', 'pattern', 'product', 'srcpackage']),
            disable_gpg_check = dict(required=False, default='no', type='bool'),
            disable_recommends = dict(required=False, default='yes', type='bool'),
        ),
        supports_check_mode = False
    )


    params = module.params

    name  = params['name']
    state = params['state']
    type_ = params['type']
    disable_gpg_check = params['disable_gpg_check']
    disable_recommends = params['disable_recommends']

    rc = 0
    stdout = ''
    stderr = ''
    result = {}
    result['name'] = name
    result['state'] = state

    rc, out = zypper_version(module)
    match = re.match(r'zypper\s+(\d+)\.(\d+)\.(\d+)', out)
    if not match or  int(match.group(1)) > 0:
        old_zypper = False
    else:
        old_zypper = True

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

    # Perform requested action
    if state in ['installed', 'present']:
        (rc, stdout, stderr, changed) = package_present(module, name, installed_state, type_, disable_gpg_check, disable_recommends, old_zypper)
    elif state in ['absent', 'removed']:
        (rc, stdout, stderr, changed) = package_absent(module, name, installed_state, type_, old_zypper)
    elif state == 'latest':
        (rc, stdout, stderr, changed) = package_latest(module, name, installed_state, type_, disable_gpg_check, disable_recommends, old_zypper)

    if rc != 0:
        if stderr:
            module.fail_json(msg=stderr, rc=rc)
        else:
            module.fail_json(msg=stdout, rc=rc)

    result['changed'] = changed
    result['rc'] = rc

    module.exit_json(**result)

# import module snippets
from ansible.module_utils.basic import *
main()