Skip to content

Commit ca33919

Browse files
authored
Merge pull request #92 from amazeeio/dev
Final helm changes
2 parents 29ce32b + 8d342b7 commit ca33919

File tree

10 files changed

+100
-40
lines changed

10 files changed

+100
-40
lines changed

backend-start.sh

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,15 @@
11
#!/bin/bash
22

3-
# Function to test database connection
4-
postgres_ready() {
5-
python << END
6-
import sys
7-
import psycopg2
8-
import os
9-
try:
10-
psycopg2.connect(
11-
os.getenv("DATABASE_URL", "postgres://postgres:postgres@postgres:5432/postgres_service")
12-
)
13-
except psycopg2.OperationalError:
14-
sys.exit(-1)
15-
sys.exit(0)
16-
END
17-
}
3+
# Wait for PostgreSQL to be ready and create database if needed
4+
echo "Waiting for PostgreSQL to be ready..."
5+
python /app/scripts/wait_for_database.py
186

19-
# Wait for PostgreSQL to be ready
20-
until postgres_ready; do
21-
>&2 echo "PostgreSQL is unavailable - sleeping"
22-
sleep 1
23-
done
7+
if [ $? -ne 0 ]; then
8+
echo "Failed to connect to database"
9+
exit 1
10+
fi
2411

25-
>&2 echo "PostgreSQL is up - initializing database"
12+
echo "PostgreSQL is up - initializing database"
2613

2714
# Initialize the database using the Python script
2815
python /app/scripts/initialise_resources.py

helm/Chart.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: amazee-ai
22
description: A Helm Chart for amazee.ai with independent frontend and backend services
3-
version: 0.0.6
3+
version: 0.1.0
44
apiVersion: v2
5-
appVersion: "0.0.6"
5+
appVersion: "0.1.0"
66
keywords:
77
- amazee.ai
88
- frontend
@@ -15,10 +15,10 @@ dependencies:
1515
repository: https://charts.bitnami.com/bitnami
1616
condition: postgresql.enabled
1717
- name: backend
18-
version: 0.0.6
18+
version: 0.1.0
1919
condition: backend.enabled
2020
dependsOn:
2121
- postgresql
2222
- name: frontend
23-
version: 0.0.6
23+
version: 0.1.0
2424
condition: frontend.enabled

helm/charts/backend/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: backend
22
description: Backend API service for amazee.ai
3-
version: 0.0.6
3+
version: 0.1.0
44
apiVersion: v2
5-
appVersion: "0.0.6"
5+
appVersion: "0.1.0"
66
keywords:
77
- api
88
- fastapi

helm/charts/backend/templates/_helpers.tpl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ Backend-specific database URL generation.
5555
This provides a backend-specific override if needed.
5656
*/}}
5757
{{- define "backend.databaseUrl" -}}
58-
{{- $backend := .Values.backend | default dict -}}
59-
{{- $database := $backend.database | default dict -}}
58+
{{- $database := .Values.database | default dict -}}
6059
{{- if $database.url }}
6160
{{- $database.url | quote }}
6261
{{- else }}

helm/charts/backend/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ enabled: true
44
replicas: 1
55
image:
66
repository: ghcr.io/amazeeio/amazee.ai-backend
7-
tag: "dev"
7+
tag: "main"
88
pullPolicy: IfNotPresent
99

1010
# Database configuration

helm/charts/frontend/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
name: frontend
22
description: Frontend web application for amazee.ai
3-
version: 0.0.6
3+
version: 0.1.0
44
apiVersion: v2
5-
appVersion: "0.0.6"
5+
appVersion: "0.1.0"
66
keywords:
77
- frontend
88
- nextjs

helm/charts/frontend/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ enabled: true
44
replicas: 1
55
image:
66
repository: ghcr.io/amazeeio/amazee.ai-frontend
7-
tag: "dev"
7+
tag: "main"
88
pullPolicy: IfNotPresent
99

1010
# Application configuration

helm/charts/postgresql-16.7.12.tgz

-83.1 KB
Binary file not shown.

