2
0
mirror of https://github.com/cmehay/pyentrypoint synced 2024-10-30 15:21:11 +00:00
pyentrypoint/README.md

264 lines
7.0 KiB
Markdown
Raw Normal View History

2016-02-21 21:11:37 +00:00
# pyentrypoint
2015-08-03 14:37:32 +00:00
2016-03-21 22:54:00 +00:00
__pyentrypoint__ is a tool written in `Python` to manage Docker containers `ENTRYPOINT`.
2015-08-03 14:37:32 +00:00
2016-02-21 21:11:37 +00:00
This tool avoids writing shell scripts to:
- Handle commands and sub commands
- Identify linked containers
- Generate configuration using `jinja2` templates
- Run commands before starting service
- Reload service when configuration has changed
2015-08-03 14:37:32 +00:00
2016-04-29 23:26:13 +00:00
[![Documentation Status](https://readthedocs.org/projects/pyentrypoint/badge/?version=latest)](http://pyentrypoint.readthedocs.io/en/latest/?badge=latest)
2015-08-03 14:37:32 +00:00
## Usages
2016-02-21 21:11:37 +00:00
### Install in container
All you need to do is to setup a `yaml` file called `entrypoint-config.yml` and to install __pyentrypoint__ in your `Dockerfile` using pip.
```dockerfile
FROM debian
# Installing git for example
2016-02-29 13:12:24 +00:00
RUN apt-get update && apt-get install git python-pip -y
2016-02-21 21:11:37 +00:00
# Install pyentrypoint
RUN pip install pyentrypoint
# Copy config file in the current WORKDIR
COPY entrypoint-config.yml .
# Set ENTRYPOINT
ENTRYPOINT ['pyentrypoint']
# git will be the default command
CMD ['git']
```
2016-03-16 21:18:19 +00:00
```dockerfile
FROM alpine
# Installing git for example
RUN apk add --update py-pip git
# Install pyentrypoint
RUN pip install pyentrypoint
# Copy config file in the current WORKDIR
COPY entrypoint-config.yml .
# Set ENTRYPOINT
ENTRYPOINT ['pyentrypoint']
# git will be the default command
CMD ['git']
```
2016-03-14 22:53:02 +00:00
### Working examples
- [Tor hidden service](https://github.com/cmehay/docker-tor-hidden-service)
2016-02-21 21:11:37 +00:00
### Setup entrypoint
This is an example of `entrypoint-config.yml` file.
2015-11-10 00:27:12 +00:00
```yaml
2016-02-21 21:11:37 +00:00
# Entrypoint configuration example
# This entry should reflect CMD in Dockerfile
command: git
# This is a list with some subcommands to handle
# when CMD is not `git` here.
# By default, all args started with hyphen are handled.
subcommands:
- "-*"
- 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: 1000
group: 1000
# These files should exist (ADD or COPY)
# and should be jinja templated.
2016-03-27 17:29:53 +00:00
# Note: if config files end with ".tpl", the extension will be removed.
2016-02-21 21:11:37 +00:00
config_files:
- /etc/gitconfig
2016-03-27 17:29:53 +00:00
- .ssh/config.tpl # Will apply to ".ssh/config"
2016-04-25 21:23:21 +00:00
- /tmp/id_rsa: .ssh/id_rsa # Will apply "/tmp/id_rsa" template to ".ssh/id_rsa"
2016-02-21 21:11:37 +00:00
# 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 anyway
secret_env:
- SSHKEY
2016-09-22 23:14:54 +00:00
- '*' # Support globbing, all environment will be wiped
2016-02-21 21:11:37 +00:00
# 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
links:
'ssh':
port: 22
name: 'ssh*'
protocol: tcp
# env can be list, dictionary or string
env:
FOO: bar
# Single doesn't allow multiple links for this ID
# false by default
single: true
# Set to false to get optional link
# true by default
required: true
# Commands to run before applying configuration
pre_conf_commands:
- echo something > to_this_file
# commands to run after applying configuration
post_conf_commands:
- echo "something else" > to_this_another_file
2016-10-01 01:29:04 +00:00
# Reload service when configuration change by sending a signal to process
reload:
signal: SIGHUP # Optional, signal to send, default is SIGHUP
pid: 1 # Optional, pid to send signal, default is 1
watch_config_files: true # Optional, watch defined config files, default True
files: # Optional, list of files to watch
- /etc/conf/to/watch
# can also be enabled like this:
reload: true
2016-02-21 21:11:37 +00:00
# Cleanup environment from variables created by linked containers
# before running command (True by default)
2016-10-01 01:29:04 +00:00
clean_env: true
2016-02-21 21:11:37 +00:00
# Enable debug to debug
debug: true
2016-10-01 01:29:04 +00:00
# Do not output anything except error
quiet: false
2015-08-03 14:37:32 +00:00
```
2016-02-21 21:11:37 +00:00
### Config templates
2015-11-10 00:27:12 +00:00
2016-02-21 21:11:37 +00:00
You can generate configuration for your service with jinga2 template.
2016-03-21 22:54:00 +00:00
Here is an example for an hypothetical ssh config file:
2015-08-03 14:37:32 +00:00
2016-02-21 21:11:37 +00:00
```jinga
host server:
hostname {{links.ssh.ip}}
port {{links.ssh.port}}
2015-08-03 14:37:32 +00:00
```
2016-03-21 22:54:00 +00:00
Templates will be replaced with ip address and port of the identified link. All links can be accessed from `links.all`, this is a tuple of links you can iterate on it.
2015-08-03 14:37:32 +00:00
2016-02-21 21:11:37 +00:00
```jinga
{% for link in links.all %}
host {{link.names[0]}}
hostname {{link.ip}}
port {{links.port}}
{% endfor %}
```
2015-08-03 14:37:32 +00:00
2016-02-21 21:11:37 +00:00
If you change the option `single` to `false` in the `entrypoint-config.yml`, the identified link `ssh` will become a tuple of links. You must iterate on it in the `jinja` template.
2015-11-10 00:27:12 +00:00
2016-02-21 21:11:37 +00:00
```jinga
{% for link in links.ssh %}
host {{link.names[0]}}
hostname {{link.ip}}
port {{links.port}}
{% endfor %}
2015-08-03 14:37:32 +00:00
```
2015-11-10 00:27:12 +00:00
2016-03-16 21:18:19 +00:00
Accessing environment in template.
```jinga
{% if 'SSHKEY in env' %}
{{env['SSHKEY']}}
{% endfor %}
```
2016-03-21 22:54:00 +00:00
### Accessible objects
2015-08-03 14:37:32 +00:00
2016-02-21 21:11:37 +00:00
You have 4 available objects in your templates.
2015-08-03 14:37:32 +00:00
2016-02-21 21:11:37 +00:00
- `config`
- `links`
- `containers`
2016-03-21 22:54:00 +00:00
- `environ`
2015-08-03 14:37:32 +00:00
2016-02-21 21:11:37 +00:00
#### config
2015-11-10 00:27:12 +00:00
2016-02-21 21:11:37 +00:00
`Config` reflect the config file. You can retrieve any setup in this object.
2015-08-03 14:37:32 +00:00
2016-02-21 21:11:37 +00:00
(see `config.py`)
2015-08-03 14:37:32 +00:00
2016-02-21 21:11:37 +00:00
#### links
2015-11-10 00:27:12 +00:00
2016-03-21 22:54:00 +00:00
`Links` handles `Link` objects. You can identify links using wildcard patterns in the configuration file.
2015-08-03 14:37:32 +00:00
2016-02-21 21:11:37 +00:00
`link` is related to one physical link (one ip and one port).
`link` handles the following attributes:
- `ip`
- link ip
- `port`
- link port (integer)
- `environ`
- related container environment
- `protocol`
- link protocol (`tcp` or `udp`)
- `uri`
2016-03-21 22:54:00 +00:00
- link URI (example: `tcp://10.0.0.3:80`)
2016-02-21 21:11:37 +00:00
- `names`
- tuple of related container names
2015-11-11 13:32:50 +00:00
2016-02-21 21:11:37 +00:00
#### containers
`containers` handles a tuple of `container` object.
`container` handles the following attributes:
- `ip`
- container ip
- `environ`
- container environment
- `names`
- List of containers names
2016-03-07 23:12:13 +00:00
- Names are sorted by length, but container ID will be the last element.
- `id`
2016-03-21 22:54:00 +00:00
- Hexadecimal container ID (if available, empty string else)
2016-02-21 21:11:37 +00:00
- `links`
2016-03-21 22:54:00 +00:00
- Tuple of `link` objects related to this container
2016-02-21 21:11:37 +00:00
#### environ
2016-03-21 18:29:21 +00:00
`environ` is the environment of the container (os.environ).
`env` is an alias to `environ`.
2015-11-11 13:32:50 +00:00
2016-03-07 23:12:13 +00:00
## Setup
Some setups can be overridden using environment variables.
2016-03-27 17:29:53 +00:00
- `ENTRYPOINT_CONFIG` overrides path of `entrypoint-config.yml` file.
- `ENTRYPOINT_FORCE` is applying configuration and runs pre and post conf commands even if the `command` provided is not handled.
- `ENTRYPOINT_PRECONF_COMMAND` run an extra pre conf shell command after all pre conf commands.
- `ENTRYPOINT_POSTCONF_COMMAND` run an extra post conf shell command after all post conf commands.
- `ENTRYPOINT_DEBUG` enables debug logs.
- `ENTRYPOINT_RAW` does not use logging to display pre and post conf commands.
This can be useful if output is serialized.
- `ENTRYPOINT_DISABLE_RELOAD` disable reload system even if it is enabled in `entrypoint-config.yml`.
- `ENTRYPOINT_USER` overrides `user` in config.
- `ENTRYPOINT_GROUP` overrides `group` in config.
2016-03-07 23:12:13 +00:00
2015-11-11 13:32:50 +00:00
### Running Tests
2016-02-21 21:11:37 +00:00
To run tests, ensure that `docker-compose` and `make` are installed and run
2015-11-11 13:32:50 +00:00
```shell
2016-02-21 21:11:37 +00:00
$ make test
```