~canonical-ci-engineering/ubuntu-ci-services-itself/ansible

« back to all changes in this revision

Viewing changes to library/packaging/portinstall

  • Committer: Package Import Robot
  • Author(s): Michael Vogt
  • Date: 2013-11-24 10:41:27 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20131124104127-zppejr80jahk3av6
Tags: 1.4.0+dfsg-1
* new upstream version
* debian/rules:
  - remove sed manpage fixes, fixed upstream
* debian/patches/fix-html-makefile:
  - removed, fixed upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# (c) 2013, berenddeboer
 
5
# Written by berenddeboer <berend@pobox.com>
 
6
# Based on pkgng module written by bleader <bleader at ratonland.org>
 
7
#
 
8
# This module is free software: you can redistribute it and/or modify
 
9
# it under the terms of the GNU General Public License as published by
 
10
# the Free Software Foundation, either version 3 of the License, or
 
11
# (at your option) any later version.
 
12
#
 
13
# This software is distributed in the hope that it will be useful,
 
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
# GNU General Public License for more details.
 
17
#
 
18
# You should have received a copy of the GNU General Public License
 
19
# along with this software.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
 
 
22
DOCUMENTATION = '''
 
23
---
 
24
module: portinstall
 
25
short_description: Installing packages from FreeBSD's ports system
 
26
description:
 
27
    - Manage packages for FreeBSD using 'portinstall'.
 
28
version_added: "1.3"
 
29
options:
 
30
    name:
 
31
        description:
 
32
            - name of package to install/remove
 
33
        required: true
 
34
    state:
 
35
        description:
 
36
            - state of the package
 
37
        choices: [ 'present', 'absent' ]
 
38
        required: false
 
39
        default: present
 
40
    use_packages:
 
41
        description:
 
42
            - use packages instead of ports whenever available
 
43
        choices: [ 'yes', 'no' ]
 
44
        required: false
 
45
        default: yes
 
46
author: berenddeboer
 
47
'''
 
48
 
 
49
EXAMPLES = '''
 
50
# Install package foo
 
51
- portinstall: name=foo state=present
 
52
 
 
53
# Install package security/cyrus-sasl2-saslauthd
 
54
- portinstall: name=security/cyrus-sasl2-saslauthd state=present
 
55
 
 
56
# Remove packages foo and bar
 
57
- portinstall: name=foo,bar state=absent
 
58
'''
 
59
 
 
60
 
 
61
import json
 
62
import shlex
 
63
import os
 
64
import sys
 
65
 
 
66
def query_package(module, name):
 
67
 
 
68
    pkg_info_path = module.get_bin_path('pkg_info', False)
 
69
 
 
70
    # Assume that if we have pkg_info, we haven't upgraded to pkgng
 
71
    if pkg_info_path:
 
72
        pkgng = False
 
73
        pkg_glob_path = module.get_bin_path('pkg_glob', True)
 
74
        rc, out, err = module.run_command("%s -e `pkg_glob %s`" % (pkg_info_path, name))
 
75
    else:
 
76
        pkgng = True
 
77
        pkg_info_path = module.get_bin_path('pkg', True)
 
78
        pkg_info_path = pkg_info_path + " info"
 
79
        rc, out, err = module.run_command("%s %s" % (pkg_info_path, name))
 
80
 
 
81
    found = rc == 0
 
82
 
 
83
    if not found:
 
84
        # databases/mysql55-client installs as mysql-client, so try solving
 
85
        # that the ugly way. Pity FreeBSD doesn't have a fool proof way of checking
 
86
        # some package is installed
 
87
        name_without_digits = re.sub('[0-9]', '', name)
 
88
        if name != name_without_digits:
 
89
            if pkgng:
 
90
                rc, out, err = module.run_command("%s %s" % (pkg_info_path, name_without_digits))
 
91
            else:
 
92
                rc, out, err = module.run_command("%s `%s %s`" % (pkg_info_path, pkg_glob_path, name_without_digits))
 
93
 
 
94
        found = rc == 0
 
95
 
 
96
    return found
 
97
 
 
98
 
 
99
def matching_packages(module, name):
 
100
 
 
101
    ports_glob_path = module.get_bin_path('ports_glob', True)
 
102
    rc, out, err = module.run_command("%s %s | wc" % (ports_glob_path, name))
 
103
    parts = out.split()
 
104
    occurrences = int(parts[0])
 
