Skip to content

Commit 9de757e

Browse files
ci: fix disaster recovery GH flow (#1994)
* ci: fix disaster recovery GH flow * validate date is in the past * aws session duration * fix path in staging * increase session duration
1 parent 204d29b commit 9de757e

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

.github/workflows/recover_s3_repository.yml

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,25 @@ name: . ⚠️⚠️⚠️ Recover S3 Repository back in time ⚠️⚠️⚠️
33
on:
44
workflow_dispatch:
55
inputs:
6-
date_time:
7-
description: 'UTC DateTime to recover the S3 repository back in time (MM-DD-YYYY HH:MM:SS +0)'
6+
#description: 'UTC DateTime to recover the S3 repository back in time (MM-DD-YYYY HH:MM:SS +0)'
7+
date_year:
8+
description: 'UTC Date YEAR (YYYY) to recover the S3 repository back in time'
9+
type: string
10+
required: true
11+
date_month:
12+
description: 'UTC Date MONTH (MM) to recover the S3 repository back in time'
13+
type: string
14+
required: true
15+
date_day:
16+
description: 'UTC Date DAY (DD) to recover the S3 repository back in time'
17+
type: string
18+
required: true
19+
time:
20+
description: 'UTC Time (HH:MM:SS) to recover the S3 repository back in time'
821
type: string
922
required: true
1023
path:
11-
description: 'Path under infrastructure_agent folder to recover (w/o leading slash, with trailing slash)'
24+
description: 'Path under infrastructure_agent folder to recover (w/o leading nor trailing slash)'
1225
type: string
1326
required: true
1427
environment:
@@ -29,31 +42,46 @@ env:
2942
jobs:
3043
recover-s3-repository:
3144
name: Execute S3 PIT restore
32-
runs-on: ubuntu-20.04
45+
runs-on: ubuntu-24.04
3346
steps:
3447
- name: Validate datetime
3548
run: |
36-
datetime="${{ github.event.inputs.date_time }}"
49+
echo "Validating that datetime is in correct format"
50+
datetime="${{ github.event.inputs.date_month }}-${{ github.event.inputs.date_day }}-${{ github.event.inputs.date_year }} ${{ github.event.inputs.time }} +0000"
3751
# Use Python's strptome (same as s3-pit-restore) to check if it's a valid datetime
3852
python3 -c "from datetime import datetime; datetime.strptime('$datetime', '%m-%d-%Y %H:%M:%S %z')" 2> /dev/null
3953
exit_code=$?
40-
if [ $exit_code -ne 0 ]]; then
54+
if [ $exit_code -ne 0 ]; then
4155
exit 1
4256
fi
57+
echo "datetime format is correct"
58+
echo ""
59+
60+
echo "Validating that datetime is not in the future"
61+
# check that datetime is not in the future
62+
python3 -c "from datetime import datetime,timezone; import sys; sys.exit(1) if datetime.strptime('$datetime', '%m-%d-%Y %H:%M:%S %z') >= datetime.now(timezone.utc) else sys.exit(0)"
63+
exit_code=$?
64+
if [ $exit_code -ne 0 ]; then
65+
exit 1
66+
fi
67+
echo "datetime is not in the future"
68+
69+
echo "DATE_TIME=$datetime" >> $GITHUB_ENV
4370
4471
- name: Validate path input does not have leading nor trailing slash
4572
run: |
73+
set -e
4674
s3_path="${{ github.event.inputs.path }}"
4775
# Check if the path has a leading slash
4876
if [[ "$s3_path" == /* ]]; then
4977
echo "Invalid path: should not have a leading slash."
50-
return 1
78+
exit 1
5179
fi
5280
5381
# Check if the path has a trailing slash
5482
if [[ "$s3_path" == */ ]]; then
5583
echo "Invalid path: should not have a trailing slash."
56-
return 1
84+
exit 1
5785
fi
5886

5987
- name: Checkout repository
@@ -63,43 +91,45 @@ jobs:
6391
ref: master
6492

6593
- name: Setup AWS credentials for Production
66-
if: ${{ env.ENVIRONMENT == 'production' }}
94+
if: ${{ github.event.inputs.environment == 'production' }}
6795
run: |
6896
./setup_aws_credentials.sh
6997
env:
7098
AWS_ACCESS_KEY_ID: ${{ secrets.OHAI_AWS_ACCESS_KEY_ID_PRODUCTION }}
7199
AWS_SECRET_ACCESS_KEY: ${{ secrets.OHAI_AWS_SECRET_ACCESS_KEY_PRODUCTION }}
72100
AWS_ROLE_ARN: ${{ secrets.OHAI_AWS_ROLE_ARN_PRODUCTION }}
73101
AWS_ROLE_SESSION_NAME: ${{ secrets.OHAI_AWS_ROLE_SESSION_NAME_PRODUCTION }}
102+
AWS_SESSION_DURATION_SECONDS: 14400
74103
TEMP_AWS_PROFILE: ${{ env.TEMP_AWS_PROFILE }}
75104

76105
- name: Run S3 PIT restore
77-
if: ${{ env.ENVIRONMENT == 'production' }}
106+
if: ${{ github.event.inputs.environment == 'production' }}
78107
run: |
79108
BUCKET="nr-downloads-main" \
80109
PREFIX="${{ env.MANDATORY_PREFIX }}${{ github.event.inputs.path }}" \
81-
TIME="${{ github.event.inputs.date_time }}" \
110+
TIME="${{ env.DATE_TIME }}" \
82111
IMAGE="${{ env.IMAGE }}" \
83112
AWS_PROFILE="${{ env.TEMP_AWS_PROFILE }}" \
84113
make restore
85114
86115
- name: Setup AWS credentials for Staging
87-
if: ${{ env.ENVIRONMENT == 'staging' }}
116+
if: ${{ github.event.inputs.environment == 'staging' }}
88117
run: |
89118
./setup_aws_credentials.sh
90119
env:
91120
AWS_ACCESS_KEY_ID: ${{ secrets.OHAI_AWS_ACCESS_KEY_ID_STAGING }}
92121
AWS_SECRET_ACCESS_KEY: ${{ secrets.OHAI_AWS_SECRET_ACCESS_KEY_STAGING }}
93122
AWS_ROLE_ARN: ${{ secrets.OHAI_AWS_ROLE_ARN_STAGING }}
94123
AWS_ROLE_SESSION_NAME: ${{ secrets.OHAI_AWS_ROLE_SESSION_NAME_STAGING }}
124+
AWS_SESSION_DURATION_SECONDS: 14400
95125
TEMP_AWS_PROFILE: ${{ env.TEMP_AWS_PROFILE }}
96126

97127
- name: Run S3 PIT restore in Staging S3
98-
if: ${{ env.ENVIRONMENT == 'staging' }}
128+
if: ${{ github.event.inputs.environment == 'staging' }}
99129
run: |
100130
BUCKET="nr-downloads-ohai-staging" \
101-
PREFIX="${{ env.MANDATORY_PREFIX }}${{ env.S3_PATH }}" \
102-
TIME="${{ env.DATETIME }}" \
131+
PREFIX="${{ env.MANDATORY_PREFIX }}${{ github.event.inputs.path }}" \
132+
TIME="${{ env.DATE_TIME }}" \
103133
IMAGE="${{ env.IMAGE }}" \
104134
AWS_PROFILE="${{ env.TEMP_AWS_PROFILE }}" \
105135
make restore

