start.sh 4.8 KB

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