start.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/usr/bin/env bash
  2. # Required Notice: Copyright
  3. # Umbrel (https://umbrel.com)
  4. set -e # Exit immediately if a command exits with a non-zero status.
  5. # use greadlink instead of readlink on osx
  6. if [[ "$(uname)" == "Darwin" ]]; then
  7. readlink=greadlink
  8. else
  9. readlink=readlink
  10. fi
  11. NGINX_PORT=80
  12. NGINX_PORT_SSL=443
  13. PROXY_PORT=8080
  14. DOMAIN=tipi.localhost
  15. while [ -n "$1" ]; do # while loop starts
  16. case "$1" in
  17. --rc) rc="true" ;;
  18. --ci) ci="true" ;;
  19. --port)
  20. port="$2"
  21. if [[ "${port}" =~ ^[0-9]+$ ]]; then
  22. NGINX_PORT="${port}"
  23. else
  24. echo "--port must be a number"
  25. exit 1
  26. fi
  27. shift
  28. ;;
  29. --ssl-port)
  30. ssl_port="$2"
  31. if [[ "${ssl_port}" =~ ^[0-9]+$ ]]; then
  32. NGINX_PORT_SSL="${ssl_port}"
  33. else
  34. echo "--ssl-port must be a number"
  35. exit 1
  36. fi
  37. shift
  38. ;;
  39. --proxy-port)
  40. proxy_port="$2"
  41. if [[ "${proxy_port}" =~ ^[0-9]+$ ]]; then
  42. PROXY_PORT="${proxy_port}"
  43. else
  44. echo "--proxy-port must be a number"
  45. exit 1
  46. fi
  47. shift
  48. ;;
  49. --domain)
  50. domain="$2"
  51. if [[ "${domain}" =~ ^[a-zA-Z0-9.-]+$ ]]; then
  52. DOMAIN="${domain}"
  53. else
  54. echo "--domain must be a valid domain"
  55. exit 1
  56. fi
  57. shift
  58. ;;
  59. --)
  60. shift # The double dash makes them parameters
  61. break
  62. ;;
  63. *) echo "Option $1 not recognized" && exit 1 ;;
  64. esac
  65. shift
  66. done
  67. # Ensure BASH_SOURCE is ./scripts/start.sh
  68. if [[ "${BASH_SOURCE[0]}" != "./scripts/start.sh" ]]; then
  69. echo "Please make sure this script is executed from runtipi/"
  70. exit 1
  71. fi
  72. # Check we are on linux
  73. if [[ "$(uname)" != "Linux" ]]; then
  74. echo "Tipi only works on Linux"
  75. exit 1
  76. fi
  77. # If port is not 80 and domain is not tipi.localhost, we exit
  78. if [[ "${NGINX_PORT}" != "80" ]] && [[ "${DOMAIN}" != "tipi.localhost" ]]; then
  79. echo "Using a custom domain with a custom port is not supported"
  80. exit 1
  81. fi
  82. ROOT_FOLDER="$($readlink -f $(dirname "${BASH_SOURCE[0]}")/..)"
  83. STATE_FOLDER="${ROOT_FOLDER}/state"
  84. SED_ROOT_FOLDER="$(echo $ROOT_FOLDER | sed 's/\//\\\//g')"
  85. NETWORK_INTERFACE="$(ip route | grep default | awk '{print $5}' | uniq)"
  86. INTERNAL_IP="$(ip addr show "${NETWORK_INTERFACE}" | grep "inet " | awk '{print $2}' | cut -d/ -f1)"
  87. DNS_IP=9.9.9.9 # Default to Quad9 DNS
  88. ARCHITECTURE="$(uname -m)"
  89. TZ="$(timedatectl | grep "Time zone" | awk '{print $3}' | sed 's/\//\\\//g' || Europe\/Berlin)"
  90. APPS_REPOSITORY="https://github.com/meienberger/runtipi-appstore"
  91. REPO_ID="$(${ROOT_FOLDER}/scripts/git.sh get_hash ${APPS_REPOSITORY})"
  92. APPS_REPOSITORY_ESCAPED="$(echo ${APPS_REPOSITORY} | sed 's/\//\\\//g')"
  93. if [[ "$ARCHITECTURE" == "aarch64" ]]; then
  94. ARCHITECTURE="arm64"
  95. fi
  96. if [[ $UID != 0 ]]; then
  97. echo "Tipi must be started as root"
  98. echo "Please re-run this script as"
  99. echo " sudo ./scripts/start"
  100. exit 1
  101. fi
  102. # Configure Tipi if it isn't already configured
  103. "${ROOT_FOLDER}/scripts/configure.sh"
  104. # Get field from json file
  105. function get_json_field() {
  106. local json_file="$1"
  107. local field="$2"
  108. echo $(jq -r ".${field}" "${json_file}")
  109. }
  110. # Deterministically derives 128 bits of cryptographically secure entropy
  111. function derive_entropy() {
  112. SEED_FILE="${STATE_FOLDER}/seed"
  113. identifier="${1}"
  114. tipi_seed=$(cat "${SEED_FILE}") || true
  115. if [[ -z "$tipi_seed" ]] || [[ -z "$identifier" ]]; then
  116. echo >&2 "Missing derivation parameter, this is unsafe, exiting."
  117. exit 1
  118. fi
  119. # We need `sed 's/^.* //'` to trim the "(stdin)= " prefix from some versions of openssl
  120. printf "%s" "${identifier}" | openssl dgst -sha256 -hmac "${tipi_seed}" | sed 's/^.* //'
  121. }
  122. # Copy the config sample if it isn't here
  123. if [[ ! -f "${STATE_FOLDER}/apps.json" ]]; then
  124. cp "${ROOT_FOLDER}/templates/config-sample.json" "${STATE_FOLDER}/config.json"
  125. fi
  126. # Get current dns from host
  127. if [[ -f "/etc/resolv.conf" ]]; then
  128. 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)
  129. fi
  130. # Create seed file with cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
  131. if [[ ! -f "${STATE_FOLDER}/seed" ]]; then
  132. echo "Generating seed..."
  133. cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 >"${STATE_FOLDER}/seed"
  134. fi
  135. export DOCKER_CLIENT_TIMEOUT=240
  136. export COMPOSE_HTTP_TIMEOUT=240
  137. echo "Generating config files..."
  138. # Remove current .env file
  139. [[ -f "${ROOT_FOLDER}/.env" ]] && rm -f "${ROOT_FOLDER}/.env"
  140. # Store paths to intermediary config files
  141. ENV_FILE=$(mktemp)
  142. # Copy template configs to intermediary configs
  143. [[ -f "$ROOT_FOLDER/templates/env-sample" ]] && cp "$ROOT_FOLDER/templates/env-sample" "$ENV_FILE"
  144. JWT_SECRET=$(derive_entropy "jwt")
  145. POSTGRES_PASSWORD=$(derive_entropy "postgres")
  146. TIPI_VERSION=$(get_json_field "${ROOT_FOLDER}/package.json" version)
  147. echo "Creating .env file with the following values:"
  148. echo " DOMAIN=${DOMAIN}"
  149. echo " INTERNAL_IP=${INTERNAL_IP}"
  150. echo " NGINX_PORT=${NGINX_PORT}"
  151. echo " NGINX_PORT_SSL=${NGINX_PORT_SSL}"
  152. echo " PROXY_PORT=${PROXY_PORT}"
  153. echo " DNS_IP=${DNS_IP}"
  154. echo " ARCHITECTURE=${ARCHITECTURE}"
  155. echo " TZ=${TZ}"
  156. echo " APPS_REPOSITORY=${APPS_REPOSITORY}"
  157. echo " REPO_ID=${REPO_ID}"
  158. echo " JWT_SECRET=<redacted>"
  159. echo " POSTGRES_PASSWORD=<redacted>"
  160. echo " TIPI_VERSION=${TIPI_VERSION}"
  161. echo " ROOT_FOLDER=${SED_ROOT_FOLDER}"
  162. echo " APPS_REPOSITORY=${APPS_REPOSITORY_ESCAPED}"
  163. for template in ${ENV_FILE}; do
  164. sed -i "s/<dns_ip>/${DNS_IP}/g" "${template}"
  165. sed -i "s/<internal_ip>/${INTERNAL_IP}/g" "${template}"
  166. sed -i "s/<tz>/${TZ}/g" "${template}"
  167. sed -i "s/<jwt_secret>/${JWT_SECRET}/g" "${template}"
  168. sed -i "s/<root_folder>/${SED_ROOT_FOLDER}/g" "${template}"
  169. sed -i "s/<tipi_version>/${TIPI_VERSION}/g" "${template}"
  170. sed -i "s/<architecture>/${ARCHITECTURE}/g" "${template}"
  171. sed -i "s/<nginx_port>/${NGINX_PORT}/g" "${template}"
  172. sed -i "s/<nginx_port_ssl>/${NGINX_PORT_SSL}/g" "${template}"
  173. sed -i "s/<proxy_port>/${PROXY_PORT}/g" "${template}"
  174. sed -i "s/<postgres_password>/${POSTGRES_PASSWORD}/g" "${template}"
  175. sed -i "s/<apps_repo_id>/${REPO_ID}/g" "${template}"
  176. sed -i "s/<apps_repo_url>/${APPS_REPOSITORY_ESCAPED}/g" "${template}"
  177. sed -i "s/<domain>/${DOMAIN}/g" "${template}"
  178. done
  179. mv -f "$ENV_FILE" "$ROOT_FOLDER/.env"
  180. # Run system-info.sh
  181. echo "Running system-info.sh..."
  182. bash "${ROOT_FOLDER}/scripts/system-info.sh"
  183. # Add crontab to run system-info.sh every minute
  184. ! (crontab -l | grep -q "${ROOT_FOLDER}/scripts/system-info.sh") && (
  185. crontab -l
  186. echo "* * * * * ${ROOT_FOLDER}/scripts/system-info.sh"
  187. ) | crontab -
  188. ## Don't run if config-only
  189. if [[ ! $ci == "true" ]]; then
  190. if [[ $rc == "true" ]]; then
  191. docker compose -f docker-compose.rc.yml --env-file "${ROOT_FOLDER}/.env" pull
  192. # Run docker compose
  193. docker compose -f docker-compose.rc.yml --env-file "${ROOT_FOLDER}/.env" up --detach --remove-orphans --build || {
  194. echo "Failed to start containers"
  195. exit 1
  196. }
  197. else
  198. docker compose --env-file "${ROOT_FOLDER}/.env" pull
  199. # Run docker compose
  200. docker compose --env-file "${ROOT_FOLDER}/.env" up --detach --remove-orphans --build || {
  201. echo "Failed to start containers"
  202. exit 1
  203. }
  204. fi
  205. fi
  206. echo "Tipi is now running"
  207. echo ""
  208. cat <<"EOF"
  209. _,.
  210. ,` -.)
  211. '( _/'-\\-.
  212. /,|`--._,-^| ,
  213. \_| |`-._/|| ,'|
  214. | `-, / | / /
  215. | || | / /
  216. `r-._||/ __ / /
  217. __,-<_ )`-/ `./ /
  218. ' \ `---' \ / /
  219. | |./ /
  220. / // /
  221. \_/' \ |/ /
  222. | | _,^-'/ /
  223. | , `` (\/ /_
  224. \,.->._ \X-=/^
  225. ( / `-._//^`
  226. `Y-.____(__}
  227. | {__)
  228. ()`
  229. EOF
  230. port_display=""
  231. if [[ $NGINX_PORT != "80" ]]; then
  232. port_display=":${NGINX_PORT}"
  233. fi
  234. echo ""
  235. echo "Visit http://${INTERNAL_IP}${port_display}/ to view the dashboard"
  236. echo ""