Skip to content

Commit 038a3f9

Browse files
Refactor code structure
1 parent 06ac7af commit 038a3f9

6 files changed

Lines changed: 106 additions & 46 deletions

File tree

samples/web-app-mysql-flexible-server/python/README.md

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,7 @@ This sample demonstrates a Python Flask single-page web application called *Vaca
44

55
## Architecture
66

7-
```mermaid
8-
flowchart LR
9-
user([User])
10-
11-
subgraph rg["Resource Group: local-rg"]
12-
direction LR
13-
law["Log Analytics<br/>Workspace"]
14-
nat["NAT Gateway"]
15-
dns["Private DNS Zone<br/>privatelink.mysql.database.azure.com"]
16-
asp["App Service Plan<br/>S1 · Linux"]
17-
mysql[("MySQL Flexible Server<br/>8.0.21 · Burstable B1ms<br/>DB: plannerdb")]
18-
19-
subgraph vnet["Virtual Network 10.0.0.0/8"]
20-
direction TB
21-
subgraph appsub["app-subnet 10.0.0.0/24 — delegated to Microsoft.Web/serverFarms"]
22-
webapp["Web App<br/>Vacation Planner<br/>Flask + gunicorn"]
23-
end
24-
subgraph pesub["pe-subnet 10.0.1.0/24"]
25-
pe["Private Endpoint<br/>group: mysqlServer"]
26-
end
27-
end
28-
end
29-
30-
user -->|HTTP| webapp
31-
webapp -->|"MYSQL_HOST:MYSQL_PORT<br/>(resolved via Private DNS)"| pe
32-
pe -->|Private Link| mysql
33-
dns -.->|A record| pe
34-
dns -.->|linked| vnet
35-
appsub -->|outbound| nat
36-
webapp -.->|hosted on| asp
37-
webapp -.->|diagnostics| law
38-
mysql -.->|diagnostics| law
39-
```
7+
![Architecture Diagram](./images/architecture.png)
408

419
The web app enables users to plan and manage vacation activities; all data is persisted in MySQL. The solution is composed of the following Azure resources:
4210

samples/web-app-mysql-flexible-server/python/bicep/deploy.sh

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ LOCATION="westeurope"
1010
VALIDATE_TEMPLATE=1
1111
USE_WHAT_IF=0
1212
SUBSCRIPTION_NAME=$(az account show --query name --output tsv)
13-
MYSQL_ADMIN_USER="myadmin"
14-
MYSQL_ADMIN_PASSWORD="P@ssw0rd1234!"
15-
MYSQL_APP_USER="testuser"
16-
MYSQL_APP_PASSWORD="TestP@ssw0rd123"
13+
MYSQL_ADMIN_USER="${MYSQL_ADMIN_USER:-myadmin}"
14+
MYSQL_ADMIN_PASSWORD="${MYSQL_ADMIN_PASSWORD:-P@ssw0rd1234!}"
15+
MYSQL_APP_USER="${MYSQL_APP_USER:-testuser}"
16+
MYSQL_APP_PASSWORD="${MYSQL_APP_PASSWORD:-TestP@ssw0rd123}"
1717
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
1818
ZIPFILE="planner_website.zip"
1919

@@ -134,6 +134,29 @@ else
134134
fi
135135
echo "MySQL host = $MYSQL_FQDN, port = $MYSQL_PORT"
136136

137+
echo "Waiting for the [$MYSQL_SERVER_NAME] MySQL flexible server to accept connections..."
138+
MYSQL_READY=0
139+
for attempt in $(seq 1 30); do
140+
if MYSQL_PWD="$MYSQL_ADMIN_PASSWORD" mysql \
141+
--host="$MYSQL_FQDN" \
142+
--port="$MYSQL_PORT" \
143+
--user="$MYSQL_ADMIN_USER" \
144+
--protocol=TCP \
145+
--connect-timeout=5 \
146+
-e "SELECT 1;" &>/dev/null; then
147+
MYSQL_READY=1
148+
echo "MySQL flexible server is accepting connections (attempt $attempt/30)"
149+
break
150+
fi
151+
echo "MySQL flexible server not ready yet (attempt $attempt/30)..."
152+
sleep 2
153+
done
154+
155+
if [ "$MYSQL_READY" -ne 1 ]; then
156+
echo "MySQL flexible server did not become reachable after 30 attempts. Exiting."
157+
exit 1
158+
fi
159+
137160
# Create application user [$MYSQL_APP_USER] on the MySQL flexible server
138161
echo "Creating login [$MYSQL_APP_USER] on the [$MYSQL_SERVER_NAME] MySQL flexible server..."
139162
MYSQL_PWD="$MYSQL_ADMIN_PASSWORD" mysql \
224 KB
Loading

samples/web-app-mysql-flexible-server/python/scripts/deploy.sh

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ MYSQL_SKU_NAME="Standard_B1ms"
3131
MYSQL_SKU_TIER="Burstable"
3232
MYSQL_STORAGE_SIZE_GB=32
3333
MYSQL_BACKUP_RETENTION_DAYS=7
34-
MYSQL_DATABASE_NAME="plannerdb"
35-
MYSQL_ADMIN_USER="myadmin"
36-
MYSQL_ADMIN_PASSWORD="P@ssw0rd1234!"
37-
MYSQL_APP_USER="testuser"
38-
MYSQL_APP_PASSWORD="TestP@ssw0rd123"
34+
MYSQL_DATABASE_NAME="${MYSQL_DATABASE_NAME:-plannerdb}"
35+
MYSQL_ADMIN_USER="${MYSQL_ADMIN_USER:-myadmin}"
36+
MYSQL_ADMIN_PASSWORD="${MYSQL_ADMIN_PASSWORD:-P@ssw0rd1234!}"
37+
MYSQL_APP_USER="${MYSQL_APP_USER:-testuser}"
38+
MYSQL_APP_PASSWORD="${MYSQL_APP_PASSWORD:-TestP@ssw0rd123}"
3939
FIREWALL_RULE_NAME="AllowAllIPs"
4040
RUNTIME="python"
4141
RUNTIME_VERSION="3.13"
@@ -584,6 +584,29 @@ else
584584
echo "Private DNS zone group [$PRIVATE_DNS_ZONE_GROUP_NAME] for the [$PRIVATE_ENDPOINT_NAME] private endpoint already exists"
585585
fi
586586

587+
echo "Waiting for the [$MYSQL_SERVER_NAME] MySQL flexible server to accept connections..."
588+
MYSQL_READY=0
589+
for attempt in $(seq 1 30); do
590+
if MYSQL_PWD="$MYSQL_ADMIN_PASSWORD" mysql \
591+
--host="$MYSQL_FQDN" \
592+
--port="$MYSQL_PORT" \
593+
--user="$MYSQL_ADMIN_USER" \
594+
--protocol=TCP \
595+
--connect-timeout=5 \
596+
-e "SELECT 1;" &>/dev/null; then
597+
MYSQL_READY=1
598+
echo "MySQL flexible server is accepting connections (attempt $attempt/30)"
599+
break
600+
fi
601+
echo "MySQL flexible server not ready yet (attempt $attempt/30)..."
602+
sleep 2
603+
done
604+
605+
if [ "$MYSQL_READY" -ne 1 ]; then
606+
echo "MySQL flexible server did not become reachable after 30 attempts. Exiting."
607+
exit 1
608+
fi
609+
587610
# Create application user [$MYSQL_APP_USER] on the MySQL flexible server
588611
echo "Creating login [$MYSQL_APP_USER] on the [$MYSQL_SERVER_NAME] MySQL flexible server..."
589612
MYSQL_PWD="$MYSQL_ADMIN_PASSWORD" mysql \

