|
26 | 26 |
|
27 | 27 |
|
28 | 28 | @click.command() |
29 | | -@click.option("--min-age-hr", type=int, default=8) |
30 | | -@click.option("--key-prefix", type=str, default="nhsd-apm-management-ptl-terraform/env:/api-deployment:ptl:") |
| 29 | +@click.option("--min-age-hr", type=int, default=4) |
| 30 | +@click.option( |
| 31 | + "--key-prefix", |
| 32 | + type=str, |
| 33 | + default="nhsd-apm-management-ptl-terraform/env:/api-deployment:ptl:", |
| 34 | +) |
31 | 35 | @click.option("--table-name", type=str, default="terraform-state-lock") |
32 | 36 | @click.option("--profile", type=str, default="apm_ptl") |
33 | 37 | def main(min_age_hr, key_prefix, table_name, profile): |
34 | | - |
35 | 38 | accepted_envs = ["apm_ptl", "apm_prod"] |
36 | 39 |
|
37 | 40 | if profile not in accepted_envs: |
38 | 41 | raise ValueError("Profile must be apm_ptl or apm_prod") |
39 | 42 |
|
40 | | - terraform_lock_table = boto3.Session(profile_name=profile).resource("dynamodb").Table(table_name) |
| 43 | + terraform_lock_table = ( |
| 44 | + boto3.Session(profile_name=profile).resource("dynamodb").Table(table_name) |
| 45 | + ) |
41 | 46 |
|
42 | 47 | filter_expr = "begins_with(#n0, :v0) AND attribute_exists(#n1)" |
43 | 48 |
|
44 | 49 | ExpressionAttributeNames = {"#n0": "LockID", "#n1": "Info"} |
45 | 50 | ExpressionAttributeValues = { |
46 | 51 | ":v0": key_prefix, |
47 | 52 | } |
48 | | - items = terraform_lock_table.scan(FilterExpression=filter_expr, ExpressionAttributeNames=ExpressionAttributeNames, ExpressionAttributeValues=ExpressionAttributeValues) |
49 | | - print(f"Found {len(items['Items'])} locks which start with key prefix '{key_prefix}'") |
| 53 | + items = terraform_lock_table.scan( |
| 54 | + FilterExpression=filter_expr, |
| 55 | + ExpressionAttributeNames=ExpressionAttributeNames, |
| 56 | + ExpressionAttributeValues=ExpressionAttributeValues, |
| 57 | + ) |
| 58 | + |
| 59 | + total_items = items["Items"] |
| 60 | + |
| 61 | + while "LastEvaluatedKey" in items: |
| 62 | + items = terraform_lock_table.scan( |
| 63 | + FilterExpression=filter_expr, |
| 64 | + ExpressionAttributeNames=ExpressionAttributeNames, |
| 65 | + ExpressionAttributeValues=ExpressionAttributeValues, |
| 66 | + ExclusiveStartKey=items["LastEvaluatedKey"], |
| 67 | + ) |
| 68 | + total_items.extend(items["Items"]) |
| 69 | + |
| 70 | + print( |
| 71 | + f"Found {len(items['Items'])} locks which start with key prefix '{key_prefix}'" |
| 72 | + ) |
50 | 73 |
|
51 | 74 | removed_count = 0 |
52 | | - for lock_item in items["Items"]: |
| 75 | + for lock_item in total_items: |
53 | 76 | lock_item_info = json.loads(lock_item["Info"]) |
54 | 77 | lock_id = lock_item["LockID"] |
55 | 78 | created_at = dateutil.parser.parse(lock_item_info["Created"]) |
56 | 79 |
|
57 | | - if datetime.datetime.now(datetime.timezone.utc) - created_at > datetime.timedelta(hours=min_age_hr): |
58 | | - print(f"{lock_id} {created_at=} is more than {min_age_hr} hours old, deleting lock...") |
| 80 | + if datetime.datetime.now( |
| 81 | + datetime.timezone.utc |
| 82 | + ) - created_at > datetime.timedelta(hours=min_age_hr): |
| 83 | + print( |
| 84 | + f"{lock_id} {created_at=} is more than {min_age_hr} hours old, deleting lock..." |
| 85 | + ) |
59 | 86 | terraform_lock_table.delete_item(Key={"LockID": lock_id}) |
60 | 87 | removed_count += 1 |
61 | 88 |
|
62 | 89 | else: |
63 | | - print(f"{lock_id} {created_at=} is not more than {min_age_hr} hours old, leaving it alone!") |
| 90 | + print( |
| 91 | + f"{lock_id} {created_at=} is not more than {min_age_hr} hours old, leaving it alone!" |
| 92 | + ) |
64 | 93 |
|
65 | 94 | print(f"Removed {removed_count} locks") |
66 | 95 |
|
|
0 commit comments