|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +''' |
| 4 | +get-server-info |
| 5 | +
|
| 6 | +Retrieve CAS server information using CAS log. The CAS server command |
| 7 | +is expected to have a -display option with a unique key for the invoked |
| 8 | +server. This is used to retrieve the PID of the server process. The |
| 9 | +basename of the CAS log file must match the value in the -display option. |
| 10 | +
|
| 11 | +''' |
| 12 | + |
| 13 | +import argparse |
| 14 | +import os |
| 15 | +import re |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +import time |
| 19 | + |
| 20 | + |
| 21 | +def main(args): |
| 22 | + ''' |
| 23 | + Main routine |
| 24 | +
|
| 25 | + Parameters |
| 26 | + ---------- |
| 27 | + args : argparse arguments |
| 28 | + Arguments to the command-line |
| 29 | +
|
| 30 | + ''' |
| 31 | + if not os.path.isfile(args.log_file): |
| 32 | + sys.stderr.write('error: file not found: {}'.format(args.log_file)) |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | + iters = 0 |
| 36 | + for i in range(args.retries): |
| 37 | + iters += 1 |
| 38 | + time.sleep(args.interval) |
| 39 | + if iters > args.retries: |
| 40 | + sys.stderr.write('error: could not locate CAS log file\n') |
| 41 | + sys.exit(1) |
| 42 | + |
| 43 | + with open(args.log_file, 'r') as logf: |
| 44 | + txt = logf.read() |
| 45 | + m = re.search(r'===\s+.+?(\S+):(\d+)\s+.+?\s+.+?:(\d+)\s+===', txt) |
| 46 | + if m: |
| 47 | + hostname = m.group(1) |
| 48 | + binary_port = m.group(2) |
| 49 | + http_port = m.group(3) |
| 50 | + |
| 51 | + sys.stdout.write('CASHOST={} '.format(hostname)) |
| 52 | + sys.stdout.write('CAS_HOST={} '.format(hostname)) |
| 53 | + sys.stdout.write('CAS_BINARY_PORT={} '.format(binary_port)) |
| 54 | + sys.stdout.write('CAS_HTTP_PORT={} '.format(http_port)) |
| 55 | + sys.stdout.write('CAS_BINARY_URL=cas://{}:{} '.format(hostname, |
| 56 | + binary_port)) |
| 57 | + sys.stdout.write('CAS_HTTP_URL=http://{}:{} '.format(hostname, |
| 58 | + http_port)) |
| 59 | + |
| 60 | + if 'CASPROTOCOL' in os.environ or 'CAS_PROTOCOL' in os.environ: |
| 61 | + protocol = os.environ.get('CASPROTOCOL', |
| 62 | + os.environ.get('CAS_PROTOCOL', 'http')) |
| 63 | + if protocol == 'cas': |
| 64 | + sys.stdout.write('CASPROTOCOL=cas ') |
| 65 | + sys.stdout.write('CAS_PROTOCOL=cas ') |
| 66 | + sys.stdout.write('CASPORT={} '.format(binary_port)) |
| 67 | + sys.stdout.write('CAS_PORT={} '.format(binary_port)) |
| 68 | + sys.stdout.write('CASURL=cas://{}:{} '.format(hostname, |
| 69 | + binary_port)) |
| 70 | + sys.stdout.write('CAS_URL=cas://{}:{} '.format(hostname, |
| 71 | + binary_port)) |
| 72 | + else: |
| 73 | + sys.stdout.write('CASPROTOCOL={} '.format(protocol)) |
| 74 | + sys.stdout.write('CAS_PROTOCOL={} '.format(protocol)) |
| 75 | + sys.stdout.write('CASPORT={} '.format(http_port)) |
| 76 | + sys.stdout.write('CAS_PORT={} '.format(http_port)) |
| 77 | + sys.stdout.write('CASURL={}://{}:{} '.format(protocol, hostname, |
| 78 | + http_port)) |
| 79 | + sys.stdout.write('CAS_URL={}://{}:{} '.format(protocol, hostname, |
| 80 | + http_port)) |
| 81 | + |
| 82 | + elif 'REQUIRES_TK' in os.environ: |
| 83 | + if os.environ.get('REQUIRES_TK', '') == 'true': |
| 84 | + sys.stdout.write('CASPROTOCOL=cas ') |
| 85 | + sys.stdout.write('CAS_PROTOCOL=cas ') |
| 86 | + sys.stdout.write('CASPORT={} '.format(binary_port)) |
| 87 | + sys.stdout.write('CAS_PORT={} '.format(binary_port)) |
| 88 | + sys.stdout.write('CASURL=cas://{}:{} '.format(hostname, |
| 89 | + binary_port)) |
| 90 | + sys.stdout.write('CAS_URL=cas://{}:{} '.format(hostname, |
| 91 | + binary_port)) |
| 92 | + else: |
| 93 | + sys.stdout.write('CASPROTOCOL=http ') |
| 94 | + sys.stdout.write('CAS_PROTOCOL=http ') |
| 95 | + sys.stdout.write('CASPORT={} '.format(http_port)) |
| 96 | + sys.stdout.write('CAS_PORT={} '.format(http_port)) |
| 97 | + sys.stdout.write('CASURL=http://{}:{} '.format(hostname, |
| 98 | + http_port)) |
| 99 | + sys.stdout.write('CAS_URL=http://{}:{} '.format(hostname, |
| 100 | + http_port)) |
| 101 | + |
| 102 | + # Get CAS server pid |
| 103 | + cmd = ('ssh -x -o StrictHostKeyChecking=no {} ' |
| 104 | + 'ps ax | grep {} | grep -v grep | head -1' |
| 105 | + ).format(hostname, '.'.join(args.log_file.split('.')[:-1])) |
| 106 | + pid = subprocess.check_output(cmd, shell=True) \ |
| 107 | + .decode('utf-8').strip().split(' ', 1)[0] |
| 108 | + sys.stdout.write('CAS_PID={} '.format(pid)) |
| 109 | + |
| 110 | + break |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + |
| 115 | + opts = argparse.ArgumentParser(description=__doc__.strip()) |
| 116 | + |
| 117 | + opts.add_argument('log_file', type=str, metavar='log-file', |
| 118 | + help='path to CAS server log') |
| 119 | + |
| 120 | + opts.add_argument('--retries', '-r', default=5, type=int, metavar='#', |
| 121 | + help='number of retries in attempting to locate the log file') |
| 122 | + opts.add_argument('--interval', '-i', default=3, type=int, metavar='#', |
| 123 | + help='number of seconds between each retry') |
| 124 | + |
| 125 | + args = opts.parse_args() |
| 126 | + |
| 127 | + main(args) |
0 commit comments