Debian Codegraph @

src/lib389/cli/dscreate

raw
1 #!/usr/bin/python3
2
3 # --- BEGIN COPYRIGHT BLOCK ---
4 # Copyright (C) 2020 Red Hat, Inc.
5 # All rights reserved.
6 #
7 # License: GPL (version 3 or any later version).
8 # See LICENSE for details.
9 # --- END COPYRIGHT BLOCK ---
10 #
11 # PYTHON_ARGCOMPLETE_OK
12
13 import argparse, argcomplete
14 import sys
15 import signal
16 import json
17 from textwrap import dedent
18 from lib389 import DirSrv
19 from lib389.cli_ctl import instance as cli_instance
20 from lib389.cli_base import setup_script_logger
21 from lib389.cli_base import format_error_to_dict, format_pretty_error
22
23
24 epilog = """
25 Example of install by a non root user:
26 PATH=$HOME/bin:$PATH # Should be in .profile
27 dscreate ds-root $HOME/mydsroot $HOME/bin
28 hash -d dscreate # bash command to insure new dscreate wrapper will be used
29 dscreate interactive
30 # Note: Make sure to use non priviledged port number (i.e > 1000)
31 """
32
33 parser = argparse.ArgumentParser(epilog=dedent(epilog), formatter_class=argparse.RawDescriptionHelpFormatter)
34 parser.add_argument('-v', '--verbose',
35 help="Display verbose operation tracing during command execution",
36 action='store_true', default=False, dest='verbose')
37 parser.add_argument('-j', '--json',
38 help="Return the result as a json message",
39 action='store_true', default=False, dest='json')
40 subparsers = parser.add_subparsers(help="action")
41
42 fromfile_parser = subparsers.add_parser('from-file', help="Create an instance of Directory Server from an inf answer file")
43 fromfile_parser.add_argument('file', help="Inf file to use with prepared answers. You can generate an example of this with 'dscreate create-template'")
44 fromfile_parser.add_argument('-n', '--dryrun', help="Validate system and configurations only. Do not alter the system.",
45 action='store_true', default=False)
46 fromfile_parser.set_defaults(func=cli_instance.instance_create)
47
48 interactive_parser = subparsers.add_parser('interactive', help="Start interactive installer for Directory Server installation")
49 interactive_parser.set_defaults(func=cli_instance.instance_create_interactive)
50
51 template_parser = subparsers.add_parser('create-template', help="Display an example inf answer file, or provide a file name to write it to disk.")
52 template_parser.add_argument('--advanced', action='store_true', default=False,
53 help="Add advanced options to the template - changing the advanced options may make your instance install fail")
54 template_parser.add_argument('template_file', nargs="?", default=None, help="Write example template to this file")
55 template_parser.set_defaults(func=cli_instance.instance_example)
56
57 subtree_parser = subparsers.add_parser('ds-root', help="Prepare a root directory in which non root user can create, run and administer instances.")
58 subtree_parser.add_argument('root_dir', default=None, help="A directory that will be used as virtual root.")
59 subtree_parser.add_argument('bin_dir', nargs="?", default=None, help="A directory in which administration wrappers are installed. (Should be in PATH)")
60 subtree_parser.set_defaults(func=cli_instance.prepare_ds_root)
61 argcomplete.autocomplete(parser)
62
63 # handle a control-c gracefully
64 def signal_handler(signal, frame):
65 print('\n\nExiting interactive installation...')
66 sys.exit(0)
67
68
69 if __name__ == '__main__':
70 args = parser.parse_args()
71
72 log = setup_script_logger("dscreate", args.verbose)
73
74 log.debug("The 389 Directory Server Creation Tool")
75 # Leave this comment here: UofA let me take this code with me provided
76 # I gave attribution. -- wibrown
77 log.debug("Inspired by works of: ITS, The University of Adelaide")
78
79 log.debug("Called with: %s", args)
80
81 # Assert we have a resources to work on.
82 if not hasattr(args, 'func'):
83 errmsg = "No action provided, here is some --help."
84 if args.json:
85 sys.stderr.write('{"desc": "%s"}\n' % errmsg)
86 else:
87 log.error(errmsg)
88 parser.print_help()
89 sys.exit(1)
90
91 if not args.verbose:
92 signal.signal(signal.SIGINT, signal_handler)
93
94 inst = DirSrv(verbose=args.verbose)
95
96 result = False
97
98 try:
99 result = args.func(inst, log, args)
100 except Exception as e:
101 log.debug(e, exc_info=True)
102 msg = format_error_to_dict(e)
103 if args and args.json:
104 sys.stderr.write(f"{json.dumps(msg, indent=4)}\n")
105 else:
106 if not args.verbose:
107 msg = format_pretty_error(msg)
108 log.error("Error: %s" % " - ".join(str(val) for val in msg.values()))
109 result = False
110
111 # Done!
112 if result is False:
113 sys.exit(1)