comparison env/lib/python3.7/site-packages/planemo/database/postgres_docker.py @ 2:6af9afd405e9 draft

"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author shellac
date Thu, 14 May 2020 14:56:58 -0400
parents 26e78fe6e8c4
children
comparison
equal deleted inserted replaced
1:75ca89e9b81c 2:6af9afd405e9
1 """Module describes a :class:`DatabaseSource` for managed, dockerized postgres databases."""
2 import time
3
4 from galaxy.tool_util.deps import (
5 docker_util,
6 dockerfiles,
7 )
8 from galaxy.tool_util.deps.commands import execute
9 from galaxy.util import unicodify
10
11 from .interface import DatabaseSource
12 from .postgres import _CommandBuilder, ExecutesPostgresSqlMixin
13
14 DEFAULT_CONTAINER_NAME = "planemopostgres"
15 DEFAULT_POSTGRES_PASSWORD = "mysecretpassword"
16 DEFAULT_POSTGRES_PORT_EXPOSE = 15432
17
18
19 def docker_ps(args, **kwds):
20 return docker_util.command_list("ps", args, **kwds)
21
22
23 def docker_exec(name, commands=[], **kwds):
24 return docker_util.command_list("exec", [name] + commands, **kwds)
25
26
27 def is_running_container(name=DEFAULT_CONTAINER_NAME, **kwds):
28 ps_command = docker_ps(["--format", "{{.Names}}"], **kwds)
29 running_containers = unicodify(execute(ps_command))
30 containers = running_containers.splitlines()
31 return name in containers
32
33
34 def start_postgres_docker(name=DEFAULT_CONTAINER_NAME, password=DEFAULT_POSTGRES_PASSWORD, port=DEFAULT_POSTGRES_PORT_EXPOSE, **kwds):
35 run_command = docker_util.command_list(
36 "run",
37 ["-p", "%d:5432" % port, "--name", name, "-e", "POSTGRES_PASSWORD=%s" % password, "--rm", "-d", "postgres"],
38 **kwds
39 )
40 execute(run_command)
41
42
43 def stop_postgres_docker(name=DEFAULT_CONTAINER_NAME, **kwds):
44 stop_command = docker_util.command_list(
45 "stop",
46 [name],
47 **kwds
48 )
49 execute(stop_command)
50
51
52 class DockerPostgresDatabaseSource(ExecutesPostgresSqlMixin, DatabaseSource):
53 """Postgres database running inside a Docker container."""
54
55 def __init__(self, **kwds):
56 """Construct a postgres database source from planemo configuration."""
57 self.psql_path = 'psql'
58 self.database_user = 'postgres'
59 self.database_password = DEFAULT_POSTGRES_PASSWORD
60 self.database_host = 'localhost' # TODO: Make docker host
61 self.database_port = DEFAULT_POSTGRES_PORT_EXPOSE
62 self._kwds = kwds
63 self._docker_host_kwds = dockerfiles.docker_host_args(**kwds)
64 if not is_running_container(**self._docker_host_kwds):
65 start_postgres_docker(**self._docker_host_kwds)
66 # Hack to give docker a bit of time to boot up and allow psql to start.
67 time.sleep(30)
68
69 def sqlalchemy_url(self, identifier):
70 """Return URL or form postgresql://username:password@localhost/mydatabase."""
71 return "postgresql://%s:%s@%s:%d/%s" % (
72 self.database_user,
73 self.database_password,
74 self.database_host,
75 self.database_port,
76 identifier
77 )
78
79 def _psql_command_builder(self, *args):
80 base_command = docker_exec(DEFAULT_CONTAINER_NAME, [self.psql_path], **self._docker_host_kwds)
81 command_builder = _CommandBuilder(*base_command)
82 # Print only tuples so output is easier to parse
83 command_builder.append_command("--tuples-only")
84 command_builder.append_command("--username", self.database_user)
85 command_builder.append_command("-P", "pager=off")
86 command_builder.extend_command(args)
87 return command_builder
88
89
90 __all__ = (
91 'DockerPostgresDatabaseSource',
92 )