From 094e512fb5eb5373b86f306f87f207ab89604152 Mon Sep 17 00:00:00 2001 From: naderzare Date: Sun, 1 Sep 2024 15:55:06 -0300 Subject: [PATCH 1/6] add download scripts --- scripts/download-proxy.sh | 22 ++++++++++++++++++++++ scripts/download-rcssserver.sh | 24 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100755 scripts/download-proxy.sh create mode 100755 scripts/download-rcssserver.sh diff --git a/scripts/download-proxy.sh b/scripts/download-proxy.sh new file mode 100755 index 0000000..e8b58d9 --- /dev/null +++ b/scripts/download-proxy.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +# check proxy directory exists, if exists, remove it +if [ -d proxy ]; then + echo "proxy directory exists, remove it" + rm -rf proxy +fi + +mkdir proxy + +cd proxy + +# download soccer simulation proxy +wget $(curl -s "https://api.github.com/repos/clsframework/soccer-simulation-proxy/releases/latest" | grep -oP '"browser_download_url": "\K[^"]*' | grep "soccer-simulation-proxy.tar.gz") + +tar -xvf soccer-simulation-proxy.tar.gz + +mv soccer-simulation-proxy/* . + +rm -rf soccer-simulation-proxy + +rm soccer-simulation-proxy.tar.gz diff --git a/scripts/download-rcssserver.sh b/scripts/download-rcssserver.sh new file mode 100755 index 0000000..9eb5a82 --- /dev/null +++ b/scripts/download-rcssserver.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +# check rcssserver directory exists, if exists, remove it +if [ -d rcssserver ]; then + echo "rcssserver directory exists, remove it" + rm -rf rcssserver +fi + +mkdir rcssserver + +cd rcssserver + +# download soccer simulation server App Image +wget $(curl -s https://api.github.com/repos/clsframework/rcssserver/releases/latest | grep -oP '"browser_download_url": "\K(.*rcssserver-x86_64-.*\.AppImage)' | head -n 1) + +# check download is successful +if [ ! -f *.AppImage ]; then + echo "Download failed" + exit 1 +fi + +mv rcssserver-x86_64-*.AppImage rcssserver + +chmod +x rcssserver \ No newline at end of file From 0a5ba877e584197991d03d4d25a19200d9320897 Mon Sep 17 00:00:00 2001 From: naderzare Date: Sun, 1 Sep 2024 15:55:58 -0300 Subject: [PATCH 2/6] add start-team scripts --- check_requirements.py | 20 +++++++++++ start-team.py | 84 +++++++++++++++++++++++++++++++++++++++++++ start-team.sh | 70 ++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+) create mode 100644 check_requirements.py create mode 100644 start-team.py create mode 100755 start-team.sh diff --git a/check_requirements.py b/check_requirements.py new file mode 100644 index 0000000..7c0047b --- /dev/null +++ b/check_requirements.py @@ -0,0 +1,20 @@ + +import pkg_resources +import sys + +def check_requirements(requirements_file='requirements.txt'): + with open(requirements_file, 'r') as file: + requirements = file.readlines() + + for requirement in requirements: + requirement = requirement.strip() + try: + pkg_resources.require(requirement) + except pkg_resources.VersionConflict as e: + print(f"WARNING: {str(e)}") + except pkg_resources.DistributionNotFound as e: + print(f"ERROR: {str(e)}") + sys.exit(1) + +if __name__ == "__main__": + check_requirements() diff --git a/start-team.py b/start-team.py new file mode 100644 index 0000000..576b936 --- /dev/null +++ b/start-team.py @@ -0,0 +1,84 @@ +import subprocess +import os +import signal +import threading +import logging +import argparse +import check_requirements + + +# Set up logging +logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') + +def run_server_script(): + # Start the server.py script as a new process group + process = subprocess.Popen( + ['python3', 'server.py'], + preexec_fn=os.setsid, # Create a new session and set the process group ID + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT # Capture stderr and redirect it to stdout + ) + return process + +def run_start_script(args): + # Start the start.sh script in its own directory as a new process group + process = subprocess.Popen( + ['bash', 'start.sh', '-t', args.team_name], + cwd='scripts/proxy', # Corrected directory to where start.sh is located + preexec_fn=os.setsid, # Create a new session and set the process group ID + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT # Capture stderr and redirect it to stdout + ) + return process + +def stream_output(process, prefix): + # Stream output from the process and log it with a prefix + for line in iter(process.stdout.readline, b''): + logging.debug(f'{prefix} {line.decode().strip()}') + process.stdout.close() + +def kill_process_group(process): + try: + os.killpg(os.getpgid(process.pid), signal.SIGTERM) # Send SIGTERM to the process group + except ProcessLookupError: + pass # The process might have already exited + +if __name__ == "__main__": + # Set up argument parsing + parser = argparse.ArgumentParser(description='Run server and team scripts.') + parser.add_argument('-t', '--team_name', required=False, help='The name of the team', default='CLS') + args = parser.parse_args() + + try: + # Check Python requirements + logging.debug("Checking Python requirements...") + check_requirements.check_requirements() + + # Run the server.py script first + server_process = run_server_script() + logging.debug(f"Started server.py process with PID: {server_process.pid}") + + # Run the start.sh script after server.py with the given arguments + start_process = run_start_script(args) + logging.debug(f"Started start.sh process with PID: {start_process.pid} with team name {args=}") + + # Monitor both processes and log their outputs + server_thread = threading.Thread(target=stream_output, args=(server_process, 'server:')) + start_thread = threading.Thread(target=stream_output, args=(start_process, 'team:')) + + server_thread.start() + start_thread.start() + + # Wait for both threads to finish + server_thread.join() + start_thread.join() + + except KeyboardInterrupt: + logging.debug("Interrupted! Killing all processes.") + kill_process_group(server_process) + kill_process_group(start_process) + + finally: + # Ensure all processes are killed on exit + kill_process_group(server_process) + kill_process_group(start_process) diff --git a/start-team.sh b/start-team.sh new file mode 100755 index 0000000..82382f4 --- /dev/null +++ b/start-team.sh @@ -0,0 +1,70 @@ +#!/bin/bash + +# Ensure the script exits if any command fails +set -e +# check scripts/proxy directory does not exist, raise error +if [! -d scripts/proxy ]; then + echo "scripts/proxy directory does not exist" + exit 1 +fi + +team_name="CLS" + +# help function +usage() { + echo "Usage: $0 [options]" + echo "Options:" + echo " -t team_name: specify team name" + exit 1 +} + +while [ $# -gt 0 ] +do + case $1 in + -t) + team_name=$2 + shift + ;; + *) + echo 1>&2 + echo "invalid option \"${1}\"." 1>&2 + echo 1>&2 + usage + exit 1 + ;; + esac + + shift 1 +done + +# Check Python requirements +echo "Checking Python requirements..." +python3 check_requirements.py + +# Start server.py in the background +echo "Starting server.py..." +python3 server.py & +server_pid=$! + +# Function to kill server and team processes on exit +cleanup() { + echo "Cleaning up..." + kill $server_pid + kill $start_pid +} + +# Trap the exit signal to cleanup processes +trap cleanup EXIT + +# Wait a moment to ensure the server has started (optional) +sleep 2 + +# Start start.sh script in the correct directory with arguments +echo "Starting start.sh with team name: $team_name and ..." +cd scripts/proxy +bash start.sh -t "$team_name" & +start_pid=$! + +# Wait for both background processes to finish +wait $server_pid +wait $start_pid From c5f97594e6ee14f8ee0b87392aedad8fdba7b63a Mon Sep 17 00:00:00 2001 From: naderzare Date: Sun, 1 Sep 2024 15:56:10 -0300 Subject: [PATCH 3/6] add gitignore --- .gitignore | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c9eb2a --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +.idea/.gitignore +.idea/misc.xml +.idea/modules.xml +.idea/sample-playmaker-server-python-thrift.iml +.idea/vcs.xml +.idea/inspectionProfiles/profiles_settings.xml +.idea/inspectionProfiles/Project_Default.xml +venv/ +utils/__pycache__/PFProcessServer.cpython-310.pyc +soccer/__pycache__/ +scripts/proxy +scripts/rcssserver +__pycache__/ From 33c73293a6ac626233216d93d4dedfacfe7d07cc Mon Sep 17 00:00:00 2001 From: naderzare Date: Sun, 1 Sep 2024 16:05:49 -0300 Subject: [PATCH 4/6] add g-port to scripts and server --- server.py | 7 ++++++- start-team.py | 9 +++++---- start-team.sh | 10 ++++++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/server.py b/server.py index 71db0ee..7138511 100644 --- a/server.py +++ b/server.py @@ -14,6 +14,8 @@ from multiprocessing import Manager, Lock import logging from pyrusgeom.vector_2d import Vector2D +import argparse + logging.basicConfig(level=logging.DEBUG) @@ -161,4 +163,7 @@ def serve(port): if __name__ == '__main__': - serve(50051) + parser = argparse.ArgumentParser(description='Run play maker server') + parser.add_argument('-p', '--g-port', required=False, help='The port of the server', default=50051) + args = parser.parse_args() + serve(args.g_port) diff --git a/start-team.py b/start-team.py index 576b936..61a6e23 100644 --- a/start-team.py +++ b/start-team.py @@ -10,10 +10,10 @@ # Set up logging logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s') -def run_server_script(): +def run_server_script(args): # Start the server.py script as a new process group process = subprocess.Popen( - ['python3', 'server.py'], + ['python3', 'server.py', '--g-port', args.g_port], preexec_fn=os.setsid, # Create a new session and set the process group ID stdout=subprocess.PIPE, stderr=subprocess.STDOUT # Capture stderr and redirect it to stdout @@ -23,7 +23,7 @@ def run_server_script(): def run_start_script(args): # Start the start.sh script in its own directory as a new process group process = subprocess.Popen( - ['bash', 'start.sh', '-t', args.team_name], + ['bash', 'start.sh', '-t', args.team_name, '--g-port', args.g_port], cwd='scripts/proxy', # Corrected directory to where start.sh is located preexec_fn=os.setsid, # Create a new session and set the process group ID stdout=subprocess.PIPE, @@ -47,6 +47,7 @@ def kill_process_group(process): # Set up argument parsing parser = argparse.ArgumentParser(description='Run server and team scripts.') parser.add_argument('-t', '--team_name', required=False, help='The name of the team', default='CLS') + parser.add_argument('--g-port', required=False, help='The port of the server', default=50051) args = parser.parse_args() try: @@ -55,7 +56,7 @@ def kill_process_group(process): check_requirements.check_requirements() # Run the server.py script first - server_process = run_server_script() + server_process = run_server_script(args) logging.debug(f"Started server.py process with PID: {server_process.pid}") # Run the start.sh script after server.py with the given arguments diff --git a/start-team.sh b/start-team.sh index 82382f4..5492057 100755 --- a/start-team.sh +++ b/start-team.sh @@ -9,12 +9,14 @@ if [! -d scripts/proxy ]; then fi team_name="CLS" +g_port=50051 # help function usage() { echo "Usage: $0 [options]" echo "Options:" echo " -t team_name: specify team name" + echo " --g-port GRPC PORT - specifies grpc port (default: 50051)" exit 1 } @@ -25,6 +27,10 @@ do team_name=$2 shift ;; + --g-port) + g_port=$2 + shift + ;; *) echo 1>&2 echo "invalid option \"${1}\"." 1>&2 @@ -43,7 +49,7 @@ python3 check_requirements.py # Start server.py in the background echo "Starting server.py..." -python3 server.py & +python3 server.py --g-port $g_port & server_pid=$! # Function to kill server and team processes on exit @@ -62,7 +68,7 @@ sleep 2 # Start start.sh script in the correct directory with arguments echo "Starting start.sh with team name: $team_name and ..." cd scripts/proxy -bash start.sh -t "$team_name" & +bash start.sh -t "$team_name" --g-port $g_port & start_pid=$! # Wait for both background processes to finish From 099759a366c91b401c0217fd4064e76cf876a326 Mon Sep 17 00:00:00 2001 From: naderzare Date: Mon, 2 Sep 2024 17:43:56 -0300 Subject: [PATCH 5/6] fix a bug in start-team.py --- .gitignore | 1 + start-team.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4c9eb2a..427aef1 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ .idea/inspectionProfiles/profiles_settings.xml .idea/inspectionProfiles/Project_Default.xml venv/ +.venv/ utils/__pycache__/PFProcessServer.cpython-310.pyc soccer/__pycache__/ scripts/proxy diff --git a/start-team.py b/start-team.py index 61a6e23..4ec7f4f 100644 --- a/start-team.py +++ b/start-team.py @@ -47,7 +47,7 @@ def kill_process_group(process): # Set up argument parsing parser = argparse.ArgumentParser(description='Run server and team scripts.') parser.add_argument('-t', '--team_name', required=False, help='The name of the team', default='CLS') - parser.add_argument('--g-port', required=False, help='The port of the server', default=50051) + parser.add_argument('--g-port', required=False, help='The port of the server', default='50051') args = parser.parse_args() try: From 455a800103312c6764f0ee1a4759e162c63f6a28 Mon Sep 17 00:00:00 2001 From: naderzare Date: Mon, 2 Sep 2024 17:49:35 -0300 Subject: [PATCH 6/6] update download scripts --- scripts/download-proxy.sh | 16 ++++++++++++++++ scripts/download-rcssserver.sh | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/scripts/download-proxy.sh b/scripts/download-proxy.sh index e8b58d9..58bbba8 100755 --- a/scripts/download-proxy.sh +++ b/scripts/download-proxy.sh @@ -10,6 +10,22 @@ mkdir proxy cd proxy +# Check if curl exists +if command -v curl >/dev/null 2>&1; then + echo "curl is installed." +else + echo "curl is not installed. Please install it." + exit 1 +fi + +# Check if get exists +if command -v wget >/dev/null 2>&1; then + echo "wget is installed." +else + echo "wget is not installed. Please install it." + exit 1 +fi + # download soccer simulation proxy wget $(curl -s "https://api.github.com/repos/clsframework/soccer-simulation-proxy/releases/latest" | grep -oP '"browser_download_url": "\K[^"]*' | grep "soccer-simulation-proxy.tar.gz") diff --git a/scripts/download-rcssserver.sh b/scripts/download-rcssserver.sh index 9eb5a82..3696619 100755 --- a/scripts/download-rcssserver.sh +++ b/scripts/download-rcssserver.sh @@ -10,6 +10,22 @@ mkdir rcssserver cd rcssserver +# Check if curl exists +if command -v curl >/dev/null 2>&1; then + echo "curl is installed." +else + echo "curl is not installed. Please install it." + exit 1 +fi + +# Check if get exists +if command -v wget >/dev/null 2>&1; then + echo "wget is installed." +else + echo "wget is not installed. Please install it." + exit 1 +fi + # download soccer simulation server App Image wget $(curl -s https://api.github.com/repos/clsframework/rcssserver/releases/latest | grep -oP '"browser_download_url": "\K(.*rcssserver-x86_64-.*\.AppImage)' | head -n 1)