.github/workflows/repository_lastet_added_files.yml renamed to .github/workflows/repository_latest_added_files.yml

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,20 @@ on:
88
type: string
99
default: '500'
1010
required: true
11-
date_time:
12-
description: 'UTC Date and time to show files added before and after (YYYY-MM-DD HH:MM:SS)'
11+
date_year:
12+
description: 'UTC Date YEAR (YYYY) to recover the S3 repository back in time'
13+
type: string
14+
required: true
15+
date_month:
16+
description: 'UTC Date MONTH (MM) to recover the S3 repository back in time'
17+
type: string
18+
required: true
19+
date_day:
20+
description: 'UTC Date DAY (DD) to recover the S3 repository back in time'
21+
type: string
22+
required: true
23+
time:
24+
description: 'UTC Time (HH:MM:SS) to recover the S3 repository back in time'
1325
type: string
1426
required: true
1527
environment:
@@ -24,11 +36,12 @@ on:
2436
jobs:
2537
list-files:
2638
name: List files added before and after datetime
27-
runs-on: ubuntu-20.04
39+
runs-on: ubuntu-24.04
2840
steps:
2941
- name: Validate datetime
3042
run: |
31-
datetime="${{ github.event.inputs.date_time }}"
43+
set -e
44+
datetime="${{ github.event.inputs.date_year }}-${{ github.event.inputs.date_month }}-${{ github.event.inputs.date_day }} ${{ github.event.inputs.time }}"
3245
# Regular expression to match the format YYYY-MM-DD HH:MM:SS
3346
regex="^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$"
3447
@@ -38,11 +51,12 @@ jobs:
3851
else
3952
exit 1
4053
fi
54+
echo "DATE_TIME=$datetime" >> $GITHUB_ENV
4155

4256
- name: List files created before and after datetime in Production S3
4357
if: ${{ github.event.inputs.environment == 'production' }}
4458
run: |
45-
TZ="UTC" aws s3 ls s3://nr-downloads-main/infrastructure_agent/ --recursive | sort | grep --color -m 1 -C {{ github.event.inputs.count }} "^${{ github.event.inputs.date_time }}"
59+
TZ="UTC" aws s3 ls s3://nr-downloads-main/infrastructure_agent/ --recursive | sort | grep --color -m 1 -C ${{ github.event.inputs.count }} "^${{ env.DATE_TIME }}"
4660
env:
4761
AWS_ACCESS_KEY_ID: ${{ secrets.OHAI_AWS_ACCESS_KEY_ID_PRODUCTION }}
4862
AWS_SECRET_ACCESS_KEY: ${{ secrets.OHAI_AWS_SECRET_ACCESS_KEY_PRODUCTION }}
@@ -52,7 +66,7 @@ jobs:
5266
- name: List files created before and after datetime in Staging S3
5367
if: ${{ github.event.inputs.environment == 'staging' }}
5468
run: |
55-
TZ="UTC" aws s3 ls s3://nr-downloads-ohai-staging/infrastructure_agent/ --recursive | sort | grep --color -m 1 -C {{ github.event.inputs.count }} "^${{ github.event.inputs.date_time }}"
69+
TZ="UTC" aws s3 ls s3://nr-downloads-ohai-staging/infrastructure_agent/ --recursive | sort | grep --color -m 1 -C ${{ github.event.inputs.count }} "^${{ env.DATE_TIME }}"
5670
env:
5771
AWS_ACCESS_KEY_ID: ${{ secrets.OHAI_AWS_ACCESS_KEY_ID_STAGING }}
5872
AWS_SECRET_ACCESS_KEY: ${{ secrets.OHAI_AWS_SECRET_ACCESS_KEY_STAGING }}

.github/workflows/repository_lastet_added_packages.yml renamed to .github/workflows/repository_latest_added_packages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ on:
2121
jobs:
2222
list-packages:
2323
name: List latest added packages into the S3 repository
24-
runs-on: ubuntu-20.04
24+
runs-on: ubuntu-24.04
2525
steps:
2626
- name: List latest packages in Production S3
2727
if: ${{ github.event.inputs.environment == 'production' }}

0 commit comments

Comments
 (0)