105
    if occurrences == 0:
 
106
        name_without_digits = re.sub('[0-9]', '', name)
 
107
        if name != name_without_digits:
 
108
            rc, out, err = module.run_command("%s %s | wc" % (ports_glob_path, name_without_digits))
 
109
            parts = out.split()
 
110
            occurrences = int(parts[0])
 
111
    return occurrences
 
112
 
 
113
 
 
114
def remove_packages(module, packages):
 
115
 
 
116
    remove_c = 0
 
117
    pkg_glob_path = module.get_bin_path('pkg_glob', True)
 
118
 
 
119
    # If pkg_delete not found, we assume pkgng
 
120
    pkg_delete_path = module.get_bin_path('pkg_delete', False)
 
121
    if not pkg_delete_path:
 
122
        pkg_delete_path = module.get_bin_path('pkg', True)
 
123
        pkg_delete_path = pkg_delete_path + " delete -y"
 
124
 
 
125
    # Using a for loop incase of error, we can report the package that failed
 
126
    for package in packages:
 
127
        # Query the package first, to see if we even need to remove
 
128
        if not query_package(module, package):
 
129
            continue
 
130
 
 
131
        rc, out, err = module.run_command("%s `%s %s`" % (pkg_delete_path, pkg_glob_path, package))
 
132
 
 
133
        if query_package(module, package):
 
134
            name_without_digits = re.sub('[0-9]', '', package)
 
135
            rc, out, err = module.run_command("%s `%s %s`" % (pkg_delete_path, pkg_glob_path, name_without_digits))
 
136
            if query_package(module, package):
 
137
                module.fail_json(msg="failed to remove %s: %s" % (package, out))
 
138
 
 
139
        remove_c += 1
 
140
 
 
141
    if remove_c > 0:
 
142
 
 
143
        module.exit_json(changed=True, msg="removed %s package(s)" % remove_c)
 
144
 
 
145
    module.exit_json(changed=False, msg="package(s) already absent")
 
146
 
 
147
 
 
148
def install_packages(module, packages, use_packages):
 
149
 
 
150
    install_c = 0
 
151
 
 
152
    # If portinstall not found, automagically install
 
153
    portinstall_path = module.get_bin_path('portinstall', False)
 
154
    if not portinstall_path:
 
155
        pkg_path = module.get_bin_path('pkg', False)
 
156
        if pkg_path:
 
157
            module.run_command("pkg install -y portupgrade")
 
158
        portinstall_path = module.get_bin_path('portinstall', True)
 
159
 
 
160
    if use_packages == "yes":
 
161
        portinstall_params="--use-packages"
 
162
    else:
 
163
        portinstall_params=""
 
164
 
 
165
    for package in packages:
 
166
        if query_package(module, package):
 
167
            continue
 
168
 
 
169
        # TODO: check how many match
 
170
        matches = matching_packages(module, package)
 
171
        if matches == 1:
 
172
            rc, out, err = module.run_command("%s --batch %s %s" % (portinstall_path, portinstall_params, package))
 
173
            if not query_package(module, package):
 
174
                module.fail_json(msg="failed to install %s: %s" % (package, out))
 
175
        elif matches == 0:
 
176
            module.fail_json(msg="no matches for package %s" % (package))
 
177
        else:
 
178
            module.fail_json(msg="%s matches found for package name %s" % (matches, package))
 
179
 
 
180
        install_c += 1
 
181
 
 
182
    if install_c > 0:
 
183
        module.exit_json(changed=True, msg="present %s package(s)" % (install_c))
 
184
 
 
185
    module.exit_json(changed=False, msg="package(s) already present")
 
186
 
 
187
 
 
188
def main():
 
189
    module = AnsibleModule(
 
190
            argument_spec    = dict(
 
191
                state        = dict(default="present", choices=["present","absent"]),
 
192
                name         = dict(aliases=["pkg"], required=True),
 
193
                use_packages = dict(type='bool', default='yes')))
 
194
 
 
195
    p = module.params
 
196
 
 
197
    pkgs = p["name"].split(",")
 
198
 
 
199
    if p["state"] == "present":
 
200
        install_packages(module, pkgs, p["use_packages"])
 
201
 
 
202
    elif p["state"] == "absent":
 
203
        remove_packages(module, pkgs)
 
204
 
 
205
# this is magic, see lib/ansible/module_common.py
 
206
#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
 
207
 
 
208
main()