Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Delete data dump from S3 after data load has been successful #600

Merged
merged 57 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
f10ce2a
Removed database-copy image
antroy-madetech Oct 17, 2024
e1a3ed6
Moved new database copy docker image into the database-copy dir
antroy-madetech Oct 17, 2024
b2e3576
feat: Added dump file deletion from S3 at the end of the data load pr…
antroy-madetech Oct 17, 2024
5f37279
Added a copy command to chain the dump and load commands
antroy-madetech Oct 17, 2024
7462c7f
Refactoring of helper methods
antroy-madetech Oct 17, 2024
434f77b
Refactored the commands to rationalize arguments. Added required addi…
antroy-madetech Oct 17, 2024
eb1ef9d
Better messaging
antroy-madetech Oct 17, 2024
18ef5b0
Removed wait for task to stop function as we already check for a spec…
antroy-madetech Oct 17, 2024
a67dcbb
Can now run the commands from the non-prod AWS profile
antroy-madetech Oct 17, 2024
94960d6
Refactored the start task function into the DatabaseCopy class
antroy-madetech Oct 18, 2024
156cd26
Add missing type hints
antroy-madetech Oct 18, 2024
d93f946
Error handling for database not found
antroy-madetech Oct 21, 2024
6aa7cc7
Bad env case
antroy-madetech Oct 21, 2024
aec6c4d
Bad app case
antroy-madetech Oct 21, 2024
a22f4c4
Handling bad account id errors
antroy-madetech Oct 21, 2024
262a1ed
Added in error cases for the load command
antroy-madetech Oct 21, 2024
295f9d4
Inferring the account_id from the environment
antroy-madetech Oct 21, 2024
70bf629
Test refactor
antroy-madetech Oct 21, 2024
9e7839e
Refactored tests part 2
antroy-madetech Oct 21, 2024
4f38db6
Refactored tests part 3
antroy-madetech Oct 21, 2024
05668aa
Drop --account-id cli option
antroy-madetech Oct 21, 2024
a12ace7
Application can now be picked up from platform-config.yml
antroy-madetech Oct 22, 2024
05f5fbd
Added documentation to the commands
antroy-madetech Oct 22, 2024
15a6f96
Updated log message.
antroy-madetech Oct 22, 2024
e5bb6f9
Look up vpcs from deploy repo if possible
antroy-madetech Oct 22, 2024
ed41959
Make cli options consistent
antroy-madetech Oct 22, 2024
a871a38
Now handling errors in the commands and aborting
antroy-madetech Oct 22, 2024
7307022
Added database-copy buildspec
antroy-madetech Oct 22, 2024
8103066
Added auto-approve option and error handling of task errors
antroy-madetech Oct 23, 2024
61f3349
Remove the type hint that python 3.9 can't deal with
antroy-madetech Oct 23, 2024
3eb729f
Scale down and up around the restore command
antroy-madetech Oct 24, 2024
32368fc
Use AWS repo for the postgres image
antroy-madetech Oct 24, 2024
db7d086
Clean down database with sql rather than the --clean option
antroy-madetech Oct 24, 2024
4e92cc4
Add connection string to psql command
antroy-madetech Oct 24, 2024
8c76716
Change how psql is ingesting commands
antroy-madetech Oct 24, 2024
67766ff
Single quotes to suppress $$ expansion
antroy-madetech Oct 24, 2024
8503448
Move clear down sql into a file
antroy-madetech Oct 24, 2024
3afce56
Move data delete to immediately after the restore
antroy-madetech Oct 24, 2024
ec1f095
Handle errors and clean up
ksugden Oct 25, 2024
f230960
Make error handling work (jq swallowing errors)
antroy-madetech Oct 28, 2024
0821fe8
Added in the required CLUSTER_NAME env var for the load action
antroy-madetech Oct 28, 2024
8a26a9f
Changed message to a warning that the operation is destructive
antroy-madetech Oct 28, 2024
610c8a5
Wired in the online/offline functionality
antroy-madetech Oct 28, 2024
e225c8c
Enriching the vpc from config in the copy command
antroy-madetech Oct 28, 2024
3be8f59
Add `--no-maintenance-page` flag to copy command.
NickFitz Oct 29, 2024
1384485
Don't show maintenance page if `no_maintenance_page` is `True`.
NickFitz Oct 29, 2024
958f1d1
Black was unhappy :-/
NickFitz Oct 29, 2024
1aadf26
Document `--no-maintenance-page` flag.
NickFitz Oct 29, 2024
2db56c9
Undocument `--no-maintenance-page` flag.
NickFitz Oct 29, 2024
162415d
Initial refactoring
antroy-madetech Oct 29, 2024
7873453
Moved load balancer functions out into a provider file
antroy-madetech Oct 29, 2024
b22b8d8
Copied offline_command and related functions into domain file
antroy-madetech Oct 29, 2024
b99dd7a
Refactored online/offline out of the environment command
antroy-madetech Oct 29, 2024
f5bd1de
Use itertools.count to provide rule priorities.
NickFitz Oct 29, 2024
4d90dd5
Updated docs
antroy-madetech Oct 30, 2024
a97b69b
Rename online/offline methods
antroy-madetech Oct 30, 2024
9e554db
Merge branch 'main' into DBTP-1109-DataCopy_2
antroy-madetech Oct 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions buildspec.database-copy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: 0.2

