start.sh 7.3 KB

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