-
Notifications
You must be signed in to change notification settings - Fork 5
/
forgejo_user.py
424 lines (350 loc) · 11.7 KB
/
forgejo_user.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2023, Bodo Schulz <[email protected]>
# Apache (see LICENSE or https://opensource.org/licenses/Apache-2.0)
from __future__ import absolute_import, print_function
import os
import re
from ansible.module_utils.basic import AnsibleModule
__metaclass__ = type
DOCUMENTATION = r"""
---
module: forgejo_user
author: Bodo 'bodsch' Schulz <[email protected]>
version_added: 1.0.0
short_description: Forgejo User handling.
description:
- Forgejo User handling.
options:
state:
description:
- (C(present))
- (C(absent))
- (C(list))
- (C(check))
required: true
default: present
admin:
description: TBD
required: false
type: bool
default: false
username:
description: TBD
required: false
type: str
password:
description: TBD
required: false
type: str
no_log: true
email:
description: TBD
required: false
type: str
working_dir:
description: TBD
required: true
type: str
config:
description: TBD
required: false
default: /etc/forgejo/forgejo.ini
type: str
"""
EXAMPLES = r"""
- name: list forgejo users
remote_user: "{{ forgejo_system_user }}"
become_user: "{{ forgejo_system_user }}"
become: true
bodsch.scm.forgejo_user:
state: list
- name: check forgejo admin user '{{ forgejo_admin_user.username }}'
remote_user: "{{ forgejo_system_user }}"
become_user: "{{ forgejo_system_user }}"
become: true
bodsch.scm.forgejo_user:
state: check
username: "{{ forgejo_admin_user.username }}"
register: forgejo_admin_user_present
- name: create admin user
remote_user: "{{ forgejo_system_user }}"
become_user: "{{ forgejo_system_user }}"
become: true
bodsch.scm.forgejo_user:
admin: true
username: "{{ forgejo_admin_user.username }}"
password: "{{ forgejo_admin_user.password }}"
email: "{{ forgejo_admin_user.email }}"
"""
RETURN = r"""
"""
# ----------------------------------------------------------------------
class ForgejoUser(object):
"""
"""
module = None
def __init__(self, module):
"""
"""
self.module = module
self.state = module.params.get("state")
self.admin = module.params.get("admin")
self.username = module.params.get("username")
self.password = module.params.get("password")
self.email = module.params.get("email")
self.working_dir = module.params.get("working_dir")
self.config = module.params.get("config")
self.forgejo_bin = module.get_bin_path('forgejo', True)
def run(self):
"""
"""
result = dict(
changed=False,
failed=True
)
if os.path.isdir(self.working_dir):
os.chdir(self.working_dir)
existing_users = self.list_users()
# self.module.log(msg=f" existing_users : '{existing_users}'")
if self.state == "list":
result = dict(
changed=False,
failed=False,
msg=existing_users
)
if self.state == "check":
if not existing_users.get(self.username):
result = dict(
changed=False,
failed=False,
msg=f"User {self.username} is not created.",
rc=1
)
else:
result = dict(
changed=False,
failed=False,
msg=f"User {self.username} is present.",
rc=0
)
if self.state == "present":
if not existing_users.get(self.username):
result = self.add_user()
else:
result = dict(
changed=False,
msg=f"user {self.username} already created."
)
if self.state == "absent":
result["msg"] = "This part is currently not supported."
return result
def list_users(self):
"""
"""
result = {}
args_list = [
self.forgejo_bin,
"admin",
"user",
"list",
"--work-path", self.working_dir,
"--config", self.config,
]
# self.module.log(msg=f" args_list : '{args_list}'")
rc, out, err = self._exec(args_list)
outer_pattern = re.compile(r".*ID\s+Username\s+Email\s+IsActive\s+IsAdmin\s+2FA\n(?P<data>.*)", flags=re.MULTILINE | re.DOTALL)
inner_pattern = re.compile(r"(?P<ID>\d+)\s+(?P<username>\w+)\s+(?P<email>[a-zA-Z+_\-@\.]+)\s+(?P<active>\w+)\s+(?P<admin>\w+)\s+(?P<twofa>\w+)", flags=re.MULTILINE | re.DOTALL)
outer_re_result = re.search(outer_pattern, out)
if outer_re_result:
data = outer_re_result.group("data")
inner_re_result = re.finditer(inner_pattern, data)
if inner_re_result:
for x in inner_re_result:
if x.group("username"):
username = x.group("username")
if x.group("email"):
email = x.group("email")
else:
email = "unknown"
if x.group("active"):
active = bool(x.group("active"))
else:
active = False
if x.group("admin"):
admin = bool(x.group("admin"))
else:
admin = False
result[str(username)] = dict(
email=email,
active=active,
admin=admin
)
return result
def add_user(self):
"""
forgejo admin user create --admin --username root --password admin1234 --email [email protected]
"""
args_list = [
self.forgejo_bin,
"admin",
"user",
"create",
"--work-path", self.working_dir,
"--config", self.config,
]
if self.admin:
args_list.append("--admin")
args_list += [
"--username", self.username,
"--password", self.password,
"--email", self.email
]
# self.module.log(msg=f" args_list : '{args_list}'")
rc, out, err = self._exec(args_list)
if rc == 0:
return dict(
failed=False,
changed=True,
msg=f"user {self.username} successful created."
)
else:
return dict(
failed=True,
msg=err
)
def _exec(self, commands, check_rc=True):
"""
"""
rc, out, err = self.module.run_command(commands, check_rc=check_rc)
# self.module.log(msg=f" rc : '{rc}'")
if rc != 0:
self.module.log(msg=f" out: '{out}'")
self.module.log(msg=f" err: '{err}'")
return rc, out, err
def main():
"""
"""
specs = dict(
state=dict(
default="present",
choices=[
"present",
"absent",
"list",
"check"
]
),
admin=dict(
required=False,
type=bool,
default=False
),
username=dict(
required=False,
type=str
),
password=dict(
required=False,
type=str,
no_log=True,
),
email=dict(
required=False,
type=str
),
working_dir=dict(
required=False,
default="/var/lib/forgejo",
type=str
),
config=dict(
required=False,
default="/etc/forgejo/forgejo.ini",
type=str
)
)
module = AnsibleModule(
argument_spec=specs,
supports_check_mode=False,
)
params = module.params
_state = params.get("state")
if _state in ["present", "absent", "check"]:
res_args = dict(
rc=1,
changed=False
)
_username = params.get("username", None)
_password = params.get("password", None)
_email = params.get("email", None)
if _state == "present":
_missing = []
if _username is None:
_missing.append("username")
if _password is None:
_missing.append("password")
if _email is None:
_missing.append("email")
if len(_missing) > 0:
_missing = ", ".join(_missing)
res_args['msg'] = f"missing required arguments: {_missing}"
module.exit_json(**res_args)
if _state in ["absent", "check"]:
if _username is None:
res_args['msg'] = "missing required arguments: username"
module.exit_json(**res_args)
kc = ForgejoUser(module)
result = kc.run()
module.log(msg=f"= result : '{result}'")
module.exit_json(**result)
# import module snippets
if __name__ == '__main__':
main()
"""
root@instance:/# forgejo --help
NAME:
Forgejo - A painless self-hosted Git service
USAGE:
forgejo [global options] command [command options] [arguments...]
VERSION:
1.19.0 built with GNU Make 4.1, go1.20.2 : bindata, sqlite, sqlite_unlock_notify
DESCRIPTION:
By default, forgejo will start serving using the webserver with no
arguments - which can alternatively be run by running the subcommand web.
COMMANDS:
web Start Forgejo web server
serv This command should only be called by SSH shell
hook Delegate commands to corresponding Git hooks
dump Dump Forgejo files and database
cert Generate self-signed certificate
admin Command line interface to perform common administrative operations
generate Command line interface for running generators
migrate Migrate the database
keys This command queries the Forgejo database to get the authorized command for a given ssh key fingerprint
convert Convert the database
doctor Diagnose and optionally fix problems
manager Manage the running forgejo process
embedded Extract embedded resources
migrate-storage Migrate the storage
docs Output CLI documentation
dump-repo Dump the repository from git/github/forgejo/gitlab
restore-repo Restore the repository from disk
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--port value, -p value Temporary port number to prevent conflict (default: "3000")
--install-port value Temporary port number to run the install page on to prevent conflict (default: "3000")
--pid value, -P value Custom pid file path (default: "/run/forgejo.pid")
--quiet, -q Only display Fatal logging errors until logging is set-up
--verbose Set initial logging to TRACE level until logging is properly set-up
--custom-path value, -C value Custom path file path (default: "/usr/bin/custom")
--config value, -c value Custom configuration file path (default: "/usr/bin/custom/conf/app.ini")
--version, -v print the version
--work-path value, -w value Set the forgejo working path (default: "/usr/bin")
--help, -h show help
DEFAULT CONFIGURATION:
CustomPath: /usr/bin/custom
CustomConf: /usr/bin/custom/conf/app.ini
AppPath: /usr/bin/forgejo
AppWorkPath: /usr/bin
"""