diff --git a/Dockerfile b/Dockerfile index 6b320ce..27dfa88 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM python:3.10-alpine WORKDIR /app -COPY requirements.txt /app +COPY src/requirements.txt /app RUN pip3 install -r requirements.txt COPY src /app diff --git a/README.md b/README.md index 1160747..5b3b658 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,12 @@ cp example.env .env just run # Provision infrastructure -just terraform apply +just deploy -# Deploy new code +## RELEASE PROCESS +# Release new code +just release +# At this point, you have to modify .env to point to new image +# And then we can deploy the new image. just deploy ``` diff --git a/devbox.json b/devbox.json index f3663df..fd473eb 100644 --- a/devbox.json +++ b/devbox.json @@ -5,7 +5,9 @@ "python312Packages.pip@latest", "terraform@latest", "azure-cli@latest", - "azure-functions-core-tools@latest" + "azure-functions-core-tools@latest", + "black@latest", + "postgresql@latest" ], "env": { "VENV_DIR": ".venv" diff --git a/devbox.lock b/devbox.lock index 48a9a2e..64ae8b4 100644 --- a/devbox.lock +++ b/devbox.lock @@ -38,6 +38,26 @@ } } }, + "black@latest": { + "last_modified": "2024-02-26T19:46:43Z", + "resolved": "github:NixOS/nixpkgs/548a86b335d7ecd8b57ec617781f5e652ab0c38e#black", + "source": "devbox-search", + "version": "23.11.0", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/4aks55v8kdck409q1ga0qkdnrphabv53-python3.11-black-23.11.0" + }, + "aarch64-linux": { + "store_path": "/nix/store/y7kcifhk0v58pfhqdmpdbpd0yxwv860z-python3.11-black-23.11.0" + }, + "x86_64-darwin": { + "store_path": "/nix/store/41jmbap8q6gsjs8g59xwr6f222pdzpp8-python3.11-black-23.11.0" + }, + "x86_64-linux": { + "store_path": "/nix/store/g8k9vjxgsy8d3dizjw1jlcmj2iphy512-python3.11-black-23.11.0" + } + } + }, "just@latest": { "last_modified": "2024-02-26T19:46:43Z", "resolved": "github:NixOS/nixpkgs/548a86b335d7ecd8b57ec617781f5e652ab0c38e#just", @@ -58,6 +78,27 @@ } } }, + "postgresql@latest": { + "last_modified": "2024-02-26T19:46:43Z", + "plugin_version": "0.0.2", + "resolved": "github:NixOS/nixpkgs/548a86b335d7ecd8b57ec617781f5e652ab0c38e#postgresql", + "source": "devbox-search", + "version": "15.6", + "systems": { + "aarch64-darwin": { + "store_path": "/nix/store/j5m8ndrxpmk1444lzdk6p33vfzwrg6l7-postgresql-15.6" + }, + "aarch64-linux": { + "store_path": "/nix/store/3gbmk22frf8l07k3mhb16d43dy7bfd2s-postgresql-15.6" + }, + "x86_64-darwin": { + "store_path": "/nix/store/3a6lihh8nk9fvhf87vwkszfknjx27yfb-postgresql-15.6" + }, + "x86_64-linux": { + "store_path": "/nix/store/84xh621k32sc7ykqx37vcqdpjz9zxmdf-postgresql-15.6" + } + } + }, "python312Packages.pip@latest": { "last_modified": "2024-02-26T19:46:43Z", "plugin_version": "0.0.2", diff --git a/justfile b/justfile index 7e08cf6..1a75c15 100644 --- a/justfile +++ b/justfile @@ -4,10 +4,17 @@ set shell := ["devbox", "run"] set dotenv-load export TF_VAR_name := env("APP_NAME") +export GIT_SHA := `git rev-parse --short HEAD` # Run server locally run: - flask --app src/server run + initdb --username=postgres || true + devbox services start postgresql + python src/app.py + +run-docker: + docker build -t local/${APP_NAME}:${GIT_SHA} . + docker run -it --rm -p 8080:8080 local/${APP_NAME}:${GIT_SHA} dependencies: pip install -r src/requirements.txt @@ -15,11 +22,30 @@ dependencies: dependencies-lock: pip freeze -l > src/requirements.txt -deploy: +release: + #!/bin/env bash + USERNAME="00000000-0000-0000-0000-000000000000" + REGISTRY=$(terraform -chdir=terraform output -raw container_registry_name) -terraform-apply: + TAG=${REGISTRY}.azurecr.io/develop:${GIT_SHA}-$(date +"%F-%H-%M-%S") + + docker build -t ${TAG} . + + # For podman support we must use this hacks, + # otherwise az acr login would also do the docker login. + docker login \ + --username=${USERNAME} \ + --password=$(az acr login --name ${REGISTRY} --expose-token 2>/dev/null | jq -r '.accessToken') \ + "${REGISTRY}.azurecr.io" + + docker push ${TAG} + + echo "Image pushed to ${TAG}" + echo "Modify your .env with TF_VAR_image=${TAG}" + +deploy: terraform -chdir=terraform init terraform -chdir=terraform apply -terraform-destroy: +destroy: terraform -chdir=terraform destroy diff --git a/src/app.py b/src/app.py index 8aa1c4e..64278ee 100644 --- a/src/app.py +++ b/src/app.py @@ -1,11 +1,28 @@ -from flask import Flask +from flask import Flask, render_template +from database import get_db_connection, migrate app = Flask(__name__) + @app.route("/health") def health(): + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute("SELECT 1") + cursor.fetchone() return "OK" + @app.route("/") def hello_world(): - return "
Hello, World!
" + conn = get_db_connection() + cursor = conn.cursor() + cursor.execute("SELECT * FROM users") + users = cursor.fetchall() + print(users) + return render_template("index.html", users=users) + + +if __name__ == "__main__": + migrate() + app.run(host="0.0.0.0", port=8080) diff --git a/src/database.py b/src/database.py new file mode 100644 index 0000000..0c6b8f5 --- /dev/null +++ b/src/database.py @@ -0,0 +1,28 @@ +import os +import psycopg2 + +conn = None + + +def get_db_connection(): + global conn + if not conn: + conn = psycopg2.connect( + host=os.environ.get("DB_HOST", "localhost"), + database=os.environ.get("DB_NAME", "postgres"), + user=os.environ.get("DB_USER", "postgres"), + password=os.environ.get("DB_PASSWORD", "postgres"), + sslmode=os.environ.get("DB_SSLMODE", "prefer"), + ) + return conn + + +def migrate(): + conn = get_db_connection() + cur = conn.cursor() + cur.execute( + "CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, name VARCHAR(255))" + ) + cur.execute("INSERT INTO users (name) VALUES ('John')") + cur.execute("INSERT INTO users (name) VALUES ('Oliver')") + conn.commit() diff --git a/requirements.txt b/src/requirements.txt similarity index 73% rename from requirements.txt rename to src/requirements.txt index d40fb70..919dedd 100644 --- a/requirements.txt +++ b/src/requirements.txt @@ -1,5 +1,5 @@ blinker==1.7.0 -click==8.1.7 Flask==3.0.2 itsdangerous==2.1.2 +psycopg2-binary==2.9.9 Werkzeug==3.0.1 diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..e0be914 --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,10 @@ + +