Skip to content

Commit 292682d

Browse files
Allow setting a common bucket for the environment (#90)
1 parent a01cb4b commit 292682d

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ AIRFLOW_VAR_MATRIX_WEBHOOK_API_KEY=api_key
4545

4646
S3_LOCAL_ENDPOINT=http://s3:5000
4747
SPACES_CONN_ID=aws_default
48+
# Optional bucket to use, necessary prefix in all cases for DigitalOcean Spaces
49+
# SPACES_BUCKET_NAME=bucketname
4850

4951
########################################################################################
5052
# Other config

techbloc_airflow/dags/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
SSH_MONOLITH_CONN_ID = "ssh_monolith"
55
SPACES_CONN_ID = os.getenv("SPACES_CONN_ID")
6+
SPACES_BUCKET_NAME = os.getenv("SPACES_BUCKET_NAME", "")
7+
# DigitalOcean Spaces has a common shared bucket for everything, whereas locally we
8+
# have a separate bucket for each service. If the bucket name is defined, all keys
9+
# will be prefixed with that.
10+
SPACES_KEY_PREFIX = f"s3://{SPACES_BUCKET_NAME}/" if SPACES_BUCKET_NAME else "s3://"
611

712
MATRIX_WEBHOOK_CONN_ID = "matrix_webhook"
813
MATRIX_WEBHOOK_API_KEY = "matrix_webhook_api_key"

techbloc_airflow/dags/maintenance/backups/offsite_backups.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414

1515
LOCAL_BACKUPS_FOLDER = "/opt/backups"
16-
BUCKET_NAME = "monolith-backups"
16+
KEY_BASE = constants.SPACES_KEY_PREFIX + "monolith-backups"
1717

1818

1919
@task_group
2020
def backup_service(config: OffsiteConfig):
2121
local_backup = f"{LOCAL_BACKUPS_FOLDER}/{config.filename}"
22+
s3_key = f"{KEY_BASE}/{config.filename}"
2223

2324
backup = SSHOperator(
2425
task_id=f"backup_{config.name}",
@@ -35,17 +36,14 @@ def backup_service(config: OffsiteConfig):
3536
copy_to_spaces = LocalFilesystemToS3Operator(
3637
task_id=f"copy_local_{config.name}_to_spaces",
3738
aws_conn_id=constants.SPACES_CONN_ID,
38-
dest_bucket=BUCKET_NAME,
39-
dest_key=config.filename,
39+
dest_key=s3_key,
4040
replace=True,
4141
filename=local_backup,
4242
)
4343

4444
@task(retries=3, retry_exponential_backoff=True)
4545
def notify_backup_complete():
46-
matrix.send_message(
47-
f"{config.name} backup complete at: `s3://{BUCKET_NAME}/{config.filename}`"
48-
)
46+
matrix.send_message(f"{config.name} backup complete at: `{s3_key}`")
4947

5048
restart_if_failed = SSHOperator(
5149
task_id=f"start_if_failed_{config.name}",

0 commit comments

Comments
 (0)