Skip to content

Commit 72941a6

Browse files
committed
Add database prompts to init wizard
1 parent fd58950 commit 72941a6

3 files changed

Lines changed: 132 additions & 3 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.3.3"
10+
VERSION="1.3.4"
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.3.3"
93+
VERSION="1.3.4"
9494
INSTALL_DIR="$HOME/.shipnode"
9595
9696
# Parse flags

lib/commands/init.sh

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,117 @@ IGNORE_EOF
7171
success "Created .shipnodeignore"
7272
}
7373

74+
sanitize_db_identifier() {
75+
local value="$1"
76+
value=$(echo "$value" | tr '[:upper:]' '[:lower:]' | sed -E 's/[^a-z0-9_]+/_/g' | sed -E 's/^_+|_+$//g')
77+
[ -z "$value" ] && value="app"
78+
if [[ "$value" =~ ^[0-9] ]]; then
79+
value="app_$value"
80+
fi
81+
echo "$value"
82+
}
83+
84+
prompt_database_setup() {
85+
local app_name="$1"
86+
local remote_path="$2"
87+
local default_db_prefix
88+
default_db_prefix=$(sanitize_db_identifier "$app_name")
89+
90+
db_setup_enabled="false"
91+
db_type="postgresql"
92+
db_name="${default_db_prefix}_db"
93+
db_user="${default_db_prefix}_user"
94+
db_password_expr='${DB_PASSWORD:-}'
95+
db_sqlite_path="$remote_path/shared/database.sqlite"
96+
redis_setup_enabled="false"
97+
98+
echo ""
99+
info "Database and cache setup"
100+
if [ "$USE_GUM" = false ]; then
101+
echo "Optionally provision a local database/cache on the remote server during shipnode setup"
102+
fi
103+
echo ""
104+
105+
if [ "$USE_GUM" = true ]; then
106+
db_type=$(gum choose "none" "postgresql" "mysql" "sqlite" --header "Database to provision on server:" --cursor "> ")
107+
db_type="${db_type:-none}"
108+
else
109+
local db_choice
110+
echo "Database to provision:"
111+
echo " 1) none"
112+
echo " 2) postgresql"
113+
echo " 3) mysql"
114+
echo " 4) sqlite"
115+
read -p "Choose database [1]: " db_choice
116+
case "${db_choice:-1}" in
117+
2) db_type="postgresql" ;;
118+
3) db_type="mysql" ;;
119+
4) db_type="sqlite" ;;
120+
*) db_type="none" ;;
121+
esac
122+
fi
123+
124+
if [ "$db_type" != "none" ]; then
125+
db_setup_enabled="true"
126+
127+
if [ "$db_type" = "postgresql" ] || [ "$db_type" = "mysql" ]; then
128+
if [ "$USE_GUM" = true ]; then
129+
db_name=$(gum input --placeholder "$db_name" --prompt "Database name: " --value "$db_name")
130+
db_user=$(gum input --placeholder "$db_user" --prompt "Database user: " --value "$db_user")
131+
else
132+
echo " (Database and user are created by shipnode setup)"
133+
prompt_with_default "Database name" "$db_name" "db_name"
134+
prompt_with_default "Database user" "$db_user" "db_user"
135+
fi
136+
db_name=$(sanitize_db_identifier "$db_name")
137+
db_user=$(sanitize_db_identifier "$db_user")
138+
elif [ "$db_type" = "sqlite" ]; then
139+
if [ "$USE_GUM" = true ]; then
140+
db_sqlite_path=$(gum input --placeholder "$db_sqlite_path" --prompt "SQLite path: " --value "$db_sqlite_path")
141+
else
142+
echo " (Keep SQLite under shared/ so it survives releases)"
143+
prompt_with_default "SQLite database path" "$db_sqlite_path" "db_sqlite_path"
144+
fi
145+
fi
146+
else
147+
db_type="postgresql"
148+
fi
149+
150+
if [ "$USE_GUM" = true ]; then
151+
if gum confirm "Set up Redis on localhost?" --default=false; then
152+
redis_setup_enabled="true"
153+
fi
154+
else
155+
if prompt_yes_no "Set up Redis on localhost?" "n"; then
156+
redis_setup_enabled="true"
157+
fi
158+
fi
159+
}
160+
161+
emit_database_config() {
162+
echo ""
163+
echo "# Database setup"
164+
echo "DB_SETUP_ENABLED=$db_setup_enabled"
165+
echo "DB_TYPE=$db_type"
166+
167+
if [ "$db_type" = "postgresql" ] || [ "$db_type" = "mysql" ]; then
168+
echo "DB_NAME=$db_name"
169+
echo "DB_USER=$db_user"
170+
echo "DB_PASSWORD=$db_password_expr"
171+
elif [ "$db_type" = "sqlite" ]; then
172+
echo "DB_SQLITE_PATH=$db_sqlite_path"
173+
else
174+
echo "# DB_NAME=myapp_db"
175+
echo "# DB_USER=myapp_user"
176+
echo "# DB_PASSWORD=\${DB_PASSWORD:-}"
177+
echo "# DB_SQLITE_PATH=$remote_path/shared/database.sqlite"
178+
fi
179+
180+
echo ""
181+
echo "# Redis setup"
182+
echo "REDIS_SETUP_ENABLED=$redis_setup_enabled"
183+
}
184+
74185
# Generate .shipnode/ directory with smart hook templates
75186
generate_shipnode_hooks() {
76187
# Create .shipnode directory
@@ -1385,6 +1496,9 @@ cmd_init_interactive() {
13851496
local health_path="/health"
13861497
local health_timeout="30"
13871498
local health_retries="3"
1499+
1500+
local db_setup_enabled db_type db_name db_user db_password_expr db_sqlite_path redis_setup_enabled
1501+
prompt_database_setup "$app_name" "$remote_path"
13881502

13891503
# 7. Configuration summary
13901504
echo ""
@@ -1403,6 +1517,8 @@ cmd_init_interactive() {
14031517
$([ "$app_type" = "backend" ] && echo "Backend Port: $backend_port") \
14041518
$([ -n "$domain" ] && echo "Domain: $domain") \
14051519
"Zero-downtime: $zero_downtime" \
1520+
$([ "$db_setup_enabled" = "true" ] && echo "Database: $db_type") \
1521+
$([ "$redis_setup_enabled" = "true" ] && echo "Redis: localhost") \
14061522
$([ "$app_type" = "backend" ] && [ "$health_enabled" = "true" ] && echo "Health Checks: $health_path (${health_timeout}s, $health_retries retries)")
14071523
else
14081524
echo -e "${BLUE}════════════════════════════════════${NC}"
@@ -1419,6 +1535,8 @@ cmd_init_interactive() {
14191535

14201536
[ -n "$domain" ] && echo "Domain: $domain"
14211537
echo "Zero-downtime: $zero_downtime"
1538+
[ "$db_setup_enabled" = "true" ] && echo "Database: $db_type"
1539+
[ "$redis_setup_enabled" = "true" ] && echo "Redis: localhost"
14221540

14231541
if [ "$app_type" = "backend" ] && [ "$health_enabled" = "true" ]; then
14241542
echo "Health Checks: $health_path (${health_timeout}s timeout, ${health_retries} retries)"
@@ -1507,6 +1625,8 @@ HEALTH_CHECK_RETRIES=$health_retries
15071625
EOF
15081626
fi
15091627

1628+
emit_database_config >> shipnode.conf
1629+
15101630
success "Created shipnode.conf"
15111631

15121632
# 9. Generate .shipnode/ hooks
@@ -1839,6 +1959,9 @@ cmd_init_interactive_print() {
18391959
local health_path="/health"
18401960
local health_timeout="30"
18411961
local health_retries="3"
1962+
1963+
local db_setup_enabled db_type db_name db_user db_password_expr db_sqlite_path redis_setup_enabled
1964+
prompt_database_setup "$app_name" "$remote_path" >&2
18421965

18431966
# 7. Configuration summary
18441967
echo ""
@@ -1857,6 +1980,8 @@ cmd_init_interactive_print() {
18571980
$([ "$app_type" = "backend" ] && echo "Backend Port: $backend_port") \
18581981
$([ -n "$domain" ] && echo "Domain: $domain") \
18591982
"Zero-downtime: $zero_downtime" \
1983+
$([ "$db_setup_enabled" = "true" ] && echo "Database: $db_type") \
1984+
$([ "$redis_setup_enabled" = "true" ] && echo "Redis: localhost") \
18601985
$([ "$app_type" = "backend" ] && [ "$health_enabled" = "true" ] && echo "Health Checks: $health_path (${health_timeout}s, $health_retries retries)")
18611986
else
18621987
echo -e "${BLUE}════════════════════════════════════${NC}"
@@ -1873,6 +1998,8 @@ cmd_init_interactive_print() {
18731998

18741999
[ -n "$domain" ] && echo "Domain: $domain"
18752000
echo "Zero-downtime: $zero_downtime"
2001+
[ "$db_setup_enabled" = "true" ] && echo "Database: $db_type"
2002+
[ "$redis_setup_enabled" = "true" ] && echo "Redis: localhost"
18762003

18772004
if [ "$app_type" = "backend" ] && [ "$health_enabled" = "true" ]; then
18782005
echo "Health Checks: $health_path (${health_timeout}s, $health_retries retries)"
@@ -1949,6 +2076,8 @@ cmd_init_interactive_print() {
19492076
echo "HEALTH_CHECK_TIMEOUT=$health_timeout"
19502077
echo "HEALTH_CHECK_RETRIES=$health_retries"
19512078
fi
2079+
2080+
emit_database_config
19522081
}
19532082

19542083
# Initialize config file (router)

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.3.3"
13+
VERSION="1.3.4"
1414

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

0 commit comments

Comments
 (0)