start.sh 5.9 KB

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