samples/web-app-mysql-flexible-server/python/terraform/deploy.sh

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
PREFIX='local'
55
SUFFIX='test'
66
LOCATION='westeurope'
7-
MYSQL_ADMIN_USER="myadmin"
8-
MYSQL_ADMIN_PASSWORD="P@ssw0rd1234!"
9-
MYSQL_APP_USER="testuser"
10-
MYSQL_APP_PASSWORD="TestP@ssw0rd123"
7+
MYSQL_ADMIN_USER="${MYSQL_ADMIN_USER:-myadmin}"
8+
MYSQL_ADMIN_PASSWORD="${MYSQL_ADMIN_PASSWORD:-P@ssw0rd1234!}"
9+
MYSQL_APP_USER="${MYSQL_APP_USER:-testuser}"
10+
MYSQL_APP_PASSWORD="${MYSQL_APP_PASSWORD:-TestP@ssw0rd123}"
1111
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
1212
ZIPFILE="planner_website.zip"
1313

@@ -64,6 +64,29 @@ else
6464
fi
6565
echo "MySQL host = $MYSQL_FQDN, port = $MYSQL_PORT"
6666

67+
echo "Waiting for the [$MYSQL_SERVER_NAME] MySQL flexible server to accept connections..."
68+
MYSQL_READY=0
69+
for attempt in $(seq 1 30); do
70+
if MYSQL_PWD="$MYSQL_ADMIN_PASSWORD" mysql \
71+
--host="$MYSQL_FQDN" \
72+
--port="$MYSQL_PORT" \
73+
--user="$MYSQL_ADMIN_USER" \
74+
--protocol=TCP \
75+
--connect-timeout=5 \
76+
-e "SELECT 1;" &>/dev/null; then
77+
MYSQL_READY=1
78+
echo "MySQL flexible server is accepting connections (attempt $attempt/30)"
79+
break
80+
fi
81+
echo "MySQL flexible server not ready yet (attempt $attempt/30)..."
82+
sleep 2
83+
done
84+
85+
if [ "$MYSQL_READY" -ne 1 ]; then
86+
echo "MySQL flexible server did not become reachable after 30 attempts. Exiting."
87+
exit 1
88+
fi
89+
6790
# Create application user [$MYSQL_APP_USER] on the MySQL flexible server
6891
echo "Creating login [$MYSQL_APP_USER] on the [$MYSQL_SERVER_NAME] MySQL flexible server..."
6992
MYSQL_PWD="$MYSQL_ADMIN_PASSWORD" mysql \

samples/web-app-postgresql-flexible-server/python/bicep/deploy.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,29 @@ else
134134
fi
135135
echo "PostgreSQL host = $POSTGRES_FQDN, port = $POSTGRES_PORT"
136136

137+
echo "Waiting for the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server to accept connections..."
138+
POSTGRES_READY=0
139+
for attempt in $(seq 1 30); do
140+
if PGPASSWORD="$PG_ADMIN_PASSWORD" PGCONNECT_TIMEOUT=5 psql \
141+
--host="$POSTGRES_FQDN" \
142+
--port="$POSTGRES_PORT" \
143+
--username="$PG_ADMIN_USER" \
144+
--dbname=postgres \
145+
--no-password \
146+
-c "SELECT 1;" &>/dev/null; then
147+
POSTGRES_READY=1
148+
echo "PostgreSQL flexible server is accepting connections (attempt $attempt/30)"
149+
break
150+
fi
151+
echo "PostgreSQL flexible server not ready yet (attempt $attempt/30)..."
152+
sleep 2
153+
done
154+
155+
if [ "$POSTGRES_READY" -ne 1 ]; then
156+
echo "PostgreSQL flexible server did not become reachable after 30 attempts. Exiting."
157+
exit 1
158+
fi
159+
137160
# Create application role [$PG_APP_USER] on the PostgreSQL flexible server
138161
echo "Creating login [$PG_APP_USER] on the [$POSTGRES_SERVER_NAME] PostgreSQL flexible server..."
139162
PGPASSWORD="$PG_ADMIN_PASSWORD" psql \

0 commit comments

Comments
 (0)