-
Notifications
You must be signed in to change notification settings - Fork 3
/
luarocks.py
254 lines (214 loc) · 7.04 KB
/
luarocks.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2016, Rahul AG <r@hul.ag>
# (c) 2018, Ahmad Amireh <ahmad@instructure.com>
#
# This program 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.
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
import re
# Import module snippets
from ansible.module_utils.basic import AnsibleModule
ANSIBLE_METADATA = {
'status': ['preview'],
'supported_by': 'community',
'version': '1.0.2',
}
DOCUMENTATION = '''
---
module: luarocks
short_description: Manage lua rocks with luarocks
description:
- Manage lua rocks with luarocks
options:
deps_mode:
description:
- How to handle dependencies (corresponds to the --deps-mode flag of luarocks)
required: false
choices: [ "all", "one", "order", "none" ]
executable:
description:
- The name of the luarocks executable to use
required: false
keep_other_versions:
description:
- Keep other versions of the rock
required: false
default: no
choices: [ "yes", "no" ]
local:
description:
- Use the local luarocks tree
required: false
default: no
choices: [ "yes", "no" ]
name:
description:
- The name of a luarocks package to install
required: true
override_servers:
description:
- Whether to override the servers specified in the luarocks config
required: false
default: no
state:
description:
- The state of the lua rocks
required: false
default: present
choices: [ "present", "absent", "only_deps" ]
server:
description:
- A server URI to fetch rocks and rockspecs from
required: false
tree:
description:
- The luarocks tree to operate on
required: false
version:
description:
- The version to be installed
required: false
'''
EXAMPLES = '''
- name: Install the "luasocket" rock.
luarocks:
name: luasocket
- name: Install the "luasocket" rock from a specific server.
luarocks:
name: luasocket
server: http://http://rocks.moonscript.org
- name: Install the "luasocket" rock into your local tree.
luarocks:
name: luasocket
local: yes
- name: Install version 2.0.2 of the "luasocket" rock.
luarocks:
name: luasocket
version: '2.0.2'
- name: Remove the "luasocket" rock.
luarocks:
name: luasocket
state: absent
'''
class Luarocks(object):
def __init__(self, module, **kwargs):
self.module = module
self.executable = kwargs['executable']
self.name = kwargs['name']
self.server = kwargs['server']
self.state = kwargs['state']
self.override_servers = kwargs['override_servers']
self.tree = kwargs['tree']
self.local = kwargs['local']
self.keep_other_versions = kwargs['keep_other_versions']
self.version = kwargs['version']
self.deps_mode = kwargs['deps_mode']
def _exec(self, args, run_in_check_mode=False, check_rc=True):
if not self.module.check_mode or (self.module.check_mode and run_in_check_mode):
cmd = [self.executable]
if self.local and self.tree:
self.module.fail_json(msg='cannot set local=True and tree')
if self.local:
cmd.append('--local')
if self.tree:
cmd.append('--tree={}'.format(self.tree))
if self.server:
if self.override_servers:
s = '--only-'
else:
s = '--'
cmd.append('{}server={}'.format(s, self.server))
cmd.extend(args)
if self.name:
cmd.append(self.name)
if self.version:
cmd.append(self.version)
rc, out, err = self.module.run_command(cmd, check_rc=check_rc)
return out
return ''
def list(self):
cmd = ['list', '--porcelain']
out = self._exec(cmd, run_in_check_mode=True, check_rc=False)
installed = re.findall(r'^(\S+)\s+\S+\s+installed\s+\S+', out, re.MULTILINE)
return installed
def install(self):
cmd = ['install']
if self.keep_other_versions:
cmd.append('--keep')
if self.deps_mode:
cmd.append('--deps-mode={}'.format(self.deps_mode))
if self.state == 'only_deps':
cmd.append('--only-deps')
return self._exec(cmd)
def remove(self):
cmd = ['remove']
if self.deps_mode:
cmd.append('--deps-mode={}'.format(self.deps_mode))
return self._exec(cmd)
def main():
arg_spec = dict(
executable=dict(default='luarocks', required=False),
name=dict(default=None),
state=dict(default='present', choices=['present', 'absent', 'only_deps', ]),
version=dict(default=None, required=False),
keep_other_versions=dict(default=False, required=False, type='bool'),
local=dict(default=False, required=False, type='bool'),
tree=dict(default=None, required=False, type='path'),
server=dict(default=None, required=False),
override_servers=dict(default=False, required=False, type='bool'),
deps_mode=dict(default=None, choices=['all', 'one', 'order', 'none', ]),
)
module = AnsibleModule(
argument_spec=arg_spec,
supports_check_mode=True,
)
executable = module.params['executable']
name = module.params['name']
state = module.params['state']
version = module.params['version']
keep_other_versions = module.params['keep_other_versions']
local = module.params['local']
tree = module.params['tree']
server = module.params['server']
override_servers = module.params['override_servers']
deps_mode = module.params['deps_mode']
luarocks = Luarocks(
module,
executable=executable,
name=name,
version=version,
keep_other_versions=keep_other_versions,
local=local,
tree=tree,
server=server,
state=state,
override_servers=override_servers,
deps_mode=deps_mode,
)
changed = False
if state == 'only_deps':
out = luarocks.install()
missing_deps = re.search(r'^Missing dependencies for .+:\n', out)
changed = missing_deps != None
elif state == 'present':
installed = luarocks.list()
if name not in installed:
changed = True
luarocks.install()
else: # Absent
installed = luarocks.list()
if name in installed:
changed = True
luarocks.remove()
module.exit_json(changed=changed)
if __name__ == '__main__':
main()