Commit 366b189c authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Copy the run script from limbra in order to run behind a container.

parent 8526c313
......@@ -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
......@@ -19,7 +27,7 @@
context (model, modules and database are available). By default
the application is the current one.
> cd ... track_publications/scripts
> cd ...limbra/scripts
> ./run script my_script.py
When the same software serves several applications, it is possible
......@@ -62,12 +70,12 @@
Display the help and exit.
-S, --shell
By default the application is track_publications.
By default the application is limbra.
This allow to select another one.
EXAMPLE
> cd ... track_publications/scripts
> cd ... limbra/scripts
> ./run -h
......@@ -101,6 +109,7 @@ import subprocess
import sys
DOCKER = "/usr/bin/docker"
MSG_DUMP = '\nDump the database of "%s" [y/N]: '
MSG_SCRIPT = '\nExecute "%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 li and args.docker_container == li[-1]:
cmd = [DOCKER,
"exec",
"-it",
args.docker_container,
os.path.join(W2P_CNT, WEB2PY)]
return cmd
print "\n\tweb2py.py is not found on the local host!"
print "\tNo directory", args.web2py_dir, "on the local host!"
print "\tDocker container", args.docker_container, "is not running!"
print "\tStart container or tune option --web2py-dir, --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,15 +341,16 @@ def run_pytest(args):
"""Run python test.
Args:
args
args (argparse.Namespace):
"""
cmd = ""
skip_options = ("docker_container", "func", "path", "shell", "web2py_dir")
# collect active option
for k, v in vars(args).iteritems():
if not v or k in ("func", "path", "shell"):
if (not v) or (k in skip_options):
continue
elif k == "capture":
......@@ -324,16 +391,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",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment