#!/bin/bash set -eu -o pipefail echo "==== Cloudron Start ====" readonly USER="yellowtent" readonly BOX_SRC_DIR="/home/${USER}/box" readonly DATA_DIR="/home/${USER}/data" readonly CONFIG_DIR="/home/${USER}/configs" readonly SETUP_PROGRESS_JSON="/home/yellowtent/setup/website/progress.json" readonly ADMIN_LOCATION="my" # keep this in sync with constants.js readonly curl="curl --fail --connect-timeout 20 --retry 10 --retry-delay 2 --max-time 2400" script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "${script_dir}/argparser.sh" "$@" # this injects the arg_* variables used below # keep this is sync with config.js appFqdn() admin_fqdn=$([[ "${arg_is_custom_domain}" == "true" ]] && echo "${ADMIN_LOCATION}.${arg_fqdn}" || echo "${ADMIN_LOCATION}-${arg_fqdn}") admin_origin="https://${admin_fqdn}" readonly is_update=$([[ -f "${CONFIG_DIR}/cloudron.conf" ]] && echo "true" || echo "false") set_progress() { local percent="$1" local message="$2" echo "==== ${percent} - ${message} ====" (echo "{ \"update\": { \"percent\": \"${percent}\", \"message\": \"${message}\" }, \"backup\": {} }" > "${SETUP_PROGRESS_JSON}") 2> /dev/null || true # as this will fail in non-update mode } set_progress "1" "Create container" $script_dir/container.sh set_progress "5" "Adjust system settings" hostnamectl set-hostname "${arg_fqdn}" set_progress "10" "Ensuring directories" # keep these in sync with paths.js [[ "${is_update}" == "false" ]] && btrfs subvolume create "${DATA_DIR}/box" mkdir -p "${DATA_DIR}/box/appicons" mkdir -p "${DATA_DIR}/box/certs" mkdir -p "${DATA_DIR}/box/mail/dkim/${arg_fqdn}" mkdir -p "${DATA_DIR}/box/acme" # acme keys mkdir -p "${DATA_DIR}/graphite" mkdir -p "${DATA_DIR}/mysql" mkdir -p "${DATA_DIR}/postgresql" mkdir -p "${DATA_DIR}/mongodb" mkdir -p "${DATA_DIR}/snapshots" mkdir -p "${DATA_DIR}/addons" mkdir -p "${DATA_DIR}/collectd/collectd.conf.d" mkdir -p "${DATA_DIR}/acme" # acme challenges # bookkeep the version as part of data echo "{ \"version\": \"${arg_version}\", \"boxVersionsUrl\": \"${arg_box_versions_url}\" }" > "${DATA_DIR}/box/version" # remove old snapshots. if we do want to keep this around, we will have to fix the chown -R below # which currently fails because these are readonly fs echo "Cleaning up snapshots" find "${DATA_DIR}/snapshots" -mindepth 1 -maxdepth 1 | xargs --no-run-if-empty btrfs subvolume delete # restart mysql to make sure it has latest config # wait for all running mysql jobs while true; do if ! systemctl list-jobs | grep mysql; then break; fi echo "Waiting for mysql jobs..." sleep 1 done systemctl restart mysql readonly mysql_root_password="password" mysqladmin -u root -ppassword password password # reset default root password mysql -u root -p${mysql_root_password} -e 'CREATE DATABASE IF NOT EXISTS box' if [[ -n "${arg_restore_url}" ]]; then set_progress "15" "Downloading restore data" echo "Downloading backup: ${arg_restore_url} and key: ${arg_restore_key}" while true; do if $curl -L "${arg_restore_url}" | openssl aes-256-cbc -d -pass "pass:${arg_restore_key}" | tar -zxf - -C "${DATA_DIR}/box"; then break; fi echo "Failed to download data, trying again" done set_progress "21" "Setting up MySQL" if [[ -f "${DATA_DIR}/box/box.mysqldump" ]]; then echo "Importing existing database into MySQL" mysql -u root -p${mysql_root_password} box < "${DATA_DIR}/box/box.mysqldump" fi fi set_progress "25" "Migrating data" sudo -u "${USER}" -H bash < "${DATA_DIR}/nginx/applications/admin.conf" mkdir -p "${DATA_DIR}/nginx/cert" if [[ -f "${DATA_DIR}/box/certs/host.cert" && -f "${DATA_DIR}/box/certs/host.key" ]]; then cp "${DATA_DIR}/box/certs/host.cert" "${DATA_DIR}/nginx/cert/host.cert" cp "${DATA_DIR}/box/certs/host.key" "${DATA_DIR}/nginx/cert/host.key" else echo "${arg_tls_cert}" > "${DATA_DIR}/nginx/cert/host.cert" echo "${arg_tls_key}" > "${DATA_DIR}/nginx/cert/host.key" fi set_progress "33" "Changing ownership" chown "${USER}:${USER}" -R "${DATA_DIR}/nginx" "${DATA_DIR}/collectd" "${DATA_DIR}/addons" "${DATA_DIR}/acme" # during updates, do not trample mail ownership behind the the mail container's back find "${DATA_DIR}/box" -mindepth 1 -maxdepth 1 -not -path "${DATA_DIR}/box/mail" -print0 | xargs -0 chown -R "${USER}:${USER}" chown "${USER}:${USER}" "${DATA_DIR}/box" chown "${USER}:${USER}" -R "${DATA_DIR}/box/mail/dkim" # this is owned by box currently since it generates the keys chown "${USER}:${USER}" "${DATA_DIR}/INFRA_VERSION" || true chown "${USER}:${USER}" "${DATA_DIR}" set_progress "65" "Creating cloudron.conf" sudo -u yellowtent -H bash < "${CONFIG_DIR}/cloudron.conf" < "${BOX_SRC_DIR}/webadmin/dist/config.json" <