Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Open sidebar
limbra
limbra
Commits
36510812
Commit
36510812
authored
Mar 12, 2016
by
LE GAC Renaud
Browse files
The run script works when the web2py is running in a docker container.
parent
6f93a780
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
20 deletions
+94
-20
scripts/run
scripts/run
+94
-20
No files found.
scripts/run
View file @
36510812
...
...
@@ -7,9 +7,17 @@
run [options] <command> [args]
DESCRIPTION
The command has to be run in the scripts directory
It can execute the subcommands 'dump', 'loop', 'mysql', 'script'
and 'pytest'.
the application framework can be located in the localhost or in
a docker container.
The user should belong to the docker group
when using the latter.
The command has to be run in the scripts directory when the
framework is in the localhost.
The command can execute the subcommands 'dump', 'loop', 'mysql',
'script' and 'pytest'.
The same software can serve several applications with only differ
by their databases. the subcommand can be applies to one of the
...
...
@@ -101,6 +109,7 @@ import subprocess
import
sys
DOCKER
=
"/usr/bin/docker"
MSG_DUMP
=
'
\n
Dump the database of "%s" [y/N]: '
MSG_SCRIPT
=
'
\n
Execute "%s" on "%s" [y/N]: '
MYSQL
=
'/usr/bin/mysql'
...
...
@@ -108,8 +117,9 @@ MYSQLDUMP = '/usr/bin/mysqldump'
PYTEST_SCRIPT
=
'_run_pytest.py'
REG_MYSQL
=
re
.
compile
(
r
"mysql://(\w+):(\w+)@([\w\.]+)/(\w+)"
)
TEST_DIR
=
'tests'
WEB2PY_DIR
=
'../../web2py'
WEB2PY
=
os
.
path
.
join
(
WEB2PY_DIR
,
'web2py.py'
)
WEB2PY
=
"web2py.py"
W2P_CNT
=
"/opt/web2py"
W2P_DIR
=
'/opt/web2py'
def
get_application
():
...
...
@@ -125,6 +135,49 @@ def get_application():
return
os
.
getcwd
().
split
(
os
.
sep
)[
-
2
]
def
get_web2py
(
args
):
"""Return the command to execute the web2py.py script.
The script can be located either in the localhost or within
a docker container. The preference is given to the localhost.
Args:
args (argparse.Namespace): the argument docker_container and web2py_dir
are used to localised the web2py.py script
Return
list: the command to execute the web2py.py script
"""
cmd
=
[]
# web2py is on the local system
if
os
.
path
.
exists
(
args
.
web2py_dir
):
cmd
.
append
(
os
.
path
.
join
(
W2P_DIR
,
WEB2PY
))
return
cmd
# web2py is in a docker container
# Check if it is running
elif
args
.
docker_container
:
out
=
subprocess
.
check_output
([
DOCKER
,
"ps"
])
if
out
:
for
line
in
out
.
split
(
"
\n
"
):
li
=
line
.
split
()
if
args
.
docker_container
==
li
[
-
1
]:
cmd
=
[
DOCKER
,
"exec"
,
"-it"
,
args
.
docker_container
,
os
.
path
.
join
(
W2P_CNT
,
WEB2PY
)]
return
cmd
print
"
\n\t
web2py.py is not found !"
print
"
\t
Directory"
,
args
.
web2py_dir
,
"don't exists !"
print
"
\t
Docker container"
,
args
.
docker_container
,
"don't exist !"
print
"
\t
Tune the option --web2py-dir or --docker-container.
\n
"
sys
.
exit
(
1
)
def
mysql
(
dburi
,
script
):
"""Exceute the script on the mysql database.
...
...
@@ -191,13 +244,26 @@ def process(application, script, args):
int: return code of the subprocess
"""
script_path
=
os
.
path
.
join
(
os
.
getcwd
(),
script
)
cmd
=
get_web2py
(
ARGS
)
# the script path depends on the localisation of web2py.py
# either on the localhost or within a docker container
# the length of the command allows to separate the two cases
cmd
=
[
WEB2PY
,
'--no-banner'
,
'--shell'
,
application
,
'--import_models'
,
'--run'
,
script_path
]
if
len
(
cmd
)
==
1
:
script_path
=
os
.
path
.
join
(
os
.
getcwd
(),
script
)
else
:
script_path
=
os
.
path
.
join
(
W2P_CNT
,
"applications"
,
application
,
"scripts"
,
script
)
cmd
.
extend
([
'--no-banner'
,
'--shell'
,
application
,
'--import_models'
,
'--run'
,
script_path
])
if
args
:
cmd
.
extend
([
'--args'
,
args
])
...
...
@@ -210,7 +276,7 @@ def run_dump(args):
It used the command mysqldump.
Args:
args
args
(argparse.Namespace):
"""
# instantiate the DBURIS dictionary
...
...
@@ -232,7 +298,7 @@ def run_loop(args):
"""Run the python script on several applications.
Args:
args
args
(argparse.Namespace):
"""
# instantiate the DBURIS dictionary
...
...
@@ -253,7 +319,7 @@ def run_mysql(args):
"""Run a sql script on all the databases defined in the DBURIS dictionary.
Args:
args
args
(argparse.Namespace):
"""
# instantiate the DBURIS dictionary
...
...
@@ -275,7 +341,7 @@ def run_pytest(args):
"""Run python test.
Args:
args
args
(argparse.Namespace):
"""
cmd
=
""
...
...
@@ -324,16 +390,24 @@ if __name__ == "__main__":
print
"Should be run in the scripts directory."
sys
.
exit
(
1
)
if
not
os
.
path
.
exists
(
WEB2PY
):
print
"The application web2py is not located in ../../web2py."
sys
.
exit
(
1
)
# command line options
PARSER
=
ArgumentParser
()
PARSER
.
add_argument
(
"-d"
,
"--docker-container"
,
default
=
"dev"
,
help
=
"docker container running web2py [%(default)s]"
,
metavar
=
"<name>"
)
PARSER
.
add_argument
(
"-w"
,
"--web2py-dir"
,
default
=
W2P_DIR
,
help
=
"local web2py directory [%(default)s]."
,
metavar
=
"<path>"
)
PARSER
.
add_argument
(
"-S"
,
"--shell"
,
default
=
get_application
(),
help
=
"run web2py in interactive shell "
"with specified appname [%(default)s]"
)
"with specified appname [%(default)s]"
,
metavar
=
"<application>"
)
SUBPARSERS
=
PARSER
.
add_subparsers
(
title
=
"subcommands"
,
description
=
"valid subcommands"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment