-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01-deploy-resources.sh
More file actions
executable file
·286 lines (255 loc) · 8.91 KB
/
Copy path01-deploy-resources.sh
File metadata and controls
executable file
·286 lines (255 loc) · 8.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/bin/bash
# Variables
source ./00-variables.sh
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
# Create a resource group
echo "Checking if resource group [$RESOURCE_GROUP_NAME] exists in the subscription [$SUBSCRIPTION_NAME]..."
az group show --name $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "Creating resource group [$RESOURCE_GROUP_NAME]..."
az group create \
--name $RESOURCE_GROUP_NAME \
--location "$LOCATION" \
--only-show-errors 1>/dev/null
if [[ $? == 0 ]]; then
echo "Resource group [$RESOURCE_GROUP_NAME] created."
else
echo "Failed to create resource group [$RESOURCE_GROUP_NAME]."
exit 1
fi
else
echo "Resource group [$RESOURCE_GROUP_NAME] already exists."
fi
# Create the Azure Container Registry
echo "Checking if [$ACR_NAME] Azure Container Registry exists..."
az acr show \
--name "$ACR_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "Creating Azure Container Registry [$ACR_NAME]..."
az acr create \
--name "$ACR_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--sku "$ACR_SKU" \
--admin-enabled "true" \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "Azure Container Registry [$ACR_NAME] created."
else
echo "Failed to create Azure Container Registry [$ACR_NAME]."
exit 1
fi
else
echo "[$ACR_NAME] Azure Container Registry already exists."
fi
# Create the Azure Database for PostgreSQL flexible server
echo "Checking if PostgreSQL flexible server [$PG_SERVER_NAME] exists..."
az postgres flexible-server show \
--name "$PG_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "Creating PostgreSQL flexible server [$PG_SERVER_NAME]..."
az postgres flexible-server create \
--name "$PG_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--location "$LOCATION" \
--admin-user "$PG_ADMIN_USER" \
--admin-password "$PG_ADMIN_PASSWORD" \
--version "$PG_VERSION" \
--tier "$PG_SKU_TIER" \
--sku-name "$PG_SKU_NAME" \
--storage-size "$PG_STORAGE_SIZE_GB" \
--backup-retention "$PG_BACKUP_RETENTION_DAYS" \
--public-access Enabled \
--yes \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "PostgreSQL flexible server [$PG_SERVER_NAME] created."
else
echo "Failed to create PostgreSQL flexible server [$PG_SERVER_NAME]."
exit 1
fi
else
echo "PostgreSQL flexible server [$PG_SERVER_NAME] already exists."
fi
# Add a permissive firewall rule (dev/test only)
echo "Ensuring firewall rule [$FIREWALL_RULE_NAME] exists on PostgreSQL flexible server [$PG_SERVER_NAME]..."
az postgres flexible-server firewall-rule create \
--resource-group "$RESOURCE_GROUP_NAME" \
--server-name "$PG_SERVER_NAME" \
--name "$FIREWALL_RULE_NAME" \
--start-ip-address 0.0.0.0 \
--end-ip-address 255.255.255.255 \
--only-show-errors 1>/dev/null
# Create the PostgreSQL database
echo "Checking if PostgreSQL database [$PG_DATABASE_NAME] exists..."
az postgres flexible-server db show \
--name "$PG_DATABASE_NAME" \
--server-name "$PG_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--only-show-errors &>/dev/null
if [[ $? != 0 ]]; then
echo "Creating PostgreSQL database [$PG_DATABASE_NAME]..."
az postgres flexible-server db create \
--name "$PG_DATABASE_NAME" \
--server-name "$PG_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--charset UTF8 \
--collation en_US.utf8 \
--only-show-errors 1>/dev/null
if [ $? -eq 0 ]; then
echo "PostgreSQL database [$PG_DATABASE_NAME] created."
else
echo "Failed to create PostgreSQL database [$PG_DATABASE_NAME]."
exit 1
fi
else
echo "PostgreSQL database [$PG_DATABASE_NAME] already exists."
fi
# Retrieve PostgreSQL server FQDN
PG_FQDN_FULL=$(az postgres flexible-server show \
--name "$PG_SERVER_NAME" \
--resource-group "$RESOURCE_GROUP_NAME" \
--query "fullyQualifiedDomainName" \
--output tsv)
if [ -z "$PG_FQDN_FULL" ]; then
echo "Failed to retrieve PostgreSQL server FQDN."
exit 1
fi
# Split host:port — the LocalStack emulator embeds the dynamically allocated TCP-proxy port
# directly in fullyQualifiedDomainName, mirroring the storage / container registry emulators.
# Real Azure returns just the bare host so PG_PORT stays at the value from 00-variables.sh (5432).
PG_FQDN="${PG_FQDN_FULL%%:*}"
if [[ "$PG_FQDN_FULL" == *:* ]]; then
PG_PORT="${PG_FQDN_FULL##*:}"
fi
echo "PostgreSQL host = $PG_FQDN, port = $PG_PORT"
# Create application role + grants + schema + seed data.
# psql must be available on the host machine.
if ! command -v psql &>/dev/null; then
echo "psql is not installed on the host. Install the PostgreSQL client (postgresql-client) and re-run."
exit 1
fi
echo "Creating login [$PG_USER_NAME] on the [$PG_SERVER_NAME] PostgreSQL flexible server..."
PGPASSWORD="$PG_ADMIN_PASSWORD" psql \
--host="$PG_FQDN" \
--port="$PG_PORT" \
--username="$PG_ADMIN_USER" \
--dbname=postgres \
--no-password \
--set=ON_ERROR_STOP=on \
-c "DO \$\$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = '$PG_USER_NAME') THEN
CREATE ROLE \"$PG_USER_NAME\" WITH LOGIN PASSWORD '$PG_USER_PASSWORD';
END IF;
END
\$\$;"
if [ $? -eq 0 ]; then
echo "Login [$PG_USER_NAME] created successfully"
else
echo "Failed to create login [$PG_USER_NAME]"
exit 1
fi
# Grant CONNECT on the database to [$PG_USER_NAME]
echo "Granting CONNECT on [$PG_DATABASE_NAME] to [$PG_USER_NAME]..."
PGPASSWORD="$PG_ADMIN_PASSWORD" psql \
--host="$PG_FQDN" \
--port="$PG_PORT" \
--username="$PG_ADMIN_USER" \
--dbname=postgres \
--no-password \
--set=ON_ERROR_STOP=on \
-c "GRANT CONNECT ON DATABASE \"$PG_DATABASE_NAME\" TO \"$PG_USER_NAME\";"
if [ $? -eq 0 ]; then
echo "CONNECT granted successfully to [$PG_USER_NAME]"
else
echo "Failed to grant CONNECT to [$PG_USER_NAME]"
exit 1
fi
# Grant schema privileges to [$PG_USER_NAME]
echo "Granting schema privileges on [$PG_DATABASE_NAME] to [$PG_USER_NAME]..."
PGPASSWORD="$PG_ADMIN_PASSWORD" psql \
--host="$PG_FQDN" \
--port="$PG_PORT" \
--username="$PG_ADMIN_USER" \
--dbname="$PG_DATABASE_NAME" \
--no-password \
--set=ON_ERROR_STOP=on \
-c "GRANT USAGE, CREATE ON SCHEMA public TO \"$PG_USER_NAME\";
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO \"$PG_USER_NAME\";
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO \"$PG_USER_NAME\";"
if [ $? -eq 0 ]; then
echo "Schema privileges granted successfully to [$PG_USER_NAME]"
else
echo "Failed to grant schema privileges to [$PG_USER_NAME]"
exit 1
fi
# Create [activities] table
echo "Creating [activities] table in the [$PG_DATABASE_NAME] database..."
PGPASSWORD="$PG_USER_PASSWORD" psql \
--host="$PG_FQDN" \
--port="$PG_PORT" \
--username="$PG_USER_NAME" \
--dbname="$PG_DATABASE_NAME" \
--no-password \
--set=ON_ERROR_STOP=on \
-c "CREATE TABLE IF NOT EXISTS activities (
id TEXT PRIMARY KEY,
username TEXT NOT NULL,
activity TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_activities_username ON activities(username);
CREATE INDEX IF NOT EXISTS idx_activities_created_at ON activities(created_at DESC);"
if [ $? -eq 0 ]; then
echo "[activities] table created successfully"
else
echo "Failed to create [activities] table"
exit 1
fi
# Insert sample data
echo "Inserting sample data into [activities] table..."
PGPASSWORD="$PG_USER_PASSWORD" psql \
--host="$PG_FQDN" \
--port="$PG_PORT" \
--username="$PG_USER_NAME" \
--dbname="$PG_DATABASE_NAME" \
--no-password \
--set=ON_ERROR_STOP=on \
-c "INSERT INTO activities (id, username, activity) VALUES
(md5('paolo_pisa_seed'), 'paolo', 'Visit the Leaning Tower in Pisa'),
(md5('paolo_volterra_seed'), 'paolo', 'Explore Etruscan walls in Volterra'),
(md5('paolo_san_gimignano_seed'), 'paolo', 'Climb Torre Grossa in San Gimignano'),
(md5('paolo_siena_seed'), 'paolo', 'Walk across Piazza del Campo in Siena'),
(md5('paolo_montalcino_seed'), 'paolo', 'Taste Brunello wine in Montalcino'),
(md5('paolo_pienza_seed'), 'paolo', 'Sample Pecorino cheese in Pienza'),
(md5('paolo_florence_seed'), 'paolo', 'Admire Michelangelo''s David in Florence'),
(md5('paolo_viareggio_beach_seed'), 'paolo', 'Relax by the beach in Viareggio'),
(md5('paolo_viareggio_promenade_seed'), 'paolo', 'Stroll along the Viareggio promenade')
ON CONFLICT (id) DO NOTHING;"
if [ $? -eq 0 ]; then
echo "Test data inserted successfully into [activities] table"
else
echo "Failed to insert test data into [activities] table"
exit 1
fi
# Query data
echo "Querying test data from [activities] table..."
PGPASSWORD="$PG_USER_PASSWORD" psql \
--host="$PG_FQDN" \
--port="$PG_PORT" \
--username="$PG_USER_NAME" \
--dbname="$PG_DATABASE_NAME" \
--no-password \
-c "SELECT id, username, activity, created_at FROM activities;"
if [ $? -eq 0 ]; then
echo "Test data queried successfully from [activities] table"
else
echo "Failed to query test data from [activities] table"
exit 1
fi