-
Notifications
You must be signed in to change notification settings - Fork 9
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 #4690 from GSA-TTS/main
- Loading branch information
Showing
16 changed files
with
254 additions
and
22 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
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
72 changes: 72 additions & 0 deletions
72
backend/curation/management/commands/fix_migrationinspectionrecord.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,72 @@ | ||
from django.core.management.base import BaseCommand | ||
from dissemination.models import MigrationInspectionRecord | ||
from config.settings import ENVIRONMENT, GSA_MIGRATION | ||
from django.db.models import Q | ||
|
||
import logging | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.WARNING) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = """ | ||
Replace 'GSA_MIGRATION' with '' in policies_content and rate_content | ||
of census_data in a note in dissemination_migrationinspectionrecord | ||
Usage: | ||
manage.py update_migrationinspectionrecord | ||
--year <audit year> | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--year", help="Year(2016 through 2022)", type=str, default="2022" | ||
) | ||
|
||
def is_year_invalid(self, year): | ||
valid_years = ["2016", "2017", "2018", "2019", "2020", "2021", "2022"] | ||
return year not in valid_years | ||
|
||
def handle(self, *args, **options): | ||
if ENVIRONMENT not in [ | ||
"LOCAL", | ||
"DEVELOPMENT", | ||
"PREVIEW", | ||
"STAGING", | ||
"PRODUCTION", | ||
]: | ||
print(f"Environment is not as expected, ENVIRONMENT={ENVIRONMENT}") | ||
return | ||
|
||
year = options.get("year") | ||
if self.is_year_invalid(year): | ||
print( | ||
f"Invalid year {year}. Expecting 2016 / 2017 / 2018 / 2019 / 2020 / 2021 / 2022" | ||
) | ||
return | ||
|
||
migrationinspectionrecords = MigrationInspectionRecord.objects.filter( | ||
Q(audit_year=year) | ||
) | ||
print(f"Count of {year} submissions: {len(migrationinspectionrecords)}") | ||
|
||
count = 0 | ||
for migrationinspectionrecord in migrationinspectionrecords: | ||
notes = [] | ||
is_updated = False | ||
for note in migrationinspectionrecord.note: | ||
if ( | ||
note[0]["transformation_functions"][0] | ||
== "xform_missing_notes_records" | ||
) & (note[0]["census_data"][0]["value"] == GSA_MIGRATION): | ||
note[0]["census_data"][0]["value"] = "" | ||
is_updated = True | ||
notes += [note] | ||
if is_updated: | ||
migrationinspectionrecord.note = notes | ||
migrationinspectionrecord.save() | ||
count += 1 | ||
|
||
print("Number of records updated = ", count) |
80 changes: 80 additions & 0 deletions
80
backend/support/management/commands/delete_stale_backups.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,80 @@ | ||
# Usage: | ||
# Do a delete: python manage.py delete_stale_backups --days X --delete true | ||
# List objects: python manage.py delete_stale_backups --days X | ||
|
||
import boto3 | ||
from datetime import datetime, timezone, timedelta | ||
from django.conf import settings | ||
from django.core.management.base import BaseCommand | ||
import sys | ||
|
||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--days", | ||
type=int, | ||
required=True, | ||
help="Max age a key(file) in days can have before we want to delete it. Value must be (14) or greater.", | ||
) | ||
parser.add_argument( | ||
"--delete", | ||
required=False, | ||
default=False, | ||
help="True/False. Actually do a delete. If not specified, just list the keys found that match.", | ||
) | ||
|
||
def handle(self, *args, **options): | ||
days = options["days"] | ||
delete = options["delete"] | ||
|
||
if days < 14: | ||
print( | ||
"Days cannot less than 14 to prevent up-to-date backups from being deleted. Exiting..." | ||
) | ||
sys.exit(1) | ||
|
||
s3_client = boto3.client( | ||
"s3", | ||
aws_access_key_id=settings.AWS_BACKUPS_ACCESS_KEY_ID, | ||
aws_secret_access_key=settings.AWS_BACKUPS_SECRET_ACCESS_KEY, | ||
endpoint_url=settings.AWS_S3_BACKUPS_ENDPOINT_URL, | ||
) | ||
|
||
paginator = s3_client.get_paginator("list_objects_v2") | ||
pages = paginator.paginate( | ||
Bucket=settings.AWS_BACKUPS_STORAGE_BUCKET_NAME, Prefix="backups/" | ||
) | ||
|
||
delete_older_than = datetime.now(timezone.utc) - timedelta(days=days) | ||
total_count = 0 | ||
for page in pages: | ||
if "Contents" in page: | ||
for obj in page["Contents"]: | ||
|
||
# Get the last modified date of the object | ||
last_modified = obj["LastModified"] | ||
|
||
# If the object is older than one week, delete it | ||
# s3_client.delete_object(Bucket=settings.AWS_STORAGE_BUCKET_NAME, Key=f"backups/{item.file.name}") | ||
if delete: | ||
if last_modified < delete_older_than: | ||
print( | ||
f"Deleting {obj['Key']} last modified on {last_modified}" | ||
) | ||
s3_client.delete_object( | ||
Bucket=settings.AWS_BACKUPS_STORAGE_BUCKET_NAME, | ||
Key=obj["Key"], | ||
) | ||
total_count += 1 | ||
else: | ||
print( | ||
f"Object {obj['Key']} younger than {delete_older_than}. Not deleting." | ||
) | ||
else: | ||
print( | ||
f"Delete not sent. {obj['Key']} was last modified on {last_modified}" | ||
) | ||
else: | ||
print("No objects found in the bucket.") | ||
print(f"Total number of objects deleted: {total_count}") |
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,53 @@ | ||
### Informational document regarding Management Command "delete_stale_backups" | ||
|
||
The purpose of this document is to highlight examples for when a developer wishes to delete stale backups from the s3 bucket `backups`. | ||
|
||
**Warning:** This command is classified as a destructive command, and should only be run after receiving confirmation from members of the team, and after putting a formal annoucement in the developer slack channel. It is advised that after this command is run, to take a formal backup of the environment just for extra precautions. | ||
|
||
#### Information: | ||
The management command is located here: [delete_stale_backups.py](../backend/support/management/commands/delete_stale_backups.py). This command accepts two inputs. `--days` & `--delete`. | ||
- The value of `--days` must be greater than or equal to `14` (`--days 14`) | ||
- The value of `--delete` is required to actually perform the delete, and is a boolean (`--delete true`) | ||
- The full command to perform a delete will look like this: | ||
`python manage.py delete_stale_backups --days 14 --delete true` | ||
|
||
#### How to perform a delete | ||
1. Login to cloud.gov `cf login -a api.fr.cloud.gov --sso` | ||
2. Select the target environment if you have not done so after successful authentication `cf t -s <env>` | ||
3. Open a new terminal and tail the logs `cf logs gsa-fac | grep "delete_stale_backups"` | ||
4. Run the command via tasks: | ||
`cf run-task gsa-fac -k 2G -m 3G --name delete_stale_backups --command "python manage.py delete_stale_backups --days 14 --delete true" --wait` | ||
5. Wait for the command to finish. | ||
6. Navigate to [The backup environment action](https://github.com/GSA-TTS/FAC/actions/workflows/fac-backup-util.yml) and perform a backup with the following inputs or alternatively, navigate to [the scheduled backup action](https://github.com/GSA-TTS/FAC/actions/workflows/fac-backup-scheduler.yml) and run. | ||
```sh | ||
branch: main | ||
environment: <env where backups were just deleted (dev/staging/prod)> | ||
version: v0.1.11 | ||
operation: on_demand_backup | ||
``` | ||
|
||
#### Operation outputs examples (Fail): | ||
``` | ||
~$ python manage.py delete_stale_backups --days 13 | ||
Days cannot less than 14 to prevent up-to-date backups from being deleted. Exiting... | ||
~$ | ||
~$ python manage.py delete_stale_backups --days 0 --delete true | ||
Days cannot less than 14 to prevent up-to-date backups from being deleted. Exiting... | ||
~$ | ||
~$ python manage.py delete_stale_backups --days 14 --delete true | ||
Object backups/on-demand/02-04-13/public-audit_access.dump younger than 2025-01-22 18:44:02.406263+00:00. Not deleting. | ||
Object backups/on-demand/02-04-13/public-audit_deletedaccess.dump younger than 2025-01-22 18:44:02.406263+00:00. Not deleting. | ||
[...] | ||
``` | ||
|
||
#### Operation outputs example (Pass): | ||
``` | ||
~$ python manage.py delete_stale_backups --days 14 --delete true | ||
Deleting backups/on-demand/02-03-19/public-audit_access.dump last modified on 2025-01-22 18:44:02.406263+00:00 | ||
Deleting backups/on-demand/02-03-19/public-audit_deletedaccess.dump last modified on 2025-01-22 18:44:02.406263+00:00 | ||
[...] | ||
``` | ||
|
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
Oops, something went wrong.