This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
aur
executable file
·194 lines (161 loc) · 6.15 KB
/
aur
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
#!/usr/bin/python
# The MIT License (MIT)
#
# Copyright (c) 2014 Austin Hyde
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os
import pwd
import platform
def cower_in_path(module):
"""
Determine if cower is available.
"""
rc, stdout, stderr = module.run_command('which cower', check_rc=False)
return rc == 0
def pacman_in_path(module):
"""
Determine if pacman is available.
"""
rc, stdout, stderr = module.run_command('which pacman', check_rc=False)
return rc == 0
def package_installed(module, pkg):
"""
Determine if a package is already installed.
"""
rc, stdout, stderr = module.run_command('pacman -Q %s' % pkg, check_rc=False)
return rc == 0
def check_packages(module, pkgs):
"""
Inform the user what would change if the module were run.
"""
would_be_changed = []
for pkg in pkgs:
installed = package_installed(module, pkg)
if not installed:
would_be_changed.append(pkg)
if would_be_changed:
module.exit_json(changed=True, msg='%s package(s) would be installed' % (len(would_be_changed)))
else:
module.exit_json(changed=False, msg='all packages are already installed')
def download_packages(module, pkgs, dir, user):
"""
Download the specified packages.
"""
# Use cower, if available.
if cower_in_path(module):
cmds = ['sudo -u %s cower -dqf %s', ]
# Otherwise, fall back to cURL
else:
cmds = ['sudo -u %s curl -O https://aur.archlinux.org/cgit/aur.git/snapshot/%s.tar.gz',
'sudo -u %s tar xzf %s.tar.gz']
for pkg in pkgs:
# If the package is already installed, skip the download.
if package_installed(module, pkg):
continue
# Change into the specified directory for download.
os.chdir(dir)
# Attempt to install the package.
for cmd in cmds:
rc, stdout, stderr = module.run_command(cmd % (user, pkg), check_rc=False)
if rc != 0:
module.fail_json(msg='failed to download package %s, because: %s' % (pkg,stderr))
def install_packages(module, pkgs, dir, user, virtual):
"""
Install the specified packages via makepkg.
"""
num_installed = 0
if platform.machine().startswith('arm'):
makepkg_args = '-Acsri'
else:
makepkg_args = '-csri'
cmd = 'sudo -u %s PKGEXT=".pkg.tar" makepkg %s --noconfirm --needed --noprogressbar' % (user, makepkg_args)
if module.params['skip_pgp']:
cmd = ' --skippgpcheck'
for pkg in pkgs:
# If the package is already installed, skip the install.
if package_installed(module, pkg):
continue
# Change into the package directory.
# Check if the package is a virtual package
if virtual:
os.chdir(os.path.join(dir, virtual))
else:
os.chdir(os.path.join(dir, pkg))
# Attempt to install the directory
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
if rc != 0:
module.fail_json(msg='failed to install package %s, because: %s' % (pkg,stderr))
num_installed = 1
# Exit with the number of packages succesfully installed.
if num_installed > 0:
module.exit_json(changed=True, msg='installed %s package(s)' % num_installed)
else:
module.exit_json(changed=False, msg='all packages were already installed')
def main():
module = AnsibleModule(
argument_spec = dict(
name = dict(required=True),
user = dict(required=True),
dir = dict(),
skip_pgp = dict(default=False, type='bool'),
virtual = dict(),
),
supports_check_mode = True
)
# Fail of pacman is not available.
if not pacman_in_path(module):
module.fail_json(msg="could not locate pacman executable")
p = module.params
# Get all the requested package names.
pkgs = p['name'].split(',')
# Fail if the specified user does not exist.
try:
pwd.getpwnam(p['user'])
except KeyError:
module.fail_json(msg="user %s does not exist" % p['user'])
else:
user = p['user']
# If no directory was given, assume the packages should be downloaded to
# ~user/aur.
if not p['dir']:
home = os.path.expanduser('~%s' % user)
if not os.path.exists(home):
module.fail_json(msg="%s's home directory %s does not exist" % (user, home))
dir = os.path.join(home, 'aur')
if not os.path.exists(dir):
os.makedirs(dir)
uid = pwd.getpwnam(user).pw_uid
os.chown(dir, uid, -1)
else:
dir = os.path.expanduser(p['dir'])
# Fail if the specified directory does not exist.
if not os.path.exists(dir):
module.fail_json(msg="directory %s does not exist" % dir)
if module.check_mode:
check_packages(module, pkgs)
download_packages(module, pkgs, dir, user)
# Check if the package is virtual
if p['virtual']:
virtual = p['virtual']
else:
virtual = False
install_packages(module, pkgs, dir, user, virtual)
from ansible.module_utils.basic import *
main()