start.sh 4.9 KB

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