-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathaddcount.py
96 lines (74 loc) · 2.92 KB
/
addcount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import logging
from botocore import model
from awscli.arguments import BaseCLIArgument
logger = logging.getLogger(__name__)
DEFAULT = 1
HELP = """
<p>Number of instances to launch. If a single number is provided, it
is assumed to be the minimum to launch (defaults to %d). If a range is
provided in the form <code>min:max</code> then the first number is
interpreted as the minimum number of instances to launch and the second
is interpreted as the maximum number of instances to launch.</p>""" % DEFAULT
def register_count_events(event_handler):
event_handler.register(
'building-argument-table.ec2.run-instances', ec2_add_count)
event_handler.register(
'before-parameter-build.ec2.RunInstances', set_default_count)
def ec2_add_count(argument_table, **kwargs):
argument_table['count'] = CountArgument('count')
del argument_table['min-count']
del argument_table['max-count']
def set_default_count(params, **kwargs):
params.setdefault('MaxCount', DEFAULT)
params.setdefault('MinCount', DEFAULT)
class CountArgument(BaseCLIArgument):
def __init__(self, name):
self.argument_model = model.Shape('CountArgument', {'type': 'string'})
self._name = name
self._required = False
@property
def cli_name(self):
return '--' + self._name
@property
def cli_type_name(self):
return 'string'
@property
def required(self):
return self._required
@required.setter
def required(self, value):
self._required = value
@property
def documentation(self):
return HELP
def add_to_parser(self, parser):
# We do NOT set default value here. It will be set later by event hook.
parser.add_argument(self.cli_name, metavar=self.py_name,
help='Number of instances to launch')
def add_to_params(self, parameters, value):
if value is None:
# NO-OP if value is not explicitly set by user
return
try:
if ':' in value:
minstr, maxstr = value.split(':')
else:
minstr, maxstr = (value, value)
parameters['MinCount'] = int(minstr)
parameters['MaxCount'] = int(maxstr)
except:
msg = ('count parameter should be of '
'form min[:max] (e.g. 1 or 1:10)')
raise ValueError(msg)