| 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 json |
| 14 |
import argparse, argcomplete |
| 15 |
import sys |
| 16 |
import signal |
| 17 |
import os |
| 18 |
from lib389.utils import get_instance_list, instance_choices |
| 19 |
from lib389 import DirSrv |
| 20 |
from lib389.cli_ctl import instance as cli_instance |
| 21 |
from lib389.cli_ctl import dbtasks as cli_dbtasks |
| 22 |
from lib389.cli_ctl import tls as cli_tls |
| 23 |
from lib389.cli_ctl import health as cli_health |
| 24 |
from lib389.cli_ctl import nsstate as cli_nsstate |
| 25 |
from lib389.cli_ctl import dbgen as cli_dbgen |
| 26 |
from lib389.cli_ctl import dsrc as cli_dsrc |
| 27 |
from lib389.cli_ctl import cockpit as cli_cockpit |
| 28 |
from lib389.cli_ctl import dblib as cli_dblib |
| 29 |
from lib389.cli_ctl.instance import instance_remove_all |
| 30 |
from lib389.cli_base import ( |
| 31 |
disconnect_instance, |
| 32 |
setup_script_logger, |
| 33 |
format_error_to_dict, |
| 34 |
format_pretty_error, |
| 35 |
parent_argparser |
| 36 |
) |
| 37 |
from lib389._constants import DSRC_CONTAINER |
| 38 |
|
| 39 |
parser = argparse.ArgumentParser(allow_abbrev=False, parents=[parent_argparser]) |
| 40 |
parser.add_argument('instance', nargs='?', default=False, |
| 41 |
help="The name of the instance to act upon", |
| 42 |
).completer = instance_choices |
| 43 |
parser.add_argument('-l', '--list', |
| 44 |
help="List available Directory Server instances", |
| 45 |
default=False, action='store_true' |
| 46 |
) |
| 47 |
parser.add_argument('--remove-all', default=False, action='store_true', |
| 48 |
help=argparse.SUPPRESS |
| 49 |
) |
| 50 |
|
| 51 |
subparsers = parser.add_subparsers(help="action") |
| 52 |
# We can only use the instance tools like start/stop etc in a non-container |
| 53 |
# environment. If we are in a container, we only allow the tasks. |
| 54 |
if not os.path.exists(DSRC_CONTAINER): |
| 55 |
cli_instance.create_parser(subparsers) |
| 56 |
cli_dbtasks.create_parser(subparsers) |
| 57 |
cli_tls.create_parser(subparsers) |
| 58 |
cli_health.create_parser(subparsers) |
| 59 |
cli_nsstate.create_parser(subparsers) |
| 60 |
cli_dbgen.create_parser(subparsers) |
| 61 |
cli_dsrc.create_parser(subparsers) |
| 62 |
cli_cockpit.create_parser(subparsers) |
| 63 |
cli_dblib.create_parser(subparsers) |
| 64 |
|
| 65 |
argcomplete.autocomplete(parser) |
| 66 |
|
| 67 |
|
| 68 |
# handle a control-c gracefully |
| 69 |
def signal_handler(signal, frame): |
| 70 |
print('\n\nExiting...') |
| 71 |
sys.exit(0) |
| 72 |
|
| 73 |
|
| 74 |
if __name__ == '__main__': |
| 75 |
args = parser.parse_args() |
| 76 |
|
| 77 |
log = setup_script_logger('dsctl', args.verbose) |
| 78 |
|
| 79 |
log.debug("The 389 Directory Server Administration Tool") |
| 80 |
# Leave this comment here: UofA let me take this code with me provided |
| 81 |
# I gave attribution. -- wibrown |
| 82 |
log.debug("Inspired by works of: ITS, The University of Adelaide") |
| 83 |
|
| 84 |
log.debug("Called with: %s", args) |
| 85 |
|
| 86 |
if args.list: |
| 87 |
insts = get_instance_list() |
| 88 |
if args.json: |
| 89 |
print(json.dumps({"type": "result", "insts": insts}, indent=4)) |
| 90 |
else: |
| 91 |
for inst in insts: |
| 92 |
print(inst) |
| 93 |
sys.exit(0) |
| 94 |
elif args.remove_all is not False: |
| 95 |
instance_remove_all(log, args) |
| 96 |
sys.exit(0) |
| 97 |
elif not args.instance: |
| 98 |
errmsg = "error: the following arguments are required: instance" |
| 99 |
if args.json: |
| 100 |
sys.stderr.write("{'desc': '%s'}\n" % errmsg) |
| 101 |
else: |
| 102 |
log.error(errmsg) |
| 103 |
parser.print_help() |
| 104 |
sys.exit(1) |
| 105 |
|
| 106 |
# Assert we have a resources to work on. |
| 107 |
if not hasattr(args, 'func'): |
| 108 |
errmsg = "No action provided, here is some --help." |
| 109 |
if args.json: |
| 110 |
sys.stderr.write("{'desc': '%s'}\n" % errmsg) |
| 111 |
else: |
| 112 |
log.error(errmsg) |
| 113 |
parser.print_help() |
| 114 |
sys.exit(1) |
| 115 |
|
| 116 |
# Connect |
| 117 |
inst = DirSrv(verbose=args.verbose) |
| 118 |
|
| 119 |
result = True |
| 120 |
|
| 121 |
# Allocate the instance based on name |
| 122 |
insts = [] |
| 123 |
if args.verbose: |
| 124 |
insts = inst.list(serverid=args.instance) |
| 125 |
else: |
| 126 |
signal.signal(signal.SIGINT, signal_handler) |
| 127 |
try: |
| 128 |
insts = inst.list(serverid=args.instance) |
| 129 |
except (PermissionError, IOError) as e: |
| 130 |
log.error("Unable to access instance information. Are you running as the correct user? (usually dirsrv or root)") |
| 131 |
msg = format_error_to_dict(e) |
| 132 |
if args.json: |
| 133 |
sys.stderr.write(f"{json.dumps(msg, indent=4)}\n") |
| 134 |
else: |
| 135 |
if not args.verbose: |
| 136 |
msg = format_pretty_error(msg) |
| 137 |
log.error("Error: %s" % " - ".join(msg.values())) |
| 138 |
sys.exit(1) |
| 139 |
except Exception as e: |
| 140 |
msg = format_error_to_dict(e) |
| 141 |
if args.json: |
| 142 |
sys.stderr.write(f"{json.dumps(msg, indent=4)}\n") |
| 143 |
else: |
| 144 |
if not args.verbose: |
| 145 |
msg = format_pretty_error(msg) |
| 146 |
log.error("Error: %s" % " - ".join(msg.values())) |
| 147 |
sys.exit(1) |
| 148 |
if len(insts) != 1: |
| 149 |
errmsg = "No such instance '%s'" % args.instance |
| 150 |
if args.json: |
| 151 |
sys.stderr.write('{"desc": "%s"}\n' % errmsg) |
| 152 |
else: |
| 153 |
log.error(errmsg) |
| 154 |
log.error("Unable to access instance information. Are you running as the correct user? (usually dirsrv or root)") |
| 155 |
sys.exit(1) |
| 156 |
|
| 157 |
inst.local_simple_allocate(insts[0]['server-id']) |
| 158 |
log.debug('Instance allocated') |
| 159 |
|
| 160 |
try: |
| 161 |
result = args.func(inst, log, args) |
| 162 |
except Exception as e: |
| 163 |
log.debug(e, exc_info=True) |
| 164 |
msg = format_error_to_dict(e) |
| 165 |
if args.json: |
| 166 |
sys.stderr.write(f"{json.dumps(msg, indent=4)}\n") |
| 167 |
else: |
| 168 |
if not args.verbose: |
| 169 |
msg = format_pretty_error(msg) |
| 170 |
log.error("Error: %s" % " - ".join(str(val) for val in msg.values())) |
| 171 |
result = False |
| 172 |
disconnect_instance(inst) |
| 173 |
|
| 174 |
# Done! |
| 175 |
if result is False: |
| 176 |
sys.exit(1) |