deploy: Support new JSON-formatted responses from Botfarm.

This commit is contained in:
novokrest 2018-05-06 12:20:45 +03:00 committed by Rohitt Vashishtha
parent e81942421e
commit bf9ad09bc2

View file

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from typing import Any, List from typing import Any, List, Dict, Callable
import os import os
import sys import sys
@ -11,6 +11,7 @@ import requests
import urllib import urllib
import json import json
from urllib import parse from urllib import parse
from requests import Response
red = '\033[91m' # type: str red = '\033[91m' # type: str
green = '\033[92m' # type: str green = '\033[92m' # type: str
@ -71,6 +72,35 @@ def check_common_options(options: argparse.Namespace) -> None:
print('tools/deploy: Botfarm deploy token not specified.') print('tools/deploy: Botfarm deploy token not specified.')
sys.exit(1) sys.exit(1)
def handle_common_response_without_data(response: Response,
operation: str,
success_message: str) -> bool:
return handle_common_response(
response=response,
operation=operation,
success_handler=lambda r: print('{}: {}'.format(operation, success_message))
)
def handle_common_response(response: Response,
operation: str,
success_handler: Callable[[Dict[str, Any]], Any]) -> bool:
if response.status_code == requests.codes.ok:
response_data = response.json()
if response_data['status'] == 'success':
success_handler(response_data)
return True
elif response_data['status'] == 'error':
print('{}: {}'.format(operation, response_data['message']))
return False
else:
print('{}: Unexpected success response format'.format(operation))
return False
if response.status_code == requests.codes.unauthorized:
print('{}: Authentication error with the server. Aborting.'.format(operation))
else:
print('{}: Error {}. Aborting.'.format(operation, response.status_code))
return False
def upload(options: argparse.Namespace) -> None: def upload(options: argparse.Namespace) -> None:
check_common_options(options) check_common_options(options)
file_path = os.path.join(bots_dir, options.botname + '.zip') file_path = os.path.join(bots_dir, options.botname + '.zip')
@ -80,14 +110,9 @@ def upload(options: argparse.Namespace) -> None:
files = {'file': open(file_path, 'rb')} files = {'file': open(file_path, 'rb')}
headers = {'key': options.key} headers = {'key': options.key}
url = urllib.parse.urljoin(options.server, 'bots/upload') url = urllib.parse.urljoin(options.server, 'bots/upload')
r = requests.post(url, files=files, headers=headers) response = requests.post(url, files=files, headers=headers)
if r.status_code == requests.codes.ok: result = handle_common_response_without_data(response, 'upload', 'Uploaded the bot package to botfarm.')
print('upload: Uploaded the bot package to botfarm.') if result is False:
return
if r.status_code == 401:
print('upload: Authentication error with the server. Aborting.')
else:
print('upload: Error {}. Aborting.'.format(r.status_code))
sys.exit(1) sys.exit(1)
def clean(options: argparse.Namespace) -> None: def clean(options: argparse.Namespace) -> None:
@ -103,14 +128,9 @@ def process(options: argparse.Namespace) -> None:
headers = {'key': options.key} headers = {'key': options.key}
url = urllib.parse.urljoin(options.server, 'bots/process') url = urllib.parse.urljoin(options.server, 'bots/process')
payload = {'name': options.botname} payload = {'name': options.botname}
r = requests.post(url, headers=headers, json=payload) response = requests.post(url, headers=headers, json=payload)
if r.status_code == requests.codes.ok and r.text == 'done': result = handle_common_response_without_data(response, 'process', 'The bot has been processed by the botfarm.')
print('process: The bot has been processed by the botfarm.') if result is False:
return
if r.status_code == 401:
print('process: Authentication error with the server. Aborting.')
else:
print('process: Error {}: {}. Aborting.'.format(r.status_code, r.text))
sys.exit(1) sys.exit(1)
def start(options: argparse.Namespace) -> None: def start(options: argparse.Namespace) -> None:
@ -118,14 +138,9 @@ def start(options: argparse.Namespace) -> None:
headers = {'key': options.key} headers = {'key': options.key}
url = urllib.parse.urljoin(options.server, 'bots/start') url = urllib.parse.urljoin(options.server, 'bots/start')
payload = {'name': options.botname} payload = {'name': options.botname}
r = requests.post(url, headers=headers, json=payload) response = requests.post(url, headers=headers, json=payload)
if r.status_code == requests.codes.ok and r.text == 'done': result = handle_common_response_without_data(response, 'start', 'The bot has been started by the botfarm.')
print('start: The bot has been started by the botfarm.') if result is False:
return
if r.status_code == 401:
print('start: Authentication error with the server. Aborting.')
else:
print('start: Error {}: {}. Aborting.'.format(r.status_code, r.text))
sys.exit(1) sys.exit(1)
def stop(options: argparse.Namespace) -> None: def stop(options: argparse.Namespace) -> None:
@ -133,14 +148,9 @@ def stop(options: argparse.Namespace) -> None:
headers = {'key': options.key} headers = {'key': options.key}
url = urllib.parse.urljoin(options.server, 'bots/stop') url = urllib.parse.urljoin(options.server, 'bots/stop')
payload = {'name': options.botname} payload = {'name': options.botname}
r = requests.post(url, headers=headers, json=payload) response = requests.post(url, headers=headers, json=payload)
if r.status_code == requests.codes.ok and r.text == 'done': result = handle_common_response_without_data(response, 'stop', 'The bot has been stopped by the botfarm.')
print('stop: The bot has been stopped by the botfarm.') if result is False:
return
if r.status_code == 401:
print('stop: Authentication error with the server. Aborting.')
else:
print('stop: Error {}: {}. Aborting.'.format(r.status_code, r.text))
sys.exit(1) sys.exit(1)
def prepare(options: argparse.Namespace) -> None: def prepare(options: argparse.Namespace) -> None:
@ -158,14 +168,9 @@ def log(options: argparse.Namespace) -> None:
lines = None lines = None
payload = {'name': options.botname, 'lines': lines} payload = {'name': options.botname, 'lines': lines}
url = urllib.parse.urljoin(options.server, 'bots/logs/' + options.botname) url = urllib.parse.urljoin(options.server, 'bots/logs/' + options.botname)
r = requests.get(url, json=payload, headers=headers) response = requests.get(url, json=payload, headers=headers)
if r.status_code == requests.codes.ok: result = handle_common_response(response, 'log', lambda r: print(r['logs']['content']))
print(r.text) if result is False:
return
if r.status_code == 401:
print('log: Authentication error with the server. Aborting.')
else:
print('log: Error {}: {}. Aborting.'.format(r.status_code, r.text))
sys.exit(1) sys.exit(1)
def delete(options: argparse.Namespace) -> None: def delete(options: argparse.Namespace) -> None:
@ -173,14 +178,9 @@ def delete(options: argparse.Namespace) -> None:
headers = {'key': options.key} headers = {'key': options.key}
url = urllib.parse.urljoin(options.server, 'bots/delete') url = urllib.parse.urljoin(options.server, 'bots/delete')
payload = {'name': options.botname} payload = {'name': options.botname}
r = requests.post(url, headers=headers, json=payload) response = requests.post(url, headers=headers, json=payload)
if r.status_code == requests.codes.ok and r.text == 'done': result = handle_common_response_without_data(response, 'delete', 'The bot has been removed from the botfarm.')
print('delete: The bot has been removed from the botfarm.') if result is False:
return
if r.status_code == 401:
print('delete: Authentication error with the server. Aborting.')
else:
print('delete: Error {}: {}. Aborting.'.format(r.status_code, r.text))
sys.exit(1) sys.exit(1)
def list_bots(options: argparse.Namespace) -> None: def list_bots(options: argparse.Namespace) -> None:
@ -191,16 +191,9 @@ def list_bots(options: argparse.Namespace) -> None:
else: else:
pretty_print = False pretty_print = False
url = urllib.parse.urljoin(options.server, 'bots/list') url = urllib.parse.urljoin(options.server, 'bots/list')
r = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
if r.status_code == requests.codes.ok: result = handle_common_response(response, 'ls', lambda r: print_bots(r['bots']['list'], pretty_print))
data = json.loads(r.text) if result is False:
if 'bots' in data:
print_bots(data['bots'], pretty_print)
return
if r.status_code == 401:
print('ls: Authentication error with the server. Aborting.')
else:
print('ls: Error {}: {}. Aborting.'.format(r.status_code, r.text))
sys.exit(1) sys.exit(1)
def print_bots(bots: List[Any], pretty_print: bool) -> None: def print_bots(bots: List[Any], pretty_print: bool) -> None: