common.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env bash
  2. # Get field from json file
  3. function get_json_field() {
  4. local json_file="$1"
  5. local field="$2"
  6. jq -r ".${field}" "${json_file}"
  7. }
  8. function write_log() {
  9. local message="$1"
  10. local log_file="/app/logs/script.log"
  11. echo "$(date) - ${message}" >>"${log_file}"
  12. }
  13. function derive_entropy() {
  14. SEED_FILE="${STATE_FOLDER}/seed"
  15. identifier="${1}"
  16. tipi_seed=$(cat "${SEED_FILE}") || true
  17. if [[ -z "$tipi_seed" ]] || [[ -z "$identifier" ]]; then
  18. echo >&2 "Missing derivation parameter, this is unsafe, exiting."
  19. exit 1
  20. fi
  21. printf "%s" "${identifier}" | openssl dgst -sha256 -hmac "${tipi_seed}" | sed 's/^.* //'
  22. }
  23. function ensure_pwd() {
  24. # # Ensure PWD ends with /runtipi
  25. cd /runtipi || echo ""
  26. if [[ $(basename "$(pwd)") != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; then
  27. echo "Please run this script from the runtipi directory"
  28. exit 1
  29. fi
  30. }
  31. function ensure_root() {
  32. if [[ $UID != 0 ]]; then
  33. echo "Tipi must be started as root"
  34. echo "Please re-run this script as"
  35. echo " sudo ./scripts/start"
  36. exit 1
  37. fi
  38. }
  39. function ensure_linux() {
  40. # Check we are on linux
  41. if [[ "$(uname)" != "Linux" ]]; then
  42. echo "Tipi only works on Linux"
  43. exit 1
  44. fi
  45. }
  46. function clean_logs() {
  47. # Clean logs folder
  48. logs_folder="${ROOT_FOLDER}/logs"
  49. if [ "$(find "${logs_folder}" -maxdepth 1 -type f | wc -l)" -gt 0 ]; then
  50. echo "Cleaning logs folder..."
  51. files=($(ls -d "${logs_folder}"/* | xargs -n 1 basename | sed 's/\///g'))
  52. for file in "${files[@]}"; do
  53. echo "Removing ${file}"
  54. rm -rf "${ROOT_FOLDER}/logs/${file}"
  55. done
  56. fi
  57. }