runtipi/scripts/start.sh

167 lines
5.1 KiB
Bash
Raw Normal View History

2022-03-29 23:40:04 +03:00
#!/usr/bin/env bash
2022-04-15 15:26:17 +03:00
set -e # Exit immediately if a command exits with a non-zero status.
2022-03-29 23:40:04 +03:00
2022-03-30 22:26:01 +03:00
# use greadlink instead of readlink on osx
if [[ "$(uname)" == "Darwin" ]]; then
readlink=greadlink
else
readlink=readlink
fi
ROOT_FOLDER="$($readlink -f $(dirname "${BASH_SOURCE[0]}")/..)"
2022-03-29 22:26:52 +03:00
STATE_FOLDER="${ROOT_FOLDER}/state"
2022-05-07 10:09:11 +03:00
SED_ROOT_FOLDER="$(echo $ROOT_FOLDER | sed 's/\//\\\//g')"
2022-04-21 22:41:56 +03:00
INTERNAL_IP="$(hostname -I | awk '{print $1}')"
DNS_IP=9.9.9.9 # Default to Quad9 DNS
ARCHITECTURE="$(uname -m)"
if [[ "$ARCHITECTURE" == "x86_64" ]]; then
ARCHITECTURE="amd64"
else if [[ "$ARCHITECTURE" == "aarch64" ]]; then
ARCHITECTURE="arm64"
fi
2022-05-03 23:55:55 +03:00
2022-05-07 09:27:26 +03:00
# Get field from json file
function get_json_field() {
local json_file="$1"
local field="$2"
echo $(jq -r ".${field}" "${json_file}")
}
# Deterministically derives 128 bits of cryptographically secure entropy
function derive_entropy() {
SEED_FILE="${STATE_FOLDER}/seed"
identifier="${1}"
tipi_seed=$(cat "${SEED_FILE}") || true
if [[ -z "$tipi_seed" ]] || [[ -z "$identifier" ]]; then
>&2 echo "Missing derivation parameter, this is unsafe, exiting."
exit 1
fi
# We need `sed 's/^.* //'` to trim the "(stdin)= " prefix from some versions of openssl
printf "%s" "${identifier}" | openssl dgst -sha256 -hmac "${tipi_seed}" | sed 's/^.* //'
}
2022-05-03 23:55:55 +03:00
# Get dns ip if pihole is installed
str=$(get_json_field ${STATE_FOLDER}/apps.json installed)
# if pihole is present in str add it as DNS
if [[ $str = *"pihole"* ]]; then
DNS_IP=10.21.21.201
fi
2022-04-21 22:41:56 +03:00
PUID="$(id -u)"
PGID="$(id -g)"
2022-04-21 23:14:47 +03:00
TZ="$(cat /etc/timezone | sed 's/\//\\\//g' || echo "Europe/Berlin")"
2022-04-21 22:41:56 +03:00
2022-03-29 20:41:49 +03:00
if [[ $UID != 0 ]]; then
echo "Tipi must be started as root"
echo "Please re-run this script as"
echo " sudo ./scripts/start"
exit 1
2022-03-29 21:53:48 +03:00
fi
# Configure Tipi if it isn't already configured
2022-03-30 22:26:01 +03:00
if [[ ! -f "${STATE_FOLDER}/configured" ]]; then
"${ROOT_FOLDER}/scripts/configure.sh"
fi
2022-04-21 21:54:45 +03:00
# Copy the app state if it isn't here
2022-04-21 23:14:47 +03:00
if [[ ! -f "${STATE_FOLDER}/apps.json" ]]; then
2022-05-04 09:51:29 +03:00
cp "${ROOT_FOLDER}/templates/apps-sample.json" "${STATE_FOLDER}/apps.json" && chown -R "1000:1000" "${STATE_FOLDER}/users.json"
fi
# Copy the user state if it isn't here
if [[ ! -f "${STATE_FOLDER}/users.json" ]]; then
cp "${ROOT_FOLDER}/templates/users-sample.json" "${STATE_FOLDER}/users.json" && chown -R "1000:1000" "${STATE_FOLDER}/users.json"
2022-04-21 21:54:45 +03:00
fi
2022-05-07 10:09:11 +03:00
# Create seed file with cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
if [[ ! -f "${STATE_FOLDER}/seed" ]]; then
echo "Generating seed..."
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 > "${STATE_FOLDER}/seed"
fi
2022-03-30 22:26:01 +03:00
export DOCKER_CLIENT_TIMEOUT=240
export COMPOSE_HTTP_TIMEOUT=240
2022-04-21 23:14:47 +03:00
echo "Generating config files..."
# Remove current .env file
[[ -f "${ROOT_FOLDER}/.env" ]] && rm -f "${ROOT_FOLDER}/.env"
[[ -f "${ROOT_FOLDER}/packages/system-api/.env" ]] && rm -f "${ROOT_FOLDER}/packages/system-api/.env"
2022-04-21 23:14:47 +03:00
2022-04-21 22:41:56 +03:00
# Store paths to intermediary config files
ENV_FILE="$ROOT_FOLDER/templates/.env"
2022-05-07 09:27:26 +03:00
ENV_FILE_SYSTEM_API="$ROOT_FOLDER/templates/.env-api"
2022-04-21 22:41:56 +03:00
# Remove intermediary config files
[[ -f "$ENV_FILE" ]] && rm -f "$ENV_FILE"
2022-05-07 09:27:26 +03:00
[[ -f "$ENV_FILE_SYSTEM_API" ]] && rm -f "$ENV_FILE_SYSTEM_API"
2022-04-21 22:41:56 +03:00
# Copy template configs to intermediary configs
2022-04-21 23:14:47 +03:00
[[ -f "$ROOT_FOLDER/templates/env-sample" ]] && cp "$ROOT_FOLDER/templates/env-sample" "$ENV_FILE"
2022-05-07 09:27:26 +03:00
[[ -f "$ROOT_FOLDER/templates/env-api-sample" ]] && cp "$ROOT_FOLDER/templates/env-api-sample" "$ENV_FILE_SYSTEM_API"
JWT_SECRET=$(derive_entropy "jwt")
for template in "${ENV_FILE}" "${ENV_FILE_SYSTEM_API}"; do
# Replace placeholders with actual values
2022-05-03 23:55:55 +03:00
sed -i "s/<dns_ip>/${DNS_IP}/g" "${template}"
2022-04-21 22:41:56 +03:00
sed -i "s/<internal_ip>/${INTERNAL_IP}/g" "${template}"
sed -i "s/<puid>/${PUID}/g" "${template}"
sed -i "s/<pgid>/${PGID}/g" "${template}"
sed -i "s/<tz>/${TZ}/g" "${template}"
2022-05-07 09:27:26 +03:00
sed -i "s/<jwt_secret>/${JWT_SECRET}/g" "${template}"
2022-05-07 10:09:11 +03:00
sed -i "s/<root_folder>/${SED_ROOT_FOLDER}/g" "${template}"
sed -i "s/<architecture>/${ARCHITECTURE}/g" "${template}"
2022-04-21 22:41:56 +03:00
done
mv -f "$ENV_FILE" "$ROOT_FOLDER/.env"
mv -f "$ENV_FILE_SYSTEM_API" "$ROOT_FOLDER/packages/system-api/.env"
2022-04-21 22:41:56 +03:00
ansible-playbook ansible/start.yml -i ansible/hosts -K -e username="$USER"
2022-04-21 22:41:56 +03:00
2022-03-29 22:26:52 +03:00
# Run docker-compose
2022-04-21 22:41:56 +03:00
docker-compose --env-file "${ROOT_FOLDER}/.env" up --detach --remove-orphans --build || {
2022-03-30 22:26:01 +03:00
echo "Failed to start containers"
exit 1
}
2022-03-29 22:26:52 +03:00
2022-04-21 23:14:47 +03:00
str=$(get_json_field ${STATE_FOLDER}/apps.json installed)
apps_to_start=($str)
2022-04-29 22:42:25 +03:00
# for app in "${apps_to_start[@]}"; do
# "${ROOT_FOLDER}/scripts/app.sh" start $app
# done
2022-04-21 23:14:47 +03:00
echo "Tipi is now running"
echo ""
cat << "EOF"
_,.
,` -.)
'( _/'-\\-.
/,|`--._,-^| ,
\_| |`-._/|| ,'|
| `-, / | / /
| || | / /
`r-._||/ __ / /
__,-<_ )`-/ `./ /
' \ `---' \ / /
| |./ /
/ // /
\_/' \ |/ /
| | _,^-'/ /
| , `` (\/ /_
\,.->._ \X-=/^
( / `-._//^`
`Y-.____(__}
| {__)
()`
EOF
echo ""
2022-04-28 21:18:00 +03:00
echo "Visit http://${INTERNAL_IP}/ to view the dashboard"
echo ""