start.sh 9.1 KB

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