helm/values.yaml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ global:
77

88
# PostgreSQL configuration (using Bitnami chart)
99
postgresql:
10+
# Set to false to use external managed PostgreSQL
1011
enabled: true
11-
# Set to true to use external managed PostgreSQL
12-
external:
13-
enabled: false
14-
url: "postgresql://user:password@host:port/database"
15-
# Self-hosted PostgreSQL configuration (used when external.enabled = false)
1612
auth:
1713
postgresPassword: "postgres"
1814
database: "postgres_service"
@@ -31,7 +27,7 @@ backend:
3127
replicas: 1
3228
image:
3329
repository: ghcr.io/amazeeio/amazee.ai-backend
34-
tag: "dev"
30+
tag: "main"
3531
pullPolicy: IfNotPresent
3632
# Database configuration - set based on postgresql configuration
3733
# The DATABASE_URL will be automatically generated using the release name
@@ -65,7 +61,7 @@ frontend:
6561
replicas: 1
6662
image:
6763
repository: ghcr.io/amazeeio/amazee.ai-frontend
68-
tag: "dev"
64+
tag: "main"
6965
pullPolicy: IfNotPresent
7066
apiUrl: "" # Will be auto-generated using release name and backend service
7167
stripePublishableKey: "pk_test_your_stripe_publishable_key"

scripts/wait_for_database.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Script to wait for PostgreSQL database to be ready and create it if it doesn't exist.
4+
"""
5+
6+
import sys
7+
import psycopg2
8+
import os
9+
import re
10+
from urllib.parse import urlparse
11+
import time
12+
13+
14+
def create_database_if_not_exists():
15+
"""Attempt to connect to database and create it if it doesn't exist."""
16+
database_url = os.getenv("DATABASE_URL", "postgres://postgres:postgres@postgres:5432/postgres_service")
17+
18+
# Parse the database URL to extract components
19+
parsed = urlparse(database_url)
20+
21+
# Create connection URL without database name (to connect to postgres default database)
22+
base_url = f"postgresql://{parsed.username}:{parsed.password}@{parsed.hostname}:{parsed.port or 5432}/postgres"
23+
24+
try:
25+
# First try to connect to the target database
26+
psycopg2.connect(database_url)
27+
print("Database connection successful")
28+
return True
29+
except psycopg2.OperationalError as e:
30+
# If database doesn't exist, try to create it
31+
if "does not exist" in str(e) or "database" in str(e).lower():
32+
print(f"Database does not exist, attempting to create it...")
33+
try:
34+
# Connect to postgres database to create the target database
35+
conn = psycopg2.connect(base_url)
36+
conn.autocommit = True
37+
cursor = conn.cursor()
38+
39+
# Extract database name from the original URL
40+
db_name = parsed.path.lstrip('/')
41+
42+
# Create the database
43+
cursor.execute(f"CREATE DATABASE {db_name}")
44+
cursor.close()
45+
conn.close()
46+
47+
print(f"Database '{db_name}' created successfully")
48+
49+
# Try connecting again to verify
50+
psycopg2.connect(database_url)
51+
print("Database connection verified after creation")
52+
return True
53+
except Exception as create_error:
54+
print(f"Failed to create database: {create_error}")
55+
return False
56+
else:
57+
# Other connection error (host unreachable, etc.)
58+
print(f"Database connection error: {e}")
59+
return False
60+
61+
62+
def wait_for_database(max_retries=60, retry_interval=1):
63+
"""Wait for database to be available with retries."""
64+
for attempt in range(max_retries):
65+
if create_database_if_not_exists():
66+
return True
67+
print(f"Database not ready, retrying in {retry_interval} seconds... (attempt {attempt + 1}/{max_retries})")
68+
time.sleep(retry_interval)
69+
70+
print("Failed to connect to database after maximum retries")
71+
return False
72+
73+
74+
if __name__ == "__main__":
75+
if wait_for_database():
76+
sys.exit(0)
77+
else:
78+
sys.exit(1)

0 commit comments

Comments
 (0)