-
Notifications
You must be signed in to change notification settings - Fork 2
/
sjconf.in
executable file
·775 lines (693 loc) · 24.4 KB
/
sjconf.in
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
#!/usr/bin/python3
"""Command line interface for sjconf
sjconf is a configuration wrapper that handles global and server specific
configuration, deployment and service restart.
"""
__version__ = "@PACKAGE_VERSION@"
__all__ = []
import os
import sys
import pipes
import argparse
import subprocess
import sjconf
class ArgumentParserExtension(argparse.Action):
"""Custom action from 'argparse' that comes in order to modify the
behaviour of the parser, permitting thus the repeated use of some arguments
(sjconf actions arguments)."""
@staticmethod
def matcher(rule, candidate):
"""Custom matcher function."""
if type(rule) is str:
return rule == candidate
elif isinstance(rule, re._pattern_type):
return bool(rule.match(candidate))
elif isinstance(rule, tuple) or isinstance(rule, list):
return bool(candidate in rule)
else:
raise TypeError("Unsupported matching rule")
def __call__(self, parser, namespace, values, option_string=None):
"""Function called by the 'argparse' parser for the processing of the
user-given shell arguments before being append in the namespace as
list."""
getattr(namespace, self.dest).append(tuple(values))
def parse_arguments():
"""Parses and loads the user settings from the shell arguments"""
global args
# Init the argument parser and configure it properly
parser = argparse.ArgumentParser(
prog="sjconf",
description="%(prog)s is a configuration "
"wrapper that handles global and server "
"specific \nconfiguration, deployment and"
" service restart.\n\n"
"If sjconf is called without any "
"argument, the current configuration will"
" be \nshown.",
add_help=False,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
about_group = parser.add_argument_group("about sjconf")
about_group.add_argument(
"-h", "--help", action="help", help="show this help message and exit"
)
about_group.add_argument(
"-V",
"--version",
action="version",
help="print version and exit",
version="%(prog)s " + str(__version__) + "\n"
"Copyright © 2008-2021 CJI\n\n"
"License LGPLv2.1: GNU Lesser GPL version 2.1 \n"
"<http://www.gnu.org/licenses/old-licenses/lgpl-2"
".1.html>.\n"
"This is free software: you are free to change "
"and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by"
" law.",
)
behaviour_group = parser.add_argument_group("sjconf behaviour")
behaviour_group.add_argument(
"-v", "--verbose", action="store_true", help="be verbose"
)
behaviour_group.add_argument(
"-q",
"--quiet",
action="store_true",
help="be quiet: don't print anything to " "stdout",
)
editor_group = parser.add_argument_group("sjconf editor")
editor_group.add_argument(
"-e",
"--edit",
action="store_true",
help="open 'local.conf' with $EDITOR in order "
"to edit the sjconf manually (this option cannot"
" be mixed with other non-behavioural options)",
)
info_group = parser.add_argument_group("sjconf information")
info_group.add_argument(
"--get", nargs=2, metavar=("SECTION", "KEY"), help="get a configuration key"
)
info_group.add_argument(
"--list-plugins", action="store_true", help="list installed plugins"
)
info_group.add_argument(
"--list-profiles", action="store_true", help="list installed profiles"
)
commit_group = parser.add_argument_group("sjconf commit")
commit_group.add_argument(
"--save",
action="store_true",
help="save modifications (but do not deploy " "them)",
)
commit_group.add_argument(
"--deploy",
action="store_true",
help="deploy configuration (needs '--save' "
"parameter if the modifications are set in the"
"modifications are set in the same command "
"line)",
)
commit_group.add_argument(
"--no-backup",
action="store_true",
help="do not backup configuration files " "during deployment",
)
modifier_group = parser.add_argument_group("sjconf modifiers")
modifier_group.add_argument(
"--set",
default=[],
nargs=3,
metavar=("SECTION", "KEY", "VALUE"),
action=ArgumentParserExtension,
help="set a configuration key",
)
modifier_group.add_argument(
"--add-to-list",
default=[],
nargs=3,
metavar=("SECTION", "KEY", "VALUE"),
action=ArgumentParserExtension,
help="add a value to a configuration key " "(list)",
)
modifier_group.add_argument(
"--remove-from-list",
default=[],
nargs=3,
metavar=("SECTION", "KEY", "VALUE"),
action=ArgumentParserExtension,
help="remove a value from a configuration key " "(list)",
)
modifier_group.add_argument(
"--add-to-sequence",
default=[],
nargs=3,
metavar=("SECTION", "KEY", "VALUE"),
action=ArgumentParserExtension,
help="add a value to a configuration key " "(sequence)",
)
modifier_group.add_argument(
"--remove-from-sequence",
default=[],
nargs=3,
metavar=("SECTION", "KEY", "VALUE"),
action=ArgumentParserExtension,
help="remove a value from a configuration key " "(sequence)",
)
modifier_group.add_argument(
"--delete-key",
default=[],
nargs=2,
metavar=("SECTION", "KEY"),
action=ArgumentParserExtension,
help="delete a key within a section",
)
modifier_group.add_argument(
"--delete-section",
default=[],
nargs=1,
metavar=("SECTION",),
action=ArgumentParserExtension,
help="delete a complete section",
)
diff_group = parser.add_argument_group("sjconf diff")
diff_group.add_argument(
"--diff",
action="store_true",
help="show the differences ('diff -u1') "
"between the previous and the new (to be deployed "
"or not) configuration files (this option is "
"useful only associated with sjconf modifiers "
"options)",
)
extension_group = parser.add_argument_group("sjconf extensions")
extension_group.add_argument(
"--restart",
default=[],
nargs=1,
metavar=("PLUGIN",),
action=ArgumentParserExtension,
help="restart all services of PLUGIN (if "
'PLUGIN is "all", then all services from all '
"the plugins will be restarted)",
)
extension_group.add_argument(
"--reload",
default=[],
nargs=1,
metavar=("PLUGIN",),
action=ArgumentParserExtension,
help="reload all services of PLUGIN (if "
'PLUGIN is "all", then all services from all '
"the plugins will be reloaded)",
)
extension_group.add_argument(
"--install-plugin",
default=[],
nargs=1,
metavar=("PLUGIN",),
action=ArgumentParserExtension,
help="install PLUGIN into the plugins path",
)
extension_group.add_argument(
"--install-plugin-with-symlink",
default=[],
nargs=1,
metavar=("PLUGIN",),
action=ArgumentParserExtension,
help="install PLUGIN into the plugins path by" " using a symbolic link",
)
extension_group.add_argument(
"--uninstall-plugin",
default=[],
nargs=1,
metavar=("PLUGIN",),
action=ArgumentParserExtension,
help="uninstall PLUGIN from the plugins path",
)
extension_group.add_argument(
"--enable-plugin",
default=[],
nargs=1,
metavar=("PLUGIN",),
action=ArgumentParserExtension,
help="add PLUGIN to the list of plugins to " "use",
)
extension_group.add_argument(
"--disable-plugin",
default=[],
nargs=1,
metavar=("PLUGIN",),
action=ArgumentParserExtension,
help="remove PLUGIN from the list of plugins " "to use",
)
extension_group.add_argument(
"--install-template",
default=[],
nargs=1,
metavar=("TEMPLATE",),
action=ArgumentParserExtension,
help="install TEMPLATE into the templates " "path",
)
extension_group.add_argument(
"--install-template-with-symlink",
default=[],
nargs=1,
metavar=("TEMPLATE",),
action=ArgumentParserExtension,
help="install TEMPLATE into the templates " "path by using a symbolic link",
)
extension_group.add_argument(
"--uninstall-template",
default=[],
nargs=1,
metavar=("TEMPLATE",),
action=ArgumentParserExtension,
help="uninstall TEMPLATE from the templates " "path",
)
extension_group.add_argument(
"--install-conf",
default=[],
nargs=1,
metavar=("CONF",),
action=ArgumentParserExtension,
help='install CONF into the "base" ' "configuration directory",
)
extension_group.add_argument(
"--install-conf-with-symlink",
default=[],
nargs=1,
metavar=("CONF",),
action=ArgumentParserExtension,
help='install CONF into the "base" '
"configuration directory by using a symbolic "
"link",
)
extension_group.add_argument(
"--uninstall-conf",
default=[],
nargs=1,
metavar=("CONF",),
action=ArgumentParserExtension,
help="uninstall CONF from the base.conf file "
"(CONF should be the basename of a conf file "
"previously installed)",
)
extension_group.add_argument(
"--install-profile",
default=[],
nargs=1,
metavar=("PROFILE",),
action=ArgumentParserExtension,
help="install PROFILE file into the " "configuration directory",
)
extension_group.add_argument(
"--install-profile-with-symlink",
default=[],
nargs=1,
metavar=("PROFILE",),
action=ArgumentParserExtension,
help='install PROFILE into the "base" '
"configuration directory by using a symbolic "
"link",
)
extension_group.add_argument(
"--uninstall-profile",
default=[],
nargs=1,
metavar=("PROFILE",),
action=ArgumentParserExtension,
help="uninstall PROFILE file (PROFILE should"
" be the basename of a profile file "
"previously installed)",
)
extension_group.add_argument(
"--enable-profile",
default=[],
nargs=2,
metavar=("PROFILE", "LEVEL"),
action=ArgumentParserExtension,
help="enable the profile PROFILE at level " "LEVEL",
)
extension_group.add_argument(
"--disable-profile",
default=[],
nargs=1,
metavar=("PROFILE",),
action=ArgumentParserExtension,
help="disable the profile PROFILE",
)
# Run the argument parser
args = parser.parse_args()
#
# /!\ LITTLE HACK /!\
# We split args into the different argument groups (modifiers, information
# and actions) in order to make some semantic verification over the
# arguments that the user have passed.
behaviour_args = [elts.dest for elts in behaviour_group._group_actions]
editor_args = [elts.dest for elts in editor_group._group_actions]
info_args = [elts.dest for elts in info_group._group_actions]
commit_args = [elts.dest for elts in commit_group._group_actions]
modifier_args = [elts.dest for elts in modifier_group._group_actions]
diff_args = [elts.dest for elts in diff_group._group_actions]
extension_args = [elts.dest for elts in extension_group._group_actions]
# /!\ END OF LITTLE HACK /!\
#
# "Semantic" checks over the user arguments
if any([getattr(args, arg) for arg in editor_args]) and (
any([getattr(args, arg) for arg in info_args])
or any([getattr(args, arg) for arg in commit_args])
or any([getattr(args, arg) for arg in modifier_args])
or any([getattr(args, arg) for arg in diff_args])
or any([getattr(args, arg) for arg in extension_args])
):
parser.error(
"Sorry but you cannot call the sjconf editor with other "
"options. sjconf unchanged."
)
if any([getattr(args, arg) for arg in info_args]) and (
any([getattr(args, arg) for arg in commit_args])
or any([getattr(args, arg) for arg in modifier_args])
or any([getattr(args, arg) for arg in extension_args])
):
parser.error(
"Sorry but you cannot mix informative options with "
"options modifying sjconf. Please split your command. "
"sjconf unchanged."
)
if (
any([getattr(args, arg) for arg in modifier_args])
and args.deploy
and not args.save
):
parser.error(
"Please add the '--save' parameter to your command in "
"order to save your inlined changes before deploying "
"them. sjconf unchanged."
)
if args.verbose and args.quiet:
parser.error(
"You cannot mix the quiet and verbose option. sjconf " "unchanged."
)
if args.no_backup and not args.deploy:
parser.error(
"You cannot use the '--no-backup' option without "
"deploying changes. sjconf unchanged."
)
if any([getattr(args, arg) for arg in diff_args]) and not any(
[getattr(args, arg) for arg in modifier_args]
):
parser.error(
"You cannot use the '--diff' option without "
"making changes to sjconf with modifiers options. sjconf "
"unchanged."
)
if any([getattr(args, arg) for arg in modifier_args]) and any(
[getattr(args, arg) for arg in extension_args]
):
parser.error(
"Sorry but you cannot mix options that modify sjconf with"
" options that act upon sjconf extensions. Please split "
"your command. sjconf unchanged."
)
def sjprint(str_to_print):
"""Custom 'print' function"""
if not args.quiet:
print(str_to_print)
def sjconf_error(msg="Unknown error.", return_code=1):
"""Error handler for sjconf with shell return code"""
if not args.quiet:
print("sjconf: error: " + msg, file=sys.stderr)
sys.exit(return_code)
def print_plugin_info(plugin_info, printer=print):
"""Plugin information 'pretty' printer"""
plugin_instance = plugin_info["plugin"]
printer(
"Plugin %(name)s:\n"
" Version: %(version)s\n"
" Enabled: %(is_enabled)s"
% {
"name": plugin_instance.name(),
"version": str(plugin_instance.version()),
"is_enabled": "yes" if plugin_info["is_enabled"] else "no",
}
)
if plugin_instance.dependencies():
printer(" Dependencies:")
for dep_key, dep in plugin_info["dependencies"].items():
# Formatting the requirement string with the operators
# according to the plugin versions
requirements_str = ", ".join(
map(
lambda op_ver: op_ver[0] + " " + op_ver[1],
list(dep["dependency"].requirements.items()),
)
)
printer(
" Plugin %(plugin_name)s:\n"
" Required: %(is_required)s\n"
" Requirements: %(requirements)s\n"
" Installed version: %(installed_version)s"
% {
"plugin_name": str(dep_key),
"is_required": "yes" if not dep["dependency"].optional else "no",
"requirements": requirements_str,
"installed_version": str(dep["plugin"].version())
if dep["plugin"]
else "Not installed",
}
)
if dep["plugin"]:
printer(
" Enabled: %(is_enabled)s"
% {"is_enabled": "yes" if dep["is_enabled"] else "no"}
)
printer(
" State: %(state)s"
% {
"state": "OK"
if dep["state"] is True
else "Error: " + str(dep["state"])
}
)
def print_profile_info(profile_name, profile_level, printer=print):
"""Profile information 'pretty' printer"""
printer("Profile %s:" % profile_name)
if profile_level is not None:
printer(" Enabled at level %s" % profile_level)
else:
printer(" Not enabled")
def main():
"""Where the magic happens"""
global my_sjconf
# Instantiate the sjconf object with which we will work
my_sjconf = sjconf.SJConf(verbose=args.verbose, logger=sjprint)
#
# sjconf verbosity:
#
if args.verbose:
my_sjconf._load_conf_base()
my_sjconf._load_conf_local()
sjprint("Using base configuration file: %s" % my_sjconf.confs["base"].file_path)
sjprint(
"Using local configuration file: %s" % my_sjconf.confs["local"].file_path
)
#
# Launch the sjconf editor:
#
if args.edit:
my_sjconf._load_conf_local()
editor = os.getenv("EDITOR") or "vi"
editor_cmdline = [editor, pipes.quote(my_sjconf.confs["local"].file_path)]
subprocess.call(editor_cmdline)
#
# Display configuration:
#
if not any(vars(args).values()):
my_sjconf._load_conf_local()
sjprint(open(my_sjconf.confs["local"].file_path).read())
#
# List plugins (sorted by their names):
#
if args.list_plugins:
plugins_info = my_sjconf.plugins_infos()
sorted_plugin_keys = sorted(plugins_info)
for plugin_key in sorted_plugin_keys:
plugin_info = plugins_info[plugin_key]
print_plugin_info(plugin_info, printer=sjprint)
#
# List profiles (sorted by their names):
#
if args.list_profiles:
profiles_info = my_sjconf.profiles_infos()
sorted_profiles_info = sorted(profiles_info)
for profile_name in sorted_profiles_info:
profile_level = profiles_info[profile_name]
print_profile_info(profile_name, profile_level, printer=sjprint)
#
# Display sjconf values ('--get' argument):
#
if args.get:
section_to_get = args.get[0]
key_to_get = args.get[1]
if (
section_to_get in my_sjconf.conf_local()
and key_to_get in my_sjconf.conf_local()[section_to_get]
):
sjprint(my_sjconf.conf_local()[section_to_get][key_to_get])
else:
sjconf_error(
"Key %s in section %s not found." % (key_to_get, section_to_get)
)
#
# Install plugins, templates, confs or profiles:
#
if args.install_plugin:
for plugin_file in args.install_plugin:
my_sjconf.file_install("plugin", *plugin_file)
if args.install_plugin_with_symlink:
for plugin_file in args.install_plugin_with_symlink:
my_sjconf.file_install("plugin", *plugin_file, link=True)
if args.install_conf:
for conf_file in args.install_conf:
my_sjconf.file_install("conf", *conf_file)
if args.install_conf_with_symlink:
for conf_file in args.install_conf_with_symlink:
my_sjconf.file_install("conf", *conf_file, link=True)
if args.install_template:
for template_file in args.install_template:
my_sjconf.file_install("template", *template_file)
if args.install_template_with_symlink:
for template_file in args.install_template_with_symlink:
my_sjconf.file_install("template", *template_file, link=True)
if args.install_profile:
for profile_file in args.install_profile:
my_sjconf.file_install("profile", *profile_file)
if args.install_profile_with_symlink:
for profile_file in args.install_profile_with_symlink:
my_sjconf.file_install("profile", *profile_file, link=True)
#
# Uninstall plugins, templates, confs or profiles:
#
if args.uninstall_plugin:
for plugin in args.uninstall_plugin:
my_sjconf.file_uninstall("plugin", *plugin)
if args.uninstall_conf:
for conf in args.uninstall_conf:
my_sjconf.file_uninstall("conf", *conf)
if args.uninstall_template:
for template in args.uninstall_template:
my_sjconf.file_uninstall("template", *template)
if args.uninstall_profile:
for profile in args.uninstall_profile:
my_sjconf.file_uninstall("profile", *profile)
#
# Enable plugins:
#
if args.enable_plugin:
for plugin in args.enable_plugin:
my_sjconf.plugin_enable(*plugin)
#
# Disable plugins:
#
if args.disable_plugin:
for plugin in args.disable_plugin:
my_sjconf.plugin_disable(*plugin)
#
# Enable profiles:
#
if args.enable_profile:
for profile in args.enable_profile:
my_sjconf.profile_enable(*profile)
#
# Disable profiles:
#
if args.disable_profile:
for profile in args.disable_profile:
my_sjconf.profile_disable(*profile)
#
# Set sjconf values ('--set', '--add-to-list', '--remove-from-list',
# '--add-to-sequence', '--remove-from-sequence', '--delete-key',
# '--delete-section' arguments):
#
if (
args.set
or args.add_to_list
or args.remove_from_list
or args.add_to_sequence
or args.remove_from_sequence
or args.delete_key
or args.delete_section
):
my_sjconf.apply_conf_modifications(
temp=(not args.save),
sets=args.set,
list_adds=args.add_to_list,
list_removes=args.remove_from_list,
sequence_adds=args.add_to_sequence,
sequence_removes=args.remove_from_sequence,
delete_keys=args.delete_key,
delete_sections=args.delete_section,
)
#
# Show the diff between the currenf sjconf file and the temporary (the
# one which actually will be saved and/or deployed):
#
if args.diff:
diff_cmdline = [
"diff",
"-u1",
pipes.quote(my_sjconf.confs["local"].file_path),
pipes.quote(my_sjconf.temp_file_path),
]
process = subprocess.Popen(diff_cmdline, stdout=subprocess.PIPE)
proc_out, proc_err = process.communicate()
proc_returncode = process.poll()
if proc_returncode == 0:
sjprint(
"The diff result is empty. The two configuration files"
" are identical."
)
elif proc_returncode == 1:
sjprint("############### local.conf diffs ################")
sjprint(proc_out)
sjprint("#################################################")
else:
# This should not happen!
sjconf_error("diff returned an error exit status.")
if args.save:
sjprint("Configuration saved.")
else:
sjprint("No configuration saved (please use '--save').")
#
# Reload/Restart services
#
if not args.deploy and args.restart:
my_sjconf.restart_services(*args.restart)
if not args.deploy and args.reload:
my_sjconf.restart_services(*args.reload, reload=True)
#
# Deployment
#
if args.deploy:
# since deploy_conf() takes only lists of services names, we have to
# get rid of the tuples containing the services names
svc_names_to_restart = [svc[0] for svc in args.restart]
svc_names_to_reload = [svc[0] for svc in args.reload]
my_sjconf.deploy_conf(
services_to_restart=svc_names_to_restart,
services_to_reload=svc_names_to_reload,
backup=(not args.no_backup),
)
if __name__ == "__main__":
global args
try:
# Parse the shell arguments
parse_arguments()
# Hand over to the main function
main()
except sjconf.Error as exception:
if args.verbose:
raise
sjconf_error(str(exception))