phases:
pre_build:
commands:
- echo "3.9" > .python-version
- cd images/tools/database-copy/
- echo Login to Amazon ECR
- aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/uktrade
- SHORT_HASH=$(git rev-parse --short HEAD)

build:
commands:
- echo "Build database-copy ($SHORT_HASH) started on $(date)"
- docker build --tag public.ecr.aws/uktrade/database-copy:tag-latest .
- docker tag public.ecr.aws/uktrade/database-copy:tag-latest public.ecr.aws/uktrade/database-copy:$SHORT_HASH
- echo "Build database-copy ($SHORT_HASH) completed on $(date)"

post_build:
commands:
- echo "Push database-copy ($SHORT_HASH) started on $(date)"
- docker push public.ecr.aws/uktrade/database-copy:tag-latest
- docker push public.ecr.aws/uktrade/database-copy:$SHORT_HASH
- echo "Push database-copy ($SHORT_HASH) completed on $(date)"
79 changes: 60 additions & 19 deletions dbt_platform_helper/COMMANDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
- [platform-helper database](#platform-helper-database)
- [platform-helper database dump](#platform-helper-database-dump)
- [platform-helper database load](#platform-helper-database-load)
- [platform-helper database copy](#platform-helper-database-copy)
- [platform-helper version](#platform-helper-version)
- [platform-helper version get-platform-helper-for-project](#platform-helper-version-get-platform-helper-for-project)

Expand Down Expand Up @@ -962,10 +963,12 @@ platform-helper notify add-comment <slack_channel_id> <slack_token>

[↩ Parent](#platform-helper)

Commands to copy data between databases.

## Usage

```
platform-helper database (dump|load)
platform-helper database (dump|load|copy)
```

## Options
Expand All @@ -975,6 +978,7 @@ platform-helper database (dump|load)

## Commands

- [`copy` ↪](#platform-helper-database-copy)
- [`dump` ↪](#platform-helper-database-dump)
- [`load` ↪](#platform-helper-database-load)

Expand All @@ -987,23 +991,20 @@ platform-helper database (dump|load)
## Usage

```
platform-helper database dump --account-id <account_id> --app <application>
--env <environment> --database <database>
--vpc-name <vpc_name>
platform-helper database dump --from <from_env> --database <database>
[--app <application>] [--from-vpc <from_vpc>]
```

## Options

- `--account-id <text>`

- `--app <text>`

- `--env <text>`

- The application name. Required unless you are running the command from your deploy repo
- `--from <text>`
- The environment you are dumping data from
- `--database <text>`

- `--vpc-name <text>`

- The name of the database you are dumping data from
- `--from-vpc <text>`
- The vpc the specified environment is running in. Required unless you are running the command from your deploy repo
- `--help <boolean>` _Defaults to False._
- Show this message and exit.

Expand All @@ -1016,22 +1017,62 @@ platform-helper database dump --account-id <account_id> --app <application>
## Usage

```
platform-helper database load --account-id <account_id> --app <application>
--env <environment> --database <database>
--vpc-name <vpc_name>
platform-helper database load --to <to_env> --database <database>
[--app <application>] [--to-vpc <to_vpc>]
[--auto-approve]
```

## Options

- `--account-id <text>`

- `--app <text>`
- The application name. Required unless you are running the command from your deploy repo
- `--to <text>`
- The environment you are loading data into
- `--database <text>`
- The name of the database you are loading data into
- `--to-vpc <text>`
- The vpc the specified environment is running in. Required unless you are running the command from your deploy repo
- `--auto-approve <boolean>` _Defaults to False._

- `--env <text>`
- `--help <boolean>` _Defaults to False._
- Show this message and exit.

# platform-helper database copy

[↩ Parent](#platform-helper-database)

Copy a database between environments.

## Usage

```
platform-helper database copy --from <from_env> --to <to_env> --database <database>
--svc <service> [--app <application>] [--from-vpc <from_vpc>]
[--to-vpc <to_vpc>] [--template (default|migration|dmas-migration)]
[--auto-approve] [--no-maintenance-page]
```

## Options

- `--app <text>`
- The application name. Required unless you are running the command from your deploy repo
- `--from <text>`
- The environment you are copying data from
- `--to <text>`
- The environment you are copying data into
- `--database <text>`
- The name of the database you are copying
- `--from-vpc <text>`
- The vpc the environment you are copying from is running in. Required unless you are running the command from your deploy repo
- `--to-vpc <text>`
- The vpc the environment you are copying into is running in. Required unless you are running the command from your deploy repo
- `--auto-approve <boolean>` _Defaults to False._

- `--vpc-name <text>`
- `--svc <text>` _Defaults to ['web']._

- `--template <choice>` _Defaults to default._
- The maintenance page you wish to put up.
- `--no-maintenance-page <boolean>` _Defaults to False._

- `--help <boolean>` _Defaults to False._
- Show this message and exit.
Expand Down
115 changes: 97 additions & 18 deletions dbt_platform_helper/commands/database.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,112 @@
import click

from dbt_platform_helper.commands.database_helpers import DatabaseCopy
from dbt_platform_helper.commands.environment import AVAILABLE_TEMPLATES
from dbt_platform_helper.domain.database_copy import DatabaseCopy
from dbt_platform_helper.utils.click import ClickDocOptGroup


@click.group(chain=True, cls=ClickDocOptGroup)
def database():
pass
"""Commands to copy data between databases."""


@database.command(name="dump")
@click.option("--account-id", type=str, required=True)
@click.option("--app", type=str, required=True)
@click.option("--env", type=str, required=True)
@click.option("--database", type=str, required=True)
@click.option("--vpc-name", type=str, required=True)
def dump(account_id, app, env, database, vpc_name):
@click.option(
"--app",
type=str,
help="The application name. Required unless you are running the command from your deploy repo",
)
@click.option(
"--from",
"from_env",
type=str,
required=True,
help="The environment you are dumping data from",
)
@click.option(
"--database", type=str, required=True, help="The name of the database you are dumping data from"
)
@click.option(
"--from-vpc",
type=str,
help="The vpc the specified environment is running in. Required unless you are running the command from your deploy repo",
)
def dump(app, from_env, database, from_vpc):
"""Dump a database into an S3 bucket."""
data_copy = DatabaseCopy(account_id, app, env, database, vpc_name)
data_copy.dump()
data_copy = DatabaseCopy(app, database)
data_copy.dump(from_env, from_vpc)


@database.command(name="load")
@click.option("--account-id", type=str, required=True)
@click.option("--app", type=str, required=True)
@click.option("--env", type=str, required=True)
@click.option("--database", type=str, required=True)
@click.option("--vpc-name", type=str, required=True)
def load(account_id, app, env, database, vpc_name):
@click.option(
"--app",
type=str,
help="The application name. Required unless you are running the command from your deploy repo",
)
@click.option(
"--to", "to_env", type=str, required=True, help="The environment you are loading data into"
)
@click.option(
"--database", type=str, required=True, help="The name of the database you are loading data into"
)
@click.option(
"--to-vpc",
type=str,
help="The vpc the specified environment is running in. Required unless you are running the command from your deploy repo",
)
@click.option("--auto-approve/--no-auto-approve", default=False)
def load(app, to_env, database, to_vpc, auto_approve):
"""Load a database from an S3 bucket."""
data_copy = DatabaseCopy(account_id, app, env, database, vpc_name)
data_copy.load()
data_copy = DatabaseCopy(app, database, auto_approve)
data_copy.load(to_env, to_vpc)


@database.command(name="copy")
@click.option(
"--app",
type=str,
help="The application name. Required unless you are running the command from your deploy repo",
)
@click.option(
"--from", "from_env", type=str, required=True, help="The environment you are copying data from"
)
@click.option(
"--to", "to_env", type=str, required=True, help="The environment you are copying data into"
)
@click.option(
"--database", type=str, required=True, help="The name of the database you are copying"
)
@click.option(
"--from-vpc",
type=str,
help="The vpc the environment you are copying from is running in. Required unless you are running the command from your deploy repo",
)
@click.option(
"--to-vpc",
type=str,
help="The vpc the environment you are copying into is running in. Required unless you are running the command from your deploy repo",
)
@click.option("--auto-approve/--no-auto-approve", default=False)
@click.option("--svc", type=str, required=True, multiple=True, default=["web"])
@click.option(
"--template",
type=click.Choice(AVAILABLE_TEMPLATES),
default="default",
help="The maintenance page you wish to put up.",
)
@click.option("--no-maintenance-page", flag_value=True)
def copy(
app,
from_env,
to_env,
database,
from_vpc,
to_vpc,
auto_approve,
svc,
template,
no_maintenance_page,
):
"""Copy a database between environments."""
data_copy = DatabaseCopy(app, database, auto_approve)
data_copy.copy(from_env, to_env, from_vpc, to_vpc, svc, template, no_maintenance_page)
Loading