@@ -10,6 +10,15 @@ declare -A containers=(
10
10
[" prosper-dev" ]=" dev,5060,3060"
11
11
)
12
12
13
+ # PostgreSQL configuration
14
+ PG_CONTAINER_NAME=" agents-db"
15
+ PG_PORT=" 5432"
16
+ PG_USER=" postgres"
17
+ PG_PASSWORD=" postgres"
18
+ PG_DB=" postgres" # Default PostgreSQL database
19
+ PG_VOLUME=" agents-db-data"
20
+ DOCKER_NETWORK=" agents-network"
21
+
13
22
# navigate to the directory containing this script
14
23
cd $( dirname $0 )
15
24
@@ -25,6 +34,124 @@ command -v docker >/dev/null 2>&1 || { log_message "Error: docker is required bu
25
34
26
35
log_message " All required commands are available"
27
36
37
+ # Function to ensure the Docker network exists
38
+ ensure_network () {
39
+ log_message " Checking Docker network..."
40
+ if ! docker network ls --format ' {{.Name}}' | grep -q " ^${DOCKER_NETWORK} $" ; then
41
+ log_message " Creating Docker network ${DOCKER_NETWORK} ..."
42
+ docker network create ${DOCKER_NETWORK}
43
+ else
44
+ log_message " Docker network ${DOCKER_NETWORK} already exists."
45
+ fi
46
+ }
47
+
48
+ # Function to setup PostgreSQL container if it doesn't exist
49
+ setup_postgres () {
50
+ log_message " Checking PostgreSQL container..."
51
+
52
+ # Check if PostgreSQL container exists and is running
53
+ if docker ps --format ' {{.Names}}' | grep -q " ^${PG_CONTAINER_NAME} $" ; then
54
+ log_message " PostgreSQL container is already running."
55
+
56
+ # Ensure the container is in the right network
57
+ if ! docker network inspect ${DOCKER_NETWORK} | grep -q " \" ${PG_CONTAINER_NAME} \" " ; then
58
+ log_message " Connecting PostgreSQL container to ${DOCKER_NETWORK} network..."
59
+ docker network connect ${DOCKER_NETWORK} ${PG_CONTAINER_NAME}
60
+ fi
61
+
62
+ return 0
63
+ fi
64
+
65
+ # Check if container exists but is not running
66
+ if docker ps -a --format ' {{.Names}}' | grep -q " ^${PG_CONTAINER_NAME} $" ; then
67
+ log_message " PostgreSQL container exists but is not running. Starting it..."
68
+ docker start ${PG_CONTAINER_NAME}
69
+
70
+ # Ensure the container is in the right network
71
+ if ! docker network inspect ${DOCKER_NETWORK} | grep -q " \" ${PG_CONTAINER_NAME} \" " ; then
72
+ log_message " Connecting PostgreSQL container to ${DOCKER_NETWORK} network..."
73
+ docker network connect ${DOCKER_NETWORK} ${PG_CONTAINER_NAME}
74
+ fi
75
+
76
+ return 0
77
+ fi
78
+
79
+ # Check if volume exists, if not create it
80
+ if ! docker volume ls --format ' {{.Name}}' | grep -q " ^${PG_VOLUME} $" ; then
81
+ log_message " Creating PostgreSQL volume ${PG_VOLUME} ..."
82
+ docker volume create ${PG_VOLUME}
83
+ fi
84
+
85
+ # Create PostgreSQL container
86
+ log_message " Creating and starting PostgreSQL container..."
87
+ docker run -d \
88
+ --name ${PG_CONTAINER_NAME} \
89
+ -e POSTGRES_USER=${PG_USER} \
90
+ -e POSTGRES_PASSWORD=${PG_PASSWORD} \
91
+ -e POSTGRES_DB=${PG_DB} \
92
+ -v ${PG_VOLUME} :/var/lib/postgresql/data \
93
+ --network=${DOCKER_NETWORK} \
94
+ --restart unless-stopped \
95
+ postgres
96
+
97
+ # Wait for PostgreSQL to be ready
98
+ log_message " Waiting for PostgreSQL to be ready..."
99
+ sleep 10
100
+
101
+ # Check if PostgreSQL is running
102
+ if ! docker ps --format ' {{.Names}}' | grep -q " ^${PG_CONTAINER_NAME} $" ; then
103
+ log_message " Error: Failed to start PostgreSQL container. Aborting." >&2
104
+ return 1
105
+ fi
106
+
107
+ log_message " PostgreSQL container is ready."
108
+ return 0
109
+ }
110
+
111
+ # Function to ensure database for a container if it doesn't exist
112
+ ensure_container_database () {
113
+ local container_name=$1
114
+ # Replace hyphens with underscores in database name to avoid SQL syntax errors
115
+ local container_db=" db_${container_name// -/ _} "
116
+
117
+ log_message " Ensuring database exists for ${container_name} ..." >&2
118
+
119
+ # Check if database exists
120
+ local db_exists=$( docker exec -i ${PG_CONTAINER_NAME} psql -U ${PG_USER} -t -c " SELECT 1 FROM pg_database WHERE datname='${container_db} '" )
121
+
122
+ # Create database if it doesn't exist
123
+ if [ -z " $db_exists " ] || [ " $db_exists " != " 1" ]; then
124
+ log_message " Creating database '${container_db} ' for ${container_name} ..." >&2
125
+ docker exec -i ${PG_CONTAINER_NAME} psql -U ${PG_USER} -c " CREATE DATABASE ${container_db} "
126
+ else
127
+ log_message " Database '${container_db} ' already exists for ${container_name} ." >&2
128
+ fi
129
+
130
+ echo " ${container_db} "
131
+ }
132
+
133
+ # Function to run SQL script for a container
134
+ run_sql_for_container () {
135
+ local container_name=$1
136
+
137
+ # Ensure container database exists and get its name
138
+ local container_db=$( ensure_container_database " ${container_name} " )
139
+
140
+ log_message " Running tracing-schema.sql for ${container_name} in database ${container_db} ..."
141
+
142
+ # Create a temporary file that customizes the SQL for this container
143
+ local temp_sql_file=$( mktemp)
144
+ cat tracing-schema.sql | sed " s/{{container_name}}/${container_name} /g" > ${temp_sql_file}
145
+
146
+ # Run the SQL script against the container's database
147
+ docker exec -i ${PG_CONTAINER_NAME} psql -U ${PG_USER} -d ${container_db} < ${temp_sql_file}
148
+
149
+ # Clean up
150
+ rm ${temp_sql_file}
151
+
152
+ log_message " SQL script execution completed for ${container_name} in database ${container_db} ."
153
+ }
154
+
28
155
# Function to get the specific version tag from an image
29
156
get_version_tag () {
30
157
local image_name=$1
@@ -95,6 +222,9 @@ deploy_containers() {
95
222
continue
96
223
fi
97
224
225
+ # Ensure container database exists and get its name
226
+ local container_db=$( ensure_container_database " ${container} " )
227
+
98
228
# Stop and remove existing container if it exists
99
229
if docker ps -a --format ' {{.Names}}' | grep -q " ^${container} $" ; then
100
230
log_message " Stopping and removing existing ${container} container..."
@@ -110,11 +240,29 @@ deploy_containers() {
110
240
-p ${port2} :${port2} \
111
241
-v $( pwd) /${container} .env:/app/.env \
112
242
-e VERSION=" ${version_tag} " \
243
+ -e POSTGRES_HOST=" ${PG_CONTAINER_NAME} " \
244
+ -e POSTGRES_PORT=" ${PG_PORT} " \
245
+ -e POSTGRES_USER=" ${PG_USER} " \
246
+ -e POSTGRES_PASSWORD=" ${PG_PASSWORD} " \
247
+ -e POSTGRES_DB=" ${container_db} " \
248
+ --network=${DOCKER_NETWORK} \
113
249
--restart unless-stopped \
114
250
ghcr.io/sifchain/realityspiral:${image_tag}
251
+
252
+ # Run SQL script for this container
253
+ run_sql_for_container " ${container} "
115
254
done
116
255
}
117
256
257
+ # Ensure Docker network exists
258
+ ensure_network
259
+
260
+ # Setup PostgreSQL container
261
+ if ! setup_postgres; then
262
+ log_message " Failed to setup PostgreSQL container. Aborting deployment."
263
+ exit 1
264
+ fi
265
+
118
266
# Deploy production containers with specific version tag
119
267
log_message " Deploying production containers..."
120
268
deploy_containers " prod" " latest" # The actual tag will be replaced in the function
0 commit comments