Skip to content

Commit

Permalink
Merge branch '31-add-envfile' into 'master'
Browse files Browse the repository at this point in the history
Add more environment configuration support

See merge request ix.ai/swarm-launcher!32
  • Loading branch information
tlex committed Nov 6, 2022
2 parents 22f1bb3 + 527275f commit 0ec6d1b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ The following environment variables are important if you don't supply a `/docker
| `LAUNCH_CONTAINER_NAME` | random (by docker) | NO | If you want to use a specific name for the container (similar to the task name) |
| `LAUNCH_HOSTNAME` | - | NO | If you want to use a specific hostname for the container |
| `LAUNCH_PRIVILEGED` | `false` | NO | Set this to `true` if you want to start a privileged container |
| `LAUNCH_ENVIRONMENTS` | - | NO | Space separated list of Key=Value pairs. **Note**: `@_@` gets replaced with a single whitespace, so you can expose environment values containing spaces. |
| `LAUNCH_DEVICES` | - | NO | Space separated list of DeviceOnHost:DeviceInContainer |
| `LAUNCH_VOLUMES` | - | NO | Space separated list of File/FolderOnHost:File/FolderInContainer |
| `LAUNCH_HOST_NETWORK` | `false` | NO | Set this to `true` to start the container on the host network. This option is not compatible with `LAUNCH_PORTS`, `LAUNCH_NETWORKS`, `LAUNCH_EXT_NETWORKS` and `LAUNCH_EXT_NETWORKS_IPV4` |
Expand All @@ -83,6 +82,9 @@ The following environment variables are important if you don't supply a `/docker
| `LAUNCH_DNS` | - | NO | Space separated list of DNS servers |
| `LAUNCH_DNS_SEARCH` | - | NO | Space separated list of DNS search domains |
| `LAUNCH_MAC_ADDRESS` | - | NO | Valid mac address for the launched container |
| `LAUNCH_ENVIRONMENTS` | - | NO | Space separated list of Key=Value pairs. **Note**: `@_@` gets replaced with a single whitespace, so you can expose environment values containing spaces. |
| `LAUNCH_ENVFILES` | - | NO | Space separated list of Key=Value pairs. **Note**: These files *must* be present on the host where the container is started|
| `LAUNCH_ARG_ENVFILE` | - | NO | The path inside the `swarm-launcher` container with the [`env` file](https://docs.docker.com/compose/environment-variables/) used by `docker compose --env-file XXX up` |

**Note**: Make sure you check out the [documentation](docs/).

Expand Down
48 changes: 40 additions & 8 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ _startup_check(){
fi
fi

if [ -n "${LAUNCH_ARG_ENVFILE}" ]; then
if [ ! -f "${LAUNCH_ARG_ENVFILE}" ]; then
_echo "ERROR! LAUNCH_ARG_ENVFILE is set but ${LAUNCH_ARG_ENVFILE} cannot be found. Exiting."
fi
fi

# Check if there's a need to exit now
if [ "${NEED_EXIT}" == true ]; then
exit 1
Expand All @@ -67,6 +73,7 @@ if [ -f "${COMPOSE_FILE}" ]; then
'LAUNCH_PRIVILEGED'
'LAUNCH_HOSTNAME'
'LAUNCH_ENVIRONMENTS'
'LAUNCH_ENVFILES'
'LAUNCH_DEVICES'
'LAUNCH_VOLUMES'
'LAUNCH_HOST_NETWORK'
Expand Down Expand Up @@ -147,7 +154,7 @@ xEOF
if [ "${LAUNCH_PRIVILEGED}" = true ]; then
echo " privileged: true" >> "${COMPOSE_FILE}"
fi

# specify an optional parent cgroup for the container
if [ "${LAUNCH_CGROUP_PARENT}" = true ]; then
echo " cgroup_parent: ${LAUNCH_CGROUP_PARENT}" >> "${COMPOSE_FILE}"
Expand All @@ -162,6 +169,15 @@ xEOF
done
fi

# the environment files
if [ -n "${LAUNCH_ENVFILES}" ]; then
echo " env_file:" >> "${COMPOSE_FILE}"
read -ra ARR <<<"${LAUNCH_ENVFILES}"
for ENVFILE in "${ARR[@]}"; do
echo " - ${ENVFILE}" >> "${COMPOSE_FILE}"
done
fi

# devices
if [ -n "${LAUNCH_DEVICES}" ]; then
echo " devices:" >> "${COMPOSE_FILE}"
Expand Down Expand Up @@ -347,7 +363,14 @@ _echo "Testing compose file:"
_echo "-----------------------------------"
cat "${COMPOSE_FILE}"
_echo "-----------------------------------"
docker compose config

COMMAND=(
config
)

[[ -n "${LAUNCH_ARG_ENVFILE}" ]] && COMMAND=(--env-file "${LAUNCH_ARG_ENVFILE}" "${COMMAND[@]}")

docker compose "${COMMAND[@]}"

# pull latest image version
if [ "${LAUNCH_PULL}" = true ] && [ -n "${LAUNCH_IMAGE}" ] ; then
Expand All @@ -359,13 +382,22 @@ fi
trap _term SIGTERM

_echo "-----------------------------------"
docker compose --project-name "${LAUNCH_PROJECT_NAME}" up \
--always-recreate-deps\
--force-recreate\
--abort-on-container-exit\
--remove-orphans\
--no-color &

COMMAND=(
--project-name "${LAUNCH_PROJECT_NAME}" up
--always-recreate-deps
--force-recreate
--abort-on-container-exit
--remove-orphans
--no-color
)

[[ -n "${LAUNCH_ARG_ENVFILE}" ]] && COMMAND=(--env-file "${LAUNCH_ARG_ENVFILE}" "${COMMAND[@]}")

# Here is the container started
docker compose "${COMMAND[@]}" &

child=$!
wait "$child"
_cleanup

0 comments on commit 0ec6d1b

Please sign in to comment.