Path: blob/develop/awscli/customizations/codedeploy/utils.py
1567 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 platform14import re1516import awscli.compat17from awscli.compat import urlopen, URLError18from awscli.customizations.codedeploy.systems import System, Ubuntu, Windows, RHEL19from socket import timeout202122MAX_INSTANCE_NAME_LENGTH = 10023MAX_TAGS_PER_INSTANCE = 1024MAX_TAG_KEY_LENGTH = 12825MAX_TAG_VALUE_LENGTH = 2562627INSTANCE_NAME_PATTERN = r'^[A-Za-z0-9+=,.@_-]+$'28IAM_USER_ARN_PATTERN = r'^arn:aws:iam::[0-9]{12}:user/[A-Za-z0-9/+=,.@_-]+$'2930INSTANCE_NAME_ARG = {31'name': 'instance-name',32'synopsis': '--instance-name <instance-name>',33'required': True,34'help_text': (35'Required. The name of the on-premises instance.'36)37}3839IAM_USER_ARN_ARG = {40'name': 'iam-user-arn',41'synopsis': '--iam-user-arn <iam-user-arn>',42'required': False,43'help_text': (44'Optional. The IAM user associated with the on-premises instance.'45)46}474849def validate_region(params, parsed_globals):50if parsed_globals.region:51params.region = parsed_globals.region52else:53params.region = params.session.get_config_variable('region')54if not params.region:55raise RuntimeError('Region not specified.')565758def validate_instance_name(params):59if params.instance_name:60if not re.match(INSTANCE_NAME_PATTERN, params.instance_name):61raise ValueError('Instance name contains invalid characters.')62if params.instance_name.startswith('i-'):63raise ValueError('Instance name cannot start with \'i-\'.')64if len(params.instance_name) > MAX_INSTANCE_NAME_LENGTH:65raise ValueError(66'Instance name cannot be longer than {0} characters.'.format(67MAX_INSTANCE_NAME_LENGTH68)69)707172def validate_tags(params):73if params.tags:74if len(params.tags) > MAX_TAGS_PER_INSTANCE:75raise ValueError(76'Instances can only have a maximum of {0} tags.'.format(77MAX_TAGS_PER_INSTANCE78)79)80for tag in params.tags:81if len(tag['Key']) > MAX_TAG_KEY_LENGTH:82raise ValueError(83'Tag Key cannot be longer than {0} characters.'.format(84MAX_TAG_KEY_LENGTH85)86)87if len(tag['Value']) > MAX_TAG_VALUE_LENGTH:88raise ValueError(89'Tag Value cannot be longer than {0} characters.'.format(90MAX_TAG_VALUE_LENGTH91)92)939495def validate_iam_user_arn(params):96if params.iam_user_arn and \97not re.match(IAM_USER_ARN_PATTERN, params.iam_user_arn):98raise ValueError('Invalid IAM user ARN.')99100101def validate_instance(params):102if platform.system() == 'Linux':103distribution = awscli.compat.linux_distribution()[0]104if 'Ubuntu' in distribution:105params.system = Ubuntu(params)106if 'Red Hat Enterprise Linux Server' in distribution:107params.system = RHEL(params)108elif platform.system() == 'Windows':109params.system = Windows(params)110if 'system' not in params:111raise RuntimeError(112System.UNSUPPORTED_SYSTEM_MSG113)114try:115urlopen('http://169.254.169.254/latest/meta-data/', timeout=1)116raise RuntimeError('Amazon EC2 instances are not supported.')117except (URLError, timeout):118pass119120121def validate_s3_location(params, arg_name):122arg_name = arg_name.replace('-', '_')123if arg_name in params:124s3_location = getattr(params, arg_name)125if s3_location:126matcher = re.match('s3://(.+?)/(.+)', str(s3_location))127if matcher:128params.bucket = matcher.group(1)129params.key = matcher.group(2)130else:131raise ValueError(132'--{0} must specify the Amazon S3 URL format as '133's3://<bucket>/<key>.'.format(134arg_name.replace('_', '-')135)136)137138139