mirror of
https://port.numenaute.org/aleajactaest/khanat-code-old.git
synced 2024-11-22 23:06:18 +00:00
189 lines
8.2 KiB
Python
Executable file
189 lines
8.2 KiB
Python
Executable file
#!/usr/bin/python3
|
|
#
|
|
# script to send command to manager khaganat process
|
|
#
|
|
# Copyright (C) 2017 AleaJactaEst
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
Manipulate manager khaganat
|
|
|
|
We can end some command to manager.
|
|
Global:
|
|
SHUTDOWN : Stop manager & Stop all programs
|
|
STARTALL : Start all programs
|
|
STATUSALL : Get status of all programs
|
|
STOPALL : Stop all programs
|
|
LIST : List all programs available
|
|
For one program :
|
|
START : Start program
|
|
STOP : Stop program
|
|
STATUS : Get status
|
|
LOG : Get log
|
|
firstline : option to define first line we need send
|
|
ACTION : Send action (command) in stdin
|
|
action : option to define which action you need send to stdin
|
|
|
|
Example :
|
|
./client.py --log="debug" --show-log-console --server='172.17.0.2' --program="aes" --command="START"
|
|
./client.py --log="debug" --show-log-console --server='172.17.0.2' --program="aes" --command="STATUS"
|
|
./client.py --log="debug" --show-log-console --server='172.17.0.2' --program="aes" --command="ACTION" --action="coucou"
|
|
./client.py --log="debug" --show-log-console --server='172.17.0.2' --program="aes" --command="LOG" --firstline=0
|
|
./client.py --log="debug" --show-log-console --server='172.17.0.2' --program="aes" --command="STOP"
|
|
./client.py --log="debug" --show-log-console --server='172.17.0.2' --command="LIST"
|
|
./client.py --log="debug" --show-log-console --server='172.17.0.2' --command="SHUTDOWN"
|
|
./client.py --log="debug" --show-log-console --server='172.17.0.2' --command="STARTALL"
|
|
./client.py --log="debug" --show-log-console --server='172.17.0.2' --command="STATUSALL"
|
|
./client.py --key="/home/gameserver/khanat/key.pem" --cert="/home/gameserver/khanat/cert.pem" --log="debug" --show-log-console --command="STATUSALL"
|
|
"""
|
|
|
|
import argparse
|
|
import logging
|
|
import logging.config
|
|
import http.client
|
|
import json
|
|
|
|
def cmp_to_key():
|
|
'compare key (check if int or other)'
|
|
class K(object):
|
|
def __init__(self, obj, *args):
|
|
self.obj = obj
|
|
def __lt__(self, other):
|
|
try:
|
|
return int(self.obj) < int(other.obj)
|
|
except:
|
|
return self.obj < other.obj
|
|
def __gt__(self, other):
|
|
try:
|
|
return int(self.obj) > int(other.obj)
|
|
except:
|
|
return self.obj > other.obj
|
|
def __eq__(self, other):
|
|
try:
|
|
return int(self.obj) == int(other.obj)
|
|
except:
|
|
return self.obj == other.obj
|
|
def __le__(self, other):
|
|
try:
|
|
return int(self.obj) <= int(other.obj)
|
|
except:
|
|
return self.obj <= other.obj
|
|
def __ge__(self, other):
|
|
try:
|
|
return int(self.obj) >= int(other.obj)
|
|
except:
|
|
return self.obj >= other.obj
|
|
def __ne__(self, other):
|
|
try:
|
|
return int(self.obj) != int(other.obj)
|
|
except:
|
|
return self.obj != other.obj
|
|
return K
|
|
|
|
def send_json(jsonin={}, command='GET', path='/', host='localhost', port=8000, raw_data=False, remove_color=False,
|
|
key_file=None, cert_file=None):
|
|
"send command with https & json format"
|
|
conn = http.client.HTTPSConnection(host=host, port=port, key_file=key_file, cert_file=cert_file )
|
|
conn.putrequest(command, path)
|
|
out=json.dumps(jsonin)
|
|
conn.putheader('Content-type', 'application/json')
|
|
#length = int(self.headers.getheader('content-length'))
|
|
conn.putheader('Content-length', len(out))
|
|
conn.endheaders()
|
|
conn.send(bytes(out, "utf-8"))
|
|
response = conn.getresponse()
|
|
if raw_data:
|
|
print(response.read())
|
|
else:
|
|
if remove_color:
|
|
endText = '\x1b[0m'
|
|
else:
|
|
endText = ''
|
|
if response.status != 200:
|
|
logging.error("Error detected (html code:%d)" % response.status)
|
|
print(response.read())
|
|
return
|
|
ret = response.read().decode()
|
|
try:
|
|
msgjson = json.loads(ret)
|
|
except:
|
|
logging.error("Impossible to decode Json output")
|
|
print(ret)
|
|
return
|
|
for key in sorted(msgjson, key=cmp_to_key()):
|
|
print("%s: %s %s" % (key, msgjson[key], endText))
|
|
|
|
|
|
def main(server, command, program, action, firstline, fileLog, logLevel, show_log_console, port=8000,
|
|
raw_data=False, remove_color=False, key_file=None, cert_file=None):
|
|
# Manage log
|
|
logging.getLogger('logging')
|
|
numeric_level = getattr(logging, logLevel.upper(), None)
|
|
if not isinstance(numeric_level, int):
|
|
raise ValueError('Invalid log level: %s' % logLevel)
|
|
handlers=[]
|
|
if show_log_console:
|
|
handlers.append(logging.StreamHandler())
|
|
if fileLog:
|
|
handlers.append(logging.FileHandler(fileLog.name))
|
|
logging.basicConfig(handlers=handlers, level=numeric_level,
|
|
format='%(asctime)s %(levelname)s [pid:%(process)d] [%(funcName)s:%(lineno)d] %(message)s')
|
|
# Send command
|
|
if command == 'START' or command == 'STOP':
|
|
send_json({'name': program}, 'POST', "/" + command, server, port)
|
|
elif command == 'STATUS':
|
|
send_json({'name': program}, 'GET', "/" + command, server, port)
|
|
elif command == 'ACTION':
|
|
send_json({'name': program, 'action' : action}, 'POST', "/" + command, server, port)
|
|
elif command == 'LOG':
|
|
send_json({'name': program, 'first-line' : firstline }, 'GET', "/" + command, server, port, raw_data, remove_color)
|
|
elif command == 'LIST':
|
|
send_json({}, 'GET', "/" + command, server, port)
|
|
elif command == 'SHUTDOWN' or command == 'STARTALL' or command == 'STOPALL':
|
|
send_json({}, 'POST', "/" + command, server, port)
|
|
elif command == 'STATUSALL':
|
|
send_json({}, 'GET', "/" + command, server, port)
|
|
else:
|
|
logging.error("command unknown (%s)" % command)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description='Manipulate khaganat process')
|
|
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
|
|
parser.add_argument( '--show-log-console', action='store_true',
|
|
help='show message in console', default=False)
|
|
parser.add_argument('--filelog', type=argparse.FileType('wt'),
|
|
default=None, help='log file')
|
|
parser.add_argument('--log',
|
|
default='INFO', help='log level [DEBUG, INFO, WARNING, ERROR')
|
|
parser.add_argument('--key', help='key file', default=None)
|
|
parser.add_argument('--cert', help='cert file', default=None)
|
|
parser.add_argument('--server', help='server khganat', default='127.0.0.1')
|
|
parser.add_argument('--command', help='command send to khganat', default='/STATUS')
|
|
parser.add_argument('--program', help='program khaganat id ', default='aes')
|
|
parser.add_argument('--action', help='action ', default='')
|
|
parser.add_argument('--firstline', type=int,
|
|
help='define fistline read for log command', default=0)
|
|
parser.add_argument( '--raw-data', action='store_true',
|
|
help='show raw message', default=False)
|
|
parser.add_argument( '--keep-color', action='store_true',
|
|
help='some message have color define, by default we reset the color (this option keep current color state)', default=False)
|
|
args = parser.parse_args()
|
|
main(server = args.server, action = args.action, firstline = args.firstline,
|
|
command = args.command, program = args.program, fileLog = args.filelog,
|
|
logLevel=args.log, show_log_console=args.show_log_console,
|
|
raw_data = args.raw_data,
|
|
key_file=args.key, cert_file=args.cert,
|
|
remove_color=not args.keep_color)
|