Blame view

PYROSW.py 5.27 KB
c3b513dc   Etienne Pallier   new script PYROS_...
1
2
3
4
5
6
7
#!/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
c3b513dc   Etienne Pallier   new script PYROS_...
8

c3b513dc   Etienne Pallier   new script PYROS_...
9
10


83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
11
12
13
14
15
16
17
18
19
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
'''
c3b513dc   Etienne Pallier   new script PYROS_...
20

83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
21
22
23
24
25
26
27
28
import subprocess
from shutil import which
import os
import sys
from time import sleep
DEBUG = False
#DEBUG = True
# print(DEBUG)
c3b513dc   Etienne Pallier   new script PYROS_...
29
30


83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
31
def run(command: str):
c3b513dc   Etienne Pallier   new script PYROS_...
32
33
34
35
36
    #print(command.split(' '))
    return subprocess.run(command.split(' '))
    #result = subprocess.run(command.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE)


83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
37
38
# Guess Context (3 possibilities) :
# - NO DOCKER,
c3b513dc   Etienne Pallier   new script PYROS_...
39
# or
83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
40
# - DOCKER OUT of container,
c3b513dc   Etienne Pallier   new script PYROS_...
41
42
43
44
# or
# - or DOCKER IN container

WITH_DOCKER_IS_SET = False if os.getenv('WITH_DOCKER') is None else True
83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
45
# print(WITH_DOCKER_IS_SET)
c3b513dc   Etienne Pallier   new script PYROS_...
46
47


83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
48
49
# APP_FOLDER=False
# [ -d ../app/ ] && APP_FOLDER=true
c3b513dc   Etienne Pallier   new script PYROS_...
50
51
APP_FOLDER = os.path.exists('../app/')

0e1af846   Alexis Koralewski   Changing option o...
52
VENV = False
c3b513dc   Etienne Pallier   new script PYROS_...
53

83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
54
args = sys.argv[1:]
0e1af846   Alexis Koralewski   Changing option o...
55
56
if args[0] == "--venv":
	VENV = True
83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
57
58
59
60
	args.pop(0)
args = ' '.join(args)
# test if docker is installed
# [ -x "$(command -v docker)" ] && DOCKER_CMD=true
c3b513dc   Etienne Pallier   new script PYROS_...
61
62
63
64
65
DOCKER_CMD = which('docker') is not None


# Pas utile vu qu'on va redémarrer systématiquement le container
# test if container is running
83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
66
67
# DOCKER_CONTAINER_STARTED=false
# $DOCKER_CMD && [ $(docker ps | grep 'pyros' | wc -l) -eq 2 ] && DOCKER_CONTAINER_STARTED=true
c3b513dc   Etienne Pallier   new script PYROS_...
68
69
70
71
72

#
# SYNTHESIS
#

83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
73
74
# DOCKER=false
# [[ $VENV == false && $DOCKER_CMD == true ]] && DOCKER=true
c3b513dc   Etienne Pallier   new script PYROS_...
75
76
77
DOCKER = DOCKER_CMD and not VENV

DOCKER_OUT_CONTAINER = DOCKER and not WITH_DOCKER_IS_SET
83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
78
# [[ $DOCKER == false && $WITH_DOCKER_IS_SET == true ]] && DOCKER_IN_CONTAINER=true
c3b513dc   Etienne Pallier   new script PYROS_...
79

83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
80
81
82
83
84
85
86
87
if DEBUG:
    print(APP_FOLDER)
    print(VENV)
    print(DOCKER_CMD)
    # print(container)
    # Synthesis
    print(DOCKER)
    print(DOCKER_OUT_CONTAINER)
c3b513dc   Etienne Pallier   new script PYROS_...
88

83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
89
90
    print(sys.argv)
    exit(0)
c3b513dc   Etienne Pallier   new script PYROS_...
91
92

# no container ? => start container first
83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
93
# [ $container == false ] && cd docker/ && docker-compose up -d
c3b513dc   Etienne Pallier   new script PYROS_...
94

83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
95
96

# print(args)
c3b513dc   Etienne Pallier   new script PYROS_...
97
98
99
100
101
102
103

#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
83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
104
105
106
107
108
109
# 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
734508a7   Alexis Koralewski   Fixing issues for...
110
    res = subprocess.run("docker exec -it pyros python3 pyros.py",shell=True,stdout=subprocess.DEVNULL)
83b4b4b2   Alexis Koralewski   Fixing PYROS.bat,...
111
112
113
    if res.returncode != 0:
        run('docker-compose up -d')
        sleep(5)
c3b513dc   Etienne Pallier   new script PYROS_...
114
115
    PYROS_CMD = 'docker exec -it pyros ' + PYROS_CMD

734508a7   Alexis Koralewski   Fixing issues for...
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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)
c3b513dc   Etienne Pallier   new script PYROS_...
136
137
138
139
140
141
142
143
144
145
146

'''
#Print the stdout and stderr
print()
print(result.args)
print()
print(result.stdout)
print()
print(result.stderr)
'''

c3b513dc   Etienne Pallier   new script PYROS_...
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

'''
##########################
# 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 $*

'''