start.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 [[ $(basename $(pwd)) != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; 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. NETWORK_INTERFACE_COUNT=$(echo "$NETWORK_INTERFACE" | wc -l)
  87. if [[ "$NETWORK_INTERFACE_COUNT" -eq 0 ]]; then
  88. echo "No network interface found!"
  89. exit 1
  90. elif [[ "$NETWORK_INTERFACE_COUNT" -gt 1 ]]; then
  91. echo "Found multiple network interfaces. Please select one of the following interfaces:"
  92. echo "$NETWORK_INTERFACE"
  93. while true; do
  94. read -p "> " USER_NETWORK_INTERFACE
  95. if echo "$NETWORK_INTERFACE" | grep -x "$USER_NETWORK_INTERFACE"; then
  96. NETWORK_INTERFACE="$USER_NETWORK_INTERFACE"
  97. break
  98. else
  99. echo "Please select one of the interfaces above. (CTRL+C to abort)"
  100. fi
  101. done
  102. fi
  103. INTERNAL_IP="$(ip addr show "${NETWORK_INTERFACE}" | grep "inet " | awk '{print $2}' | cut -d/ -f1)"
  104. DNS_IP=9.9.9.9 # Default to Quad9 DNS
  105. ARCHITECTURE="$(uname -m)"
  106. TZ="$(timedatectl | grep "Time zone" | awk '{print $3}' | sed 's/\//\\\//g' || Europe\/Berlin)"
  107. APPS_REPOSITORY="https://github.com/meienberger/runtipi-appstore"
  108. REPO_ID="$(${ROOT_FOLDER}/scripts/git.sh get_hash ${APPS_REPOSITORY})"
  109. APPS_REPOSITORY_ESCAPED="$(echo ${APPS_REPOSITORY} | sed 's/\//\\\//g')"
  110. if [[ "$ARCHITECTURE" == "aarch64" ]]; then
  111. ARCHITECTURE="arm64"
  112. fi
  113. if [[ $UID != 0 ]]; then
  114. echo "Tipi must be started as root"
  115. echo "Please re-run this script as"
  116. echo " sudo ./scripts/start"
  117. exit 1
  118. fi
  119. # Configure Tipi if it isn't already configured
  120. "${ROOT_FOLDER}/scripts/configure.sh"
  121. # Get field from json file
  122. function get_json_field() {
  123. local json_file="$1"
  124. local field="$2"
  125. echo $(jq -r ".${field}" "${json_file}")
  126. }
  127. # Deterministically derives 128 bits of cryptographically secure entropy
  128. function derive_entropy() {
  129. SEED_FILE="${STATE_FOLDER}/seed"
  130. identifier="${1}"
  131. tipi_seed=$(cat "${SEED_FILE}") || true
  132. if [[ -z "$tipi_seed" ]] || [[ -z "$identifier" ]]; then
  133. echo >&2 "Missing derivation parameter, this is unsafe, exiting."
  134. exit 1
  135. fi
  136. # We need `sed 's/^.* //'` to trim the "(stdin)= " prefix from some versions of openssl
  137. printf "%s" "${identifier}" | openssl dgst -sha256 -hmac "${tipi_seed}" | sed 's/^.* //'
  138. }
  139. # Copy the config sample if it isn't here
  140. if [[ ! -f "${STATE_FOLDER}/apps.json" ]]; then
  141. cp "${ROOT_FOLDER}/templates/config-sample.json" "${STATE_FOLDER}/config.json"
  142. fi
  143. # Get current dns from host
  144. if [[ -f "/etc/resolv.conf" ]]; then
  145. 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)
  146. fi
  147. # Create seed file with cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
  148. if [[ ! -f "${STATE_FOLDER}/seed" ]]; then
  149. echo "Generating seed..."
  150. cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 >"${STATE_FOLDER}/seed"
  151. fi
  152. export DOCKER_CLIENT_TIMEOUT=240
  153. export COMPOSE_HTTP_TIMEOUT=240
  154. echo "Generating config files..."
  155. # Remove current .env file
  156. [[ -f "${ROOT_FOLDER}/.env" ]] && rm -f "${ROOT_FOLDER}/.env"
  157. # Store paths to intermediary config files
  158. ENV_FILE=$(mktemp)
  159. # Copy template configs to intermediary configs
  160. [[ -f "$ROOT_FOLDER/templates/env-sample" ]] && cp "$ROOT_FOLDER/templates/env-sample" "$ENV_FILE"
  161. JWT_SECRET=$(derive_entropy "jwt")
  162. POSTGRES_PASSWORD=$(derive_entropy "postgres")
  163. TIPI_VERSION=$(get_json_field "${ROOT_FOLDER}/package.json" version)
  164. echo "Creating .env file with the following values:"
  165. echo " DOMAIN=${DOMAIN}"
  166. echo " INTERNAL_IP=${INTERNAL_IP}"
  167. echo " NGINX_PORT=${NGINX_PORT}"
  168. echo " NGINX_PORT_SSL=${NGINX_PORT_SSL}"
  169. echo " PROXY_PORT=${PROXY_PORT}"
  170. echo " DNS_IP=${DNS_IP}"
  171. echo " ARCHITECTURE=${ARCHITECTURE}"
  172. echo " TZ=${TZ}"
  173. echo " APPS_REPOSITORY=${APPS_REPOSITORY}"
  174. echo " REPO_ID=${REPO_ID}"
  175. echo " JWT_SECRET=<redacted>"
  176. echo " POSTGRES_PASSWORD=<redacted>"
  177. echo " TIPI_VERSION=${TIPI_VERSION}"
  178. echo " ROOT_FOLDER=${SED_ROOT_FOLDER}"
  179. echo " APPS_REPOSITORY=${APPS_REPOSITORY_ESCAPED}"
  180. for template in ${ENV_FILE}; do
  181. sed -i "s/<dns_ip>/${DNS_IP}/g" "${template}"
  182. sed -i "s/<internal_ip>/${INTERNAL_IP}/g" "${template}"
  183. sed -i "s/<tz>/${TZ}/g" "${template}"
  184. sed -i "s/<jwt_secret>/${JWT_SECRET}/g" "${template}"
  185. sed -i "s/<root_folder>/${SED_ROOT_FOLDER}/g" "${template}"
  186. sed -i "s/<tipi_version>/${TIPI_VERSION}/g" "${template}"
  187. sed -i "s/<architecture>/${ARCHITECTURE}/g" "${template}"
  188. sed -i "s/<nginx_port>/${NGINX_PORT}/g" "${template}"
  189. sed -i "s/<nginx_port_ssl>/${NGINX_PORT_SSL}/g" "${template}"
  190. sed -i "s/<proxy_port>/${PROXY_PORT}/g" "${template}"
  191. sed -i "s/<postgres_password>/${POSTGRES_PASSWORD}/g" "${template}"
  192. sed -i "s/<apps_repo_id>/${REPO_ID}/g" "${template}"
  193. sed -i "s/<apps_repo_url>/${APPS_REPOSITORY_ESCAPED}/g" "${template}"
  194. sed -i "s/<domain>/${DOMAIN}/g" "${template}"
  195. done
  196. mv -f "$ENV_FILE" "$ROOT_FOLDER/.env"
  197. # Run system-info.sh
  198. echo "Running system-info.sh..."
  199. bash "${ROOT_FOLDER}/scripts/system-info.sh"
  200. # Add crontab to run system-info.sh every minute
  201. ! (crontab -l | grep -q "${ROOT_FOLDER}/scripts/system-info.sh") && (
  202. crontab -l
  203. echo "* * * * * ${ROOT_FOLDER}/scripts/system-info.sh"
  204. ) | crontab -
  205. ## Don't run if config-only
  206. if [[ ! $ci == "true" ]]; then
  207. if [[ $rc == "true" ]]; then
  208. docker-compose -f docker-compose.rc.yml --env-file "${ROOT_FOLDER}/.env" pull
  209. # Run docker-compose
  210. docker-compose -f docker-compose.rc.yml --env-file "${ROOT_FOLDER}/.env" up --detach --remove-orphans --build || {
  211. echo "Failed to start containers"
  212. exit 1
  213. }
  214. else
  215. docker-compose --env-file "${ROOT_FOLDER}/.env" pull
  216. # Run docker-compose
  217. docker-compose --env-file "${ROOT_FOLDER}/.env" up --detach --remove-orphans --build || {
  218. echo "Failed to start containers"
  219. exit 1
  220. }
  221. fi
  222. fi
  223. echo "Tipi is now running"
  224. echo ""
  225. cat <<"EOF"
  226. _,.
  227. ,` -.)
  228. '( _/'-\\-.
  229. /,|`--._,-^| ,
  230. \_| |`-._/|| ,'|
  231. | `-, / | / /
  232. | || | / /
  233. `r-._||/ __ / /
  234. __,-<_ )`-/ `./ /
  235. ' \ `---' \ / /
  236. | |./ /
  237. / // /
  238. \_/' \ |/ /
  239. | | _,^-'/ /
  240. | , `` (\/ /_
  241. \,.->._ \X-=/^
  242. ( / `-._//^`
  243. `Y-.____(__}
  244. | {__)
  245. ()`
  246. EOF
  247. port_display=""
  248. if [[ $NGINX_PORT != "80" ]]; then
  249. port_display=":${NGINX_PORT}"
  250. fi
  251. echo ""
  252. echo "Visit http://${INTERNAL_IP}${port_display}/ to view the dashboard"
  253. echo ""