You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pyentrypoint/pyentrypoint/entrypoint.py

145 lines
4.6 KiB
Python

9 years ago
#!/usr/bin/env python
"""
Smart docker-entrypoint
"""
import json
import os
9 years ago
from sys import argv
from sys import exit
9 years ago
import toml
import yaml
9 years ago
from jinja2 import Environment
from jinja2 import FileSystemLoader
from .config import Config
from .config import envtobool
from .configparser import ConfigParser
8 years ago
from .constants import ENTRYPOINT_FILE
from .docker_links import DockerLinks
from .logs import Logs
from .runner import Runner
__all__ = ['Entrypoint', 'main']
9 years ago
class Entrypoint(object):
"""Entrypoint class."""
def _set_logguer(self):
self.log = Logs().log
9 years ago
8 years ago
def __init__(self, conf=ENTRYPOINT_FILE, args=[]):
9 years ago
self._set_logguer()
if os.environ.get('ENTRYPOINT_CONFIG'):
conf = os.environ['ENTRYPOINT_CONFIG']
9 years ago
try:
8 years ago
self.config = Config(conf=conf, args=args)
9 years ago
except Exception as err:
self.log.error(err)
self.log.critical('Fail to initialize config, exiting now')
exit(1)
else:
if self.config.debug:
Logs.set_debug()
8 years ago
if self.config.quiet:
Logs.set_critical()
9 years ago
self.args = args
self.runner = Runner(config=self.config)
9 years ago
@property
def is_handled(self):
"""Is command handled by entrypoint"""
return self.config.command.is_handled
@property
def is_disabled(self):
"""Return if service is disabled using environment"""
return self.config.is_disabled
@property
def should_config(self):
"""Check environment to tell if config should apply anyway"""
return self.config.should_config
@property
def raw_output(self):
"""Check if command output should be displayed using logging or not"""
return self.config.raw_output
def exit_if_disabled(self):
"""Exist 0 if service is disabled"""
if not self.config.is_disabled:
return
self.log.warning("Service is disabled by 'ENTRYPOINT_DISABLE_SERVICE' "
"environement variable... exiting with 0")
exit(0)
9 years ago
def apply_conf(self):
"""Apply config to template files"""
9 years ago
env = Environment(loader=FileSystemLoader('/'))
for template, conf_file in self.config.get_templates():
9 years ago
temp = env.get_template(template)
with open(conf_file, mode='w') as f:
self.log.debug('Applying conf to {}'.format(conf_file))
9 years ago
f.write(temp.render(config=self.config,
links=self.config.links,
env=os.environ,
environ=os.environ,
json=json,
yaml=yaml,
toml=toml,
ConfigParser=ConfigParser,
envtobool=envtobool,
containers=DockerLinks().to_containers()))
9 years ago
def run_set_enviroment(self):
for set_env in self.config.set_environment:
if not isinstance(set_env, dict) and not len(set_env) == 1:
raise Exception("set_environment is miss configured, "
"please check syntax in entrypoint config")
env, cmd = next(((e, c) for e, c in set_env.items()))
self.log.debug(f'Set environment variable {env}')
os.environ[env] = self.runner.run_cmd(cmd, stdout=True)
def run_pre_conf_cmds(self):
for cmd in self.config.pre_conf_commands:
self.runner.run_cmd(cmd)
if os.environ.get('ENTRYPOINT_PRECONF_COMMAND'):
self.runner.run_cmd(os.environ['ENTRYPOINT_PRECONF_COMMAND'])
def run_post_conf_cmds(self):
for cmd in self.config.post_conf_commands:
self.runner.run_cmd(cmd)
if os.environ.get('ENTRYPOINT_POSTCONF_COMMAND'):
self.runner.run_cmd(os.environ['ENTRYPOINT_POSTCONF_COMMAND'])
9 years ago
def launch(self):
self.config.command.run()
9 years ago
def main(argv):
argv.pop(0)
entry = Entrypoint(args=argv)
9 years ago
try:
entry.exit_if_disabled()
if not entry.is_handled and not entry.should_config:
entry.log.warning("Running command without config")
entry.launch()
entry.config.set_to_env()
entry.log.debug("Starting config")
entry.run_set_enviroment()
entry.run_pre_conf_cmds()
9 years ago
entry.apply_conf()
entry.run_post_conf_cmds()
9 years ago
entry.launch()
except Exception as e:
entry.log.error(str(e))
exit(1)
if __name__ == '__main__':
main(argv)