Last active
July 22, 2019 08:22
-
-
Save syguer/73f30f39bfe8675c35366dd9b3e4736e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 base64 | |
| from datetime import datetime | |
| import boto3 | |
| import docker | |
| class ECSClient(object): | |
| def __init__(self): | |
| self.client = docker.from_env() | |
| def login(self): | |
| (username, password, endpoint) = _get_auth_info() | |
| self.client.login(username=username, password=password, registry=endpoint, reauth=True) | |
| def build(self, name, repository): | |
| now = datetime.now().strftime("%Y%m%d%H%M%S") | |
| tag = "{}_{}".format(name, now) | |
| uri = repository + ':' + tag | |
| self.client.images.build(path='.', tag=uri) | |
| return uri | |
| def push(self, uri): | |
| for line in self.client.images.push(uri, stream=True, decode=True): | |
| print(line) | |
| def _get_auth_info(): | |
| client = boto3.client('ecr') | |
| res = client.get_authorization_token() | |
| auth_info = base64.b64decode(res['authorizationData'][0]['authorizationToken']) \ | |
| .decode('utf-8') \ | |
| .split(':') | |
| ep = res['authorizationData'][0]['proxyEndpoint'] | |
| return (auth_info[0], auth_info[1], ep) | |
| def build_container(name="", registory=""): | |
| client = ECSClient() | |
| client.login() | |
| uri = client.build(name, registory) | |
| client.push(uri) | |
| return uri |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment