Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aws
GitHub Repository: aws/aws-cli
Path: blob/develop/awscli/customizations/codedeploy/utils.py
1567 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 platform
15
import re
16
17
import awscli.compat
18
from awscli.compat import urlopen, URLError
19
from awscli.customizations.codedeploy.systems import System, Ubuntu, Windows, RHEL
20
from socket import timeout
21
22
23
MAX_INSTANCE_NAME_LENGTH = 100
24
MAX_TAGS_PER_INSTANCE = 10
25
MAX_TAG_KEY_LENGTH = 128
26
MAX_TAG_VALUE_LENGTH = 256
27
28
INSTANCE_NAME_PATTERN = r'^[A-Za-z0-9+=,.@_-]+$'
29
IAM_USER_ARN_PATTERN = r'^arn:aws:iam::[0-9]{12}:user/[A-Za-z0-9/+=,.@_-]+$'
30
31
INSTANCE_NAME_ARG = {
32
'name': 'instance-name',
33
'synopsis': '--instance-name <instance-name>',
34
'required': True,
35
'help_text': (
36
'Required. The name of the on-premises instance.'
37
)
38
}
39
40
IAM_USER_ARN_ARG = {
41
'name': 'iam-user-arn',
42
'synopsis': '--iam-user-arn <iam-user-arn>',
43
'required': False,
44
'help_text': (
45
'Optional. The IAM user associated with the on-premises instance.'
46
)
47
}
48
49
50
def validate_region(params, parsed_globals):
51
if parsed_globals.region:
52
params.region = parsed_globals.region
53
else:
54
params.region = params.session.get_config_variable('region')
55
if not params.region:
56
raise RuntimeError('Region not specified.')
57
58
59
def validate_instance_name(params):
60
if params.instance_name:
61
if not re.match(INSTANCE_NAME_PATTERN, params.instance_name):
62
raise ValueError('Instance name contains invalid characters.')
63
if params.instance_name.startswith('i-'):
64
raise ValueError('Instance name cannot start with \'i-\'.')
65
if len(params.instance_name) > MAX_INSTANCE_NAME_LENGTH:
66
raise ValueError(
67
'Instance name cannot be longer than {0} characters.'.format(
68
MAX_INSTANCE_NAME_LENGTH
69
)
70
)
71
72
73
def validate_tags(params):
74
if params.tags:
75
if len(params.tags) > MAX_TAGS_PER_INSTANCE:
76
raise ValueError(
77
'Instances can only have a maximum of {0} tags.'.format(
78
MAX_TAGS_PER_INSTANCE
79
)
80
)
81
for tag in params.tags:
82
if len(tag['Key']) > MAX_TAG_KEY_LENGTH:
83
raise ValueError(
84
'Tag Key cannot be longer than {0} characters.'.format(
85
MAX_TAG_KEY_LENGTH
86
)
87
)
88
if len(tag['Value']) > MAX_TAG_VALUE_LENGTH:
89
raise ValueError(
90
'Tag Value cannot be longer than {0} characters.'.format(
91
MAX_TAG_VALUE_LENGTH
92
)
93
)
94
95
96
def validate_iam_user_arn(params):
97
if params.iam_user_arn and \
98
not re.match(IAM_USER_ARN_PATTERN, params.iam_user_arn):
99
raise ValueError('Invalid IAM user ARN.')
100
101
102
def validate_instance(params):
103
if platform.system() == 'Linux':
104
distribution = awscli.compat.linux_distribution()[0]
105
if 'Ubuntu' in distribution:
106
params.system = Ubuntu(params)
107
if 'Red Hat Enterprise Linux Server' in distribution:
108
params.system = RHEL(params)
109
elif platform.system() == 'Windows':
110
params.system = Windows(params)
111
if 'system' not in params:
112
raise RuntimeError(
113
System.UNSUPPORTED_SYSTEM_MSG
114
)
115
try:
116
urlopen('http://169.254.169.254/latest/meta-data/', timeout=1)
117
raise RuntimeError('Amazon EC2 instances are not supported.')
118
except (URLError, timeout):
119
pass
120
121
122
def validate_s3_location(params, arg_name):
123
arg_name = arg_name.replace('-', '_')
124
if arg_name in params:
125
s3_location = getattr(params, arg_name)
126
if s3_location:
127
matcher = re.match('s3://(.+?)/(.+)', str(s3_location))
128
if matcher:
129
params.bucket = matcher.group(1)
130
params.key = matcher.group(2)
131
else:
132
raise ValueError(
133
'--{0} must specify the Amazon S3 URL format as '
134
's3://<bucket>/<key>.'.format(
135
arg_name.replace('_', '-')
136
)
137
)
138
139