Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/ecr.py
1566 views
1
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
#
3
# Licensed under the Apache License, Version 2.0 (the "License"). You
4
# may not use this file except in compliance with the License. A copy of
5
# the License is located at
6
#
7
# http://aws.amazon.com/apache2.0/
8
#
9
# or in the "license" file accompanying this file. This file is
10
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11
# ANY KIND, either express or implied. See the License for the specific
12
# language governing permissions and limitations under the License.
13
from awscli.customizations.commands import BasicCommand
14
from awscli.customizations.utils import create_client_from_parsed_globals
15
16
from base64 import b64decode
17
import sys
18
19
20
def register_ecr_commands(cli):
21
cli.register('building-command-table.ecr', _inject_commands)
22
23
24
def _inject_commands(command_table, session, **kwargs):
25
command_table['get-login'] = ECRLogin(session)
26
command_table['get-login-password'] = ECRGetLoginPassword(session)
27
28
29
class ECRLogin(BasicCommand):
30
"""Log in with 'docker login'"""
31
NAME = 'get-login'
32
33
DESCRIPTION = BasicCommand.FROM_FILE('ecr/get-login_description.rst')
34
35
ARG_TABLE = [
36
{
37
'name': 'registry-ids',
38
'help_text': 'A list of AWS account IDs that correspond to the '
39
'Amazon ECR registries that you want to log in to.',
40
'required': False,
41
'nargs': '+'
42
},
43
{
44
'name': 'include-email',
45
'action': 'store_true',
46
'group_name': 'include-email',
47
'dest': 'include_email',
48
'default': True,
49
'required': False,
50
'help_text': (
51
"Specify if the '-e' flag should be included in the "
52
"'docker login' command. The '-e' option has been deprecated "
53
"and is removed in Docker version 17.06 and later. You must "
54
"specify --no-include-email if you're using Docker version "
55
"17.06 or later. The default behavior is to include the "
56
"'-e' flag in the 'docker login' output."),
57
},
58
{
59
'name': 'no-include-email',
60
'help_text': 'Include email arg',
61
'action': 'store_false',
62
'default': True,
63
'group_name': 'include-email',
64
'dest': 'include_email',
65
'required': False,
66
},
67
]
68
69
def _run_main(self, parsed_args, parsed_globals):
70
ecr_client = create_client_from_parsed_globals(
71
self._session, 'ecr', parsed_globals)
72
if not parsed_args.registry_ids:
73
result = ecr_client.get_authorization_token()
74
else:
75
result = ecr_client.get_authorization_token(
76
registryIds=parsed_args.registry_ids)
77
for auth in result['authorizationData']:
78
auth_token = b64decode(auth['authorizationToken']).decode()
79
username, password = auth_token.split(':')
80
command = ['docker', 'login', '-u', username, '-p', password]
81
if parsed_args.include_email:
82
command.extend(['-e', 'none'])
83
command.append(auth['proxyEndpoint'])
84
sys.stdout.write(' '.join(command))
85
sys.stdout.write('\n')
86
return 0
87
88
89
class ECRGetLoginPassword(BasicCommand):
90
"""Get a password to be used with container clients such as Docker"""
91
NAME = 'get-login-password'
92
93
DESCRIPTION = BasicCommand.FROM_FILE(
94
'ecr/get-login-password_description.rst')
95
96
def _run_main(self, parsed_args, parsed_globals):
97
ecr_client = create_client_from_parsed_globals(
98
self._session,
99
'ecr',
100
parsed_globals)
101
result = ecr_client.get_authorization_token()
102
auth = result['authorizationData'][0]
103
auth_token = b64decode(auth['authorizationToken']).decode()
104
_, password = auth_token.split(':')
105
sys.stdout.write(password)
106
sys.stdout.write('\n')
107
return 0
108
109