start.sh 6.1 KB

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