Skip to content

Commit 642a923

Browse files
Added retrying in the case of empty API key and non-interactive quickstart
1 parent 9f214a1 commit 642a923

File tree

1 file changed

+184
-45
lines changed

1 file changed

+184
-45
lines changed

postgres_ai

Lines changed: 184 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ show_help() {
5151
echo "Usage: $0 [COMMAND] [OPTIONS]"
5252
echo
5353
echo "COMMANDS:"
54-
echo " quickstart [--demo] [--api-key=KEY] Complete setup: install, configure, and start monitoring"
54+
echo " quickstart [--demo] [--api-key=KEY] [--db-url=URL] [-y] Complete setup: install, configure, and start monitoring"
5555
echo " install [--demo] Clone repository and setup the monitoring project"
5656
echo " start Start all monitoring services using Docker Compose"
5757
echo " stop Stop all services"
@@ -77,13 +77,23 @@ show_help() {
7777
echo " show-key Show current API key (masked)"
7878
echo " remove-key Remove stored API key"
7979
echo ""
80+
echo "OPTIONS:"
81+
echo " --demo Include demo PostgreSQL database for testing"
82+
echo " --api-key=KEY Provide API key for automated report uploads"
83+
echo " --db-url=URL Provide PostgreSQL connection URL for monitoring"
84+
echo " -y, --yes Accept all defaults and skip interactive prompts"
85+
echo ""
8086
echo " help Show this help message"
8187
echo
8288
echo "EXAMPLES:"
8389
echo " $0 quickstart # Complete setup for production (install + configure + start)"
8490
echo " $0 quickstart --demo # Complete setup with demo database included"
91+
echo " $0 quickstart -y # Complete setup with auto-yes (accept all defaults)"
8592
echo " $0 quickstart --api-key=your_api_key # Complete setup with API key for automated reports"
93+
echo " $0 quickstart --db-url=postgresql://user:pass@host:5432/db # Complete setup with database URL"
8694
echo " $0 quickstart --demo --api-key=your_key # Demo mode with API key"
95+
echo " $0 quickstart --db-url=postgresql://user:pass@host:5432/db --api-key=key # Complete with both"
96+
echo " $0 quickstart -y --api-key=key --db-url=postgresql://user:pass@host:5432/db # Fully automated setup"
8797
echo " $0 install # Clone repository and setup for production"
8898
echo " $0 install --demo # Clone repository and setup with demo database"
8999
echo " $0 start # Start monitoring services"
@@ -103,6 +113,8 @@ show_help() {
103113
echo " • Run '$0 quickstart' for complete production setup (install + configure + start)"
104114
echo " • Run '$0 quickstart --demo' for demo mode with sample database"
105115
echo " • Add '--api-key=your_key' to any quickstart command for automated report uploads"
116+
echo " • Add '--db-url=postgresql://user:pass@host:port/db' to automatically configure a database"
117+
echo " • Add '-y' to accept all defaults and skip interactive prompts (useful for automation)"
106118
echo ""
107119
echo " MANUAL SETUP:"
108120
echo " PRODUCTION MODE:"
@@ -123,7 +135,7 @@ show_help() {
123135
echo " pgwatch-postgres PGWatch instance with PostgreSQL sink"
124136
echo " pgwatch-prometheus PGWatch instance with Prometheus sink"
125137
echo " grafana Grafana dashboard interface"
126-
echo " flask-backend Flask API backend"
138+
echo " flask-pgss-api Flask API backend"
127139
echo " postgres-reports PostgreSQL reports generator (runs every 24h)"
128140
echo " target-db Demo PostgreSQL database (--demo mode only)"
129141
echo
@@ -297,7 +309,7 @@ cleanup_existing_containers() {
297309
"pgwatch-postgres"
298310
"pgwatch-prometheus"
299311
"grafana"
300-
"flask-backend"
312+
"flask-pgss-api"
301313
"target-db"
302314
"postgres-reports"
303315
"sources-generator"
@@ -432,8 +444,11 @@ system_check() {
432444
# Check if demo mode is enabled
433445
is_demo_mode() {
434446
if [ -f "$SCRIPT_DIR/.pgwatch-config" ]; then
435-
grep -q "demo_mode=true" "$SCRIPT_DIR/.pgwatch-config"
436-
return $?
447+
if grep -q "demo_mode=true" "$SCRIPT_DIR/.pgwatch-config"; then
448+
return 0
449+
else
450+
return 1
451+
fi
437452
fi
438453
# Default to false if config file doesn't exist
439454
return 1
@@ -539,15 +554,21 @@ clone_repository() {
539554
# Check if project directory already exists
540555
if [ -d "$project_dir" ]; then
541556
log_warning "Directory '$project_dir' already exists"
542-
read -p "Remove it and clone fresh? (y/N): " -n 1 -r
543-
echo
544-
if [[ $REPLY =~ ^[Yy]$ ]]; then
557+
if [ "$auto_yes" = true ]; then
558+
log_info "Auto-yes mode: removing existing directory"
545559
rm -rf "$project_dir"
546560
else
547-
log_info "Using existing directory"
548-
cd "$project_dir" || exit 1
549-
return 0
561+
read -p "Remove it and clone fresh? (y/N): " -n 1 -r
562+
echo
563+
if [[ $REPLY =~ ^[Yy]$ ]]; then
564+
rm -rf "$project_dir"
565+
else
566+
log_info "Using existing directory"
567+
cd "$project_dir" || exit 1
568+
return 0
569+
fi
550570
fi
571+
551572
fi
552573

553574
log_info "Cloning Postgres AI project from GitLab..."
@@ -590,16 +611,24 @@ install_project() {
590611
mkdir -p "$SCRIPT_DIR/config/pgwatch-postgres"
591612
mkdir -p "$SCRIPT_DIR/config/pgwatch-prometheus"
592613

593-
# Store demo mode setting
614+
# Store demo mode setting and configure instances
594615
if [ "$demo_mode" = true ]; then
595616
echo "demo_mode=true" > "$SCRIPT_DIR/.pgwatch-config"
596617
log_info "Target demo database will be included for testing"
618+
log_info "Demo instance 'target-database' configured for monitoring"
597619
else
598620
echo "demo_mode=false" > "$SCRIPT_DIR/.pgwatch-config"
599621
log_info "Production mode - only monitoring infrastructure will be started"
600-
log_warning "You'll need to add your own PostgreSQL instances to monitor"
601622

623+
# Remove the default demo instance from instances.yml for production mode
624+
log_info "Removing default demo instance from configuration..."
625+
cat > "$INSTANCES_FILE" << 'EOF'
626+
# PostgreSQL instances to monitor
627+
# Add your instances using: ./postgres_ai add-instance
602628
629+
EOF
630+
log_success "Demo instance removed - instances.yml is now empty"
631+
log_warning "You'll need to add your own PostgreSQL instances to monitor"
603632
fi
604633

605634
log_success "Setup completed!"
@@ -621,7 +650,8 @@ install_project() {
621650
quickstart_setup() {
622651
local demo_mode=false
623652
local api_key=""
624-
653+
local db_url=""
654+
local auto_yes=false
625655
# Parse command line arguments
626656
for arg in "$@"; do
627657
case $arg in
@@ -631,14 +661,27 @@ quickstart_setup() {
631661
--api-key=*)
632662
api_key="${arg#*=}"
633663
;;
664+
--db-url=*)
665+
db_url="${arg#*=}"
666+
;;
667+
-y|--yes)
668+
auto_yes=true
669+
;;
634670
esac
635671
done
636-
672+
637673
print_banner
638674
log_info "Postgres AI Monitoring - Quickstart Setup"
639675
log_info "This will install, configure, and start the monitoring system"
640676
echo
641677

678+
# Validate conflicting options
679+
if [ "$demo_mode" = true ] && [ -n "$db_url" ]; then
680+
log_warning "Both --demo and --db-url provided. Demo mode includes its own database."
681+
log_warning "The --db-url will be ignored in demo mode."
682+
db_url=""
683+
fi
684+
642685
# Perform comprehensive prechecks first
643686
log_info "Performing system prechecks..."
644687
precheck_for_install
@@ -658,16 +701,52 @@ quickstart_setup() {
658701
add_api_key "$api_key"
659702
else
660703
# Interactive API key setup
661-
read -p "Do you have a Postgres AI API key? (y/N): " -n 1 -r
662-
echo
704+
local proceed_with_api_key=false
663705

664-
if [[ $REPLY =~ ^[Yy]$ ]]; then
665-
read -p "Enter your Postgres AI API key: " input_api_key
666-
if [ -n "$input_api_key" ]; then
667-
add_api_key "$input_api_key"
668-
else
669-
log_warning "No API key provided - reports will be generated locally only"
706+
if [ "$auto_yes" = true ]; then
707+
# Auto-yes mode: proceed directly to API key entry
708+
log_info "Auto-yes mode: proceeding to API key setup"
709+
proceed_with_api_key=true
710+
else
711+
# Interactive mode: ask user
712+
read -p "Do you have a Postgres AI API key? (Y/n): " -n 1 -r
713+
echo
714+
715+
# Default to Y if user just presses Enter or responds with Y/y
716+
if [[ -z $REPLY ]] || [[ $REPLY =~ ^[Yy]$ ]]; then
717+
proceed_with_api_key=true
670718
fi
719+
fi
720+
721+
if [ "$proceed_with_api_key" = true ]; then
722+
while true; do
723+
read -p "Enter your Postgres AI API key: " input_api_key
724+
if [ -n "$input_api_key" ]; then
725+
# Remove any leading/trailing whitespace
726+
input_api_key=$(echo "$input_api_key" | xargs)
727+
if [ -n "$input_api_key" ]; then
728+
add_api_key "$input_api_key"
729+
break
730+
fi
731+
fi
732+
733+
log_warning "API key cannot be empty"
734+
735+
if [ "$auto_yes" = true ]; then
736+
# Auto-yes mode: automatically retry
737+
log_info "Auto-yes mode: retrying API key entry"
738+
continue
739+
else
740+
# Interactive mode: ask user
741+
read -p "Try again or skip API key setup, retry? (Y/n): " -n 1 -r
742+
echo
743+
if [[ $REPLY =~ ^[Nn]$ ]]; then
744+
log_info "Skipping API key setup - reports will be generated locally only"
745+
log_info "You can add an API key later with: $0 add-key <api_key>"
746+
break
747+
fi
748+
fi
749+
done
671750
else
672751
log_info "Skipping API key setup - reports will be generated locally only"
673752
log_info "You can add an API key later with: $0 add-key <api_key>"
@@ -678,38 +757,76 @@ quickstart_setup() {
678757
if [ "$demo_mode" = false ]; then
679758
echo
680759
log_info "Step 3: Add PostgreSQL Instance to Monitor"
681-
log_info "You need to add at least one PostgreSQL instance to monitor"
682-
read -p "Do you want to add a PostgreSQL instance now? (Y/n): " -n 1 -r
683-
echo
684-
685-
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
686-
echo
687-
log_info "Adding PostgreSQL instance..."
688-
echo "You can provide either:"
689-
echo " 1. A full connection string: postgresql://user:pass@host:port/database"
690-
echo " 2. Individual connection details (interactive mode)"
691-
echo
692-
read -p "Enter connection string (or press Enter for interactive mode): " conn_str
760+
if [ -n "$db_url" ]; then
761+
# Database URL provided via command line
762+
log_info "Using database URL provided via --db-url parameter"
763+
log_info "Adding PostgreSQL instance from: $db_url"
693764

694-
if [ -n "$conn_str" ]; then
695-
# Use connection string
696-
read -p "Instance name (optional, will auto-generate if empty): " instance_name
697-
add_instance "$conn_str" "$instance_name"
765+
# Extract instance name from URL or use default
766+
local auto_instance_name
767+
if [[ "$db_url" =~ postgresql://[^@]+@([^:/]+) ]]; then
768+
auto_instance_name="${BASH_REMATCH[1]}"
698769
else
699-
# Interactive mode
700-
add_instance
770+
auto_instance_name="db-instance"
701771
fi
702772

773+
add_instance "$db_url" "$auto_instance_name"
774+
703775
# Test the connection
704776
echo
705777
log_info "Testing connection to the added instance..."
706-
# Get the last added instance name
707778
local last_instance=$(grep -E "^- name:" "$INSTANCES_FILE" | tail -1 | sed 's/^- name: //')
708779
if [ -n "$last_instance" ]; then
709780
test_instance "$last_instance"
710781
fi
711782
else
712-
log_warning "No PostgreSQL instance added - you can add one later with: $0 add-instance"
783+
# Interactive mode - ask user
784+
log_info "You need to add at least one PostgreSQL instance to monitor"
785+
local proceed_with_instance=false
786+
787+
if [ "$auto_yes" = true ]; then
788+
# Auto-yes mode: proceed directly to instance setup
789+
log_info "Auto-yes mode: proceeding to add PostgreSQL instance"
790+
proceed_with_instance=true
791+
else
792+
# Interactive mode: ask user
793+
read -p "Do you want to add a PostgreSQL instance now? (Y/n): " -n 1 -r
794+
echo
795+
796+
if [[ ! $REPLY =~ ^[Nn]$ ]]; then
797+
proceed_with_instance=true
798+
fi
799+
fi
800+
801+
if [ "$proceed_with_instance" = true ]; then
802+
echo
803+
log_info "Adding PostgreSQL instance..."
804+
echo "You can provide either:"
805+
echo " 1. A full connection string: postgresql://user:pass@host:port/database"
806+
echo " 2. Individual connection details (interactive mode)"
807+
echo
808+
read -p "Enter connection string (or press Enter for interactive mode): " conn_str
809+
810+
if [ -n "$conn_str" ]; then
811+
# Use connection string
812+
read -p "Instance name (optional, will auto-generate if empty): " instance_name
813+
add_instance "$conn_str" "$instance_name"
814+
else
815+
# Interactive mode
816+
add_instance
817+
fi
818+
819+
# Test the connection
820+
echo
821+
log_info "Testing connection to the added instance..."
822+
# Get the last added instance name
823+
local last_instance=$(grep -E "^- name:" "$INSTANCES_FILE" | tail -1 | sed 's/^- name: //')
824+
if [ -n "$last_instance" ]; then
825+
test_instance "$last_instance"
826+
fi
827+
else
828+
log_warning "No PostgreSQL instance added - you can add one later with: $0 add-instance"
829+
fi
713830
fi
714831
else
715832
log_info "Step 3: Demo mode enabled - using included demo PostgreSQL database"
@@ -851,7 +968,7 @@ health_check() {
851968
"pgwatch-prometheus:8089:PGWatch Prometheus"
852969
"sink-prometheus:9090:Prometheus"
853970
"grafana:3000:Grafana"
854-
"flask-backend:5000:Flask API"
971+
"flask-pgss-api:5000:Flask API"
855972
)
856973

857974
# Add target-db only in demo mode
@@ -900,6 +1017,8 @@ list_instances() {
9001017
local env=""
9011018
local cluster=""
9021019
local node_name=""
1020+
local instance_count=0
1021+
local has_displayed_instance=false
9031022

9041023
while IFS= read -r line; do
9051024
if [[ "$line" =~ ^-[[:space:]]*name:[[:space:]]*(.+)$ ]]; then
@@ -913,13 +1032,15 @@ list_instances() {
9131032
[ -n "$cluster" ] && echo " Cluster: $cluster"
9141033
[ -n "$node_name" ] && echo " Node: $node_name"
9151034
echo
1035+
has_displayed_instance=true
9161036
fi
9171037
name="${BASH_REMATCH[1]}"
9181038
conn_str=""
9191039
is_enabled=""
9201040
env=""
9211041
cluster=""
9221042
node_name=""
1043+
((instance_count++))
9231044
elif [[ "$line" =~ ^[[:space:]]*conn_str:[[:space:]]*(.+)$ ]]; then
9241045
conn_str="${BASH_REMATCH[1]}"
9251046
elif [[ "$line" =~ ^[[:space:]]*is_enabled:[[:space:]]*(.+)$ ]]; then
@@ -942,6 +1063,24 @@ list_instances() {
9421063
[ -n "$cluster" ] && echo " Cluster: $cluster"
9431064
[ -n "$node_name" ] && echo " Node: $node_name"
9441065
echo
1066+
has_displayed_instance=true
1067+
fi
1068+
1069+
# Show message if no instances are configured
1070+
if [ "$instance_count" -eq 0 ] || [ "$has_displayed_instance" = false ]; then
1071+
echo " ❌ No instances configured"
1072+
echo
1073+
log_info "To add PostgreSQL instances for monitoring:"
1074+
echo " • Interactive mode: $0 add-instance"
1075+
echo " • Connection string: $0 add-instance 'postgresql://user:pass@host:port/database'"
1076+
echo " • Example: $0 add-instance 'postgresql://postgres:mypass@localhost:5432/mydb' my-database"
1077+
echo
1078+
1079+
# Show demo mode info if applicable
1080+
if ! is_demo_mode; then
1081+
log_info "Or use demo mode to test with a sample database:"
1082+
echo "$0 reset && $0 quickstart --demo"
1083+
fi
9451084
fi
9461085
}
9471086

@@ -1318,7 +1457,7 @@ open_shell() {
13181457

13191458
if [ -z "$service" ]; then
13201459
log_error "Please specify a service name"
1321-
log_info "Available services: target-db, sink-postgres, sink-prometheus, pgwatch-postgres, pgwatch-prometheus, grafana, flask-backend, postgres-reports"
1460+
log_info "Available services: target-db, sink-postgres, sink-prometheus, pgwatch-postgres, pgwatch-prometheus, grafana, flask-pgss-api, postgres-reports"
13221461
exit 1
13231462
fi
13241463

0 commit comments

Comments
 (0)