-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmakepkg
371 lines (292 loc) · 12 KB
/
makepkg
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
#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
# (c) 2015, Ross Williams <http://github.com/gunzy83>
#
# 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/>.
DOCUMENTATION = '''
---
module: makepkg
short_description: Build and install packages with I(makepkg) and I(pacman)
description:
- Build packages with the I(makepkg) and install with I(pacman) package manager, which is used by
Arch Linux and its variants.
version_added: "1.8.2"
author: Ross Williams
notes: []
requirements: [base-devel]
options:
name:
description:
- Name of the AUR package to build and install or remove. Can also
be the path to a source package to be built.
required: true
default: null
state:
description:
- Desired state of the package.
required: true
default: "present"
choices: ["present", "absent"]
recurse:
description:
- When removing a package, also remove its dependencies, provided
that they are not required by other packages and were not
explicitly installed by a user.
required: false
default: "no"
choices: ["yes", "no"]
as_deps:
description:
- Whether or not to install the package with the **--asdeps** flag.
Useful for installing optional or AUR dependencies of AUR packages.
required: false
default: "no"
choices: ["yes", "no"]
build_dir:
description:
- Directory to extract source tarball for building the package
required: false
default: "/var/cache/pacman/pkg"
'''
EXAMPLES = '''
# Build and Install package aurvote
- makepkg: name=aurvote state=present
# Remove package aurvote
- makepkg: name=aurvote state=absent
# Recursively remove package aurvote
- makepkg: name=aurvote state=absent recurse=yes
# Build and Install package aurvote as a dependency
- makepkg: name=aurvote state=present as_deps=yes
# Build in a custom build directory
- makepkg: name=aurvote state=present build_dir="~/builds"
'''
import json
import os
import re
import pwd
import grp
import shutil
import urllib
from ansible.module_utils.six.moves.urllib.parse import urlunsplit
PACMAN_PATH = "/usr/bin/pacman"
MAKEPKG_PATH = "/usr/bin/makepkg"
PACKAGE_CACHE = "/var/cache/pacman/pkg"
AUR_SCHEME = "https"
AUR_NETLOC = "aur.archlinux.org"
AUR_RPC_PATH = "rpc.php"
AUR_SNAPSHOT_PATH = "cgit/aur.git/snapshot"
def query_package_dir(module, name, version):
matcher = re.compile('%s-%s[\.xany0-9_-]*\.pkg\.tar\.xz$' % (name,version))
for filename in os.listdir(PACKAGE_CACHE):
if matcher.match(filename):
return os.path.join(PACKAGE_CACHE,filename)
return None
def query_package(module, name):
# pacman -Q returns 0 if the package is installed,
# 1 if it is not installed
cmd = "pacman -Q %s" % (name)
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
if rc == 0:
return True
return False
def remove_package(module, pkg):
# Query the package first, to see if we even need to remove
if not query_package(module, pkg):
module.exit_json(changed=False, msg="package(s) already absent")
if module.params["recurse"]:
args = "Rs"
else:
args = "R"
cmd = "pacman -%s %s --noconfirm" % (args, pkg)
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
if rc != 0:
module.fail_json(msg="failed to remove package %s" % pkg)
module.exit_json(changed=True, msg="removed package %s" % pkg)
def prepare_build_dir(module, pkg_name, sudo_user):
build_dir = module.params['build_dir']
uid = pwd.getpwnam(sudo_user).pw_uid
gid = grp.getgrnam('users').gr_gid
directory = os.path.join(build_dir, pkg_name)
if not os.path.exists(directory):
try:
os.mkdir(directory, 0755)
except OSError:
module.fail_json(msg="failed to create build directory %s" % (directory))
if os.stat(directory).st_uid != uid:
try:
os.chown(directory,uid,gid)
except OSError as e:
module.fail_json(msg="failed to change owner of build directory %s" % (e.strerror))
return directory
def make_package(module, pkg, pkg_file_src):
sudo_user = os.environ.get('SUDO_USER')
# Build as user, not as root
command_prefix = "sudo -u %s " % sudo_user
build_dir = prepare_build_dir(module, pkg, sudo_user)
cmd = ""
pkg_file_basename = "%s.tar.gz" % pkg
pkg_file_dest = os.path.join(build_dir, pkg_file_basename)
if pkg_file_src:
# copy file to build directory
try:
shutil.copy2(pkg_file_src, build_dir)
except Exception, err:
module.fail_json(msg="failed to copy package %s to build directory %s: %s" % (pkg_file_src, build_dir, str(err)))
else:
aur_req = urlunsplit((AUR_SCHEME, AUR_NETLOC, "%s/%s" % (AUR_SNAPSHOT_PATH, pkg_file_basename), "", ""))
rsp, info = fetch_url(module, aur_req)
if info['status'] != 200:
module.fail_json(msg="Request failed", url=aur_req, status_code=info['status'], response=info['msg'])
# save response to archive
f = open(pkg_file_dest, 'wb')
try:
shutil.copyfileobj(rsp, f)
except Exception, err:
os.remove(pkg_file_dest)
module.fail_json(msg="failed to create package archive file: %s" % str(err))
f.close()
rsp.close()
pkg_file = pkg_file_dest
if pkg_file:
cmd = '%star --strip-components=1 -xf %s.tar.gz' % (command_prefix, pkg)
rc, stdout, stderr = module.run_command(cmd, check_rc=False, cwd=build_dir)
if rc != 0:
module.fail_json(msg="failed to extract %s for pkg %s" % (pkg_file, pkg), stderr=stderr)
cmd = '%smakepkg -scf --noconfirm' % command_prefix
rc, stdout, stderr = module.run_command(cmd, check_rc=False, cwd=build_dir)
if rc != 0:
module.fail_json(msg="failed to build package %s in %s from %s" % (pkg, pkg_file, build_dir), stderr=stderr)
found_file = None
matcher = re.compile('%s-.*\.pkg\.tar\.xz$' % pkg)
for filename in os.listdir(build_dir):
# check if filename not found, if not we need to fail out
if matcher.match(filename):
found_file = filename
if not found_file:
module.fail_json(msg="failed to find the built package for %s" % pkg)
# Keep package in cache for downgrades later. Use shutil.copy since we
# don't care about preserving permissions.
try:
shutil.copy(os.path.join(build_dir, found_file), PACKAGE_CACHE)
except Exception, err:
module.fail_json("Failed to copy package %s to cache %s: %s" % (found_file, PACKAGE_CACHE, str(err)))
# Don't leave build files lying around
try:
shutil.rmtree(build_dir)
except Exception, err:
module.fail_json(msg="failed to remove temporary build directory %s: %s" % (build_dir, str(err)))
return os.path.join(PACKAGE_CACHE,found_file)
def check_build_environment(module):
user = os.environ.get('USER')
sudo_user = os.environ.get('SUDO_USER')
build_dir = module.params['build_dir']
# We want to build as user, not as root
if not sudo_user:
module.fail_json(msg="building as root not permitted, run as user and use sudo")
# Check if we need to be able to install pkg and fail out if we can't
if user != 'root':
module.fail_json(msg="sudo required to install packages")
if not os.path.exists(build_dir):
module.fail_json(msg="base build directory %s does not exist" % build_dir)
if not os.access(build_dir, os.W_OK):
module.fail_json(msg="base build directory %s is not accessible" % build_dir)
def install_package(module, pkg, pkg_file):
check_build_environment(module)
if query_package(module, pkg):
module.exit_json(changed=False, msg="package already installed", package=pkg)
if not pkg_file:
# this is an aur pkg
rpc_params = urllib.urlencode({"type": "info", "arg": pkg})
rpc_req = urlunsplit((AUR_SCHEME, AUR_NETLOC, AUR_RPC_PATH, rpc_params, ""))
rsp, info = fetch_url(module, rpc_req)
# create a temporary file and copy content to do checksum-based replacement
if info['status'] != 200:
module.fail_json(msg="Request failed", status_code=info['status'], response=info['msg'], url=rpc_req)
pkg_info = json.load(rsp)
if pkg_info['resultcount'] < 1:
module.fail_json(msg="AUR query found no matching packages", package=pkg)
# check pkg cache before install
pkg_ver = pkg_info['results']['Version']
pkg_path = query_package_dir(module, pkg, pkg_ver)
if not pkg_path:
pkg_path = make_package(module, pkg, None)
else:
pkg_path = make_package(module, pkg, pkg_file)
if module.params['as_deps']:
params = '-U --asdeps %s' % pkg_path
else:
params = '-U %s' % pkg_path
cmd = "pacman %s --noconfirm" % (params)
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
if rc != 0:
module.fail_json(msg="failed to install package", pkg=pkg, stderr=stderr)
module.exit_json(changed=True, msg="installed package", pkg=pkg)
def check_package(module, pkg, state):
changed = False
installed = query_package(module, pkg)
if (state == "present" and not installed):
# if the build would fail because of the state of the system we want to know
check_build_environment(module)
state = "installed"
changed = True
if (state == "absent" and installed):
state = "removed"
changed = True
if changed:
module.exit_json(changed=True, msg="package would be %s" % state, package=pkg)
else:
module.exit_json(changed=False, msg="package already %s" % state, package=pkg)
def main():
argument_spec = dict(
name = dict(aliases=['pkg'], required=True),
state = dict(choices=['installed', 'present', 'absent', 'removed'], required=True),
recurse = dict(default='no', choices=BOOLEANS, type='bool'),
as_deps = dict(default='no', choices=BOOLEANS, type='bool'),
build_dir = dict(default=PACKAGE_CACHE)
)
module = AnsibleModule(
argument_spec = argument_spec,
required_one_of = [['name']],
supports_check_mode = True
)
if not os.path.exists(PACMAN_PATH):
module.fail_json(msg="cannot find pacman, looking for %s" % (PACMAN_PATH))
if not os.path.exists(MAKEPKG_PATH):
module.fail_json(msg="cannot find makepkg, looking for %s" % (MAKEPKG_PATH))
p = module.params
# normalize the state parameter
if p['state'] in ['present', 'installed']:
p['state'] = 'present'
elif p['state'] in ['absent', 'removed']:
p['state'] = 'absent'
pkg = p['name']
pkg_file = None
if pkg.endswith('.tar.gz'):
# The package given is a filename, extract the raw pkg name from
# it and store the filename
pkg_file = pkg
pkg = os.path.basename(pkg)[:-1 * len('.tar.gz')]
if module.check_mode:
check_package(module, pkg, p['state'])
if p['state'] == 'present':
install_package(module, pkg, pkg_file)
elif p['state'] == 'absent':
remove_package(module, pkg)
# Used for downloading packages from the AUR
from ansible.module_utils.urls import *
# import module snippets
from ansible.module_utils.basic import *
main()