Skip to content

Commit cb518aa

Browse files
committed
Update project with steps to deploy to fly.io
1 parent 24dcb7f commit cb518aa

8 files changed

Lines changed: 102 additions & 5 deletions

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,34 @@ Includes some functionality for a basic public micro-blogging app.
7676
```
7777
./build-prod
7878
```
79+
80+
81+
## Deploying to fly.io
82+
83+
### Config Type 1: Deploy on simple single webserver with SQLite database:
84+
85+
1. Set app name in project (Replace *your_app_name* with an actual app name):
86+
```
87+
sed -i.bak 's/FLY_APP_NAME/your_app_name/g' server/config/settings.py && find scripts/ -type f -name "deploy_*" -exec sed -i.bak 's/FLY_APP_NAME/your_app_name/g' {} +
88+
```
89+
90+
2. Run app deployment script:
91+
```
92+
./scripts/deploy-fly.io-sqlite.sh $app_name
93+
```
94+
95+
### Config Type 2: Deploy using HA configuration with Postgres database:
96+
97+
1. Set app name in project (Replace *your_app_name* with an actual app name):
98+
```
99+
sed -i.bak 's/FLY_APP_NAME/your_app_name/g' server/config/settings.py && find scripts/ -type f -name "deploy_*" -exec sed -i.bak 's/FLY_APP_NAME/your_app_name/g' {} +
100+
```
101+
102+
2. Run app deployment script:
103+
```
104+
./scripts/deploy-fly.io-postgres.sh
105+
```
106+
107+
### To use Cloudflare R2 object storage for user uploads (Required for Config Type 2):
108+
109+
TODO
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
3+
# Remove backup files created by fly deployment script
4+
files=(
5+
"scripts/configs/fly-postgres.toml.bak"
6+
"scripts/configs/fly-sqlite.toml.bak"
7+
"server/config/settings.py.bak"
8+
)
9+
for file in "${files[@]}"; do
10+
if [ -f "$file" ]; then
11+
rm "$file"
12+
fi
13+
done

scripts/configs/fly-postgres.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
44
#
55

6-
app = 'web-framework-2025-lively-glitter-9000'
6+
app = 'FLY_APP_NAME'
77
primary_region = 'iad'
88
console_command = '/code/manage.py shell'
99

scripts/configs/fly-sqlite.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
44
#
55

6-
app = 'webframework'
6+
app = 'FLY_APP_NAME'
77
primary_region = 'yyz'
88
console_command = '/code/manage.py shell'
99

scripts/deploy-fly.io-postgres.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
#!/usr/bin/env bash
22

3+
# Get FLY_APP_NAME from command line arguments
4+
FLY_APP_NAME=$1
5+
6+
# Display usage if no argument is provided
7+
if [ -z "$FLY_APP_NAME" ]; then
8+
echo "Usage: $0 <app_name>"
9+
exit 1
10+
fi
11+
12+
# Set app name in project via script
13+
./scripts/set-fly-app-name.sh $FLY_APP_NAME
14+
315
# Specify the target fly.toml configuration file
416
function specify_target_fly_toml() {
517
# Move currently active fly.toml configuration file to local tmp directory
@@ -14,7 +26,7 @@ function specify_target_fly_toml() {
1426
specify_target_fly_toml
1527

1628
# Launch app
17-
yes n | fly launch --now --copy-config
29+
yes n | fly launch --name $FLY_APP_NAME --now --copy-config
1830

1931
# Restore existing fly.toml
2032
mv .tmp/fly.toml fly.toml

scripts/deploy-fly.io-sqlite.sh

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
#!/usr/bin/env bash
22

3+
# Get FLY_APP_NAME from command line arguments
4+
FLY_APP_NAME=$1
5+
6+
# Display usage if no argument is provided
7+
if [ -z "$FLY_APP_NAME" ]; then
8+
echo "Usage: $0 <app_name>"
9+
exit 1
10+
fi
11+
12+
# Set app name in project via script
13+
./scripts/set-fly-app-name.sh $FLY_APP_NAME
314

415
# Specify the target fly.toml configuration file
516
function specify_target_fly_toml() {
@@ -16,11 +27,16 @@ specify_target_fly_toml
1627

1728

1829
# Create app without launching
19-
yes n | fly launch --vm-memory 512 --ha=false --no-db --now --copy-config --build-only
30+
yes n | fly launch --name $FLY_APP_NAME --vm-memory 512 --ha=false --no-db --now --copy-config --build-only
2031

2132
# Set database location
2233
flyctl secrets set DATABASE_URL=sqlite:////data/db.sqlite3
2334

35+
# Set uploads location
36+
flyctl secrets set MEDIA_URL=https://${FLY_APP_NAME}.fly.dev/media/
37+
flyctl secrets set MEDIA_ROOT=/data/uploads
38+
flyctl secrets set USE_LOCAL_FILE_STORAGE=True
39+
2440
# Use gsed on macOS
2541
SED_CMD="sed"
2642
if [[ "$OSTYPE" == "darwin"* ]]; then
@@ -35,9 +51,18 @@ $SED_CMD -i "s|^ release_command = 'python manage.py migrate --noinput'|# rele
3551

3652

3753
# Launch app
38-
fly deploy
54+
fly deploy --config ./scripts/configs/fly-sqlite.toml
55+
56+
# Wait for app to be ready
57+
echo "Waiting for app to be ready..."
58+
while ! curl -s -o /dev/null -w "%{http_code}" https://$FLY_APP_NAME.fly.dev; do
59+
sleep 2
60+
done
61+
echo
62+
echo "App is ready!"
3963

4064
# Run migrations
65+
echo "Running migrations..."
4166
fly ssh console -C 'python manage.py migrate'
4267

4368
# Restore existing fly.toml

scripts/set-fly-app-name.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
# Get FLY_APP_NAME from command line arguments
4+
FLY_APP_NAME=$1
5+
6+
# Display usage if no argument is provided
7+
if [ -z "$FLY_APP_NAME" ]; then
8+
echo "Usage: $0 <app_name>"
9+
exit 1
10+
fi
11+
12+
# Set app name in project
13+
sed -i.bak "s/FLY_APP_NAME/$FLY_APP_NAME/g" server/config/settings.py
14+
find scripts/configs/ -type f -name "fly-*" -exec sed -i.bak "s/FLY_APP_NAME/$FLY_APP_NAME/g" {} +

server/config/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
ALLOWED_HOSTS = [
7373
'127.0.0.1',
7474
'localhost',
75+
'FLY_APP_NAME.fly.dev',
7576
'webframework.fly.dev',
7677
'webframework.dev',
7778
'wut.sh',
@@ -295,6 +296,7 @@ def create_log_dir_for_server_errors():
295296
)
296297

297298
CSRF_TRUSTED_ORIGINS = [
299+
'https://FLY_APP_NAME.fly.dev',
298300
'https://webframework.fly.dev',
299301
'https://webframework.dev',
300302
'https://wut.sh',

0 commit comments

Comments
 (0)