chore(scripts): clean up setup; add start + update

This commit is contained in:
Cory Dransfeldt 2025-06-20 17:31:11 -07:00
parent 555d611c2a
commit 4bc85bde57
No known key found for this signature in database
10 changed files with 103 additions and 63 deletions

View file

@ -1,5 +1,7 @@
#!/bin/bash
set -eu
COLOR_BLUE="\033[38;2;51;100;255m"
COLOR_RESET="\033[0m"
DEPLOY=false
@ -22,9 +24,13 @@ echo "${COLOR_RESET}"
# determine sed compatibility
if [[ "$OSTYPE" == "darwin"* ]]; then
SED_INPLACE="sed -i ''"
sed_inplace() {
sed -i '' "$@"
}
else
SED_INPLACE="sed -i"
sed_inplace() {
sed -i "$@"
}
fi
# get secrets from 1password
@ -57,41 +63,46 @@ SECRETS_JSON='{
SECRETS=$(echo "$SECRETS_JSON" | op inject)
if [ -z "$SECRETS" ]; then
echo "error: failed to retrieve secrets from 1Password."
if echo "$SECRETS" | grep -q '{{'; then
echo "❌ Error: Unresolved placeholders remain in injected secrets. Check 1Password references." >&2
exit 1
fi
echo "${COLOR_BLUE}Writing .env file...${COLOR_RESET}"
echo "$SECRETS" | jq -r 'to_entries | .[] | "\(.key)=\(.value)"' > .env
export $(grep -v '^#' .env | xargs)
echo "$SECRETS" | jq -r 'to_entries | .[] | "\(.key)=\"\(.value | gsub("\""; "\\\""))\""' > .env
echo >> .env
echo "${COLOR_BLUE}📦 Installing root project dependencies...${COLOR_RESET}"
npm install
while IFS= read -r line; do
[[ "$line" =~ ^#.*$ || -z "$line" ]] && continue
export "${line?}"
done < .env
echo "${COLOR_BLUE}✅ Loaded $(grep -c '^[A-Z0-9_]\+=' .env) secrets into .env${COLOR_RESET}"
CLI_DIR="$(dirname "$0")/../cli"
echo "${COLOR_BLUE}🗳️ Installing root JS packages...${COLOR_RESET}"
npm install --loglevel=silent --no-audit --no-fund | grep -v "up to date" || :
echo "${COLOR_BLUE}🐺 Initializing Husky Git hooks...${COLOR_RESET}"
npm run prepare
echo "${COLOR_BLUE}📦 Installing PHP dependencies (composer)...${COLOR_RESET}"
composer install
echo "${COLOR_BLUE}📦 Installing PHP packages...${COLOR_RESET}"
composer install --no-progress --no-interaction 2>&1 | \
grep -Ev "^(Writing lock file|Generating optimized autoload files|Loading composer|Nothing to modify|Use the \`composer fund\`|No security vulnerability|Installing dependencies from lock file|Package operations|[0-9]+ packages you are using are looking for funding)"
echo "${COLOR_BLUE}📦 Installing CLI dependencies...${COLOR_RESET}"
(
cd cli
npm install
)
echo "${COLOR_BLUE}🗃️ Installing CLI JS packages...${COLOR_RESET}"
( cd "$CLI_DIR" && npm install --loglevel=silent --no-audit --no-fund | grep -v "up to date" || : )
if ! command -v cd_cli >/dev/null 2>&1; then
echo "${COLOR_BLUE}🔗 Linking CLI globally...${COLOR_RESET}"
(
cd cli
npm link
)
( cd "$CLI_DIR" && npm link )
fi
echo "${COLOR_BLUE}⚙️ Initializing media storage config...${COLOR_RESET}"
cd_cli init
rm -rf generated
mkdir -p generated
# escape sed replacements
@ -107,38 +118,19 @@ render_template() {
for key in $(jq -r 'keys_unsorted[]' <<< "$SECRETS"); do
value=$(jq -r --arg k "$key" '.[$k]' <<< "$SECRETS")
$SED_INPLACE "s|{{${key}}}|$(escape_special_chars "$value")|g" "$output"
sed_inplace "s|{{${key}}}|$(escape_special_chars "$value")|g" "$output"
done
}
# render templates
for file in scripts/templates/*.template; do
[ -e "$file" ] || continue
new_file="generated/$(basename "${file%.template}")"
cp "$file" "$new_file"
# replace placeholders
sed -i '' -e "s|{{POSTGREST_URL}}|$(escape_special_chars "$POSTGREST_URL")|g" "$new_file"
sed -i '' -e "s|{{POSTGREST_API_KEY}}|$(escape_special_chars "$POSTGREST_API_KEY")|g" "$new_file"
sed -i '' -e "s|{{MASTODON_ACCESS_TOKEN}}|$(escape_special_chars "$MASTODON_ACCESS_TOKEN")|g" "$new_file"
sed -i '' -e "s|{{MASTODON_SYNDICATION_TOKEN}}|$(escape_special_chars "$MASTODON_SYNDICATION_TOKEN")|g" "$new_file"
sed -i '' -e "s|{{FORWARDEMAIL_API_KEY}}|$(escape_special_chars "$FORWARDEMAIL_API_KEY")|g" "$new_file"
sed -i '' -e "s|{{BOOK_IMPORT_TOKEN}}|$(escape_special_chars "$BOOK_IMPORT_TOKEN")|g" "$new_file"
sed -i '' -e "s|{{WATCHING_IMPORT_TOKEN}}|$(escape_special_chars "$WATCHING_IMPORT_TOKEN")|g" "$new_file"
sed -i '' -e "s|{{ARTIST_IMPORT_TOKEN}}|$(escape_special_chars "$ARTIST_IMPORT_TOKEN")|g" "$new_file"
sed -i '' -e "s|{{TMDB_API_KEY}}|$(escape_special_chars "$TMDB_API_KEY")|g" "$new_file"
sed -i '' -e "s|{{SEASONS_IMPORT_TOKEN}}|$(escape_special_chars "$SEASONS_IMPORT_TOKEN")|g" "$new_file"
sed -i '' -e "s|{{NAVIDROME_SCROBBLE_TOKEN}}|$(escape_special_chars "$NAVIDROME_SCROBBLE_TOKEN")|g" "$new_file"
sed -i '' -e "s|{{NAVIDROME_API_URL}}|$(escape_special_chars "$NAVIDROME_API_URL")|g" "$new_file"
sed -i '' -e "s|{{NAVIDROME_API_TOKEN}}|$(escape_special_chars "$NAVIDROME_API_TOKEN")|g" "$new_file"
sed -i '' -e "s|{{COOLIFY_REBUILD_TOKEN}}|$(escape_special_chars "$COOLIFY_REBUILD_TOKEN")|g" "$new_file"
sed -i '' -e "s|{{COOLIFY_REBUILD_URL}}|$(escape_special_chars "$COOLIFY_REBUILD_URL")|g" "$new_file"
sed -i '' -e "s|{{GIT_REPO}}|$(escape_special_chars "$GIT_REPO")|g" "$new_file"
sed -i '' -e "s|{{SERVER_IP}}|$(escape_special_chars "$SERVER_IP")|g" "$new_file"
for filepath in scripts/templates/*.template; do
[ -e "$filepath" ] || continue
filename=$(basename "$filepath" .template)
output="generated/$filename"
render_template "$filepath" "$output"
done
echo "${COLOR_BLUE}all configurations generated in the 'generated' folder.${COLOR_RESET}"
echo "${COLOR_BLUE}✅ All configurations generated in the 'generated' folder.${COLOR_RESET}"
echo "${COLOR_BLUE}"
echo "=========================================="
@ -161,7 +153,9 @@ if [ "$DEPLOY" = true ]; then
# generate server setup script
cat > generated/setup-server.sh <<EOF
#!/bin/bash
set -e
# This file is generated by setup.sh
set -eu
echo "🔧 Enabling Apache modules..."
sudo a2enmod $REQUIRED_MODULES