refactor(scripts): refactor bash scripts

This commit is contained in:
Nicolas Meienberger 2022-09-21 23:18:30 +02:00
parent f4ec85ed15
commit b5fdac47c8
7 changed files with 32 additions and 31 deletions

View file

@ -6,7 +6,7 @@ set -euo pipefail
cd /runtipi || echo ""
# Ensure PWD ends with /runtipi
if [[ "${PWD##*/}" != "runtipi" ]]; then
if [[ $(basename "$(pwd)") != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; then
echo "Please run this script from the runtipi directory"
exit 1
fi
@ -61,14 +61,15 @@ fi
if [ -z ${3+x} ]; then
args=""
else
args="${@:3}"
args="${*:3}"
fi
compose() {
local app="${1}"
shift
local architecture="$(uname -m)"
arch=$(uname -m)
local architecture="${arch}"
if [[ "$architecture" == "aarch64" ]]; then
architecture="arm64"
@ -167,7 +168,7 @@ fi
# Passes all arguments to Docker Compose
if [[ "$command" = "compose" ]]; then
compose "${app}" ${args}
compose "${app}" "${args}"
exit
fi

View file

@ -2,10 +2,10 @@
# Don't break if command fails
cd /runtipi || echo ""
# Ensure PWD ends with /runtipi
if [[ "${PWD##*/}" != "runtipi" ]]; then
echo ${PWD}
echo "Please run this script from the runtipi directory"
if [[ $(basename "$(pwd)") != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; then
echo "Please make sure this script is executed from runtipi/"
exit 1
fi
@ -29,7 +29,7 @@ EOF
# Get a static hash based on the repo url
function get_hash() {
url="${1}"
echo $(echo -n "${url}" | sha256sum | awk '{print $1}')
echo -n "${url}" | sha256sum | awk '{print $1}'
}
if [ -z ${1+x} ]; then
@ -67,7 +67,7 @@ if [[ "$command" = "update" ]]; then
fi
echo "Updating ${repo} in ${repo_hash}"
cd "${repo_dir}"
cd "${repo_dir}" || exit
git pull origin master
echo "Done"
exit
@ -75,6 +75,6 @@ fi
if [[ "$command" = "get_hash" ]]; then
repo="$2"
echo $(get_hash "${repo}")
get_hash "${repo}"
exit
fi

View file

@ -16,6 +16,12 @@ if [[ "$(uname)" != "Linux" ]]; then
exit 1
fi
# Ensure BASH_SOURCE is ./scripts/start.sh
if [[ $(basename "$(pwd)") != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; then
echo "Please make sure this script is executed from runtipi/"
exit 1
fi
NETWORK_INTERFACE="$(ip route | grep default | awk '{print $5}' | uniq)"
INTERNAL_IP="$(ip addr show "${NETWORK_INTERFACE}" | grep "inet " | awk '{print $2}' | cut -d/ -f1)"
@ -87,12 +93,6 @@ while [ -n "$1" ]; do # while loop starts
shift
done
# Ensure BASH_SOURCE is ./scripts/start.sh
if [[ $(basename $(pwd)) != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; then
echo "Please make sure this script is executed from runtipi/"
exit 1
fi
# If port is not 80 and domain is not tipi.localhost, we exit
if [[ "${NGINX_PORT}" != "80" ]] && [[ "${DOMAIN}" != "tipi.localhost" ]]; then
echo "Using a custom domain with a custom port is not supported"
@ -101,13 +101,13 @@ fi
ROOT_FOLDER="${PWD}"
STATE_FOLDER="${ROOT_FOLDER}/state"
SED_ROOT_FOLDER="$(echo $ROOT_FOLDER | sed 's/\//\\\//g')"
SED_ROOT_FOLDER="$(echo "$ROOT_FOLDER" | sed 's/\//\\\//g')"
DNS_IP=9.9.9.9 # Default to Quad9 DNS
ARCHITECTURE="$(uname -m)"
TZ="$(timedatectl | grep "Time zone" | awk '{print $3}' | sed 's/\//\\\//g' || Europe\/Berlin)"
APPS_REPOSITORY="https://github.com/meienberger/runtipi-appstore"
REPO_ID="$(${ROOT_FOLDER}/scripts/git.sh get_hash ${APPS_REPOSITORY})"
REPO_ID="$("${ROOT_FOLDER}"/scripts/git.sh get_hash ${APPS_REPOSITORY})"
APPS_REPOSITORY_ESCAPED="$(echo ${APPS_REPOSITORY} | sed 's/\//\\\//g')"
if [[ "$ARCHITECTURE" == "aarch64" ]]; then
@ -129,7 +129,7 @@ function get_json_field() {
local json_file="$1"
local field="$2"
echo $(jq -r ".${field}" "${json_file}")
jq -r ".${field}" "${json_file}"
}
# Deterministically derives 128 bits of cryptographically secure entropy
@ -160,7 +160,7 @@ fi
# Create seed file with cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
if [[ ! -f "${STATE_FOLDER}/seed" ]]; then
echo "Generating seed..."
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 >"${STATE_FOLDER}/seed"
tr </dev/urandom -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1 >"${STATE_FOLDER}/seed"
fi
export DOCKER_CLIENT_TIMEOUT=240

View file

@ -9,7 +9,7 @@ if [[ $UID != 0 ]]; then
fi
# Ensure PWD ends with /runtipi
if [[ $(basename $(pwd)) != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; then
if [[ $(basename "$(pwd)") != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; then
echo "Please run this script from the runtipi directory"
exit 1
fi
@ -21,7 +21,7 @@ export COMPOSE_HTTP_TIMEOUT=240
# Stop all installed apps if there are any
apps_folder="${ROOT_FOLDER}/apps"
if [ "$(find ${apps_folder} -maxdepth 1 -type d | wc -l)" -gt 1 ]; then
if [ "$(find "${apps_folder}" -maxdepth 1 -type d | wc -l)" -gt 1 ]; then
apps_names=($(ls -d ${apps_folder}/*/ | xargs -n 1 basename | sed 's/\///g'))
for app_name in "${apps_names[@]}"; do

View file

@ -2,13 +2,13 @@
set -e # Exit immediately if a command exits with a non-zero status.
cd /runtipi || echo ""
# Ensure PWD ends with /runtipi
if [[ "${PWD##*/}" != "runtipi" ]]; then
echo "Please run this script from the runtipi directory"
if [[ $(basename "$(pwd)") != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; then
echo "Please make sure this script is executed from runtipi/"
exit 1
fi
ROOT_FOLDER="${PWD}"
ROOT_FOLDER="$(pwd)"
STATE_FOLDER="${ROOT_FOLDER}/state"
# Available disk space

View file

@ -3,13 +3,13 @@
# Prompt to confirm
echo "This will reset your system to factory defaults. Are you sure you want to do this? (y/n)"
read confirm
read -r confirm
if [ "$confirm" != "y" ]; then
echo "Aborting."
exit 1
fi
ROOT_FOLDER="$(readlink -f $(dirname "${BASH_SOURCE[0]}")/..)"
ROOT_FOLDER="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")"/..)"
# Stop Tipi
"${ROOT_FOLDER}/scripts/stop.sh"
@ -25,5 +25,5 @@ rm -rf "${ROOT_FOLDER}/app-data"
rm -rf "${ROOT_FOLDER}/data/postgres"
mkdir -p "${ROOT_FOLDER}/app-data"
cd "$ROOT_FOLDER"
cd "$ROOT_FOLDER" || echo ""
"${ROOT_FOLDER}/scripts/start.sh"

View file

@ -3,8 +3,8 @@
cd /runtipi || echo ""
# Ensure PWD ends with /runtipi
if [[ "${PWD##*/}" != "runtipi" ]]; then
echo "Please run this script from the runtipi directory"
if [[ $(basename "$(pwd)") != "runtipi" ]] || [[ ! -f "${BASH_SOURCE[0]}" ]]; then
echo "Please make sure this script is executed from runtipi/"
exit 1
fi