Path: blob/develop/awscli/customizations/codedeploy/register.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 awscli.customizations.commands import BasicCommand16from awscli.customizations.codedeploy.systems import DEFAULT_CONFIG_FILE17from awscli.customizations.codedeploy.utils import \18validate_region, validate_instance_name, validate_tags, \19validate_iam_user_arn, INSTANCE_NAME_ARG, IAM_USER_ARN_ARG20from awscli.utils import create_nested_client212223class Register(BasicCommand):24NAME = 'register'2526DESCRIPTION = (27"Creates an IAM user for the on-premises instance, if not provided, "28"and saves the user's credentials to an on-premises instance "29"configuration file; registers the on-premises instance with AWS "30"CodeDeploy; and optionally adds tags to the on-premises instance."31)3233TAGS_SCHEMA = {34"type": "array",35"items": {36"type": "object",37"properties": {38"Key": {39"description": "The tag key.",40"type": "string",41"required": True42},43"Value": {44"description": "The tag value.",45"type": "string",46"required": True47}48}49}50}5152ARG_TABLE = [53INSTANCE_NAME_ARG,54{55'name': 'tags',56'synopsis': '--tags <value>',57'required': False,58'nargs': '+',59'schema': TAGS_SCHEMA,60'help_text': (61'Optional. The list of key/value pairs to tag the on-premises '62'instance.'63)64},65IAM_USER_ARN_ARG66]6768def _run_main(self, parsed_args, parsed_globals):69params = parsed_args70params.session = self._session71validate_region(params, parsed_globals)72validate_instance_name(params)73validate_tags(params)74validate_iam_user_arn(params)7576self.codedeploy = create_nested_client(77self._session,78'codedeploy',79region_name=params.region,80endpoint_url=parsed_globals.endpoint_url,81verify=parsed_globals.verify_ssl82)83self.iam = create_nested_client(84self._session,85'iam',86region_name=params.region87)8889try:90if not params.iam_user_arn:91self._create_iam_user(params)92self._create_access_key(params)93self._create_user_policy(params)94self._create_config(params)95self._register_instance(params)96if params.tags:97self._add_tags(params)98sys.stdout.write(99'Copy the on-premises configuration file named {0} to the '100'on-premises instance, and run the following command on the '101'on-premises instance to install and configure the AWS '102'CodeDeploy Agent:\n'103'aws deploy install --config-file {0}\n'.format(104DEFAULT_CONFIG_FILE105)106)107except Exception as e:108sys.stdout.flush()109sys.stderr.write(110'ERROR\n'111'{0}\n'112'Register the on-premises instance by following the '113'instructions in "Configure Existing On-Premises Instances by '114'Using AWS CodeDeploy" in the AWS CodeDeploy User '115'Guide.\n'.format(e)116)117118def _create_iam_user(self, params):119sys.stdout.write('Creating the IAM user... ')120params.user_name = params.instance_name121response = self.iam.create_user(122Path='/AWS/CodeDeploy/',123UserName=params.user_name124)125params.iam_user_arn = response['User']['Arn']126sys.stdout.write(127'DONE\n'128'IamUserArn: {0}\n'.format(129params.iam_user_arn130)131)132133def _create_access_key(self, params):134sys.stdout.write('Creating the IAM user access key... ')135response = self.iam.create_access_key(136UserName=params.user_name137)138params.access_key_id = response['AccessKey']['AccessKeyId']139params.secret_access_key = response['AccessKey']['SecretAccessKey']140sys.stdout.write(141'DONE\n'142'AccessKeyId: {0}\n'143'SecretAccessKey: {1}\n'.format(144params.access_key_id,145params.secret_access_key146)147)148149def _create_user_policy(self, params):150sys.stdout.write('Creating the IAM user policy... ')151params.policy_name = 'codedeploy-agent'152params.policy_document = (153'{\n'154' "Version": "2012-10-17",\n'155' "Statement": [ {\n'156' "Action": [ "s3:Get*", "s3:List*" ],\n'157' "Effect": "Allow",\n'158' "Resource": "*"\n'159' } ]\n'160'}'161)162self.iam.put_user_policy(163UserName=params.user_name,164PolicyName=params.policy_name,165PolicyDocument=params.policy_document166)167sys.stdout.write(168'DONE\n'169'PolicyName: {0}\n'170'PolicyDocument: {1}\n'.format(171params.policy_name,172params.policy_document173)174)175176def _create_config(self, params):177sys.stdout.write(178'Creating the on-premises instance configuration file named {0}'179'...'.format(DEFAULT_CONFIG_FILE)180)181with open(DEFAULT_CONFIG_FILE, 'w') as f:182f.write(183'---\n'184'region: {0}\n'185'iam_user_arn: {1}\n'186'aws_access_key_id: {2}\n'187'aws_secret_access_key: {3}\n'.format(188params.region,189params.iam_user_arn,190params.access_key_id,191params.secret_access_key192)193)194sys.stdout.write('DONE\n')195196def _register_instance(self, params):197sys.stdout.write('Registering the on-premises instance... ')198self.codedeploy.register_on_premises_instance(199instanceName=params.instance_name,200iamUserArn=params.iam_user_arn201)202sys.stdout.write('DONE\n')203204def _add_tags(self, params):205sys.stdout.write('Adding tags to the on-premises instance... ')206self.codedeploy.add_tags_to_on_premises_instances(207tags=params.tags,208instanceNames=[params.instance_name]209)210sys.stdout.write('DONE\n')211212213