start.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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}' | uniq)"
  52. INTERNAL_IP="$(ip addr show "${NETWORK_INTERFACE}" | grep "inet " | awk '{print $2}' | cut -d/ -f1)"
  53. DNS_IP=9.9.9.9 # Default to Quad9 DNS
  54. ARCHITECTURE="$(uname -m)"
  55. TZ="$(timedatectl | grep "Time zone" | awk '{print $3}' || Europe/Berlin)"
  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. "${ROOT_FOLDER}/scripts/configure.sh"
  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. # Copy the app state if it isn't here
  86. if [[ ! -f "${STATE_FOLDER}/apps.json" ]]; then
  87. cp "${ROOT_FOLDER}/templates/apps-sample.json" "${STATE_FOLDER}/apps.json"
  88. fi
  89. # Copy the user state if it isn't here
  90. if [[ ! -f "${STATE_FOLDER}/users.json" ]]; then
  91. cp "${ROOT_FOLDER}/templates/users-sample.json" "${STATE_FOLDER}/users.json"
  92. fi
  93. # Get current dns from host
  94. if [[ -f "/etc/resolv.conf" ]]; then
  95. TEMP=$(grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /etc/resolv.conf | head -n 1)
  96. fi
  97. # Create seed file with cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
  98. if [[ ! -f "${STATE_FOLDER}/seed" ]]; then
  99. echo "Generating seed..."
  100. cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 >"${STATE_FOLDER}/seed"
  101. fi
  102. export DOCKER_CLIENT_TIMEOUT=240
  103. export COMPOSE_HTTP_TIMEOUT=240
  104. echo "Generating config files..."
  105. # Remove current .env file
  106. [[ -f "${ROOT_FOLDER}/.env" ]] && rm -f "${ROOT_FOLDER}/.env"
  107. [[ -f "${ROOT_FOLDER}/packages/system-api/.env" ]] && rm -f "${ROOT_FOLDER}/packages/system-api/.env"
  108. # Store paths to intermediary config files
  109. ENV_FILE=$(mktemp)
  110. # Copy template configs to intermediary configs
  111. [[ -f "$ROOT_FOLDER/templates/env-sample" ]] && cp "$ROOT_FOLDER/templates/env-sample" "$ENV_FILE"
  112. JWT_SECRET=$(derive_entropy "jwt")
  113. POSTGRES_PASSWORD=$(derive_entropy "postgres")
  114. TIPI_VERSION=$(get_json_field "${ROOT_FOLDER}/package.json" version)§
  115. for template in ${ENV_FILE}; do
  116. sed -i "s/<dns_ip>/${DNS_IP}/g" "${template}"
  117. sed -i "s/<internal_ip>/${INTERNAL_IP}/g" "${template}"
  118. sed -i "s/<tz>/${TZ}/g" "${template}"
  119. sed -i "s/<jwt_secret>/${JWT_SECRET}/g" "${template}"
  120. sed -i "s/<root_folder>/${SED_ROOT_FOLDER}/g" "${template}"
  121. sed -i "s/<tipi_version>/${TIPI_VERSION}/g" "${template}"
  122. sed -i "s/<architecture>/${ARCHITECTURE}/g" "${template}"
  123. sed -i "s/<nginx_port>/${NGINX_PORT}/g" "${template}"
  124. sed -i "s/<proxy_port>/${PROXY_PORT}/g" "${template}"
  125. sed -i "s/<postgres_password>/${POSTGRES_PASSWORD}/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") && (
  133. crontab -l
  134. echo "* * * * * ${ROOT_FOLDER}/scripts/system-info.sh"
  135. ) | crontab -
  136. ## Don't run if config-only
  137. if [[ ! $ci == "true" ]]; then
  138. if [[ $rc == "true" ]]; then
  139. docker-compose -f docker-compose.rc.yml --env-file "${ROOT_FOLDER}/.env" pull
  140. # Run docker-compose
  141. docker-compose -f docker-compose.rc.yml --env-file "${ROOT_FOLDER}/.env" up --detach --remove-orphans --build || {
  142. echo "Failed to start containers"
  143. exit 1
  144. }
  145. else
  146. docker-compose --env-file "${ROOT_FOLDER}/.env" pull
  147. # Run docker-compose
  148. docker-compose --env-file "${ROOT_FOLDER}/.env" up --detach --remove-orphans --build || {
  149. echo "Failed to start containers"
  150. exit 1
  151. }
  152. fi
  153. fi
  154. echo "Tipi is now running"
  155. echo ""
  156. cat <<"EOF"
  157. _,.
  158. ,` -.)
  159. '( _/'-\\-.
  160. /,|`--._,-^| ,
  161. \_| |`-._/|| ,'|
  162. | `-, / | / /
  163. | || | / /
  164. `r-._||/ __ / /
  165. __,-<_ )`-/ `./ /
  166. ' \ `---' \ / /
  167. | |./ /
  168. / // /
  169. \_/' \ |/ /
  170. | | _,^-'/ /
  171. | , `` (\/ /_
  172. \,.->._ \X-=/^
  173. ( / `-._//^`
  174. `Y-.____(__}
  175. | {__)
  176. ()`
  177. EOF
  178. port_display=""
  179. if [[ $NGINX_PORT != "80" ]]; then
  180. port_display=":${NGINX_PORT}"
  181. fi
  182. echo ""
  183. echo "Visit http://${INTERNAL_IP}${port_display}/ to view the dashboard"
  184. echo ""