Enable working expose/unexpose commands
`expose` and `unexpose` commands are now fully functionnal using the ambassador pattern. This has the advantage to be a 100% docker solution and to not have to restart the service container. Multiple ports are exposed when service specify them. `start` and `stop` commands have been made more robust. When calling `start` or `stop` on an already started (or stopped) service, an error will be printed out instead of issuing failing commands. Exposed services will still be exposed after a `restart`. Also the service containers (and ambassador containers) will start automatically when docker boot up.
This commit is contained in:
21
commands
21
commands
@@ -35,7 +35,8 @@ case "$1" in
|
|||||||
touch "$LINKS_FILE"
|
touch "$LINKS_FILE"
|
||||||
|
|
||||||
dokku_log_info1 "Starting container"
|
dokku_log_info1 "Starting container"
|
||||||
ID=$(docker run --name "dokku.redis.$SERVICE" -v "$SERVICE_ROOT/data:/data" -v "$SERVICE_ROOT/config:/usr/local/etc/redis" -d "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION")
|
SERVICE_NAME=$(get_service_name "$SERVICE")
|
||||||
|
ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_ROOT/data:/data" -v "$SERVICE_ROOT/config:/usr/local/etc/redis" -d --restart always "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION")
|
||||||
echo "$ID" > "$SERVICE_ROOT/ID"
|
echo "$ID" > "$SERVICE_ROOT/ID"
|
||||||
|
|
||||||
dokku_log_verbose_quiet "Waiting for container to be ready"
|
dokku_log_verbose_quiet "Waiting for container to be ready"
|
||||||
@@ -73,10 +74,7 @@ case "$1" in
|
|||||||
dokku_log_verbose_quiet "Deleting container data"
|
dokku_log_verbose_quiet "Deleting container data"
|
||||||
docker exec -it "$ID" chmod -R 777 /data
|
docker exec -it "$ID" chmod -R 777 /data
|
||||||
|
|
||||||
dokku_log_verbose_quiet "Stopping container"
|
service_stop "$SERVICE"
|
||||||
docker stop "$ID" > /dev/null
|
|
||||||
docker kill "$ID" > /dev/null
|
|
||||||
sleep 1
|
|
||||||
|
|
||||||
dokku_log_verbose_quiet "Removing container"
|
dokku_log_verbose_quiet "Removing container"
|
||||||
docker rm -v "$ID" > /dev/null
|
docker rm -v "$ID" > /dev/null
|
||||||
@@ -144,11 +142,8 @@ case "$1" in
|
|||||||
$PLUGIN_COMMAND_PREFIX:restart)
|
$PLUGIN_COMMAND_PREFIX:restart)
|
||||||
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
|
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
|
||||||
verify_service_name "$2"
|
verify_service_name "$2"
|
||||||
SERVICE="$2"; SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
service_stop "$2"
|
||||||
|
service_start "$2"
|
||||||
ID=$(cat "$SERVICE_ROOT/ID")
|
|
||||||
|
|
||||||
docker restart --time=10 "$ID"
|
|
||||||
dokku_log_info1 "Please call dokku ps:restart on all linked apps"
|
dokku_log_info1 "Please call dokku ps:restart on all linked apps"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
@@ -183,7 +178,7 @@ case "$1" in
|
|||||||
$PLUGIN_COMMAND_PREFIX:expose)
|
$PLUGIN_COMMAND_PREFIX:expose)
|
||||||
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
|
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
|
||||||
verify_service_name "$2"
|
verify_service_name "$2"
|
||||||
service_port_expose "$2"
|
service_port_expose "$2" "${@:3}"
|
||||||
;;
|
;;
|
||||||
|
|
||||||
$PLUGIN_COMMAND_PREFIX:unexpose)
|
$PLUGIN_COMMAND_PREFIX:unexpose)
|
||||||
@@ -207,8 +202,8 @@ case "$1" in
|
|||||||
$PLUGIN_COMMAND_PREFIX:info <name>, Print the connection information
|
$PLUGIN_COMMAND_PREFIX:info <name>, Print the connection information
|
||||||
$PLUGIN_COMMAND_PREFIX:list, List all $PLUGIN_SERVICE services
|
$PLUGIN_COMMAND_PREFIX:list, List all $PLUGIN_SERVICE services
|
||||||
$PLUGIN_COMMAND_PREFIX:clone <name> <new-name>, NOT IMPLEMENTED
|
$PLUGIN_COMMAND_PREFIX:clone <name> <new-name>, NOT IMPLEMENTED
|
||||||
$PLUGIN_COMMAND_PREFIX:expose <name> <port>, NOT IMPLEMENTED
|
$PLUGIN_COMMAND_PREFIX:expose <name> [port], Expose a $PLUGIN_SERVICE service on custom port if provided (random port otherwise)
|
||||||
$PLUGIN_COMMAND_PREFIX:unexpose <name> <port>, NOT IMPLEMENTED
|
$PLUGIN_COMMAND_PREFIX:unexpose <name>, Unexpose a previously exposed $PLUGIN_SERVICE service
|
||||||
EOF
|
EOF
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
|||||||
2
config
Normal file → Executable file
2
config
Normal file → Executable file
@@ -5,7 +5,7 @@ export REDIS_ROOT=/var/lib/dokku/services/redis
|
|||||||
|
|
||||||
export PLUGIN_COMMAND_PREFIX="redis"
|
export PLUGIN_COMMAND_PREFIX="redis"
|
||||||
export PLUGIN_DATA_ROOT=$REDIS_ROOT
|
export PLUGIN_DATA_ROOT=$REDIS_ROOT
|
||||||
export PLUGIN_DATASTORE_PORT=6379
|
export PLUGIN_DATASTORE_PORTS=(6379)
|
||||||
export PLUGIN_DEFAULT_ALIAS="REDIS"
|
export PLUGIN_DEFAULT_ALIAS="REDIS"
|
||||||
export PLUGIN_IMAGE=$REDIS_IMAGE
|
export PLUGIN_IMAGE=$REDIS_IMAGE
|
||||||
export PLUGIN_IMAGE_VERSION=$REDIS_IMAGE_VERSION
|
export PLUGIN_IMAGE_VERSION=$REDIS_IMAGE_VERSION
|
||||||
|
|||||||
110
functions
110
functions
@@ -2,19 +2,21 @@
|
|||||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||||
source "$(dirname "$0")/config"
|
source "$(dirname "$0")/config"
|
||||||
|
|
||||||
get_random_port() {
|
get_random_ports() {
|
||||||
local port=$RANDOM
|
local iterations="${1:-1}"
|
||||||
local quit=0
|
for (( i=0; i < iterations; i++ )); do
|
||||||
|
local port=$RANDOM
|
||||||
while [ "$quit" -ne 1 ]; do
|
local quit=0
|
||||||
netstat -a | grep $port >> /dev/null
|
while [ "$quit" -ne 1 ]; do
|
||||||
if [ $? -gt 0 ]; then
|
netstat -an | grep $port > /dev/null
|
||||||
quit=1
|
if [ $? -gt 0 ]; then
|
||||||
else
|
quit=1
|
||||||
port=$((port + 1))
|
else
|
||||||
fi
|
port=$((port + 1))
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo $port
|
||||||
done
|
done
|
||||||
echo $port
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get_container_ip() {
|
get_container_ip() {
|
||||||
@@ -54,11 +56,19 @@ service_list() {
|
|||||||
else
|
else
|
||||||
dokku_log_info1_quiet "$PLUGIN_SERVICE services:"
|
dokku_log_info1_quiet "$PLUGIN_SERVICE services:"
|
||||||
for SERVICE in $SERVICES; do
|
for SERVICE in $SERVICES; do
|
||||||
dokku_log_verbose "$SERVICE $(service_status $SERVICE)"
|
dokku_log_verbose "$SERVICE $(service_status "$SERVICE")$(service_exposed_ports "$SERVICE")"
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
service_exposed_ports() {
|
||||||
|
local SERVICE="$1"
|
||||||
|
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
||||||
|
local PORT_FILE="$SERVICE_ROOT/PORT"
|
||||||
|
[[ ! -f $PORT_FILE ]] && return 0
|
||||||
|
printf ", exposed port(s): %s" "$(cat "$PORT_FILE")"
|
||||||
|
}
|
||||||
|
|
||||||
service_link() {
|
service_link() {
|
||||||
local APP="$2"
|
local APP="$2"
|
||||||
local SERVICE="$1"
|
local SERVICE="$1"
|
||||||
@@ -112,14 +122,14 @@ service_status() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
service_port_expose() {
|
service_port_expose() {
|
||||||
service_port_unpause "$1" "true"
|
service_port_unpause "$1" "true" "${@:2}"
|
||||||
}
|
}
|
||||||
|
|
||||||
service_port_pause() {
|
service_port_pause() {
|
||||||
local SERVICE="$1"
|
local SERVICE="$1"
|
||||||
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
||||||
|
local EXPOSED_NAME="$(get_service_name "$SERVICE").ambassador"
|
||||||
local PORT_FILE="$SERVICE_ROOT/PORT"
|
local PORT_FILE="$SERVICE_ROOT/PORT"
|
||||||
local DESTINATION_FILE="$SERVICE_ROOT/IPTABLES_DESTINATION"
|
|
||||||
local LOG_FAIL="$2"
|
local LOG_FAIL="$2"
|
||||||
|
|
||||||
if [[ "$LOG_FAIL" == "true" ]]; then
|
if [[ "$LOG_FAIL" == "true" ]]; then
|
||||||
@@ -128,56 +138,67 @@ service_port_pause() {
|
|||||||
[[ ! -f "$PORT_FILE" ]] && return 0
|
[[ ! -f "$PORT_FILE" ]] && return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local ID=$(cat "$SERVICE_ROOT/ID")
|
docker stop "$EXPOSED_NAME" > /dev/null
|
||||||
local IP=$(get_container_ip "$ID")
|
docker rm "$EXPOSED_NAME" > /dev/null
|
||||||
local PORT=$(cat "$PORT_FILE")
|
if [[ "$LOG_FAIL" == "true" ]]; then
|
||||||
local DESTINATION=$(cat "$DESTINATION_FILE")
|
dokku_log_info1 "Service $SERVICE unexposed"
|
||||||
|
fi
|
||||||
sudo /sbin/iptables -t nat -D DOCKER -p tcp --dport "$PORT" -j DNAT --to-destination "$DESTINATION"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
service_port_unexpose() {
|
service_port_unexpose() {
|
||||||
service_port_pause "$1" "true"
|
local SERVICE="$1"
|
||||||
|
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
||||||
|
local PORT_FILE="$SERVICE_ROOT/PORT"
|
||||||
|
service_port_pause "$SERVICE" "true"
|
||||||
rm -rf "$PORT_FILE"
|
rm -rf "$PORT_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
service_port_unpause() {
|
service_port_unpause() {
|
||||||
local SERVICE="$1"
|
local SERVICE="$1"
|
||||||
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
||||||
|
local SERVICE_NAME=$(get_service_name "$SERVICE")
|
||||||
|
local EXPOSED_NAME="${SERVICE_NAME}.ambassador"
|
||||||
local PORT_FILE="$SERVICE_ROOT/PORT"
|
local PORT_FILE="$SERVICE_ROOT/PORT"
|
||||||
local DESTINATION_FILE="$SERVICE_ROOT/IPTABLES_DESTINATION"
|
|
||||||
local LOG_FAIL="$2"
|
local LOG_FAIL="$2"
|
||||||
local PORT=$(get_random_port)
|
local PORTS=(${@:3})
|
||||||
|
PORTS=(${PORTS[@]:-$(get_random_ports ${#PLUGIN_DATASTORE_PORTS[@]})})
|
||||||
local ID=$(cat "$SERVICE_ROOT/ID")
|
local ID=$(cat "$SERVICE_ROOT/ID")
|
||||||
local IP=$(get_container_ip "$ID")
|
|
||||||
local DESTINATION="$IP:$PLUGIN_DATASTORE_PORT"
|
[[ "${#PORTS[@]}" != "${#PLUGIN_DATASTORE_PORTS[@]}" ]] && dokku_log_fail "${#PLUGIN_DATASTORE_PORTS[@]} ports to be exposed need to be provided"
|
||||||
|
|
||||||
if [[ "$LOG_FAIL" == "true" ]]; then
|
if [[ "$LOG_FAIL" == "true" ]]; then
|
||||||
[[ -f "$PORT_FILE" ]] && PORT=$(cat "$PORT_FILE") && dokku_log_fail "Service $SERVICE already exposed on port $PORT"
|
[[ -f "$PORT_FILE" ]] && PORTS=($(cat "$PORT_FILE")) && dokku_log_fail "Service $SERVICE already exposed on port(s) ${PORTS[*]}"
|
||||||
else
|
else
|
||||||
[[ ! -f "$PORT_FILE" ]] && return 0
|
[[ ! -f "$PORT_FILE" ]] && return 0
|
||||||
PORT=$(cat "$PORT_FILE") && sudo /sbin/iptables -t nat -D DOCKER -p tcp --dport "$PORT" -j DNAT --to-destination "$DESTINATION"
|
PORTS=($(cat "$PORT_FILE"))
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "$PORT" > "$PORT_FILE"
|
echo "${PORTS[@]}" > "$PORT_FILE"
|
||||||
echo "$DESTINATION" > "$DESTINATION_FILE"
|
|
||||||
|
|
||||||
echo "$DESTINATION"
|
docker run -d --link "$SERVICE_NAME:redis" --name "$EXPOSED_NAME" $(docker_ports_options "${PORTS[@]}") --restart always svendowideit/ambassador > /dev/null
|
||||||
|
|
||||||
sudo /sbin/iptables -t nat -A DOCKER -p tcp --dport "$PORT" -j DNAT --to-destination "$DESTINATION"
|
|
||||||
if [[ "$LOG_FAIL" == "true" ]]; then
|
if [[ "$LOG_FAIL" == "true" ]]; then
|
||||||
dokku_log_info1 "Service $SERVICE exposed on port $PORT"
|
dokku_log_info1 "Service $SERVICE exposed on port(s) ${PORTS[*]}"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
docker_ports_options() {
|
||||||
|
local PORTS=("$@")
|
||||||
|
for (( i=0; i < ${#PLUGIN_DATASTORE_PORTS[@]}; i++ )); do
|
||||||
|
echo -n "-p ${PORTS[i]}:${PLUGIN_DATASTORE_PORTS[i]} "
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
service_start() {
|
service_start() {
|
||||||
local SERVICE="$1"
|
local SERVICE="$1"
|
||||||
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
||||||
|
local SERVICE_NAME=$(get_service_name "$SERVICE")
|
||||||
|
local ID=$(docker ps -f status=running | grep "$SERVICE_NAME" | awk '{print $1}') || true
|
||||||
|
[[ -n $ID ]] && dokku_log_fail "Service is already started"
|
||||||
|
|
||||||
dokku_log_verbose_quiet "Starting container"
|
dokku_log_verbose_quiet "Starting container"
|
||||||
if [[ -f "$SERVICE_ROOT/ID" ]] && docker ps -aq --no-trunc | grep -q $(cat "$SERVICE_ROOT/ID"); then
|
local PREVIOUS_ID=$(docker ps -f status=exited | grep "$SERVICE_NAME" | awk '{print $1}') || true
|
||||||
ID=$(cat "$SERVICE_ROOT/ID")
|
if [[ -n $PREVIOUS_ID ]]; then
|
||||||
docker start "$ID" > /dev/null
|
docker start "$PREVIOUS_ID" > /dev/null
|
||||||
service_port_unpause "$SERVICE"
|
service_port_unpause "$SERVICE"
|
||||||
dokku_log_info2 "Container started"
|
dokku_log_info2 "Container started"
|
||||||
else
|
else
|
||||||
@@ -188,11 +209,13 @@ service_start() {
|
|||||||
service_stop() {
|
service_stop() {
|
||||||
local SERVICE="$1"
|
local SERVICE="$1"
|
||||||
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE";
|
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE";
|
||||||
|
local SERVICE_NAME=$(get_service_name "$SERVICE")
|
||||||
|
local ID=$(docker ps -f status=running | grep "$SERVICE_NAME" | awk '{print $1}') || true
|
||||||
|
[[ -z $ID ]] && dokku_log_fail "Service is already stopped"
|
||||||
|
|
||||||
if [[ -f "$SERVICE_ROOT/ID" ]] && docker ps -aq --no-trunc | grep -q $(cat "$SERVICE_ROOT/ID"); then
|
if [[ -n $ID ]]; then
|
||||||
dokku_log_verbose_quiet "Stopping container"
|
dokku_log_verbose_quiet "Stopping container"
|
||||||
ID=$(cat "$SERVICE_ROOT/ID")
|
docker stop "$SERVICE_NAME" > /dev/null
|
||||||
docker stop "$ID" > /dev/null
|
|
||||||
service_port_pause "$SERVICE"
|
service_port_pause "$SERVICE"
|
||||||
dokku_log_info2 "Container stopped"
|
dokku_log_info2 "Container stopped"
|
||||||
else
|
else
|
||||||
@@ -221,7 +244,7 @@ service_url() {
|
|||||||
|
|
||||||
local ID="$(cat "$SERVICE_ROOT/ID")"
|
local ID="$(cat "$SERVICE_ROOT/ID")"
|
||||||
local IP="$(get_container_ip "$ID")"
|
local IP="$(get_container_ip "$ID")"
|
||||||
echo "$PLUGIN_SCHEME://$IP:$PLUGIN_DATASTORE_PORT/0"
|
echo "$PLUGIN_SCHEME://$IP:${PLUGIN_DATASTORE_PORTS[0]}/0"
|
||||||
}
|
}
|
||||||
|
|
||||||
is_container_status () {
|
is_container_status () {
|
||||||
@@ -235,3 +258,8 @@ is_container_status () {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get_service_name() {
|
||||||
|
local SERVICE="$1"
|
||||||
|
echo "dokku.${PLUGIN_COMMAND_PREFIX}.$SERVICE"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user