start.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env bash
  2. set -e # Exit immediately if a command exits with a non-zero status.
  3. # use greadlink instead of readlink on osx
  4. if [[ "$(uname)" == "Darwin" ]]; then
  5. readlink=greadlink
  6. else
  7. readlink=readlink
  8. fi
  9. config_only="$1"
  10. ROOT_FOLDER="$($readlink -f $(dirname "${BASH_SOURCE[0]}")/..)"
  11. STATE_FOLDER="${ROOT_FOLDER}/state"
  12. SED_ROOT_FOLDER="$(echo $ROOT_FOLDER | sed 's/\//\\\//g')"
  13. INTERNAL_IP="$(hostname -I | awk '{print $1}')"
  14. DNS_IP=9.9.9.9 # Default to Quad9 DNS
  15. USERNAME="$(id -nu 1000)"
  16. ARCHITECTURE="$(uname -m)"
  17. if [[ "$architecture" == "aarch64" ]]; then
  18. ARCHITECTURE="arm64"
  19. fi
  20. if [[ $UID != 0 ]]; then
  21. echo "Tipi must be started as root"
  22. echo "Please re-run this script as"
  23. echo " sudo ./scripts/start"
  24. exit 1
  25. fi
  26. # Configure Tipi if it isn't already configured
  27. if [[ ! -f "${STATE_FOLDER}/configured" ]]; then
  28. "${ROOT_FOLDER}/scripts/configure.sh"
  29. fi
  30. # Get field from json file
  31. function get_json_field() {
  32. local json_file="$1"
  33. local field="$2"
  34. echo $(jq -r ".${field}" "${json_file}")
  35. }
  36. # Deterministically derives 128 bits of cryptographically secure entropy
  37. function derive_entropy() {
  38. SEED_FILE="${STATE_FOLDER}/seed"
  39. identifier="${1}"
  40. tipi_seed=$(cat "${SEED_FILE}") || true
  41. if [[ -z "$tipi_seed" ]] || [[ -z "$identifier" ]]; then
  42. >&2 echo "Missing derivation parameter, this is unsafe, exiting."
  43. exit 1
  44. fi
  45. # We need `sed 's/^.* //'` to trim the "(stdin)= " prefix from some versions of openssl
  46. printf "%s" "${identifier}" | openssl dgst -sha256 -hmac "${tipi_seed}" | sed 's/^.* //'
  47. }
  48. PUID="$(id -u)"
  49. PGID="$(id -g)"
  50. TZ="$(cat /etc/timezone | sed 's/\//\\\//g' || echo "Europe/Berlin")"
  51. # Copy the app state if it isn't here
  52. if [[ ! -f "${STATE_FOLDER}/apps.json" ]]; then
  53. cp "${ROOT_FOLDER}/templates/apps-sample.json" "${STATE_FOLDER}/apps.json"
  54. fi
  55. # Copy the user state if it isn't here
  56. if [[ ! -f "${STATE_FOLDER}/users.json" ]]; then
  57. cp "${ROOT_FOLDER}/templates/users-sample.json" "${STATE_FOLDER}/users.json"
  58. fi
  59. chown -R 1000:1000 "${STATE_FOLDER}/apps.json"
  60. chown -R 1000:1000 "${STATE_FOLDER}/users.json"
  61. # Get dns ip if pihole is installed
  62. str=$(get_json_field ${STATE_FOLDER}/apps.json installed)
  63. # if pihole is present in str add it as DNS
  64. if [[ $str = *"pihole"* ]]; then
  65. DNS_IP=10.21.21.201
  66. fi
  67. # Create seed file with cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
  68. if [[ ! -f "${STATE_FOLDER}/seed" ]]; then
  69. echo "Generating seed..."
  70. cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 > "${STATE_FOLDER}/seed"
  71. fi
  72. export DOCKER_CLIENT_TIMEOUT=240
  73. export COMPOSE_HTTP_TIMEOUT=240
  74. echo "Generating config files..."
  75. # Remove current .env file
  76. [[ -f "${ROOT_FOLDER}/.env" ]] && rm -f "${ROOT_FOLDER}/.env"
  77. [[ -f "${ROOT_FOLDER}/packages/system-api/.env" ]] && rm -f "${ROOT_FOLDER}/packages/system-api/.env"
  78. # Store paths to intermediary config files
  79. ENV_FILE=$(mktemp)
  80. # Copy template configs to intermediary configs
  81. [[ -f "$ROOT_FOLDER/templates/env-sample" ]] && cp "$ROOT_FOLDER/templates/env-sample" "$ENV_FILE"
  82. JWT_SECRET=$(derive_entropy "jwt")
  83. for template in "${ENV_FILE}"; do
  84. sed -i "s/<dns_ip>/${DNS_IP}/g" "${template}"
  85. sed -i "s/<internal_ip>/${INTERNAL_IP}/g" "${template}"
  86. sed -i "s/<puid>/${PUID}/g" "${template}"
  87. sed -i "s/<pgid>/${PGID}/g" "${template}"
  88. sed -i "s/<tz>/${TZ}/g" "${template}"
  89. sed -i "s/<jwt_secret>/${JWT_SECRET}/g" "${template}"
  90. sed -i "s/<root_folder>/${SED_ROOT_FOLDER}/g" "${template}"
  91. sed -i "s/<tipi_version>/$(cat "${ROOT_FOLDER}/VERSION")/g" "${template}"
  92. sed -i "s/<architecture>/${ARCHITECTURE}/g" "${template}"
  93. done
  94. mv -f "$ENV_FILE" "$ROOT_FOLDER/.env"
  95. # Run system-info.sh
  96. echo "Running system-info.sh..."
  97. bash "${ROOT_FOLDER}/scripts/system-info.sh"
  98. # Give permissions 1000:1000 to app data
  99. chown -R 1000:1000 "${ROOT_FOLDER}/app-data"
  100. ## Don't run if config-only
  101. if [[ ! $config_only == "true" ]]; then
  102. docker-compose --env-file "${ROOT_FOLDER}/.env" pull
  103. # Run docker-compose
  104. docker-compose --env-file "${ROOT_FOLDER}/.env" up --detach --remove-orphans --build || {
  105. echo "Failed to start containers"
  106. exit 1
  107. }
  108. fi
  109. echo "Tipi is now running"
  110. echo ""
  111. cat << "EOF"
  112. _,.
  113. ,` -.)
  114. '( _/'-\\-.
  115. /,|`--._,-^| ,
  116. \_| |`-._/|| ,'|
  117. | `-, / | / /
  118. | || | / /
  119. `r-._||/ __ / /
  120. __,-<_ )`-/ `./ /
  121. ' \ `---' \ / /
  122. | |./ /
  123. / // /
  124. \_/' \ |/ /
  125. | | _,^-'/ /
  126. | , `` (\/ /_
  127. \,.->._ \X-=/^
  128. ( / `-._//^`
  129. `Y-.____(__}
  130. | {__)
  131. ()`
  132. EOF
  133. echo ""
  134. echo "Visit http://${INTERNAL_IP}/ to view the dashboard"
  135. echo ""