diff --git a/Client/Client.py b/Client/Client.py index 06f0c79..20386b1 100644 --- a/Client/Client.py +++ b/Client/Client.py @@ -21,14 +21,51 @@ import argparse import os import requests +import shutil import tempfile +import time +import traceback import zipfile +class BadVersionException(Exception): + def __init__(self, message='Wrong Client Version'): + self.message = message + super().__init__(self.message) + def url_join(*args): # Join a set of URL paths while maintaining the correct format return '/'.join([f.lstrip('/').rstrip('/') for f in args]) + '/' +def try_forever(func, args, message, timeout=15): + + # Execute func(arg1, arg2, ...) until success + + while True: + + try: + return func(*args) + + except BadVersionException: + raise BadVersionException() + + except Exception as exception: + print ('\n\n' + message) + print ('[Note] Sleeping for %d seconds' % (timeout)) + print ('[Note] Traceback:') + traceback.print_exc() + print () + + time.sleep(timeout) + + +def has_worker(): + try: + import worker + return True + except ImportError: + return False + def parse_arguments(): # We can use ENV variables for the Username, Passwords, and Servers @@ -51,6 +88,7 @@ def parse_arguments(): p.add_argument( '--syzygy' , help='Syzygy WDL' , required=False ) p.add_argument( '--fleet' , help='Fleet Mode' , action='store_true') p.add_argument( '--proxy' , help='Github Proxy' , action='store_true') + p.add_argument( '--clean' , help='Force New Client' , action='store_true') args = p.parse_args() @@ -62,32 +100,69 @@ def parse_arguments(): def download_client_files(args): - # Reponse may contain an error header for invalid credentials - payload = { 'username' : args.username, 'password' : args.password } - target = url_join(args.server, 'clientVersionRef') - version_ref = requests.post(target, data=payload).json() + try: # Reponse may contain an error header for invalid credentials + payload = { 'username' : args.username, 'password' : args.password } + target = url_join(args.server, 'clientVersionRef') + version_ref = requests.post(target, data=payload).json() - # Download the entire .zip for the branch / tag / commit ref - repo_url = version_ref['client_repo_url'] - repo_ref = version_ref['client_repo_ref'] - ## response = requests.get(url_join(repo_url, 'archive', '%s.zip' % (repo_ref))) - ## - ## # Save the content into client.zip - ## assert response.status_code == 200 - ## with open('client.zip', 'wb') as zip_file: - ## zip_file.write(response.content) + except: + raise Exception('Unable to retrieve Client Version from OpenBench server') - with tempfile.TemporaryDirectory() as temp_dir: + try: # Download the entire .zip for the branch / tag / commit ref + repo_url = version_ref['client_repo_url'] + repo_ref = version_ref['client_repo_ref'] + response = requests.get(url_join(repo_url, 'archive', '%s.zip' % (repo_ref))) + assert response.status_code == 200 - with zipfile.ZipFile('client.zip', 'r') as zip_file: - zip_file.extractall(temp_dir) + except: + raise Exception('Unable to retrieve .zip archive from Github') + + try: # Save to .zip, extract, and copy contents + + # Save response content to a temp file + with tempfile.TemporaryFile() as temp_zip_file: + temp_zip_file.write(response.content) + + # Extract the zip with a temp file + with tempfile.TemporaryDirectory() as temp_dir: + with zipfile.ZipFile(temp_zip_file) as zip_file: + zip_file.extractall(temp_dir) + + # Copy all files except client.py + client_dir = os.path.join(temp_dir, 'OpenBench-%s' % (repo_ref), 'Client') + for root, dirs, files in os.walk(client_dir): + for file in files: + if file != 'client.py': + shutil.copy2(os.path.join(root, file), os.path.join(os.getcwd(), file)) + + except: + raise Exception('Unable to extract .zip archive contents') - client_dir = os.path.join(temp_dir, 'OpenBench-%s' % (repo_ref), 'Client') - for root, dirs, files in os.walk(client_dir): - for file in files: - print (os.path.relpath(os.path.join(root, file), temp_dir)) if __name__ == '__main__': args = parse_arguments() - download_client_files(args) + + if args.clean or not has_worker(): + try_forever(download_client_files, [args], 'Failed to download Client files') + + from client import BadVersionException + + while True: + + try: + import worker + worker.run_openbench_worker(args) + + except BadVersionException: + print ('[NOTE] Downloading newer version of Client...') + try_forever(download_client_files, [args], 'Failed to download Client files') + + except Exception as err: + print (err) + print (type(err)) + print (type(BadVersionException())) + break + + except KeyboardInterrupt: + break diff --git a/Client/worker.py b/Client/worker.py index af5ec3a..fccf964 100644 --- a/Client/worker.py +++ b/Client/worker.py @@ -42,9 +42,15 @@ from subprocess import PIPE, Popen, call, STDOUT from itertools import combinations_with_replacement from concurrent.futures import ThreadPoolExecutor +# from client import BadVersionException + +from client import BadVersionException +from client import url_join +from client import try_forever + ## Basic configuration of the Client. These timeouts can be changed at will -CLIENT_VERSION = 18 # Client version to send to the Server +CLIENT_VERSION = 19 # Client version to send to the Server TIMEOUT_HTTP = 30 # Timeout in seconds for HTTP requests TIMEOUT_ERROR = 10 # Timeout in seconds when any errors are thrown TIMEOUT_WORKLOAD = 30 # Timeout in seconds between workload requests @@ -258,10 +264,22 @@ class ServerReporter: @staticmethod def report(config, endpoint, payload): + payload['machine_id'] = config.machine_id payload['secret'] = config.secret_token - target = url_join(config.server, endpoint) - return requests.post(target, data=payload, timeout=TIMEOUT_HTTP) + + target = url_join(config.server, endpoint) + reponse = requests.post(target, data=payload, timeout=TIMEOUT_HTTP) + + # Check for a json repsone, to look for Client Version Errors + try: as_json = response.json() + except: return response + + # Throw all the way back to the client.py + if 'Bad Client Version' in as_json.get('error', ''): + raise BadVersionException() + + return response @staticmethod def report_nps(config, dev_nps, base_nps): @@ -761,26 +779,6 @@ def validate_syzygy_exists(config, K): return True -def try_forever(func, args, message): - - while True: - try: - return func(*args) - - except Exception as exception: - print ('\n\n' + message) - print ('[Note] Sleeping for %d seconds' % (TIMEOUT_ERROR)) - print ('[Note] Traceback:') - traceback.print_exc() - print () - - time.sleep(TIMEOUT_ERROR) - - -def url_join(*args): - - # Join a set of URL paths while maintaining the correct format - return '/'.join([f.lstrip('/').rstrip('/') for f in args]) + '/' def download_file(source, outname, post_data=None, headers=None): @@ -915,43 +913,6 @@ def find_pgn_error(reason, command): ## Functions interacting with the OpenBench server that establish the initial ## connection and then make simple requests to retrieve Workloads as json objects -def server_download_cutechess(config): - - print('\nFetching Cutechess Binary...') - - if IS_WINDOWS and not locate_utility('cutechess-ob.exe', False, False): - - # Fetch the source location if we are missing the binary - source = requests.get( - url_join(config.server, 'clientGetFiles'), - timeout=TIMEOUT_HTTP).json()['location'] - - if config.proxy: source = 'https://ghproxy.com/' + source - - # Windows workers simply need a static compile (64-bit) - download_file(url_join(source, 'cutechess-windows.exe').rstrip('/'), 'cutechess-ob.exe') - - # Verify that we can execute Cutechess - if not locate_utility('cutechess-ob.exe', False): - raise Exception - - if IS_LINUX and not locate_utility('./cutechess-ob', False, False): - - # Fetch the source location if we are missing the binary - source = requests.get( - url_join(config.server, 'clientGetFiles'), - timeout=TIMEOUT_HTTP).json()['location'] - - if config.proxy: source = 'https://ghproxy.com/' + source - - # Linux workers need a static compile (64-bit) with execute permissions - download_file(url_join(source, 'cutechess-linux').rstrip('/'), 'cutechess-ob') - os.system('chmod 777 cutechess-ob') - - # Verify that we can execute Cutechess - if not locate_utility('./cutechess-ob', False): - raise Exception - def server_configure_worker(config): # Server tells us how to build or obtain binaries @@ -998,6 +959,10 @@ def server_configure_worker(config): config.machine_id = 'None' os.remove('machine.txt') + # Throw all the way back to the client.py + if 'Bad Client Version' in response.get('error', ''): + raise BadVersionException(); + # The 'error' header is included if there was an issue if 'error' in response: raise Exception('[Error] %s' % (response['error'])) @@ -1018,6 +983,10 @@ def server_request_workload(config): target = url_join(config.server, 'clientGetWorkload') response = requests.post(target, data=payload, timeout=TIMEOUT_HTTP).json() + # Throw all the way back to the client.py + if 'Bad Client Version' in response.get('error', ''): + raise BadVersionException(); + # The 'error' header is included if there was an issue if 'error' in response: raise Exception('[Error] %s' % (response['error'])) @@ -1165,7 +1134,7 @@ def download_network_weights(config, branch): return network_path -def download_engine(arguments, branch, network): +def download_engine(config, branch, network): engine = config.workload['test'][branch]['engine'] branch_name = config.workload['test'][branch]['name'] @@ -1366,16 +1335,14 @@ def run_and_parse_cutechess(config, command, cutechess_idx, results_queue, abort # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # -if __name__ == '__main__': +def run_openbench_worker(args): - config = Configuration() # System info, Cmdline arguments, and Workload + config = Configuration(args) # System info, Cmdline arguments, and Workload - cutechess_error = '[Note] Unable to fetch Cutechess location and download it!' setup_error = '[Note] Unable to establish initial connection with the Server!' connection_error = '[Note] Unable to reach the server to request a workload!' - try_forever(server_download_cutechess, [config], cutechess_error) - try_forever(server_configure_worker, [config], setup_error ) + try_forever(server_configure_worker, [config], setup_error) while True: try: @@ -1399,6 +1366,11 @@ if __name__ == '__main__': print('Exited via openbench.exit') break + except BadVersionException: + raise BadVersionException() + except Exception: + raise BadVersionException() traceback.print_exc() time.sleep(TIMEOUT_ERROR) + diff --git a/Config/config.json b/Config/config.json index 63a5358..34bd8f2 100644 --- a/Config/config.json +++ b/Config/config.json @@ -1,7 +1,7 @@ { - "client_version" : 18, + "client_version" : 19, "client_repo_url" : "https://github.com/AndyGrant/OpenBench", - "client_repo_ref" : "config", + "client_repo_ref" : "auto_client", "use_cross_approval" : false, "require_login_to_view" : true,