Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/codedeploy/deregister.py
2630 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
14
import sys
15
16
from botocore.exceptions import ClientError
17
18
from awscli.customizations.commands import BasicCommand
19
from awscli.customizations.codedeploy.utils import \
20
validate_region, validate_instance_name, INSTANCE_NAME_ARG
21
from awscli.utils import create_nested_client
22
23
24
class Deregister(BasicCommand):
25
NAME = 'deregister'
26
27
DESCRIPTION = (
28
'Removes any tags from the on-premises instance; deregisters the '
29
'on-premises instance from AWS CodeDeploy; and, unless requested '
30
'otherwise, deletes the IAM user for the on-premises instance.'
31
)
32
33
ARG_TABLE = [
34
INSTANCE_NAME_ARG,
35
{
36
'name': 'no-delete-iam-user',
37
'action': 'store_true',
38
'default': False,
39
'help_text': (
40
'Optional. Do not delete the IAM user for the registered '
41
'on-premises instance.'
42
)
43
}
44
]
45
46
def _run_main(self, parsed_args, parsed_globals):
47
params = parsed_args
48
params.session = self._session
49
validate_region(params, parsed_globals)
50
validate_instance_name(params)
51
52
self.codedeploy = create_nested_client(
53
self._session,
54
'codedeploy',
55
region_name=params.region,
56
endpoint_url=parsed_globals.endpoint_url,
57
verify=parsed_globals.verify_ssl
58
)
59
self.iam = create_nested_client(
60
self._session,
61
'iam',
62
region_name=params.region
63
)
64
65
try:
66
self._get_instance_info(params)
67
if params.tags:
68
self._remove_tags(params)
69
self._deregister_instance(params)
70
if not params.no_delete_iam_user:
71
self._delete_user_policy(params)
72
self._delete_access_key(params)
73
self._delete_iam_user(params)
74
sys.stdout.write(
75
'Run the following command on the on-premises instance to '
76
'uninstall the codedeploy-agent:\n'
77
'aws deploy uninstall\n'
78
)
79
except Exception as e:
80
sys.stdout.flush()
81
sys.stderr.write(
82
'ERROR\n'
83
'{0}\n'
84
'Deregister the on-premises instance by following the '
85
'instructions in "Configure Existing On-Premises Instances by '
86
'Using AWS CodeDeploy" in the AWS CodeDeploy User '
87
'Guide.\n'.format(e)
88
)
89
90
def _get_instance_info(self, params):
91
sys.stdout.write('Retrieving on-premises instance information... ')
92
response = self.codedeploy.get_on_premises_instance(
93
instanceName=params.instance_name
94
)
95
params.iam_user_arn = response['instanceInfo']['iamUserArn']
96
start = params.iam_user_arn.rfind('/') + 1
97
params.user_name = params.iam_user_arn[start:]
98
params.tags = response['instanceInfo']['tags']
99
sys.stdout.write(
100
'DONE\n'
101
'IamUserArn: {0}\n'.format(
102
params.iam_user_arn
103
)
104
)
105
if params.tags:
106
sys.stdout.write('Tags:')
107
for tag in params.tags:
108
sys.stdout.write(
109
' Key={0},Value={1}'.format(tag['Key'], tag['Value'])
110
)
111
sys.stdout.write('\n')
112
113
def _remove_tags(self, params):
114
sys.stdout.write('Removing tags from the on-premises instance... ')
115
self.codedeploy.remove_tags_from_on_premises_instances(
116
tags=params.tags,
117
instanceNames=[params.instance_name]
118
)
119
sys.stdout.write('DONE\n')
120
121
def _deregister_instance(self, params):
122
sys.stdout.write('Deregistering the on-premises instance... ')
123
self.codedeploy.deregister_on_premises_instance(
124
instanceName=params.instance_name
125
)
126
sys.stdout.write('DONE\n')
127
128
def _delete_user_policy(self, params):
129
sys.stdout.write('Deleting the IAM user policies... ')
130
list_user_policies = self.iam.get_paginator('list_user_policies')
131
try:
132
for response in list_user_policies.paginate(
133
UserName=params.user_name):
134
for policy_name in response['PolicyNames']:
135
self.iam.delete_user_policy(
136
UserName=params.user_name,
137
PolicyName=policy_name
138
)
139
except ClientError as e:
140
if e.response.get('Error', {}).get('Code') != 'NoSuchEntity':
141
raise e
142
sys.stdout.write('DONE\n')
143
144
def _delete_access_key(self, params):
145
sys.stdout.write('Deleting the IAM user access keys... ')
146
list_access_keys = self.iam.get_paginator('list_access_keys')
147
try:
148
for response in list_access_keys.paginate(
149
UserName=params.user_name):
150
for access_key in response['AccessKeyMetadata']:
151
self.iam.delete_access_key(
152
UserName=params.user_name,
153
AccessKeyId=access_key['AccessKeyId']
154
)
155
except ClientError as e:
156
if e.response.get('Error', {}).get('Code') != 'NoSuchEntity':
157
raise e
158
sys.stdout.write('DONE\n')
159
160
def _delete_iam_user(self, params):
161
sys.stdout.write('Deleting the IAM user ({0})... '.format(
162
params.user_name
163
))
164
try:
165
self.iam.delete_user(UserName=params.user_name)
166
except ClientError as e:
167
if e.response.get('Error', {}).get('Code') != 'NoSuchEntity':
168
raise e
169
sys.stdout.write('DONE\n')
170
171