start.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. ROOT_FOLDER="$($readlink -f $(dirname "${BASH_SOURCE[0]}")/..)"
  10. STATE_FOLDER="${ROOT_FOLDER}/state"
  11. INTERNAL_IP="$(hostname -I | awk '{print $1}')"
  12. DNS_IP=9.9.9.9
  13. # Get field from json file
  14. function get_json_field() {
  15. local json_file="$1"
  16. local field="$2"
  17. echo $(jq -r ".${field}" "${json_file}")
  18. }
  19. # Deterministically derives 128 bits of cryptographically secure entropy
  20. function derive_entropy() {
  21. SEED_FILE="${STATE_FOLDER}/seed"
  22. identifier="${1}"
  23. tipi_seed=$(cat "${SEED_FILE}") || true
  24. if [[ -z "$tipi_seed" ]] || [[ -z "$identifier" ]]; then
  25. >&2 echo "Missing derivation parameter, this is unsafe, exiting."
  26. exit 1
  27. fi
  28. # We need `sed 's/^.* //'` to trim the "(stdin)= " prefix from some versions of openssl
  29. printf "%s" "${identifier}" | openssl dgst -sha256 -hmac "${tipi_seed}" | sed 's/^.* //'
  30. }
  31. # Get dns ip if pihole is installed
  32. str=$(get_json_field ${STATE_FOLDER}/apps.json installed)
  33. # if pihole is present in str add it as DNS
  34. if [[ $str = *"pihole"* ]]; then
  35. DNS_IP=10.21.21.201
  36. fi
  37. PUID="$(id -u)"
  38. PGID="$(id -g)"
  39. TZ="$(cat /etc/timezone | sed 's/\//\\\//g' || echo "Europe/Berlin")"
  40. if [[ $UID != 0 ]]; then
  41. echo "Tipi must be started as root"
  42. echo "Please re-run this script as"
  43. echo " sudo ./scripts/start"
  44. exit 1
  45. fi
  46. # Configure Umbrel if it isn't already configured
  47. if [[ ! -f "${STATE_FOLDER}/configured" ]]; then
  48. "${ROOT_FOLDER}/scripts/configure.sh"
  49. fi
  50. # Copy the app state if it isn't here
  51. if [[ ! -f "${STATE_FOLDER}/apps.json" ]]; then
  52. cp "${ROOT_FOLDER}/templates/apps-sample.json" "${STATE_FOLDER}/apps.json" && chown -R "1000:1000" "${STATE_FOLDER}/users.json"
  53. fi
  54. # Copy the user state if it isn't here
  55. if [[ ! -f "${STATE_FOLDER}/users.json" ]]; then
  56. cp "${ROOT_FOLDER}/templates/users-sample.json" "${STATE_FOLDER}/users.json" && chown -R "1000:1000" "${STATE_FOLDER}/users.json"
  57. fi
  58. export DOCKER_CLIENT_TIMEOUT=240
  59. export COMPOSE_HTTP_TIMEOUT=240
  60. echo "Generating config files..."
  61. # Remove current .env file
  62. [[ -f "${ROOT_FOLDER}/.env" ]] && rm -f "${ROOT_FOLDER}/.env"
  63. [[ -f "${ROOT_FOLDER}/system-api/.env" ]] && rm -f "${ROOT_FOLDER}/system-api/.env"
  64. # Store paths to intermediary config files
  65. ENV_FILE="$ROOT_FOLDER/templates/.env"
  66. ENV_FILE_SYSTEM_API="$ROOT_FOLDER/templates/.env-api"
  67. # Remove intermediary config files
  68. [[ -f "$ENV_FILE" ]] && rm -f "$ENV_FILE"
  69. [[ -f "$ENV_FILE_SYSTEM_API" ]] && rm -f "$ENV_FILE_SYSTEM_API"
  70. # Copy template configs to intermediary configs
  71. [[ -f "$ROOT_FOLDER/templates/env-sample" ]] && cp "$ROOT_FOLDER/templates/env-sample" "$ENV_FILE"
  72. [[ -f "$ROOT_FOLDER/templates/env-api-sample" ]] && cp "$ROOT_FOLDER/templates/env-api-sample" "$ENV_FILE_SYSTEM_API"
  73. JWT_SECRET=$(derive_entropy "jwt")
  74. echo $JWT_SECRET
  75. for template in "${ENV_FILE}" "${ENV_FILE_SYSTEM_API}"; do
  76. sed -i "s/<dns_ip>/${DNS_IP}/g" "${template}"
  77. sed -i "s/<internal_ip>/${INTERNAL_IP}/g" "${template}"
  78. sed -i "s/<puid>/${PUID}/g" "${template}"
  79. sed -i "s/<pgid>/${PGID}/g" "${template}"
  80. sed -i "s/<tz>/${TZ}/g" "${template}"
  81. sed -i "s/<root_folder>/${ROOT_FOLDER}/g" "${template}"
  82. sed -i "s/<jwt_secret>/${JWT_SECRET}/g" "${template}"
  83. done
  84. mv -f "$ENV_FILE" "$ROOT_FOLDER/.env"
  85. mv -f "$ENV_FILE_SYSTEM_API" "$ROOT_FOLDER/system-api/.env"
  86. ansible-playbook ansible/start.yml -i ansible/hosts -K
  87. # Run docker-compose
  88. docker-compose --env-file "${ROOT_FOLDER}/.env" up --detach --remove-orphans --build || {
  89. echo "Failed to start containers"
  90. exit 1
  91. }
  92. str=$(get_json_field ${STATE_FOLDER}/apps.json installed)
  93. apps_to_start=($str)
  94. # for app in "${apps_to_start[@]}"; do
  95. # "${ROOT_FOLDER}/scripts/app.sh" start $app
  96. # done
  97. echo "Tipi is now running"
  98. echo ""
  99. cat << "EOF"
  100. _,.
  101. ,` -.)
  102. '( _/'-\\-.
  103. /,|`--._,-^| ,
  104. \_| |`-._/|| ,'|
  105. | `-, / | / /
  106. | || | / /
  107. `r-._||/ __ / /
  108. __,-<_ )`-/ `./ /
  109. ' \ `---' \ / /
  110. | |./ /
  111. / // /
  112. \_/' \ |/ /
  113. | | _,^-'/ /
  114. | , `` (\/ /_
  115. \,.->._ \X-=/^
  116. ( / `-._//^`
  117. `Y-.____(__}
  118. | {__)
  119. ()`
  120. EOF
  121. echo ""
  122. echo "Visit http://${INTERNAL_IP}/ to view the dashboard"
  123. echo ""