Skip to content

Commit 9be3ca5

Browse files
committed
Load database secrets from shared env
1 parent e7a970f commit 9be3ca5

4 files changed

Lines changed: 65 additions & 10 deletions

File tree

build-dist.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ GREEN='\033[0;32m'
77
BLUE='\033[0;34m'
88
NC='\033[0m'
99

10-
VERSION="1.4.7"
10+
VERSION="1.4.8"
1111
DIST_DIR="dist"
1212
ARCHIVE_NAME="shipnode-payload.tar.gz"
1313
INSTALLER_NAME="shipnode-installer.sh"
@@ -90,7 +90,7 @@ YELLOW='\033[1;33m'
9090
BLUE='\033[0;34m'
9191
NC='\033[0m'
9292
93-
VERSION="1.4.7"
93+
VERSION="1.4.8"
9494
INSTALL_DIR="$HOME/.shipnode"
9595
9696
# Parse flags

install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ BLUE='\033[0;34m'
1010
NC='\033[0m'
1111

1212
echo -e "${BLUE}╔════════════════════════════════════╗${NC}"
13-
echo -e "${BLUE}║ ShipNode Installer v1.4.7${NC}"
13+
echo -e "${BLUE}║ ShipNode Installer v1.4.8${NC}"
1414
echo -e "${BLUE}╚════════════════════════════════════╝${NC}"
1515
echo
1616

lib/core.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ BLUE='\033[0;34m'
1010
NC='\033[0m' # No Color
1111

1212
# ShipNode version
13-
VERSION="1.4.7"
13+
VERSION="1.4.8"
1414

1515
# SSH multiplexing for connection reuse
1616
SSH_CONTROL_PATH="/tmp/shipnode-ssh-%r@%h:%p"

lib/database.sh

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ validate_db_identifier() {
99
}
1010

1111
setup_databases() {
12+
info "Checking database and cache setup..."
13+
14+
if [ -z "${DB_PASSWORD:-}" ] || [ -z "${DATABASE_URL:-}" ]; then
15+
load_remote_database_env || true
16+
fi
17+
1218
if [ -z "${DB_PASSWORD:-}" ] && [ -n "${DATABASE_URL:-}" ]; then
1319
local db_url_auth="${DATABASE_URL#*://}"
1420
db_url_auth="${db_url_auth%%@*}"
@@ -18,6 +24,7 @@ setup_databases() {
1824
fi
1925

2026
if [ "${DB_SETUP_ENABLED:-false}" = "true" ]; then
27+
info "Database setup enabled (DB_TYPE=${DB_TYPE:-postgresql})"
2128
case "${DB_TYPE:-postgresql}" in
2229
postgresql|postgres|pg)
2330
setup_postgresql
@@ -33,24 +40,69 @@ setup_databases() {
3340
return 1
3441
;;
3542
esac
43+
else
44+
info "Database setup disabled (DB_SETUP_ENABLED=false)"
3645
fi
3746

3847
if [ "${REDIS_SETUP_ENABLED:-false}" = "true" ]; then
3948
setup_redis
49+
else
50+
info "Redis setup disabled (REDIS_SETUP_ENABLED=false)"
51+
fi
52+
}
53+
54+
load_remote_database_env() {
55+
local remote_env="$REMOTE_PATH/shared/.env"
56+
local env_output
57+
58+
env_output=$(remote_exec bash -s "$remote_env" <<'ENDSSH' 2>/dev/null || true
59+
ENV_FILE="$1"
60+
[ -f "$ENV_FILE" ] || exit 0
61+
62+
awk '
63+
/^[[:space:]]*#/ || /^[[:space:]]*$/ { next }
64+
/^[[:space:]]*(DB_PASSWORD|DATABASE_URL)=/ {
65+
sub(/^[[:space:]]*/, "")
66+
print
67+
}
68+
' "$ENV_FILE"
69+
ENDSSH
70+
)
71+
72+
if [ -n "$env_output" ]; then
73+
info "Loaded database secrets from $remote_env"
4074
fi
75+
76+
while IFS='=' read -r key value; do
77+
value="${value%\"}"
78+
value="${value#\"}"
79+
value="${value%\'}"
80+
value="${value#\'}"
81+
case "$key" in
82+
DB_PASSWORD)
83+
[ -z "${DB_PASSWORD:-}" ] && DB_PASSWORD="$value"
84+
;;
85+
DATABASE_URL)
86+
[ -z "${DATABASE_URL:-}" ] && DATABASE_URL="$value"
87+
;;
88+
esac
89+
done <<< "$env_output" || true
4190
}
4291

