Path: blob/develop/awscli/customizations/codedeploy/deregister.py
2630 views
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.1213import sys1415from botocore.exceptions import ClientError1617from awscli.customizations.commands import BasicCommand18from awscli.customizations.codedeploy.utils import \19validate_region, validate_instance_name, INSTANCE_NAME_ARG20from awscli.utils import create_nested_client212223class Deregister(BasicCommand):24NAME = 'deregister'2526DESCRIPTION = (27'Removes any tags from the on-premises instance; deregisters the '28'on-premises instance from AWS CodeDeploy; and, unless requested '29'otherwise, deletes the IAM user for the on-premises instance.'30)3132ARG_TABLE = [33INSTANCE_NAME_ARG,34{35'name': 'no-delete-iam-user',36'action': 'store_true',37'default': False,38'help_text': (39'Optional. Do not delete the IAM user for the registered '40'on-premises instance.'41)42}43]4445def _run_main(self, parsed_args, parsed_globals):46params = parsed_args47params.session = self._session48validate_region(params, parsed_globals)49validate_instance_name(params)5051self.codedeploy = create_nested_client(52self._session,53'codedeploy',54region_name=params.region,55endpoint_url=parsed_globals.endpoint_url,56verify=parsed_globals.verify_ssl57)58self.iam = create_nested_client(59self._session,60'iam',61region_name=params.region62)6364try:65self._get_instance_info(params)66if params.tags:67self._remove_tags(params)68self._deregister_instance(params)69if not params.no_delete_iam_user:70self._delete_user_policy(params)71self._delete_access_key(params)72self._delete_iam_user(params)73sys.stdout.write(74'Run the following command on the on-premises instance to '75'uninstall the codedeploy-agent:\n'76'aws deploy uninstall\n'77)78except Exception as e:79sys.stdout.flush()80sys.stderr.write(81'ERROR\n'82'{0}\n'83'Deregister the on-premises instance by following the '84'instructions in "Configure Existing On-Premises Instances by '85'Using AWS CodeDeploy" in the AWS CodeDeploy User '86'Guide.\n'.format(e)87)8889def _get_instance_info(self, params):90sys.stdout.write('Retrieving on-premises instance information... ')91response = self.codedeploy.get_on_premises_instance(92instanceName=params.instance_name93)94params.iam_user_arn = response['instanceInfo']['iamUserArn']95start = params.iam_user_arn.rfind('/') + 196params.user_name = params.iam_user_arn[start:]97params.tags = response['instanceInfo']['tags']98sys.stdout.write(99'DONE\n'100'IamUserArn: {0}\n'.format(101params.iam_user_arn102)103)104if params.tags:105sys.stdout.write('Tags:')106for tag in params.tags:107sys.stdout.write(108' Key={0},Value={1}'.format(tag['Key'], tag['Value'])109)110sys.stdout.write('\n')111112def _remove_tags(self, params):113sys.stdout.write('Removing tags from the on-premises instance... ')114self.codedeploy.remove_tags_from_on_premises_instances(115tags=params.tags,116instanceNames=[params.instance_name]117)118sys.stdout.write('DONE\n')119120def _deregister_instance(self, params):121sys.stdout.write('Deregistering the on-premises instance... ')122self.codedeploy.deregister_on_premises_instance(123instanceName=params.instance_name124)125sys.stdout.write('DONE\n')126127def _delete_user_policy(self, params):128sys.stdout.write('Deleting the IAM user policies... ')129list_user_policies = self.iam.get_paginator('list_user_policies')130try:131for response in list_user_policies.paginate(132UserName=params.user_name):133for policy_name in response['PolicyNames']:134self.iam.delete_user_policy(135UserName=params.user_name,136PolicyName=policy_name137)138except ClientError as e:139if e.response.get('Error', {}).get('Code') != 'NoSuchEntity':140raise e141sys.stdout.write('DONE\n')142143def _delete_access_key(self, params):144sys.stdout.write('Deleting the IAM user access keys... ')145list_access_keys = self.iam.get_paginator('list_access_keys')146try:147for response in list_access_keys.paginate(148UserName=params.user_name):149for access_key in response['AccessKeyMetadata']:150self.iam.delete_access_key(151UserName=params.user_name,152AccessKeyId=access_key['AccessKeyId']153)154except ClientError as e:155if e.response.get('Error', {}).get('Code') != 'NoSuchEntity':156raise e157sys.stdout.write('DONE\n')158159def _delete_iam_user(self, params):160sys.stdout.write('Deleting the IAM user ({0})... '.format(161params.user_name162))163try:164self.iam.delete_user(UserName=params.user_name)165except ClientError as e:166if e.response.get('Error', {}).get('Code') != 'NoSuchEntity':167raise e168sys.stdout.write('DONE\n')169170171