Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from glasnt/feat/crj
(minor): Cloud Run Jobs, cleanup
- Loading branch information
Showing
19 changed files
with
211 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
# Django self-determined URL for CSRF | ||
|
||
For Django's CSRF protections, you need to have Django know the host it's being served on. | ||
For Django's CSRF protections, you need to have Django know the host it's being served on. | ||
|
||
You can set this manually with environment variables, but you can also interrogate the [metadata server](https://cloud.google.com/run/docs/container-contract#metadata-server) and [environment variables](https://cloud.google.com/run/docs/container-contract#services-env-vars) available by default to determine this information. | ||
|
||
|
||
|
||
The implementation of this can be found in the [`get_service_url`](https://github.com/GoogleCloudPlatform/avocano/search?q=get_service_url) method. It also requires the service has [permissions to view it's own metadata](server_introspection). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Extending the example | ||
|
||
If you are wanting to extend this example application, there are some development steps you're going to need to take. | ||
|
||
## Forking the repo, and GitHub actions | ||
|
||
Please feel free to fork the repo, but know that there are GitHub Actions attached to the parent repo | ||
that probably won't automatically work with your setup. | ||
|
||
To disable: | ||
|
||
* Go to your fork's Settings | ||
* Go to Code and Automation > Actions > General | ||
* Under "Actions permissions", select "Disable actions". | ||
* Click **Save**. | ||
|
||
## Terraform needs `state.tf` file | ||
|
||
If you're running the examples in an environment outside of Cloud Shell, you'll need to be aware that | ||
the `setup.sh` created a Terraform backend state configuration for you. It's a file that's `.gitignore`d, | ||
so you might have missed it. | ||
|
||
Be sure to create a `provisioning/terraform/state.tf` file with the following contents: | ||
|
||
*Note*: The `PROJECT_ID` is a literal replacement, and must be hardcoded (`backend` doesn't accept variables) | ||
|
||
``` | ||
terraform { | ||
backend gcs { | ||
bucket = "terraform-PROJECT_ID" | ||
} | ||
} | ||
``` | ||
|
||
|
||
## Creating migrations | ||
|
||
When automatically generating Django migrations, you'll need to run these on your local machine so you can commit the | ||
results to source. | ||
|
||
To do this, you'll need a connection to your Cloud SQL database, and your settings locally. | ||
|
||
### Setup Cloud SQL Auth Proxy | ||
|
||
Follow the [Install instructions](https://cloud.google.com/sql/docs/postgres/sql-proxy#install) for your platform. | ||
|
||
Saving this into your `$PATH` is a useful administration step so you can take advantage of this executable in other projects. | ||
|
||
### Start Cloud SQL Auth Proxy | ||
|
||
In a new terminal, start the proxy: | ||
|
||
``` | ||
cloud_sql_proxy -instances="${PROJECT_ID}:us-central1:psql"=tcp:5432 | ||
``` | ||
|
||
This will redirect this Cloud SQL instance to localhost. Because of this, you'll need to change where Django knows your database to be. | ||
|
||
### Copy and edit settings | ||
|
||
1. Navigate to the server config: | ||
|
||
``` | ||
cd server | ||
``` | ||
|
||
1. Copy the Django settings from Secret Manager to your local `.env` file. | ||
Note: this file is `.gitignore`d. | ||
|
||
``` | ||
gcloud secrets versions access latest --secret django_settings > .env | ||
``` | ||
|
||
1. Edit the `DATABASE_URL` line to replace the `cloudsql` config to `localhost`: | ||
|
||
```diff | ||
-DATABASE_URL="postgres://server:password@//cloudsql/yourproject:us-central1:psql/django" | ||
+DATABASE_URL="postgres://server:password@localhost/django" | ||
``` | ||
|
||
1. Run your code on your local machine: | ||
|
||
``` | ||
python manage.py runserver | ||
``` | ||
|
||
|
||
From here, you should be able to run `makemigrations` and other tasks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Applying database migrations | ||
|
||
If you want to apply database migrations, a convenience Cloud Run job has been created for you. | ||
|
||
To run migrations: | ||
|
||
``` | ||
gcloud beta run jobs execute migrate-database \ | ||
--region us-central1 --wait | ||
``` | ||
|
||
To run migrations as part of the Cloud Build, add a step to the `cloudbuild.yaml` file: | ||
|
||
``` | ||
- id: server migrate | ||
name: "gcr.io/google.com/cloudsdktool/cloud-sdk:slim" | ||
entrypoint: gcloud | ||
args: ["beta", "run", "jobs", "execute", "migrate-database", | ||
"--region", $_REGION, "--wait"] | ||
``` | ||
|
||
This step must be after the image push step, but can be before or after Terraform (the terraform step | ||
updates the Cloud Run service to the latest image). If your database changes must be made **before** | ||
the application is updated, put it before the terraform step. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
web: gunicorn --bind 0.0.0.0:$PORT avocano_api.wsgi:application | ||
migrate: python3 manage.py migrate | ||
collectstatic: python3 manage.py collectstatic --noinput | ||
migrate: python3 manage.py migrate && python3 manage.py collectstatic --noinput --clear | ||
|
||
firstload: bash scripts/prime_database.sh | ||
setup: bash scripts/prime_database.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
server/store/migrations/0003_alter_testimonial_reviewer_location.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.1.3 on 2022-11-14 02:09 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("store", "0002_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="testimonial", | ||
name="reviewer_location", | ||
field=models.CharField(max_length=100), | ||
), | ||
] |
Oops, something went wrong.