Last active
March 21, 2021 16:39
-
-
Save ops-gaurav/29e0bb0482b4b7f8095ad2dc4e5e9284 to your computer and use it in GitHub Desktop.
Refresh ECR tokens based on config.json. Start app with pm2 start config.json
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
{ | |
"apps": [ | |
{ | |
"name": "refresh-cron", | |
"command": "python3 /etc/docker/compose/composeservice/configurations/ecr-refresh-token.py", | |
"autorestart": true, | |
"env": { | |
"REGION": "ap-south-1", | |
"ENDPOINT": "388364135013.dkr.ecr.ap-south-1.amazonaws.com" | |
} | |
} | |
] | |
} |
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
import schedule | |
import time | |
import os | |
from datetime import datetime | |
region = os.environ.get('REGION') | |
endpoint = os.environ.get('ENDPOINT') | |
command = 'aws ecr get-login-password --region '+ region + ' | docker login --username AWS --password-stdin '+ endpoint | |
os.system(command) | |
print('Done refreshing the token initially') | |
def job(): | |
print("Refreshing the token") | |
with open('cron-logs.log', 'a') as file: | |
now = datetime.now() | |
before = 'LOG:\t'+ now.strftime('%m/%d/%Y, %H:%M:%S') +' - Refereshing token.\n' | |
file.write(before) | |
os.system(command) | |
with open('cron-logs.log', 'a') as file: | |
now = datetime.now() | |
after = 'LOG:\t'+ now.strftime('%m/%d/%Y, %H:%M:%S') +' - Token referesh done.\n' | |
file.write(after) | |
print('Done refereshing the token') | |
# refresh the token every 5 hours | |
schedule.every(5).hours.do(job); | |
while 1: | |
schedule.run_pending() | |
time.sleep(1) |
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
[Unit] | |
Description=ECR token refresh service | |
[Service] | |
ExecStart=/etc/docker/compose/composeservice/configurations/refresh-cron.sh dev | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment