From f3a890ab1c06a0f480c32ea4f189053ab037e3ee Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 08:00:24 -0500 Subject: [PATCH 01/26] Test candidate for migration handler --- bin/run-api.sh | 2 -- manifests/manifest-dev-migrator.yml | 35 ++++++++++++++++++ manifests/manifest-prod-migrator.yml | 35 ++++++++++++++++++ manifests/manifest-stage-migrator.yml | 35 ++++++++++++++++++ manifests/manifest-test-migrator.yml | 35 ++++++++++++++++++ tasks.py | 52 ++++++++++++++++++++++++++- 6 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 manifests/manifest-dev-migrator.yml create mode 100644 manifests/manifest-prod-migrator.yml create mode 100644 manifests/manifest-stage-migrator.yml create mode 100644 manifests/manifest-test-migrator.yml diff --git a/bin/run-api.sh b/bin/run-api.sh index b958f015f9..d229d065c2 100755 --- a/bin/run-api.sh +++ b/bin/run-api.sh @@ -3,8 +3,6 @@ cd django-backend # Only Instance 0 runs migrations and creates views echo "------ Starting APP ------" if [ $CF_INSTANCE_INDEX = "0" ]; then - echo "----- Migrating Database -----" - python manage.py migrate --no-input --traceback --verbosity 3 echo "----- Creating committee views -----" python manage.py create_committee_views fi diff --git a/manifests/manifest-dev-migrator.yml b/manifests/manifest-dev-migrator.yml new file mode 100644 index 0000000000..36449de2b0 --- /dev/null +++ b/manifests/manifest-dev-migrator.yml @@ -0,0 +1,35 @@ +--- +applications: + - name: fecfile-web-api + instances: 1 + stack: cflinuxfs4 + buildpacks: + - python_buildpack + command: bin/run-api.sh + memory: 1G + services: + - fecfile-api-rds + - fecfile-api-s3 + - fecfile-api-redis + - fecfile-api-creds-dev + env: + DISABLE_COLLECTSTATIC: 1 + DJANGO_SETTINGS_MODULE: fecfiler.settings.production + FFAPI_COOKIE_DOMAIN: fecfile.fec.gov + LOGIN_REDIRECT_CLIENT_URL: https://dev.fecfile.fec.gov + LOGIN_REDIRECT_SERVER_URL: https://dev-api.fecfile.fec.gov/api/v1/oidc/login-redirect + LOGOUT_REDIRECT_URL: https://dev-api.fecfile.fec.gov/api/v1/oidc/logout-redirect + PRODUCTION_OPEN_FEC_API: https://api.open.fec.gov/v1/ + STAGE_OPEN_FEC_API: https://api-stage.open.fec.gov/v1/ + SESSION_COOKIE_AGE: 64800 + BP_PIP_VERSION: latest + LOG_FORMAT: KEY_VALUE + MOCK_EFO: True + SYSTEM_STATUS_CACHE_AGE: 60 + INITIAL_POLLING_INTERVAL: 10 + INITIAL_POLLING_DURATION: 900 + SECONDARY_POLLING_INTERVAL: 3600 + SECONDARY_POLLING_DURATION: 86400 + # ---- FEATURE FLAGS ---- + FLAG__COMMITTEE_DATA_SOURCE: TEST # Values are PRODUCTION, TEST, MOCKED + ENABLE_DEVELOPER_COMMANDS: True diff --git a/manifests/manifest-prod-migrator.yml b/manifests/manifest-prod-migrator.yml new file mode 100644 index 0000000000..1ebaf8a1c5 --- /dev/null +++ b/manifests/manifest-prod-migrator.yml @@ -0,0 +1,35 @@ +--- +applications: + - name: fecfile-web-api + instances: 1 + stack: cflinuxfs4 + buildpacks: + - python_buildpack + command: bin/run-api.sh + memory: 1G + services: + - fecfile-api-rds + - fecfile-api-s3 + - fecfile-api-redis + - fecfile-api-creds-prod + env: + DISABLE_COLLECTSTATIC: 1 + DJANGO_SETTINGS_MODULE: fecfiler.settings.production + FFAPI_COOKIE_DOMAIN: fecfile.fec.gov + LOGIN_REDIRECT_CLIENT_URL: https://fecfile.fec.gov + LOGIN_REDIRECT_SERVER_URL: https://api.fecfile.fec.gov/api/v1/oidc/login-redirect + LOGOUT_REDIRECT_URL: https://api.fecfile.fec.gov/api/v1/oidc/logout-redirect + PRODUCTION_OPEN_FEC_API: https://api.open.fec.gov/v1/ + STAGE_OPEN_FEC_API: https://api-stage.open.fec.gov/v1/ + FEC_FILING_API: https://efoservices.stage.efo.fec.gov + SESSION_COOKIE_AGE: 64800 + BP_PIP_VERSION: latest + LOG_FORMAT: KEY_VALUE + SYSTEM_STATUS_CACHE_AGE: 60 + INITIAL_POLLING_INTERVAL: 10 + INITIAL_POLLING_DURATION: 900 + SECONDARY_POLLING_INTERVAL: 3600 + SECONDARY_POLLING_DURATION: 86400 + # ---- FEATURE FLAGS ---- + FLAG__COMMITTEE_DATA_SOURCE: PRODUCTION # Values are PRODUCTION, TEST, and MOCKED + ENABLE_DEVELOPER_COMMANDS: False diff --git a/manifests/manifest-stage-migrator.yml b/manifests/manifest-stage-migrator.yml new file mode 100644 index 0000000000..c24db24102 --- /dev/null +++ b/manifests/manifest-stage-migrator.yml @@ -0,0 +1,35 @@ +--- +applications: + - name: fecfile-web-api + instances: 1 + stack: cflinuxfs4 + buildpacks: + - python_buildpack + command: bin/run-api.sh + memory: 1G + services: + - fecfile-api-rds + - fecfile-api-s3 + - fecfile-api-redis + - fecfile-api-creds-stage + env: + DISABLE_COLLECTSTATIC: 1 + DJANGO_SETTINGS_MODULE: fecfiler.settings.production + FFAPI_COOKIE_DOMAIN: fecfile.fec.gov + LOGIN_REDIRECT_CLIENT_URL: https://stage.fecfile.fec.gov + LOGIN_REDIRECT_SERVER_URL: https://stage-api.fecfile.fec.gov/api/v1/oidc/login-redirect + LOGOUT_REDIRECT_URL: https://stage-api.fecfile.fec.gov/api/v1/oidc/logout-redirect + PRODUCTION_OPEN_FEC_API: https://api.open.fec.gov/v1/ + STAGE_OPEN_FEC_API: https://api-stage.open.fec.gov/v1/ + FEC_FILING_API: https://efoservices.stage.efo.fec.gov + SESSION_COOKIE_AGE: 64800 + BP_PIP_VERSION: latest + LOG_FORMAT: KEY_VALUE + SYSTEM_STATUS_CACHE_AGE: 60 + INITIAL_POLLING_INTERVAL: 10 + INITIAL_POLLING_DURATION: 900 + SECONDARY_POLLING_INTERVAL: 3600 + SECONDARY_POLLING_DURATION: 86400 + # ---- FEATURE FLAGS ---- + FLAG__COMMITTEE_DATA_SOURCE: TEST # Values are TEST and PRODUCTION + ENABLE_DEVELOPER_COMMANDS: True diff --git a/manifests/manifest-test-migrator.yml b/manifests/manifest-test-migrator.yml new file mode 100644 index 0000000000..f028809ba0 --- /dev/null +++ b/manifests/manifest-test-migrator.yml @@ -0,0 +1,35 @@ +--- +applications: + - name: fecfile-web-api + instances: 1 + stack: cflinuxfs4 + buildpacks: + - python_buildpack + command: bin/run-api.sh + memory: 1G + services: + - fecfile-api-rds + - fecfile-api-s3 + - fecfile-api-redis + - fecfile-api-creds-test + env: + DISABLE_COLLECTSTATIC: 1 + DJANGO_SETTINGS_MODULE: fecfiler.settings.production + FFAPI_COOKIE_DOMAIN: fecfile.fec.gov + LOGIN_REDIRECT_CLIENT_URL: https://test.fecfile.fec.gov + LOGIN_REDIRECT_SERVER_URL: https://test-api.fecfile.fec.gov/api/v1/oidc/login-redirect + LOGOUT_REDIRECT_URL: https://test-api.fecfile.fec.gov/api/v1/oidc/logout-redirect + PRODUCTION_OPEN_FEC_API: https://api.open.fec.gov/v1/ + STAGE_OPEN_FEC_API: https://api-stage.open.fec.gov/v1/ + FEC_FILING_API: https://efoservices.stage.efo.fec.gov + SESSION_COOKIE_AGE: 64800 + BP_PIP_VERSION: latest + LOG_FORMAT: KEY_VALUE + SYSTEM_STATUS_CACHE_AGE: 60 + INITIAL_POLLING_INTERVAL: 10 + INITIAL_POLLING_DURATION: 900 + SECONDARY_POLLING_INTERVAL: 3600 + SECONDARY_POLLING_DURATION: 86400 + # ---- FEATURE FLAGS ---- + FLAG__COMMITTEE_DATA_SOURCE: TEST # Values are PRODUCTION, TEST, and MOCKED + ENABLE_DEVELOPER_COMMANDS: True diff --git a/tasks.py b/tasks.py index 4eea678eb1..6c271d366c 100644 --- a/tasks.py +++ b/tasks.py @@ -89,7 +89,6 @@ def _login_to_cf(ctx, space): def _do_deploy(ctx, space, app): - manifest_filename = f"manifests/manifest-{space}-{MANIFEST_LABEL.get(app)}.yml" existing_deploy = ctx.run(f"cf app {app}", echo=True, warn=True) print("\n") @@ -151,6 +150,50 @@ def _rollback(ctx, app): print("Unable to cancel deploy. Check logs.") +def _do_migrations(ctx, space): + print("Running migrations...") + + manifest_filename = f"manifests/manifest-{space}-migrator.yml" + app = f"fecfile-api-migrator-{space}" + + # Start migrator app + cmd = "push" + migrator = ctx.run( + f"cf {cmd} {app} -f {manifest_filename}", + echo=True, + warn=True, + ) + if not migrator.ok: + print("Failed to spin up migrator app. Check logs.") + return False + + # Run migrations + task = 'django-backend/manage.py migrate --no-input --traceback --verbosity 3' + migrations = ctx.run( + f"cf rt {app} --command '{task}' --wait --name 'Run Migrations'", + echo=True, + warn=True, + ) + if not migrations.ok: + print("Failed to run migrations. Check logs.") + return False + print("Successfully ran migrations.") + + # Stop migrator app + stop_app = ctx.run( + f"cf stop {app}", + echo=True, + warn=True + ) + if not stop_app.ok: + print(f"Failed to stop migrator app - f{app}") + return False + print("Migrator app halted successfully.") + + print("Migration process has finished successfully.") + return True + + @task def deploy(ctx, space=None, branch=None, login=False, help=False): """Deploy app to Cloud Foundry. @@ -187,6 +230,13 @@ def deploy(ctx, space=None, branch=None, login=False, help=False): with open(".cfmeta", "w") as fp: json.dump({"user": os.getenv("USER"), "branch": branch}, fp) + # Runs migrations + # tasks.py does not continue until the migrations task has completed + migrations_successful = _do_migrations(ctx, space) + if not migrations_successful: + print("Migrations process failed. Stopping deploy.") + sys.exit(1) + for app in [APP_NAME, WEB_SERVICES_NAME]: new_deploy = _do_deploy(ctx, space, app) From c70777d062274477a4c7d132f84ad319e1b3998e Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 09:47:47 -0500 Subject: [PATCH 02/26] Updates app names in new manifest files --- manifests/manifest-dev-migrator.yml | 2 +- manifests/manifest-prod-migrator.yml | 2 +- manifests/manifest-stage-migrator.yml | 2 +- manifests/manifest-test-migrator.yml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/manifests/manifest-dev-migrator.yml b/manifests/manifest-dev-migrator.yml index 36449de2b0..1789becca9 100644 --- a/manifests/manifest-dev-migrator.yml +++ b/manifests/manifest-dev-migrator.yml @@ -1,6 +1,6 @@ --- applications: - - name: fecfile-web-api + - name: fecfile-api-migrator instances: 1 stack: cflinuxfs4 buildpacks: diff --git a/manifests/manifest-prod-migrator.yml b/manifests/manifest-prod-migrator.yml index 1ebaf8a1c5..5e58213199 100644 --- a/manifests/manifest-prod-migrator.yml +++ b/manifests/manifest-prod-migrator.yml @@ -1,6 +1,6 @@ --- applications: - - name: fecfile-web-api + - name: fecfile-api-migrator instances: 1 stack: cflinuxfs4 buildpacks: diff --git a/manifests/manifest-stage-migrator.yml b/manifests/manifest-stage-migrator.yml index c24db24102..570f61ec28 100644 --- a/manifests/manifest-stage-migrator.yml +++ b/manifests/manifest-stage-migrator.yml @@ -1,6 +1,6 @@ --- applications: - - name: fecfile-web-api + - name: fecfile-api-migrator instances: 1 stack: cflinuxfs4 buildpacks: diff --git a/manifests/manifest-test-migrator.yml b/manifests/manifest-test-migrator.yml index f028809ba0..4416387564 100644 --- a/manifests/manifest-test-migrator.yml +++ b/manifests/manifest-test-migrator.yml @@ -1,6 +1,6 @@ --- applications: - - name: fecfile-web-api + - name: fecfile-api-migrator instances: 1 stack: cflinuxfs4 buildpacks: From 3311227464600e6b22a9710aa89e7c7228e871d4 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 09:55:33 -0500 Subject: [PATCH 03/26] Updates app name for consistency --- tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.py b/tasks.py index 6c271d366c..bba53458df 100644 --- a/tasks.py +++ b/tasks.py @@ -154,7 +154,7 @@ def _do_migrations(ctx, space): print("Running migrations...") manifest_filename = f"manifests/manifest-{space}-migrator.yml" - app = f"fecfile-api-migrator-{space}" + app = f"fecfile-api-migrator" # Start migrator app cmd = "push" From cca92297f508176ff7b2e302272a0185db346ce5 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 10:16:08 -0500 Subject: [PATCH 04/26] Updates deploy process to delete the migrator app after its work is done instead of stopping it --- manifests/manifest-dev-migrator.yml | 2 +- manifests/manifest-prod-migrator.yml | 2 +- manifests/manifest-stage-migrator.yml | 2 +- manifests/manifest-test-migrator.yml | 2 +- tasks.py | 22 ++++++++++------------ 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/manifests/manifest-dev-migrator.yml b/manifests/manifest-dev-migrator.yml index 1789becca9..4f4b5c27f7 100644 --- a/manifests/manifest-dev-migrator.yml +++ b/manifests/manifest-dev-migrator.yml @@ -6,7 +6,7 @@ applications: buildpacks: - python_buildpack command: bin/run-api.sh - memory: 1G + memory: 2G services: - fecfile-api-rds - fecfile-api-s3 diff --git a/manifests/manifest-prod-migrator.yml b/manifests/manifest-prod-migrator.yml index 5e58213199..a1e17ae673 100644 --- a/manifests/manifest-prod-migrator.yml +++ b/manifests/manifest-prod-migrator.yml @@ -6,7 +6,7 @@ applications: buildpacks: - python_buildpack command: bin/run-api.sh - memory: 1G + memory: 2G services: - fecfile-api-rds - fecfile-api-s3 diff --git a/manifests/manifest-stage-migrator.yml b/manifests/manifest-stage-migrator.yml index 570f61ec28..86f5a35514 100644 --- a/manifests/manifest-stage-migrator.yml +++ b/manifests/manifest-stage-migrator.yml @@ -6,7 +6,7 @@ applications: buildpacks: - python_buildpack command: bin/run-api.sh - memory: 1G + memory: 2G services: - fecfile-api-rds - fecfile-api-s3 diff --git a/manifests/manifest-test-migrator.yml b/manifests/manifest-test-migrator.yml index 4416387564..220eb6668c 100644 --- a/manifests/manifest-test-migrator.yml +++ b/manifests/manifest-test-migrator.yml @@ -6,7 +6,7 @@ applications: buildpacks: - python_buildpack command: bin/run-api.sh - memory: 1G + memory: 2G services: - fecfile-api-rds - fecfile-api-s3 diff --git a/tasks.py b/tasks.py index bba53458df..121b33f2bc 100644 --- a/tasks.py +++ b/tasks.py @@ -151,15 +151,13 @@ def _rollback(ctx, app): def _do_migrations(ctx, space): + migrator_app = 'fecfile-api-migrator' print("Running migrations...") - manifest_filename = f"manifests/manifest-{space}-migrator.yml" - app = f"fecfile-api-migrator" - # Start migrator app - cmd = "push" + manifest_filename = f"manifests/manifest-{space}-migrator.yml" migrator = ctx.run( - f"cf {cmd} {app} -f {manifest_filename}", + f"cf push {migrator_app} -f {manifest_filename}", echo=True, warn=True, ) @@ -170,7 +168,7 @@ def _do_migrations(ctx, space): # Run migrations task = 'django-backend/manage.py migrate --no-input --traceback --verbosity 3' migrations = ctx.run( - f"cf rt {app} --command '{task}' --wait --name 'Run Migrations'", + f"cf rt {migrator_app} --command '{task}' --wait --name 'Run Migrations'", echo=True, warn=True, ) @@ -179,16 +177,16 @@ def _do_migrations(ctx, space): return False print("Successfully ran migrations.") - # Stop migrator app - stop_app = ctx.run( - f"cf stop {app}", + # Delete migrator app + delete_app = ctx.run( + f"cf delete {migrator_app} -f", echo=True, warn=True ) - if not stop_app.ok: - print(f"Failed to stop migrator app - f{app}") + if not delete_app.ok: + print(f"Failed to delete migrator app - f{migrator_app}") return False - print("Migrator app halted successfully.") + print("Migrator app deleted successfully.") print("Migration process has finished successfully.") return True From 43ecdfa67a47846d44943d071824ebf423620d96 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 10:26:29 -0500 Subject: [PATCH 05/26] Test deploy --- .circleci/config.yml | 2 +- tasks.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index ea972ad9fd..96db5c010a 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -252,7 +252,7 @@ workflows: - test filters: branches: - only: /develop|release\/sprint-[\.\d]+|main/ + only: /feature\/1865/ - docs-build: requires: - test diff --git a/tasks.py b/tasks.py index 121b33f2bc..3918d64d0a 100644 --- a/tasks.py +++ b/tasks.py @@ -51,7 +51,7 @@ def _detect_space(repo, branch=None): DEPLOY_RULES = ( ("test", lambda _, branch: branch == "main"), ("stage", lambda _, branch: branch.startswith("release")), - ("dev", lambda _, branch: branch == "develop"), + ("dev", lambda _, branch: branch == "feature/1865"), ) From 833408528eed06a9476a74f8442e38d7922ace65 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 10:42:07 -0500 Subject: [PATCH 06/26] Updates flag order on cf rt --- tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.py b/tasks.py index 3918d64d0a..b25f19f8d8 100644 --- a/tasks.py +++ b/tasks.py @@ -168,7 +168,7 @@ def _do_migrations(ctx, space): # Run migrations task = 'django-backend/manage.py migrate --no-input --traceback --verbosity 3' migrations = ctx.run( - f"cf rt {migrator_app} --command '{task}' --wait --name 'Run Migrations'", + f"cf rt {migrator_app} --wait --command '{task}' --name 'Run Migrations'", echo=True, warn=True, ) From c56e82b20ccbc33ff93cb97576a8aae512683ee2 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 10:51:57 -0500 Subject: [PATCH 07/26] Updates flag order again to an order that passes manual testing --- tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.py b/tasks.py index b25f19f8d8..94cee167b5 100644 --- a/tasks.py +++ b/tasks.py @@ -168,7 +168,7 @@ def _do_migrations(ctx, space): # Run migrations task = 'django-backend/manage.py migrate --no-input --traceback --verbosity 3' migrations = ctx.run( - f"cf rt {migrator_app} --wait --command '{task}' --name 'Run Migrations'", + f"cf rt {migrator_app} --command '{task}' --name 'Run Migrations' --wait", echo=True, warn=True, ) From a673b02f1d283e2816eedd61330a787e975efc30 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 11:00:56 -0500 Subject: [PATCH 08/26] Prints cf version to logs --- tasks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tasks.py b/tasks.py index 94cee167b5..e98c99e6f2 100644 --- a/tasks.py +++ b/tasks.py @@ -166,6 +166,7 @@ def _do_migrations(ctx, space): return False # Run migrations + ctx.run('cf --version', echo=True) task = 'django-backend/manage.py migrate --no-input --traceback --verbosity 3' migrations = ctx.run( f"cf rt {migrator_app} --command '{task}' --name 'Run Migrations' --wait", From be6e7dfabf84b0c7e27df26d87060e1c25391b6a Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 11:27:46 -0500 Subject: [PATCH 09/26] Updates version of CF CLI used by Circle when deploying --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 96db5c010a..2c43b4db7e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -147,7 +147,7 @@ jobs: command: | mkdir -p $HOME/bin export PATH=$HOME/bin:$PATH - curl -L "https://cli.run.pivotal.io/stable?release=linux64-binary&version=7.1.0" | tar xzv -C $HOME/bin + curl -L "https://cli.run.pivotal.io/stable?release=linux64-binary&version=7.3.0" | tar xzv -C $HOME/bin - run: name: Deploy API From f7d8841eae40804c3bd486c9d358299eccf00c2b Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 11:56:52 -0500 Subject: [PATCH 10/26] Removes CF CLI version printing, it was only there for debugging --- tasks.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tasks.py b/tasks.py index e98c99e6f2..94cee167b5 100644 --- a/tasks.py +++ b/tasks.py @@ -166,7 +166,6 @@ def _do_migrations(ctx, space): return False # Run migrations - ctx.run('cf --version', echo=True) task = 'django-backend/manage.py migrate --no-input --traceback --verbosity 3' migrations = ctx.run( f"cf rt {migrator_app} --command '{task}' --name 'Run Migrations' --wait", From b70e933eddf31cf6a941dff9b0af0c0bb2799e81 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 11:57:22 -0500 Subject: [PATCH 11/26] Adds a test migration (will have to be manually reverted afterwards) --- .../user/migrations/0007_test_migration.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 django-backend/fecfiler/user/migrations/0007_test_migration.py diff --git a/django-backend/fecfiler/user/migrations/0007_test_migration.py b/django-backend/fecfiler/user/migrations/0007_test_migration.py new file mode 100644 index 0000000000..bf313048f5 --- /dev/null +++ b/django-backend/fecfiler/user/migrations/0007_test_migration.py @@ -0,0 +1,21 @@ +# Generated by Django 5.1.4 on 2025-01-08 16:54 + +from django.db import migrations + + +def do_nothing(apps, schema_editor): + print("We're truly doing nothing here") + + +class Migration(migrations.Migration): + + dependencies = [ + ('user', '0006_remove_old_login_accounts'), + ] + + operations = [ + migrations.RunPython( + do_nothing, + migrations.RunPython.noop, + ), + ] From d3c2e6f2c5b0624e88d9b8541df86472b60f9625 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 12:29:49 -0500 Subject: [PATCH 12/26] Adds intentionally broken migration for testing purposes --- .../{0007_test_migration.py => 0007_test_broken_migration.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename django-backend/fecfiler/user/migrations/{0007_test_migration.py => 0007_test_broken_migration.py} (89%) diff --git a/django-backend/fecfiler/user/migrations/0007_test_migration.py b/django-backend/fecfiler/user/migrations/0007_test_broken_migration.py similarity index 89% rename from django-backend/fecfiler/user/migrations/0007_test_migration.py rename to django-backend/fecfiler/user/migrations/0007_test_broken_migration.py index bf313048f5..40ca8ebb16 100644 --- a/django-backend/fecfiler/user/migrations/0007_test_migration.py +++ b/django-backend/fecfiler/user/migrations/0007_test_broken_migration.py @@ -4,7 +4,7 @@ def do_nothing(apps, schema_editor): - print("We're truly doing nothing here") + raise ValueError class Migration(migrations.Migration): From b646111ebe179351dc4f21893326d970accdd908 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 12:35:24 -0500 Subject: [PATCH 13/26] Temporarily disabling migration testing so that I can test deploy a broken migration --- .circleci/config.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2c43b4db7e..c70484e87b 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -65,11 +65,11 @@ jobs: python manage.py lintmigrations --git-commit-id d73068e23fc5b035af2b224b16d4726b7b20d67c --project-root-path '.' working_directory: ~/project/django-backend/ - - run: - name: Run migrations - command: | - python manage.py migrate --no-input --traceback --verbosity 3 - working_directory: ~/project/django-backend/ + # - run: + # name: Run migrations + # command: | + # python manage.py migrate --no-input --traceback --verbosity 3 + # working_directory: ~/project/django-backend/ - run: name: Run lint From d361692a1c28d4f8282ced9c9f7b620807b8258a Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 13:00:09 -0500 Subject: [PATCH 14/26] Reverting testing changes --- .../migrations/0007_test_broken_migration.py | 21 ------------------- 1 file changed, 21 deletions(-) delete mode 100644 django-backend/fecfiler/user/migrations/0007_test_broken_migration.py diff --git a/django-backend/fecfiler/user/migrations/0007_test_broken_migration.py b/django-backend/fecfiler/user/migrations/0007_test_broken_migration.py deleted file mode 100644 index 40ca8ebb16..0000000000 --- a/django-backend/fecfiler/user/migrations/0007_test_broken_migration.py +++ /dev/null @@ -1,21 +0,0 @@ -# Generated by Django 5.1.4 on 2025-01-08 16:54 - -from django.db import migrations - - -def do_nothing(apps, schema_editor): - raise ValueError - - -class Migration(migrations.Migration): - - dependencies = [ - ('user', '0006_remove_old_login_accounts'), - ] - - operations = [ - migrations.RunPython( - do_nothing, - migrations.RunPython.noop, - ), - ] From feb843feefad84ddf297b41baf2356dc948290f5 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Wed, 8 Jan 2025 13:59:54 -0500 Subject: [PATCH 15/26] Reverts to running migrations with circleCI testing --- .circleci/config.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index c70484e87b..2c43b4db7e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -65,11 +65,11 @@ jobs: python manage.py lintmigrations --git-commit-id d73068e23fc5b035af2b224b16d4726b7b20d67c --project-root-path '.' working_directory: ~/project/django-backend/ - # - run: - # name: Run migrations - # command: | - # python manage.py migrate --no-input --traceback --verbosity 3 - # working_directory: ~/project/django-backend/ + - run: + name: Run migrations + command: | + python manage.py migrate --no-input --traceback --verbosity 3 + working_directory: ~/project/django-backend/ - run: name: Run lint From 92ed9a907f52b0d005f214ed53da10d6209351a0 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 15:35:31 -0500 Subject: [PATCH 16/26] Testing trimmed manifest for migrator app --- manifests/manifest-dev-migrator.yml | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/manifests/manifest-dev-migrator.yml b/manifests/manifest-dev-migrator.yml index 4f4b5c27f7..a03a981fe3 100644 --- a/manifests/manifest-dev-migrator.yml +++ b/manifests/manifest-dev-migrator.yml @@ -12,24 +12,3 @@ applications: - fecfile-api-s3 - fecfile-api-redis - fecfile-api-creds-dev - env: - DISABLE_COLLECTSTATIC: 1 - DJANGO_SETTINGS_MODULE: fecfiler.settings.production - FFAPI_COOKIE_DOMAIN: fecfile.fec.gov - LOGIN_REDIRECT_CLIENT_URL: https://dev.fecfile.fec.gov - LOGIN_REDIRECT_SERVER_URL: https://dev-api.fecfile.fec.gov/api/v1/oidc/login-redirect - LOGOUT_REDIRECT_URL: https://dev-api.fecfile.fec.gov/api/v1/oidc/logout-redirect - PRODUCTION_OPEN_FEC_API: https://api.open.fec.gov/v1/ - STAGE_OPEN_FEC_API: https://api-stage.open.fec.gov/v1/ - SESSION_COOKIE_AGE: 64800 - BP_PIP_VERSION: latest - LOG_FORMAT: KEY_VALUE - MOCK_EFO: True - SYSTEM_STATUS_CACHE_AGE: 60 - INITIAL_POLLING_INTERVAL: 10 - INITIAL_POLLING_DURATION: 900 - SECONDARY_POLLING_INTERVAL: 3600 - SECONDARY_POLLING_DURATION: 86400 - # ---- FEATURE FLAGS ---- - FLAG__COMMITTEE_DATA_SOURCE: TEST # Values are PRODUCTION, TEST, MOCKED - ENABLE_DEVELOPER_COMMANDS: True From 5b246a117b689067589add19ba97b3ad0f912cc0 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 15:53:45 -0500 Subject: [PATCH 17/26] Tests using the standard manifests for each space instead of new migrator manifests --- .circleci/config.yml | 2 +- manifests/manifest-dev-migrator.yml | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2c43b4db7e..1ace9d38ea 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -252,7 +252,7 @@ workflows: - test filters: branches: - only: /feature\/1865/ + only: /develop|release\/sprint-[\.\d]+|main/ - docs-build: requires: - test diff --git a/manifests/manifest-dev-migrator.yml b/manifests/manifest-dev-migrator.yml index a03a981fe3..4f4b5c27f7 100644 --- a/manifests/manifest-dev-migrator.yml +++ b/manifests/manifest-dev-migrator.yml @@ -12,3 +12,24 @@ applications: - fecfile-api-s3 - fecfile-api-redis - fecfile-api-creds-dev + env: + DISABLE_COLLECTSTATIC: 1 + DJANGO_SETTINGS_MODULE: fecfiler.settings.production + FFAPI_COOKIE_DOMAIN: fecfile.fec.gov + LOGIN_REDIRECT_CLIENT_URL: https://dev.fecfile.fec.gov + LOGIN_REDIRECT_SERVER_URL: https://dev-api.fecfile.fec.gov/api/v1/oidc/login-redirect + LOGOUT_REDIRECT_URL: https://dev-api.fecfile.fec.gov/api/v1/oidc/logout-redirect + PRODUCTION_OPEN_FEC_API: https://api.open.fec.gov/v1/ + STAGE_OPEN_FEC_API: https://api-stage.open.fec.gov/v1/ + SESSION_COOKIE_AGE: 64800 + BP_PIP_VERSION: latest + LOG_FORMAT: KEY_VALUE + MOCK_EFO: True + SYSTEM_STATUS_CACHE_AGE: 60 + INITIAL_POLLING_INTERVAL: 10 + INITIAL_POLLING_DURATION: 900 + SECONDARY_POLLING_INTERVAL: 3600 + SECONDARY_POLLING_DURATION: 86400 + # ---- FEATURE FLAGS ---- + FLAG__COMMITTEE_DATA_SOURCE: TEST # Values are PRODUCTION, TEST, MOCKED + ENABLE_DEVELOPER_COMMANDS: True From bd8fef8d23f633e6faf6d812bbfaa3d9c93243ab Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 15:59:52 -0500 Subject: [PATCH 18/26] Test deploy --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 1ace9d38ea..2c43b4db7e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -252,7 +252,7 @@ workflows: - test filters: branches: - only: /develop|release\/sprint-[\.\d]+|main/ + only: /feature\/1865/ - docs-build: requires: - test From b1267f8f3042944ae0e7dd5aad4b8f11d57b3d46 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 16:00:18 -0500 Subject: [PATCH 19/26] Tests using the standard manifests for each space instead of new migrator manifests --- tasks.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tasks.py b/tasks.py index 94cee167b5..528499d33e 100644 --- a/tasks.py +++ b/tasks.py @@ -51,7 +51,7 @@ def _detect_space(repo, branch=None): DEPLOY_RULES = ( ("test", lambda _, branch: branch == "main"), ("stage", lambda _, branch: branch.startswith("release")), - ("dev", lambda _, branch: branch == "feature/1865"), + ("dev", lambda _, branch: branch == "develop"), ) @@ -88,8 +88,12 @@ def _login_to_cf(ctx, space): exit(1) +def _get_manifest_filename(space, app): + return f"manifests/manifest-{space}-{MANIFEST_LABEL.get(app)}.yml" + + def _do_deploy(ctx, space, app): - manifest_filename = f"manifests/manifest-{space}-{MANIFEST_LABEL.get(app)}.yml" + manifest_filename = _get_manifest_filename(space, app) existing_deploy = ctx.run(f"cf app {app}", echo=True, warn=True) print("\n") @@ -150,12 +154,12 @@ def _rollback(ctx, app): print("Unable to cancel deploy. Check logs.") -def _do_migrations(ctx, space): +def _run_migrations(ctx, space, app): migrator_app = 'fecfile-api-migrator' print("Running migrations...") # Start migrator app - manifest_filename = f"manifests/manifest-{space}-migrator.yml" + manifest_filename = _get_manifest_filename(space, app) migrator = ctx.run( f"cf push {migrator_app} -f {manifest_filename}", echo=True, @@ -230,7 +234,7 @@ def deploy(ctx, space=None, branch=None, login=False, help=False): # Runs migrations # tasks.py does not continue until the migrations task has completed - migrations_successful = _do_migrations(ctx, space) + migrations_successful = _run_migrations(ctx, space, APP_NAME) if not migrations_successful: print("Migrations process failed. Stopping deploy.") sys.exit(1) From 02b60dcf3594c4f53397a5047e3ae209f0f7fcd6 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 16:00:55 -0500 Subject: [PATCH 20/26] Test deploy --- tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.py b/tasks.py index 528499d33e..0c9096598a 100644 --- a/tasks.py +++ b/tasks.py @@ -51,7 +51,7 @@ def _detect_space(repo, branch=None): DEPLOY_RULES = ( ("test", lambda _, branch: branch == "main"), ("stage", lambda _, branch: branch.startswith("release")), - ("dev", lambda _, branch: branch == "develop"), + ("dev", lambda _, branch: branch == "feature/1865"), ) From 47cec6a3eef111d0000121c311049c9ee77576d2 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 17:19:10 -0500 Subject: [PATCH 21/26] Reverts to using separate manifests. Reverts to standard deploy rules. Attempts to clean up after itself when migrations fail. --- tasks.py | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/tasks.py b/tasks.py index 0c9096598a..f523b942bc 100644 --- a/tasks.py +++ b/tasks.py @@ -51,7 +51,7 @@ def _detect_space(repo, branch=None): DEPLOY_RULES = ( ("test", lambda _, branch: branch == "main"), ("stage", lambda _, branch: branch.startswith("release")), - ("dev", lambda _, branch: branch == "feature/1865"), + ("dev", lambda _, branch: branch == "develop"), ) @@ -88,12 +88,8 @@ def _login_to_cf(ctx, space): exit(1) -def _get_manifest_filename(space, app): - return f"manifests/manifest-{space}-{MANIFEST_LABEL.get(app)}.yml" - - def _do_deploy(ctx, space, app): - manifest_filename = _get_manifest_filename(space, app) + manifest_filename = f"manifests/manifest-{space}-{MANIFEST_LABEL.get(app)}.yml" existing_deploy = ctx.run(f"cf app {app}", echo=True, warn=True) print("\n") @@ -154,12 +150,25 @@ def _rollback(ctx, app): print("Unable to cancel deploy. Check logs.") -def _run_migrations(ctx, space, app): - migrator_app = 'fecfile-api-migrator' +def _delete_migrator_app(ctx, migrator_app): + print("Deleting migrator app...") + delete_app = ctx.run( + f"cf delete {migrator_app} -f", + echo=True, + warn=True + ) + if not delete_app.ok: + print(f"Failed to delete migrator app - {migrator_app}") + return False + print("Migrator app deleted successfully.") + return True + + +def _run_migrations(ctx, space, migrator_app): print("Running migrations...") # Start migrator app - manifest_filename = _get_manifest_filename(space, app) + manifest_filename = f"manifests/manifest-{space}-migrator.yml" migrator = ctx.run( f"cf push {migrator_app} -f {manifest_filename}", echo=True, @@ -179,18 +188,6 @@ def _run_migrations(ctx, space, app): if not migrations.ok: print("Failed to run migrations. Check logs.") return False - print("Successfully ran migrations.") - - # Delete migrator app - delete_app = ctx.run( - f"cf delete {migrator_app} -f", - echo=True, - warn=True - ) - if not delete_app.ok: - print(f"Failed to delete migrator app - f{migrator_app}") - return False - print("Migrator app deleted successfully.") print("Migration process has finished successfully.") return True @@ -234,9 +231,13 @@ def deploy(ctx, space=None, branch=None, login=False, help=False): # Runs migrations # tasks.py does not continue until the migrations task has completed - migrations_successful = _run_migrations(ctx, space, APP_NAME) - if not migrations_successful: - print("Migrations process failed. Stopping deploy.") + migrator_app = 'fecfile-api-migrator' + migrations_successful = _run_migrations(ctx, space, migrator_app) + migrator_app_deleted = _delete_migrator_app(ctx, migrator_app) + + if not (migrations_successful and migrator_app_deleted): + print("Migrations failed and/or the migrator app was not deleted successfully.") + print("Canceling deploy...") sys.exit(1) for app in [APP_NAME, WEB_SERVICES_NAME]: From 2a799c912c00631c54ce31e36e05a82614e44c12 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 17:19:48 -0500 Subject: [PATCH 22/26] Reverts to standard deploy rules --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2c43b4db7e..1ace9d38ea 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -252,7 +252,7 @@ workflows: - test filters: branches: - only: /feature\/1865/ + only: /develop|release\/sprint-[\.\d]+|main/ - docs-build: requires: - test From 56a5760bf5830ea88e2064260f10aa9552d091e1 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 17:36:10 -0500 Subject: [PATCH 23/26] Made the migrator app deletion a little safer and more informative --- tasks.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/tasks.py b/tasks.py index f523b942bc..aed21c4691 100644 --- a/tasks.py +++ b/tasks.py @@ -150,15 +150,27 @@ def _rollback(ctx, app): print("Unable to cancel deploy. Check logs.") -def _delete_migrator_app(ctx, migrator_app): +def _delete_migrator_app(ctx, space, migrator_app): print("Deleting migrator app...") + + existing_migrator_app = ctx.run(f"cf app {migrator_app}", echo=True, warn=True) + if not existing_migrator_app.ok: + print("No migrator app detected. There is nothing to delete.") + return True + + if migrator_app == APP_NAME: + print(f"Possible error: could result in deleting main app - {APP_NAME}") + print("Canceling migrator app deletion attempt.") + return False + delete_app = ctx.run( f"cf delete {migrator_app} -f", echo=True, warn=True ) if not delete_app.ok: - print(f"Failed to delete migrator app - {migrator_app}") + print('Failed to delete migrator app.') + print(f'Stray migrator app remains on {space}: "{migrator_app}"') return False print("Migrator app deleted successfully.") return True @@ -231,13 +243,13 @@ def deploy(ctx, space=None, branch=None, login=False, help=False): # Runs migrations # tasks.py does not continue until the migrations task has completed - migrator_app = 'fecfile-api-migrator' + migrator_app = 'fecfile-api-migrator' # warning: the app with this name will get deleted migrations_successful = _run_migrations(ctx, space, migrator_app) - migrator_app_deleted = _delete_migrator_app(ctx, migrator_app) + migrator_app_deleted = _delete_migrator_app(ctx, space, migrator_app) if not (migrations_successful and migrator_app_deleted): print("Migrations failed and/or the migrator app was not deleted successfully.") - print("Canceling deploy...") + print("See the logs for more information.\nCanceling deploy...") sys.exit(1) for app in [APP_NAME, WEB_SERVICES_NAME]: From a4642a7abd14b32650635dfc38877a8766092e11 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 17:37:16 -0500 Subject: [PATCH 24/26] Test deploy --- .circleci/config.yml | 2 +- tasks.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 1ace9d38ea..2c43b4db7e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -252,7 +252,7 @@ workflows: - test filters: branches: - only: /develop|release\/sprint-[\.\d]+|main/ + only: /feature\/1865/ - docs-build: requires: - test diff --git a/tasks.py b/tasks.py index aed21c4691..472813cd5e 100644 --- a/tasks.py +++ b/tasks.py @@ -51,7 +51,7 @@ def _detect_space(repo, branch=None): DEPLOY_RULES = ( ("test", lambda _, branch: branch == "main"), ("stage", lambda _, branch: branch.startswith("release")), - ("dev", lambda _, branch: branch == "develop"), + ("dev", lambda _, branch: branch == "feature/1865"), ) From 91edc3149ed22233c05a049667b66a6298a1a9eb Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 17:39:27 -0500 Subject: [PATCH 25/26] Linting... --- tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks.py b/tasks.py index 472813cd5e..8ca50bf86e 100644 --- a/tasks.py +++ b/tasks.py @@ -243,7 +243,7 @@ def deploy(ctx, space=None, branch=None, login=False, help=False): # Runs migrations # tasks.py does not continue until the migrations task has completed - migrator_app = 'fecfile-api-migrator' # warning: the app with this name will get deleted + migrator_app = 'fecfile-api-migrator' # the app with this name will get deleted! migrations_successful = _run_migrations(ctx, space, migrator_app) migrator_app_deleted = _delete_migrator_app(ctx, space, migrator_app) From 199e520a4666a90803b527d25ada037722a6fa72 Mon Sep 17 00:00:00 2001 From: Elaine Krauss Date: Thu, 9 Jan 2025 17:51:19 -0500 Subject: [PATCH 26/26] Reverts to standard deploy rules --- .circleci/config.yml | 2 +- tasks.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2c43b4db7e..1ace9d38ea 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -252,7 +252,7 @@ workflows: - test filters: branches: - only: /feature\/1865/ + only: /develop|release\/sprint-[\.\d]+|main/ - docs-build: requires: - test diff --git a/tasks.py b/tasks.py index 8ca50bf86e..4604b5ee07 100644 --- a/tasks.py +++ b/tasks.py @@ -51,7 +51,7 @@ def _detect_space(repo, branch=None): DEPLOY_RULES = ( ("test", lambda _, branch: branch == "main"), ("stage", lambda _, branch: branch.startswith("release")), - ("dev", lambda _, branch: branch == "feature/1865"), + ("dev", lambda _, branch: branch == "develop"), )