-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathexecute.py
63 lines (49 loc) · 2.61 KB
/
execute.py
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
# grown-up modules
import compose.cli.command
import docker
import logging
import os
# local modules
import context
if __name__ == "__main__":
import argparse
import logs
parser = argparse.ArgumentParser(description='Run commands on a running container as iRODS service account.')
parser.add_argument('project_path', metavar='PROJECT_PATH', type=str,
help='Path to the docker-compose project on which packages will be installed.')
parser.add_argument('commands', metavar='COMMANDS', nargs='+',
help='Space-delimited list of commands to be run')
parser.add_argument('--run-on-container', '-t', metavar='TARGET_CONTAINER', dest='run_on', type=str,
help='The name of the container on which the command will run. By default, runs on all containers in project.')
parser.add_argument('--project-name', metavar='PROJECT_NAME', type=str, dest='project_name',
help='Name of the docker-compose project on which to install packages.')
parser.add_argument('--verbose', '-v', dest='verbosity', action='count', default=1,
help='Increase the level of output to stdout. CRITICAL and ERROR messages will always be printed.')
parser.add_argument('--user', '-u', metavar='USER', dest='user', default='root',
help='Name of the user to be when executing the commands.')
parser.add_argument('--workdir', '-w', metavar='WORKING_DIRECTORY', dest='workdir', default='/',
help='Working directory for execution of commands.')
args = parser.parse_args()
logs.configure(args.verbosity)
ec = 0
containers = list()
docker_client = docker.from_env()
try:
p = compose.cli.command.get_project(os.path.abspath(args.project_path), project_name=args.project_name)
# Get the container on which the command is to be executed
containers = list()
if args.run_on:
containers.append(docker_client.containers.get(args.run_on))
else:
containers = p.containers()
# Serially execute the list of commands provided in the input
for c in containers:
if context.is_catalog_database_container(c): continue
target_container = docker_client.containers.get(c.name)
for command in args.commands:
# TODO: on --continue, save only failure ec's/commands
ec = execute_command(target_container, command, user=args.user, workdir=args.workdir, stream_output=True)
except Exception as e:
logging.critical(e)
raise
exit(ec)