start.sh 5.5 KB

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