-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathget-server-info.py
executable file
·134 lines (110 loc) · 5.82 KB
/
get-server-info.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
#!/usr/bin/env python
'''
Retrieve CAS server information from CAS log
The CAS server command is expected to have a `-display` option with a unique
key for the invoked server. This is used to retrieve the PID of the server
process. The basename of the CAS log file must match the value in the
`-display` option.
'''
import argparse
import os
import re
import subprocess
import sys
import time
def print_err(*args, **kwargs):
''' Print a message to stderr '''
sys.stderr.write(*args, **kwargs)
sys.stderr.write('\n')
def main(args):
'''
Main routine
Parameters
----------
args : argparse arguments
Arguments to the command-line
'''
if not os.path.isfile(args.log_file):
print_err('ERROR: File not found: {}.'.format(args.log_file))
sys.exit(1)
iters = 0
for i in range(args.retries):
iters += 1
time.sleep(args.interval)
if iters > args.retries:
print_err('ERROR: Could not locate CAS log file.')
sys.exit(1)
with open(args.log_file, 'r') as logf:
txt = logf.read()
m = re.search(r'===\s+.+?(\S+):(\d+)\s+.+?\s+.+?:(\d+)\s+===', txt)
if m:
hostname = m.group(1)
binary_port = m.group(2)
http_port = m.group(3)
sys.stdout.write('CASHOST={} '.format(hostname))
sys.stdout.write('CAS_HOST={} '.format(hostname))
sys.stdout.write('CAS_BINARY_PORT={} '.format(binary_port))
sys.stdout.write('CAS_HTTP_PORT={} '.format(http_port))
sys.stdout.write('CAS_BINARY_URL=cas://{}:{} '.format(hostname,
binary_port))
sys.stdout.write('CAS_HTTP_URL=http://{}:{} '.format(hostname,
http_port))
if 'CASPROTOCOL' in os.environ or 'CAS_PROTOCOL' in os.environ:
protocol = os.environ.get('CASPROTOCOL',
os.environ.get('CAS_PROTOCOL', 'http'))
if protocol == 'cas':
sys.stdout.write('CASPROTOCOL=cas ')
sys.stdout.write('CAS_PROTOCOL=cas ')
sys.stdout.write('CASPORT={} '.format(binary_port))
sys.stdout.write('CAS_PORT={} '.format(binary_port))
sys.stdout.write('CASURL=cas://{}:{} '.format(hostname,
binary_port))
sys.stdout.write('CAS_URL=cas://{}:{} '.format(hostname,
binary_port))
else:
sys.stdout.write('CASPROTOCOL={} '.format(protocol))
sys.stdout.write('CAS_PROTOCOL={} '.format(protocol))
sys.stdout.write('CASPORT={} '.format(http_port))
sys.stdout.write('CAS_PORT={} '.format(http_port))
sys.stdout.write('CASURL={}://{}:{} '.format(protocol, hostname,
http_port))
sys.stdout.write('CAS_URL={}://{}:{} '.format(protocol, hostname,
http_port))
elif 'REQUIRES_TK' in os.environ:
if os.environ.get('REQUIRES_TK', '') == 'true':
sys.stdout.write('CASPROTOCOL=cas ')
sys.stdout.write('CAS_PROTOCOL=cas ')
sys.stdout.write('CASPORT={} '.format(binary_port))
sys.stdout.write('CAS_PORT={} '.format(binary_port))
sys.stdout.write('CASURL=cas://{}:{} '.format(hostname,
binary_port))
sys.stdout.write('CAS_URL=cas://{}:{} '.format(hostname,
binary_port))
else:
sys.stdout.write('CASPROTOCOL=http ')
sys.stdout.write('CAS_PROTOCOL=http ')
sys.stdout.write('CASPORT={} '.format(http_port))
sys.stdout.write('CAS_PORT={} '.format(http_port))
sys.stdout.write('CASURL=http://{}:{} '.format(hostname,
http_port))
sys.stdout.write('CAS_URL=http://{}:{} '.format(hostname,
http_port))
# Get CAS server pid
cmd = ('ssh -x -o StrictHostKeyChecking=no {} '
'ps ax | grep {} | grep -v grep | head -1'
).format(hostname, '.'.join(args.log_file.split('.')[:-1]))
pid = subprocess.check_output(cmd, shell=True) \
.decode('utf-8').strip().split(' ', 1)[0]
sys.stdout.write('CAS_PID={} '.format(pid))
break
if __name__ == '__main__':
opts = argparse.ArgumentParser(description=__doc__.strip(),
formatter_class=argparse.RawTextHelpFormatter)
opts.add_argument('log_file', type=str, metavar='log-file',
help='path to CAS server log')
opts.add_argument('--retries', '-r', default=5, type=int, metavar='#',
help='number of retries in attempting to locate the log file')
opts.add_argument('--interval', '-i', default=3, type=int, metavar='#',
help='number of seconds between each retry')
args = opts.parse_args()
main(args)