diff --git a/package.json b/package.json index aeb09206..93661fe3 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "commit": "git-cz", "act:test-install": "act --container-architecture linux/amd64 -j test-install", "act:docker": "act --container-architecture linux/amd64 --secret-file github.secrets -j build-images", - "start:dev": "sudo ./scripts/start-dev.sh", + "start:dev": "./scripts/start-dev.sh", "start:rc": "docker-compose -f docker-compose.rc.yml --env-file .env up --build", "start:prod": "docker-compose --env-file .env up --build", "start:pg": "docker run --name test-db -p 5433:5432 -d --rm -e POSTGRES_PASSWORD=postgres postgres", diff --git a/scripts/start-dev.sh b/scripts/start-dev.sh index 852cf526..05166ac6 100755 --- a/scripts/start-dev.sh +++ b/scripts/start-dev.sh @@ -1,11 +1,97 @@ #!/usr/bin/env bash +set -e # Exit immediately if a command exits with a non-zero status. + source "${BASH_SOURCE%/*}/common.sh" +### -------------------------------- +### General variables +### -------------------------------- ROOT_FOLDER="${PWD}" +STATE_FOLDER="${ROOT_FOLDER}/state" +SED_ROOT_FOLDER="$(echo "$ROOT_FOLDER" | sed 's/\//\\\//g')" +NGINX_PORT=80 +NGINX_PORT_SSL=443 +DOMAIN=tipi.localhost +DNS_IP=9.9.9.9 # Default to Quad9 DNS +ARCHITECTURE="$(uname -m)" +TZ="UTC" +JWT_SECRET=secret +POSTGRES_PASSWORD=postgres +TIPI_VERSION=$(get_json_field "${ROOT_FOLDER}/package.json" version) +INTERNAL_IP=localhost +storage_path="${ROOT_FOLDER}" +STORAGE_PATH_ESCAPED="$(echo "${storage_path}" | sed 's/\//\\\//g')" + +### -------------------------------- +### Apps repository configuration +### -------------------------------- +apps_repository="https://github.com/meienberger/runtipi-appstore" +APPS_REPOSITORY_ESCAPED="$(echo ${apps_repository} | sed 's/\//\\\//g')" +REPO_ID="$("${ROOT_FOLDER}"/scripts/git.sh get_hash ${apps_repository})" + +# Override configs with settings.json +if [[ -f "${STATE_FOLDER}/settings.json" ]]; then + if [[ "$(get_json_field "${STATE_FOLDER}/settings.json" appsRepoUrl)" != "null" ]]; then + apps_repository=$(get_json_field "${STATE_FOLDER}/settings.json" appsRepoUrl) + APPS_REPOSITORY_ESCAPED="$(echo "${apps_repository}" | sed 's/\//\\\//g')" + REPO_ID="$("${ROOT_FOLDER}"/scripts/git.sh get_hash "${apps_repository}")" + fi +fi + +### -------------------------------- +### Watcher and system-info +### -------------------------------- +if [[ ! -f "${ROOT_FOLDER}/state/events" ]]; then + touch "${ROOT_FOLDER}/state/events" +fi + +if [[ ! -f "${ROOT_FOLDER}/state/system-info.json" ]]; then + echo "{}" >"${ROOT_FOLDER}/state/system-info.json" +fi -kill_watcher chmod -R a+rwx "${ROOT_FOLDER}/state/events" chmod -R a+rwx "${ROOT_FOLDER}/state/system-info.json" +kill_watcher "${ROOT_FOLDER}/scripts/watcher.sh" & +### -------------------------------- +### env file generation +### -------------------------------- +ENV_FILE=$(mktemp) +[[ -f "${ROOT_FOLDER}/.env" ]] && rm -f "${ROOT_FOLDER}/.env" +[[ -f "$ROOT_FOLDER/templates/env-sample" ]] && cp "$ROOT_FOLDER/templates/env-sample" "$ENV_FILE" + +OS=$(uname) +sed_args=(-i) +# If os is macos, use gnu sed +if [[ "$OS" == "Darwin" ]]; then + echo "Using gnu sed" + sed_args=(-i '') +fi + +for template in ${ENV_FILE}; do + sed "${sed_args[@]}" "s//${DNS_IP}/g" "${template}" + sed "${sed_args[@]}" "s//${INTERNAL_IP}/g" "${template}" + sed "${sed_args[@]}" "s//${TZ}/g" "${template}" + sed "${sed_args[@]}" "s//${JWT_SECRET}/g" "${template}" + sed "${sed_args[@]}" "s//${SED_ROOT_FOLDER}/g" "${template}" + sed "${sed_args[@]}" "s//${TIPI_VERSION}/g" "${template}" + sed "${sed_args[@]}" "s//${ARCHITECTURE}/g" "${template}" + sed "${sed_args[@]}" "s//${NGINX_PORT}/g" "${template}" + sed "${sed_args[@]}" "s//${NGINX_PORT_SSL}/g" "${template}" + sed "${sed_args[@]}" "s//${POSTGRES_PASSWORD}/g" "${template}" + sed "${sed_args[@]}" "s//${REPO_ID}/g" "${template}" + sed "${sed_args[@]}" "s//${APPS_REPOSITORY_ESCAPED}/g" "${template}" + sed "${sed_args[@]}" "s//${DOMAIN}/g" "${template}" + sed "${sed_args[@]}" "s//${STORAGE_PATH_ESCAPED}/g" "${template}" +done + +mv -f "$ENV_FILE" "$ROOT_FOLDER/.env.dev" +cp "$ROOT_FOLDER/.env.dev" "$ROOT_FOLDER/.env" +chmod a+rwx "$ROOT_FOLDER/.env" +chmod a+rwx "${ROOT_FOLDER}/.env.dev" + +### -------------------------------- +### Start the project +### -------------------------------- docker compose -f docker-compose.dev.yml --env-file "${ROOT_FOLDER}/.env.dev" up --build diff --git a/scripts/start.sh b/scripts/start.sh index fb67efc4..3f5f8924 100755 --- a/scripts/start.sh +++ b/scripts/start.sh @@ -63,7 +63,9 @@ if [[ "$ARCHITECTURE" == "aarch64" ]]; then ARCHITECTURE="arm64" fi -# Parse arguments +### -------------------------------- +### CLI arguments +### -------------------------------- while [ -n "$1" ]; do case "$1" in --rc) rc="true" ;; @@ -143,15 +145,6 @@ export DOCKER_CLIENT_TIMEOUT=240 export COMPOSE_HTTP_TIMEOUT=240 echo "Generating config files..." -# Remove current .env file -[[ -f "${ROOT_FOLDER}/.env" ]] && rm -f "${ROOT_FOLDER}/.env" - -# Store paths to intermediary config files -ENV_FILE=$(mktemp) - -# Copy template configs to intermediary configs -[[ -f "$ROOT_FOLDER/templates/env-sample" ]] && cp "$ROOT_FOLDER/templates/env-sample" "$ENV_FILE" - # Override vars with values from settings.json if [[ -f "${STATE_FOLDER}/settings.json" ]]; then @@ -167,12 +160,9 @@ if [[ -f "${STATE_FOLDER}/settings.json" ]]; then # If appsRepoUrl is set in settings.json, use it if [[ "$(get_json_field "${STATE_FOLDER}/settings.json" appsRepoUrl)" != "null" ]]; then - APPS_REPOSITORY_ESCAPED="$(echo ${apps_repository} | sed 's/\//\\\//g')" - fi - - # If appsRepoId is set in settings.json, use it - if [[ "$(get_json_field "${STATE_FOLDER}/settings.json" appsRepoId)" != "null" ]]; then - REPO_ID=$(get_json_field "${STATE_FOLDER}/settings.json" appsRepoId) + apps_repository=$(get_json_field "${STATE_FOLDER}/settings.json" appsRepoUrl) + APPS_REPOSITORY_ESCAPED="$(echo "${apps_repository}" | sed 's/\//\\\//g')" + REPO_ID="$("${ROOT_FOLDER}"/scripts/git.sh get_hash "${apps_repository}")" fi # If port is set in settings.json, use it @@ -197,10 +187,16 @@ if [[ -f "${STATE_FOLDER}/settings.json" ]]; then fi fi -# Set array with all new values new_values="DOMAIN=${DOMAIN}\nDNS_IP=${DNS_IP}\nAPPS_REPOSITORY=${APPS_REPOSITORY_ESCAPED}\nREPO_ID=${REPO_ID}\nNGINX_PORT=${NGINX_PORT}\nNGINX_PORT_SSL=${NGINX_PORT_SSL}\nINTERNAL_IP=${INTERNAL_IP}\nSTORAGE_PATH=${STORAGE_PATH_ESCAPED}\nTZ=${TZ}\nJWT_SECRET=${JWT_SECRET}\nROOT_FOLDER=${SED_ROOT_FOLDER}\nTIPI_VERSION=${TIPI_VERSION}\nARCHITECTURE=${ARCHITECTURE}" write_log "Final values: \n${new_values}" +### -------------------------------- +### env file generation +### -------------------------------- +ENV_FILE=$(mktemp) +[[ -f "${ROOT_FOLDER}/.env" ]] && rm -f "${ROOT_FOLDER}/.env" +[[ -f "$ROOT_FOLDER/templates/env-sample" ]] && cp "$ROOT_FOLDER/templates/env-sample" "$ENV_FILE" + for template in ${ENV_FILE}; do sed -i "s//${DNS_IP}/g" "${template}" sed -i "s//${INTERNAL_IP}/g" "${template}"