diff --git a/config/storage/storage.yml b/config/storage/storage.yml --- a/config/storage/storage.yml +++ b/config/storage/storage.yml @@ -1,8 +1,8 @@ storage: cls: local args: - db: "host=pgsql-storage port=5432 user=postgres password=testpassword" # FIXME: this should not be hardcoded + db: "{{db}}" objstorage: cls: remote args: - url: http://swh-objstorage:5003/ # FIXME: this should not be hardcoded + url: "{{objstorage_url}}" diff --git a/container-entrypoint.py b/container-entrypoint.py new file mode 100755 --- /dev/null +++ b/container-entrypoint.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# Copyright (C) 2017 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +import os +import sys + +from jinja2 import Environment, FileSystemLoader + +TPL_CONFIG_ROOT = '/usr/local/share/softwareheritage/config/' +CONFIG_ROOT = '/etc/softwareheritage/' + +SCRIPT_DOC = """\ +Syntax: ./container-entrypoint.py + +Runs Jinja (with environment variables as context) on +%s/, +and uses the result as the configuration to run python -m .\ +""" % TPL_CONFIG_ROOT + + +def mkdirs_and_open(path, *args): + """Open a file, creating directories as needed.""" + dirname = os.path.dirname(path) + if not os.path.isdir(dirname): + os.makedirs(dirname) + return open(path, *args) + + +def render_config(path): + env = Environment(loader=FileSystemLoader(TPL_CONFIG_ROOT)) + tpl = env.get_template(path) + + target_path = os.path.join(CONFIG_ROOT, path) + with mkdirs_and_open(target_path, 'a') as fd: + config = tpl.render(**os.environ) + fd.write(config) + + return target_path + + +def main(): + try: + (_, module, config_filename) = sys.argv + except ValueError: + print(SCRIPT_DOC, fd=sys.stderr) + exit(1) + + config_path = render_config(config_filename) + + os.execv('/usr/bin/env', + ['/usr/bin/env', 'python3', '-m', module, config_path]) + + +if __name__ == '__main__': + main() diff --git a/docker-compose.yml b/docker-compose.yml --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,9 @@ image: swh-storage ports: - 5002:5002 + environment: + db: "host=pgsql-storage port=5432 user=postgres password=testpassword" + objstorage_url: "http://swh-objstorage:5003/" swh-objstorage: image: swh-objstorage diff --git a/dockerfiles/Dockerfile-swh-objstorage b/dockerfiles/Dockerfile-swh-objstorage --- a/dockerfiles/Dockerfile-swh-objstorage +++ b/dockerfiles/Dockerfile-swh-objstorage @@ -10,11 +10,13 @@ RUN pip install -r ./swh-objstorage/requirements.txt -r ./swh-objstorage/requirements-swh.txt -COPY ./swh-docker-dev/config/ /etc/softwareheritage/ +COPY ./swh-docker-dev/config/ /usr/local/share/softwareheritage/config/ COPY . . RUN pip install -e ./swh-objstorage/ -ENTRYPOINT ["python", "-m", "swh.objstorage.api.server", "/etc/softwareheritage/objstorage/server.yml"] +COPY ./swh-docker-dev/container-entrypoint.py /usr/local/bin/ + +ENTRYPOINT ["/usr/local/bin/container-entrypoint.py", "swh.objstorage.api.server", "objstorage/server.yml"] EXPOSE 5003 diff --git a/dockerfiles/Dockerfile-swh-storage b/dockerfiles/Dockerfile-swh-storage --- a/dockerfiles/Dockerfile-swh-storage +++ b/dockerfiles/Dockerfile-swh-storage @@ -6,16 +6,16 @@ WORKDIR /usr/local/src/ COPY ./swh-storage/requirements*.txt ./swh-storage/ -RUN pip install -r ./swh-storage/requirements.txt -r ./swh-storage/requirements-swh.txt +RUN pip install jinja2 -r ./swh-storage/requirements.txt -r ./swh-storage/requirements-swh.txt -COPY ./swh-docker-dev/config/ /etc/softwareheritage/ +COPY ./swh-docker-dev/config/ /usr/local/share/softwareheritage/config/ COPY . . RUN pip install -e ./swh-storage/ -RUN cat /etc/softwareheritage/storage/storage.yml +COPY ./swh-docker-dev/container-entrypoint.py /usr/local/bin/ -ENTRYPOINT ["python", "-m", "swh.storage.api.server", "/etc/softwareheritage/storage/storage.yml"] +ENTRYPOINT ["/usr/local/bin/container-entrypoint.py", "swh.storage.api.server", "storage/storage.yml"] EXPOSE 5002