Files
dokku-postgres/commands
Loïc Guitaut 3ab22d6f6f Use PG tools from docker image
Instead of installing dependencies on the host, directly use the binary
shipped with the postgres docker image. This will also prevent possible
problems when API changes for example.
2015-09-09 10:31:17 +02:00

226 lines
8.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_PATH/common/functions"
source "$(dirname "$0")/functions"
source "$(dirname "$0")/config"
if [[ ! -d $PLUGIN_DATA_ROOT ]]; then
dokku_log_fail "$PLUGIN_SERVICE: Please run: sudo dokku plugins-install"
fi
case "$1" in
$PLUGIN_COMMAND_PREFIX:alias)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
[[ -z $3 ]] && dokku_log_fail "Please specify an alias for the service"
verify_service_name "$2"
service_set_alias "$2" "$3"
;;
$PLUGIN_COMMAND_PREFIX:create)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
[[ ! -d "$PLUGIN_DATA_ROOT/$2" ]] || dokku_log_fail "$PLUGIN_SERVICE service $2 already exists"
SERVICE="$2"; SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"; LINKS_FILE="$SERVICE_ROOT/LINKS"
if ! docker images | grep -e "^$PLUGIN_IMAGE " | grep -q " $PLUGIN_IMAGE_VERSION " ; then
dokku_log_fail "$PLUGIN_SERVICE image $PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION not found"
fi
mkdir -p "$SERVICE_ROOT" || dokku_log_fail "Unable to create service directory"
password=$(date +%s | sha256sum | base64 | head -c 16)
echo "$password" > "$SERVICE_ROOT/PASSWORD"
touch "$LINKS_FILE"
dokku_log_info1 "Starting container"
SERVICE_NAME=$(get_service_name "$SERVICE")
ID=$(docker run --name "$SERVICE_NAME" -v "$SERVICE_ROOT:/var/lib/postgresql" -e "POSTGRES_PASSWORD=$password" -d --restart always --label dokku=service --label dokku.service=postgres "$PLUGIN_IMAGE:$PLUGIN_IMAGE_VERSION")
echo "$ID" > "$SERVICE_ROOT/ID"
dokku_log_verbose_quiet "Waiting for container to be ready"
sleep 10
dokku_log_verbose_quiet "Creating container database"
IP=$(get_container_ip "$ID")
DATABASE_NAME="$(get_database_name $SERVICE)"
while true; do
PGPASSWORD=$password psql --quiet -h "$IP" -p 5432 -U postgres --command "CREATE DATABASE $DATABASE_NAME;" &>/dev/null || {
continue
}
break
done
dokku_log_info2 "$PLUGIN_SERVICE container created: $SERVICE"
dokku "$PLUGIN_COMMAND_PREFIX:info" "$SERVICE"
;;
$PLUGIN_COMMAND_PREFIX:destroy)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
SERVICE="$2"; SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"; LINKS_FILE="$SERVICE_ROOT/LINKS"
[[ -s "$LINKS_FILE" ]] && dokku_log_fail "Cannot delete linked service"
[[ "$3" == "force" ]] && DOKKU_APPS_FORCE_DELETE=1
if [[ -z "$DOKKU_APPS_FORCE_DELETE" ]]; then
dokku_log_warn "WARNING: Potentially Destructive Action"
dokku_log_warn "This command will destroy $SERVICE $PLUGIN_SERVICE service."
dokku_log_warn "To proceed, type \"$SERVICE\""
echo ""
read -p "> " service_name
if [[ "$service_name" != "$SERVICE" ]]; then
dokku_log_warn "Confirmation did not match $SERVICE. Aborted."
exit 1
fi
fi
dokku_log_info1 "Deleting $SERVICE"
if [[ -f "$SERVICE_ROOT/ID" ]] && docker ps -aq --no-trunc | grep -q "$(cat "$SERVICE_ROOT/ID")"; then
ID=$(cat "$SERVICE_ROOT/ID")
service_stop "$SERVICE"
dokku_log_verbose_quiet "Killing container"
docker kill "$ID" > /dev/null || true
sleep 1
dokku_log_verbose_quiet "Removing container"
docker rm -v "$ID" > /dev/null
sleep 1
else
dokku_log_verbose_quiet "No container exists for $SERVICE"
fi
dokku_log_verbose_quiet "Removing data"
rm -rf "$SERVICE_ROOT"
dokku_log_info2 "$PLUGIN_SERVICE container deleted: $SERVICE"
;;
$PLUGIN_COMMAND_PREFIX:link)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
[[ -z $3 ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$3"
verify_service_name "$2"
service_link "$2" "$3"
;;
$PLUGIN_COMMAND_PREFIX:unlink)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
[[ -z $3 ]] && dokku_log_fail "Please specify an app to run the command on"
verify_app_name "$3"
verify_service_name "$2"
service_unlink "$2" "$3"
;;
$PLUGIN_COMMAND_PREFIX:export)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
SERVICE="$2"; SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
SERVICE_NAME="$(get_service_name "$SERVICE")"
DATABASE_NAME="$(get_database_name $SERVICE)"
PASSWORD=$(cat "$SERVICE_ROOT/PASSWORD")
docker exec "$SERVICE_NAME" env PGPASSWORD="$PASSWORD" pg_dump -Fc --no-acl --no-owner -h localhost -U postgres -w "$DATABASE_NAME"
;;
$PLUGIN_COMMAND_PREFIX:import)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
SERVICE="$2"
dokku_log_fail "Not yet implemented"
;;
$PLUGIN_COMMAND_PREFIX:logs)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
service_logs "$2" "$3"
;;
$PLUGIN_COMMAND_PREFIX:start)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
service_start "$2"
;;
$PLUGIN_COMMAND_PREFIX:stop)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
service_stop "$2"
;;
$PLUGIN_COMMAND_PREFIX:restart)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
service_stop "$2"
service_start "$2"
dokku_log_info1 "Please call dokku ps:restart on all linked apps"
;;
$PLUGIN_COMMAND_PREFIX:connect)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
SERVICE="$2"; SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
DATABASE_NAME="$(get_database_name $SERVICE)"
SERVICE_NAME="$(get_service_name "$SERVICE")"
docker exec -it "$SERVICE_NAME" psql -h localhost -U postgres "$DATABASE_NAME"
;;
$PLUGIN_COMMAND_PREFIX:info)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
service_info "$2"
;;
$PLUGIN_COMMAND_PREFIX:list)
service_list
;;
$PLUGIN_COMMAND_PREFIX:clone)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
SERVICE="$2"
dokku_log_fail "Not yet implemented"
;;
$PLUGIN_COMMAND_PREFIX:expose)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
service_port_expose "$2" "${@:3}"
;;
$PLUGIN_COMMAND_PREFIX:unexpose)
[[ -z $2 ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$2"
service_port_unexpose "$2"
;;
help)
cat && cat<<EOF
$PLUGIN_COMMAND_PREFIX:alias <name> <alias>, Set an alias for the docker link
$PLUGIN_COMMAND_PREFIX:create <name>, Create a $PLUGIN_SERVICE service
$PLUGIN_COMMAND_PREFIX:destroy <name>, Delete the $PLUGIN_SERVICE service and stop its container if there are no links left
$PLUGIN_COMMAND_PREFIX:link <name> <app>, Link the $PLUGIN_SERVICE service to the app
$PLUGIN_COMMAND_PREFIX:unlink <name> <app>, Unlink the $PLUGIN_SERVICE service from the app
$PLUGIN_COMMAND_PREFIX:export <name>, Export a dump of the $PLUGIN_SERVICE service database
$PLUGIN_COMMAND_PREFIX:import <name> <file>, NOT IMPLEMENTED
$PLUGIN_COMMAND_PREFIX:connect <name>, Connect via psql to a $PLUGIN_SERVICE service
$PLUGIN_COMMAND_PREFIX:logs <name> [-t], Print the most recent log(s) for this service
$PLUGIN_COMMAND_PREFIX:restart <name>, Graceful shutdown and restart of the $PLUGIN_SERVICE service container
$PLUGIN_COMMAND_PREFIX:info <name>, Print the connection information
$PLUGIN_COMMAND_PREFIX:list, List all $PLUGIN_SERVICE services
$PLUGIN_COMMAND_PREFIX:clone <name> <new-name>, 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>, Unexpose a previously exposed $PLUGIN_SERVICE service
$PLUGIN_COMMAND_PREFIX:start <name>, Start a previously stopped $PLUGIN_SERVICE service
$PLUGIN_COMMAND_PREFIX:stop <name>, Stop a running $PLUGIN_SERVICE service
EOF
;;
*)
exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
;;
esac