Package: pyentrypoint
pyentrypointpyentrypointis a tool written inPythonto manage Docker containersENTRYPOINT.This tool avoids writing shell scripts to:Handle commands and sub commandsIdentify linked containersGenerate configuration usingjinja2templatesRun commands before starting serviceReload service when configuration has changedChangelogv0.7.2 (2020-05-30)add set_environment in settingsv0.7.1 (2020-05-24)add envtobool function in configuration templatev0.7.0 (2020-05-17)Add command matching setupV0.6.0 (2020-05-10)Drop python 2 supportDeprecation ofcommandandsubcommandssettings forcommands(see bellow)UsagesInstall in containerAll you need to do is to setup ayamlfile calledentrypoint-config.ymland to installpyentrypointin yourDockerfileusing pip.FROMdebian# Installing git for exampleRUNapt-getupdate&&apt-getinstallgitpython-pip-y# Install pyentrypointRUNpipinstallpyentrypoint# Copy config file in the current WORKDIRCOPYentrypoint-config.yml.# Set ENTRYPOINTENTRYPOINT['pyentrypoint']# git will be the default commandCMD['git']FROMalpine# Installing git for exampleRUNapkadd--updatepy-pipgit# Install pyentrypointRUNpipinstallpyentrypoint# Copy config file in the current WORKDIRCOPYentrypoint-config.yml.# Set ENTRYPOINTENTRYPOINT['pyentrypoint']# git will be the default commandCMD['git']Using docker-imageFROMgoldy/pyentrypoint:python3# ONBUILD statement add entrypoint-config.yml in current directoriesAvailable with many flavours:goldy/pyentrypoint:python3goldy/pyentrypoint:python3.6goldy/pyentrypoint:python3.7goldy/pyentrypoint:python3.8goldy/pyentrypoint:python3-alpinegoldy/pyentrypoint:python3.6-alpinegoldy/pyentrypoint:python3.7-alpinegoldy/pyentrypoint:python3.8-alpineWorking examplesTor hidden serviceSetup entrypointThis is an example ofentrypoint-config.ymlfile.# Entrypoint configuration example# This setup lists commands handled by entrypoint.# If you run the container with a command not in this list,# pyentrypoint will run the command directly without any action# If this setting and `command` are not set, all commands will be handled.# Support wildcardcommands:-git-sl*# DEPRECATED: This setup is remplaced by `commands`# This entry should reflect CMD in Dockerfile# If `commands` is present, this setup will be ignored.# DEPRECATED: This setup is remplaced by `commands`command:git# DEPRECATED: This setup will be dropped# This is a list with some subcommands to handle# when CMD is not `git` here.# By default, all args started with hyphen are handled.# DEPRECATED: This setup will be droppedsubcommands:-"-*"-clone-init-ls-files# etc...# User and group to run the cmd.# Can be name or uid/gid.# Affect only command handled.# Dockerfile USER value by default.user:1000group:1000# These files should exist (ADD, COPY or mounted)# and should be jinja templated.# Note: if config files end with ".tpl", the extension will be removed.config_files:-/etc/gitconfig-.ssh/config.tpl# Will apply to ".ssh/config"-/tmp/id_rsa:.ssh/id_rsa# Will apply "/tmp/id_rsa" template to ".ssh/id_rsa"# These environment variables will be wiped before# exec command to keep them secret# CAUTION: if the container is linked to another one,# theses variables will passed to it anywaysecret_env:-SSHKEY-'*'# Support globbing, all environment will be wiped# Links are handled here# Port, name, protocol or env variable can be used to identify the links# Raise an error if the link could not be identified# This is not supported when using docker network or docker-compose v2.links:'ssh':port:22name:'ssh*'protocol:tcp# env can be list, dictionary or stringenv:FOO:bar# Single doesn't allow multiple links for this ID# false by defaultsingle:true# Set to false to get optional link# true by defaultrequired:true# Set custom environment variable by running commands# Will run before pre_conf_commands and capture stdout only# Stop init if crashset_environment:-ENV_1:echo 'The environment variable ENV_1 will be added with this phrase'-ENV_2:head /dev/urandom | base64-ENV_3:echo ${ENV_1} and ${ENV_2} are now available here-ENV_4:exit 1 || true# Set like this if you need to ignore error# Commands to run before applying configurationpre_conf_commands:-echo something > to_this_file# commands to run after applying configurationpost_conf_commands:-echo "something else" > to_this_another_file# commands to run in parallele with the main commandpost_run_commands:-echo do something in parallele with the main command# run post_run_commands in parallele or sequentially (default is sequential)run_post_commands_in_parallele:true# default false# Reload service when configuration change by sending a signal to processreload:signal:SIGHUP# Optional, signal to send, default is SIGHUPpid:1# Optional, pid to send signal, default is 1watch_config_files:true# Optional, watch defined config files, default Truefiles:# Optional, list of files to watch-/etc/conf/to/watch-/file/support/*.matching# can also be enabled like this:reload:true# Cleanup environment from variables created by linked containers# before running command (True by default)clean_env:true# Enable debug to debugdebug:true# Do not output anything except errorquiet:falseHandled command matchingAll settings can be mapped to an handled command.For instance:# This config will handle command `abc` and `xyz`commands:-abc-xyz# you can map commands to handled commands bellowpre_conf_commands:-abc:-echo "will run for command abc"-xyz:-echo "will run for command xyz"-echo "Can be multiple"-echo "Will run for both commands"user:-abc:1000-xyz:1001# Mapping can also be a dictionnarygroup:abc:1000xyz:1001# EtcNot supported for deprecated settingscommand,subcommandsandlinks.Config templatesYou can generate configuration for your service with jinja2 template.linksandcontainersare not supported with docker network and docker-compose v2.Here is an example for an hypothetical ssh config file:host server:hostname{{links.ssh.ip}}port{{links.ssh.port}}Templates will be replaced with ip address and port of the identified link. All links can be accessed fromlinks.all, this is a tuple of links you can iterate on it.{%forlinkinlinks.all%}host{{link.names[0]}}hostname{{link.ip}}port{{links.port}}{%endfor%}If you change the optionsingletofalsein theentrypoint-config.yml, the identified linksshwill become a tuple of links. You must iterate on it in thejinjatemplate.{%forlinkinlinks.ssh%}host{{link.names[0]}}hostname{{link.ip}}port{{links.port}}{%endfor%}Accessing environment in template.{%if'SSHKEY in env'%}{{env['SSHKEY']}}{%endfor%}Accessible objectsYou have 8 available objects in your templates.configlinkscontainersenvironyamljsontomlConfigParserConfigParserConfigParser has an extrato_stringmethod to write config as string in jinja templace, or can be casted to sting.read*methods also return self object (unlike the original methods which are retuningNone){%setconfig=ConfigParser().read_string(env['SOME_CONF_IN_ENVIRONMENT'])}{#Writeparsedconfiglikethis#}{{str(config)}}configConfigreflect the config file. You can retrieve any setup in this object.(seeconfig.py)linksNot supported with docker network and docker-compose v2LinkshandlesLinkobjects. You can identify links using wildcard patterns in the configuration file.linkis related to one physical link (one ip and one port).linkhandles the following attributes:iplink ipportlink port (integer)environrelated container environmentprotocollink protocol (tcporudp)urilink URI (example:tcp://10.0.0.3:80)namestuple of related container namescontainersNot supported with docker network and docker-compose v2containershandles a tuple ofcontainerobject.containerhandles the following attributes:ipcontainer ipenvironcontainer environmentnamesList of containers namesNames are sorted by length, but container ID will be the last element.idHexadecimal container ID (if available, empty string else)linksTuple oflinkobjects related to this containerenvironenvironis the environment of the container (os.environ).envis an alias toenviron.envtoboolenvtoboolfunction is a useful to parse boolean string input in environnement to enable or disable features.The function accepts a default value as second parameter.{%ifenvtobool('SOME_ENV_VARIABLE',False)%}do stuff{%endif%}# Will write True or False here{envtobool('SOME_OTHER_ENV_VARIABLE', True)}Seehttps://docs.python.org/3/distutils/apiref.html#distutils.util.strtoboolfor information on input.yaml and jsonyamlandjsonobjects are respectively an import ofPyYAMLandjsonmodules.They are useful to load and dump serialized data from environment.# Here yaml is present in SETUP_YAML environment variable{%setdata=yaml.load(env['SETUP_YAML'])%}{{data['param']}}# Here json is present in SETUP_JSON environment variable{%setdata=json.loads(env['SETUP_JSON'])%}{{data['param']}}SetupSome setups can be overridden using environment variables.ENTRYPOINT_CONFIGoverrides path ofentrypoint-config.ymlfile.ENTRYPOINT_FORCEis applying configuration and runs pre and post conf commands even if thecommandprovided is not handled.ENTRYPOINT_PRECONF_COMMANDrun an extra pre conf shell command after all pre conf commands.ENTRYPOINT_POSTCONF_COMMANDrun an extra post conf shell command after all post conf commands.ENTRYPOINT_DEBUGenables debug logs.ENTRYPOINT_RAWdoes not use logging to display pre and post conf commands.
This can be useful if output is serialized.ENTRYPOINT_DISABLE_RELOADdisable reload system even if it is enabled inentrypoint-config.yml.ENTRYPOINT_USERoverridesuserin config.ENTRYPOINT_GROUPoverridesgroupin config.ENTRYPOINT_DISABLE_SERVICEexits container with 0 before doing anything. Useful to disable container using environement.Running TestsTo run tests, ensure thatdocker-composeandmakeare installed and run$maketest