Changeset View
Changeset View
Standalone View
Standalone View
container-entrypoint.py
- This file was added.
Property | Old Value | New Value |
---|---|---|
File Mode | null | 100755 |
#!/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 <py_module> <config_path> | |||||
Runs Jinja (with environment variables as context) on | |||||
%s/<config_path>, | |||||
and uses the result as the configuration to run python -m <py_module>.\ | |||||
""" % 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() |