#!/usr/bin/env python3 ''' Shell scripting with python : - https://www.freecodecamp.org/news/python-for-system-administration-tutorial - https://github.com/ninjaaron/replacing-bash-scripting-with-python#if-the-shell-is-so-great-what-s-the-problem - https://linuxize.com/post/python-get-change-current-working-directory USAGE: ON WINDOWS: Via PyROS Wrapper .\PYROS --docker start agentScheduler -o tnc -fg -- docker option as FIRST parameter -> force script to use docker (Useful when you have PyROS and Docker installed locally) OR .\PYROS start agentScheduler -o tnc -fg To use PyROS locally ''' import subprocess from shutil import which import os import sys from time import sleep DEBUG = False #DEBUG = True # print(DEBUG) def run(command: str): #print(command.split(' ')) return subprocess.run(command.split(' ')) #result = subprocess.run(command.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Guess Context (3 possibilities) : # - NO DOCKER, # or # - DOCKER OUT of container, # or # - or DOCKER IN container WITH_DOCKER_IS_SET = False if os.getenv('WITH_DOCKER') is None else True # print(WITH_DOCKER_IS_SET) # APP_FOLDER=False # [ -d ../app/ ] && APP_FOLDER=true APP_FOLDER = os.path.exists('../app/') VENV = False args = sys.argv[1:] if args[0] == "--venv": VENV = True args.pop(0) args = ' '.join(args) # test if docker is installed # [ -x "$(command -v docker)" ] && DOCKER_CMD=true DOCKER_CMD = which('docker') is not None # Pas utile vu qu'on va redémarrer systématiquement le container # test if container is running # DOCKER_CONTAINER_STARTED=false # $DOCKER_CMD && [ $(docker ps | grep 'pyros' | wc -l) -eq 2 ] && DOCKER_CONTAINER_STARTED=true # # SYNTHESIS # # DOCKER=false # [[ $VENV == false && $DOCKER_CMD == true ]] && DOCKER=true DOCKER = DOCKER_CMD and not VENV DOCKER_OUT_CONTAINER = DOCKER and not WITH_DOCKER_IS_SET # [[ $DOCKER == false && $WITH_DOCKER_IS_SET == true ]] && DOCKER_IN_CONTAINER=true if DEBUG: print(APP_FOLDER) print(VENV) print(DOCKER_CMD) # print(container) # Synthesis print(DOCKER) print(DOCKER_OUT_CONTAINER) print(sys.argv) exit(0) # no container ? => start container first # [ $container == false ] && cd docker/ && docker-compose up -d # print(args) #PYROS_CMD = "python3 pyros.py --help" #PYROS_CMD = "python3 pyros.py $*" PYROS_CMD = "python3 pyros.py " + args PYROS_CMD = PYROS_CMD.rstrip() # DOCKER_OUT_CONTAINER true ? => docker exec # docker exec -it pyros python3 pyros.py $* # [ $DOCKER_OUT_CONTAINER == true ] && cd docker/ && docker-compose up -d && PREFIX='docker exec -it pyros' if DOCKER_OUT_CONTAINER: # cd docker/ os.chdir('docker') # docker-compose up -d res = subprocess.run("docker-compose exec pyros python3 pyros.py",shell=True,stdout=subprocess.DEVNULL) if res.returncode != 0: run('docker-compose up -d') sleep(5) PYROS_CMD = 'docker-compose exec pyros ' + PYROS_CMD if VENV: does_venv_folder_exists = os.path.exists("./venv") print(does_venv_folder_exists) print(WITH_DOCKER_IS_SET) if not does_venv_folder_exists and not WITH_DOCKER_IS_SET: print("No virtual environment available.") exit(0) path_to_bin_folder = "" print("\n Executing command in venv :", PYROS_CMD, "\n") if os.name == "posix": res = run(f"./venv/venv_py3_pyros/"+PYROS_CMD) elif os.name == "nt": res = run("./venv/venv_py3_pyros/Scripts/python.exe pyros.py "+ args) else: # java pass else: # PYROS_CMD print("\n Executing command :", PYROS_CMD, "\n") res = run(PYROS_CMD) ''' #Print the stdout and stderr print() print(result.args) print() print(result.stdout) print() print(result.stderr) ''' ''' ########################## # Script BASH equivalent # ########################## #!/usr/bin/env bash DEBUG=true DEBUG=false # test if user passed a command as parameter #if test $# -lt 1 ; then #echo "Missing command, use one of the commands below" #python3 pyros.py --help #exit #fi # Guess Context : # - NO DOCKER, # or # - DOCKER OUT of container, # or # - or DOCKER IN container WITH_DOCKER_IS_SET=true [ -z $WITH_DOCKER ] && WITH_DOCKER_IS_SET=false APP_FOLDER=false [ -d ../app/ ] && APP_FOLDER=true VENV=false [ -d ./venv/ ] && VENV=true # test if docker is installed DOCKER_CMD=false [ -x "$(command -v docker)" ] && DOCKER_CMD=true # test if container is running DOCKER_CONTAINER_STARTED=false $DOCKER_CMD && [ $(docker ps | grep 'pyros' | wc -l) -eq 2 ] && DOCKER_CONTAINER_STARTED=true # # SYNTHESIS # DOCKER=false [[ $VENV == false && $DOCKER_CMD == true ]] && DOCKER=true DOCKER_OUT_CONTAINER=false [[ $DOCKER == true && $WITH_DOCKER_IS_SET == false ]] && DOCKER_OUT_CONTAINER=true #[[ $DOCKER == false && $WITH_DOCKER_IS_SET == true ]] && DOCKER_IN_CONTAINER=true if $DEBUG ; then echo $APP_FOLDER echo $VENV echo $DOCKER_CMD echo $container # Synthesis echo $DOCKER echo $DOCKER_OUT_CONTAINER exit fi # no container ? => start container first #[ $container == false ] && cd docker/ && docker-compose up -d # DOCKER_OUT_CONTAINER true ? docker exec PREFIX='' #docker exec -it pyros python3 pyros.py $* [ $DOCKER_OUT_CONTAINER == true ] && cd docker/ && docker-compose up -d && PREFIX='docker exec -it pyros' echo $PREFIX $PREFIX python3 pyros.py $* '''