-
-
Save ajohnstone/8adb2f06422b17789c9ff305dc790233 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
import boto3 | |
import sys | |
import argparse | |
from pprint import pprint as pp | |
import concurrent.futures | |
import os | |
parser = argparse.ArgumentParser(description='Transfer volumes between regions') | |
parser.add_argument('--repository-from', help='The repository to transfer from', required=True) | |
parser.add_argument('--repository-to', help='The repository to transfer to', required=True) | |
args = parser.parse_args() | |
region_from = args.repository_from.split('.')[3:4][0] | |
region_to = args.repository_to.split('.')[3:4][0] | |
def cmd(cmd): | |
print cmd | |
os.system(cmd) | |
def get_client(type = 'from'): | |
r = args.__dict__["repository_" + type] | |
region = r.split('.')[3:4][0] | |
ecr_client = boto3.client('ecr', region_name=region) | |
return ecr_client | |
def transfer_image(repository_from, repository_to): | |
pp({'repository_from': repository_from, 'repository_to': repository_to }) | |
cmd("docker pull " + repository_from) | |
cmd("docker tag " + repository_from + " " + repository_to) | |
cmd("docker push " + repository_to) | |
return "1" | |
IMAGES = {} | |
def main(): | |
for r in [region_from, region_to]: | |
os.system("aws ecr --region " + r + " get-login | bash > /dev/null 2>&1") | |
for r in get_client('from').describe_repositories()['repositories']: | |
try: | |
get_client('to').create_repository(repositoryName=r['repositoryName']) | |
except Exception as e: | |
if "RepositoryAlreadyExistsException" not in str(e): | |
pp(str(e)) | |
pass | |
src_images = get_client('from').list_images(repositoryName = r['repositoryName'])['imageIds'] | |
dst_images = [ x['imageTag'] for x in get_client('to').list_images(repositoryName = r['repositoryName'])['imageIds'] ] | |
for i in src_images: | |
if 'imageTag' in i and i['imageTag'] not in dst_images: | |
repository_from = args.repository_from + '/' + r['repositoryName'] + ':' + i['imageTag'] | |
repository_to = args.repository_to + '/' + r['repositoryName'] + ':' + i['imageTag'] | |
IMAGES[repository_from] = repository_to | |
pp(IMAGES) | |
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: | |
future_to_url = {executor.submit(transfer_image, x, y): x for x,y in IMAGES.items() if IMAGES} | |
for future in concurrent.futures.as_completed(future_to_url): | |
url = future_to_url[future] | |
try: | |
data = future.result() | |
pp(data) | |
except Exception as exc: | |
print('%r generated an exception: %s' % (url, exc)) | |
else: | |
pp(data) | |
return | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment