Files
dokku-ui/bootstrap.sh
2025-10-17 01:13:24 +08:00

111 lines
3.3 KiB
Bash

#!/bin/bash
set -e
# This script installs and sets up shokku with a few prerequisites:
# dokku must be installed
# a global domain must be set
# letsencrypt must be configured and enabled with global email set
DOKKU_STORAGE_DIR="/var/lib/dokku/data/storage/"
SHOKKU_DATA_DIR="$DOKKU_STORAGE_DIR/shokku"
SHOKKU_APP_DATA_MOUNT_PATH="$SHOKKU_DATA_DIR:/data"
SHOKKU_VERSION=${SHOKKU_VERSION:-"latest"}
SHOKKU_IMAGE="ccr.ccs.tencentyun.com/miaogai/shokku:$SHOKKU_VERSION"
SHOKKU_DOKKU_USER="shokkuadmin"
DISTROLESS_NONROOT_UID="65532"
clean-shokku() {
echo "=> checking for existing resources"
if dokku apps:exists shokku &>/dev/null; then
echo "==> destroying old dokku app"
dokku apps:destroy --force shokku &>/dev/null
fi
if dokku ssh-keys:list "$SHOKKU_DOKKU_USER" &>/dev/null; then
echo "==> removing existing dokku ssh key"
dokku ssh-keys:remove $SHOKKU_DOKKU_USER
fi
echo "==> done"
}
create-shokku-app() {
clean-shokku
echo "=> pulling image (version: $SHOKKU_VERSION)"
HOST_SSH_PORT=$(grep "Port " /etc/ssh/sshd_config | awk '{ print $2 }')
docker pull "$SHOKKU_IMAGE"
SHOKKU_IMAGE_DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' "$SHOKKU_IMAGE")
echo "=> creating & configuring dokku app"
dokku apps:create shokku
dokku docker-options:add shokku deploy \
"--add-host=host.docker.internal:host-gateway"
dokku config:set shokku \
DOKKU_SSH_HOST='host.docker.internal' \
DOKKU_SSH_PORT="$HOST_SSH_PORT"
echo "==> creating storage"
dokku storage:ensure-directory shokku --chown false
dokku storage:mount shokku "$SHOKKU_APP_DATA_MOUNT_PATH"
chown -R "$DISTROLESS_NONROOT_UID":"$DISTROLESS_NONROOT_UID" "$SHOKKU_DATA_DIR"
echo "==> bootstrapping"
dokku config:set shokku DOKKU_SKIP_DEPLOY=true
dokku git:from-image shokku "$SHOKKU_IMAGE_DIGEST"
shokku_ssh_key=$(dokku run shokku bootstrap)
echo "$shokku_ssh_key" | dokku ssh-keys:add "$SHOKKU_DOKKU_USER"
echo "==> deploying"
dokku config:unset shokku DOKKU_SKIP_DEPLOY
if [ -z "$SHOKKU_CERT" ]; then
echo "==> enabling letsencrypt"
dokku letsencrypt:set shokku email ssl@ssl.com
dokku letsencrypt:enable shokku
else
echo "==> adding certificate from SHOKKU_CERT"
dokku certs:add shokku < "$SHOKKU_CERT"
fi
}
main() {
if [[ "$(id -u)" != "0" ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if ! command -v dokku &>/dev/null; then
echo "Please install dokku first using the instructions at https://dokku.com" 1>&2
exit 1
fi
if [ -z "$SHOKKU_CERT" ]; then
if ! dokku plugin:installed letsencrypt; then
echo "Please setup letsencrypt using the instructions at https://dokku.com/docs/deployment/application-deployment/#setting-up-ssl" 1>&2
exit 1
fi
fi
for plugin in redis postgres mongo mysql; do
if ! dokku plugin:installed $plugin; then
echo "=> Installing plugin $plugin"
dokku plugin:install https://git.shusou.com/dokku/dokku-$plugin.git $plugin
fi
done
create-shokku-app
shokku_app_domain=$(dokku domains:report shokku --domains-app-vhosts)
shokku_setup_key=$(dokku logs -q shokku | grep setup_key | jq ".setup_key")
echo "---"
echo "=> shokku installed and running "
echo "--- proceed with setup using key $shokku_setup_key at https://$shokku_app_domain ---"
}
main