4392
setup_postgresql() {
4493
# Validate required variables
4594
if [ -z "$DB_NAME" ] || [ -z "$DB_USER" ] || [ -z "$DB_PASSWORD" ]; then
46-
warn "PostgreSQL setup enabled but DB_NAME, DB_USER, or DB_PASSWORD is missing. Set DB_PASSWORD in .env or shipnode.conf."
95+
warn "PostgreSQL setup enabled but required values are missing."
96+
[ -z "$DB_NAME" ] && warn " Missing DB_NAME"
97+
[ -z "$DB_USER" ] && warn " Missing DB_USER"
98+
[ -z "$DB_PASSWORD" ] && warn " Missing DB_PASSWORD (set DB_PASSWORD in $REMOTE_PATH/shared/.env, shipnode.conf, or provide DATABASE_URL with a password)"
4799
return 1
48100
fi
49101

50102
validate_db_identifier "DB_NAME" "$DB_NAME" || return 1
51103
validate_db_identifier "DB_USER" "$DB_USER" || return 1
52104

53-
info "Setting up PostgreSQL..."
105+
info "Setting up PostgreSQL database '$DB_NAME' for user '$DB_USER'..."
54106

55107
ssh_cmd -o ServerAliveInterval=30 -o ServerAliveCountMax=5 -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" \
56108
DB_NAME="$DB_NAME" \
@@ -122,19 +174,22 @@ setup_postgresql() {
122174
echo "Connection string: postgresql://$DB_USER:[REDACTED]@localhost:5432/$DB_NAME"
123175
ENDSSH
124176

125-
success "PostgreSQL database '$DB_NAME' configured"
177+
success "PostgreSQL database '$DB_NAME' configured for user '$DB_USER'"
126178
}
127179

128180
setup_mysql() {
129181
if [ -z "$DB_NAME" ] || [ -z "$DB_USER" ] || [ -z "$DB_PASSWORD" ]; then
130-
warn "MySQL setup enabled but DB_NAME, DB_USER, or DB_PASSWORD is missing. Set DB_PASSWORD in .env or shipnode.conf."
182+
warn "MySQL setup enabled but required values are missing."
183+
[ -z "$DB_NAME" ] && warn " Missing DB_NAME"
184+
[ -z "$DB_USER" ] && warn " Missing DB_USER"
185+
[ -z "$DB_PASSWORD" ] && warn " Missing DB_PASSWORD (set DB_PASSWORD in $REMOTE_PATH/shared/.env, shipnode.conf, or provide DATABASE_URL with a password)"
131186
return 1
132187
fi
133188

134189
validate_db_identifier "DB_NAME" "$DB_NAME" || return 1
135190
validate_db_identifier "DB_USER" "$DB_USER" || return 1
136191

137-
info "Setting up MySQL..."
192+
info "Setting up MySQL database '$DB_NAME' for user '$DB_USER'..."
138193

139194
ssh_cmd -o ServerAliveInterval=30 -o ServerAliveCountMax=5 -p "$SSH_PORT" "$SSH_USER@$SSH_HOST" \
140195
DB_NAME="$DB_NAME" \
@@ -175,7 +230,7 @@ SQL
175230
echo "Connection string: mysql://$DB_USER:[REDACTED]@localhost:3306/$DB_NAME"
176231
ENDSSH
177232

178-
success "MySQL database '$DB_NAME' configured"
233+
success "MySQL database '$DB_NAME' configured for user '$DB_USER'"
179234
}
180235

181236
setup_sqlite() {

0 commit comments

Comments
 (0)