Skip to content

Commit

Permalink
Set up fly.io toml files for deployment (#282)
Browse files Browse the repository at this point in the history
- **Log connection parameter**
- **Disable SSL in wait_for_postgres**
- **Add fly.toml**
- **Adjust SSL configuration to match fly.io**

Fixes #282
  • Loading branch information
shaldengeki authored Jul 23, 2024
1 parent 25046f9 commit e8c6c9b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
69 changes: 69 additions & 0 deletions ark_nova_stats/api/db/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# fly.toml app configuration file generated for ark-nova-stats-api-pg on 2024-07-23T01:04:41-04:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'ark-nova-stats-api-pg'
primary_region = 'ewr'

[env]
PRIMARY_REGION = 'ewr'
FLY_SCALE_TO_ZERO = "1h"

[[mounts]]
source = 'pg_data'
destination = '/data'

[[services]]
protocol = 'tcp'
internal_port = 5432
auto_start_machines = true

[[services.ports]]
port = 5432
handlers = ['pg_tls']

[services.concurrency]
type = 'connections'
hard_limit = 1000
soft_limit = 1000

[[services]]
protocol = 'tcp'
internal_port = 5433
auto_start_machines = true

[[services.ports]]
port = 5433
handlers = ['pg_tls']

[services.concurrency]
type = 'connections'
hard_limit = 1000
soft_limit = 1000

[checks]
[checks.pg]
port = 5500
type = 'http'
interval = '15s'
timeout = '10s'
path = '/flycheck/pg'

[checks.role]
port = 5500
type = 'http'
interval = '15s'
timeout = '10s'
path = '/flycheck/role'

[checks.vm]
port = 5500
type = 'http'
interval = '15s'
timeout = '10s'
path = '/flycheck/vm'

[[metrics]]
port = 9187
path = '/metrics'
34 changes: 34 additions & 0 deletions ark_nova_stats/api/fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# fly.toml app configuration file generated for ark-nova-stats-api on 2024-07-23T01:00:10-04:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'ark-nova-stats-api'
primary_region = 'ewr'

[build]
image = 'shaldengeki/ark-nova-stats-api:latest'

[deploy]
release_command = "/ark_nova_stats/api/migrations/binary"

[env]
API_PORT = '5000'
FLASK_APP = 'app.py'
FLASK_DEBUG = 'false'
FRONTEND_HOST = 'arknova.ouguo.us'
FRONTEND_PORT = ''
FRONTEND_PROTOCOL = 'https'

[http_service]
internal_port = 5000
force_https = true
auto_stop_machines = 'stop'
auto_start_machines = true
min_machines_running = 0
processes = ['app']

[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1
12 changes: 10 additions & 2 deletions base/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

def database_uri() -> str:
if os.getenv("DATABASE_URL", None) is not None:
return os.getenv("DATABASE_URL", "").replace(
"postgres://", "postgresql+pg8000://"
# On fly.io, we trim the sslmode=disable flag, because only psycopg can handle it, and we use pg8000.
return (
os.getenv("DATABASE_URL", "")
.replace("postgres://", "postgresql+pg8000://")
.replace("sslmode=disable", "")
)
else:
return "postgresql+pg8000://{user}:{password}@{host}/{db}".format(
Expand Down Expand Up @@ -39,6 +42,11 @@ def FlaskApp(name) -> tuple[Flask, CORS, SQLAlchemy, Migrate]:
FRONTEND_URL=frontend_url,
SECRET_KEY=os.getenv("FLASK_SECRET_KEY", "testing"),
SQLALCHEMY_DATABASE_URI=database_uri(),
SQLALCHEMY_ENGINE_OPTIONS={
"connect_args": {
"ssl_context": False, # Disable this by default, because fly.io requires it.
}
},
SESSION_REFRESH_EACH_REQUEST=True,
PERMANENT_SESSION_LIFETIME=datetime.timedelta(days=365),
)
Expand Down
2 changes: 2 additions & 0 deletions scripts/wait_for_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ def wait_for_postgres() -> None:
database = parsed_uri.path[1:]

while True:
print(f"Attempting connection to host {host} and port {port}...")
try:
pg8000.native.Connection(
username,
host=host,
port=int(port),
database=database,
password=password,
ssl_context=False,
)
except pg8000.exceptions.InterfaceError:
print("Postgres is unavailable - sleeping")
Expand Down

0 comments on commit e8c6c9b

Please sign in to comment.