PYROSW.py
5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/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 $*
'''