2
# -*- coding: utf-8 -*-
4
# Software License Agreement (BSD License)
6
# Copyright (c) 2009, Eucalyptus Systems, Inc.
9
# Redistribution and use of this software in source and binary forms, with or
10
# without modification, are permitted provided that the following conditions
13
# Redistributions of source code must retain the above
14
# copyright notice, this list of conditions and the
15
# following disclaimer.
17
# Redistributions in binary form must reproduce the above
18
# copyright notice, this list of conditions and the
19
# following disclaimer in the documentation and/or other
20
# materials provided with the distribution.
22
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
# POSSIBILITY OF SUCH DAMAGE.
34
# Author: Neil Soman neil@eucalyptus.com
39
from euca2ools import Euca2ool, Util, ConnectionFailed
45
euca-run-instances [-n, --instance-count count] [-g, --group group_name] [-k, --keypair keyname]
46
[-d user_data] [-f user_data_file] [--addressing addressing] [-t, --instance-type instance_type]
47
[-z, --availability-zone zone] [--kernel kernel_id] [--ramdisk ramdisk_id] [-b block_device_mapping]
48
[--monitor] [-s, --subnet subnet_id]
49
[-h, --help] [--version] [--debug] image_id
53
image_id identifier for the image to run.
57
-n, --instance-count Number of instances to run.
59
-g, --group Security group to run the instance under.
61
-k, --keypair Name of a (previously created) keypair to associate with this reservation.
62
-d, --user-data User data for instances read from the command line.
64
-f, --user-data-file User data for instances as a filename.
66
--addressing Addressing mode (e.g., private).
68
-t, --instance-type VM Image type to run the instance(s) as (default: m1.small).
70
-z, --availability-zone Availability zone to run the instance(s) in.
72
--kernel Id of the kernel to be used to launch instance(s).
74
--ramdisk Id of the ramdisk to be used to launch instance(s).
76
-b, --block-device-mapping Block device mapping for the instance(s). Option may be used multiple times.
78
--monitor Enable monitoring for instance.
80
-s, --subnet Amazon VPC subnet ID for the instance.
92
print Util().version()
96
def display_reservations(reservation):
97
reservation_string = '%s\t%s' % (reservation.id,
100
for group in reservation.groups:
101
reservation_string += '%s%s' % (group_delim, group.id)
103
print 'RESERVATION\t%s' % reservation_string
104
for instance in reservation.instances:
105
instance_string = '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s' % (
108
instance.public_dns_name,
109
instance.private_dns_name,
112
instance.launch_time,
116
print 'INSTANCE\t%s' % instance_string
119
def read_user_data(user_data_filename):
120
USER_DATA_CHUNK_SIZE = 512
122
user_data_file = open(user_data_filename, 'r')
124
data = user_data_file.read(USER_DATA_CHUNK_SIZE)
128
user_data_file.close()
135
euca = Euca2ool('k:n:t:g:d:f:z:b:', [
145
'availability-zone=',
146
'block-device-mapping=',
160
instance_type = 'm1.small'
163
user_data_file = None
164
addressing_type = None
166
block_device_map_args = []
167
block_device_map = None
170
for (name, value) in euca.opts:
171
if name in ('-k', '--key'):
173
elif name == '--kernel':
175
elif name == '--ramdisk':
177
elif name in ('-n', '--instance-count'):
178
counts = value.split('-')
180
min_count = int(counts[0])
181
max_count = int(counts[1])
183
min_count = max_count = int(counts[0])
184
elif name in ('-t', '--instance-type'):
185
instance_type = value
186
elif name in ('-g', '--group'):
187
group_names.append(value)
188
elif name in ('-d', '--user-data'):
190
elif name in ('-f', '--user-data-file'):
191
user_data_file = value
192
elif name == '--addressing':
193
addressing_type = value
194
elif name in ('-z', '--availability-zone'):
196
elif name in ('-b', '--block-device-mapping'):
197
block_device_map_args.append(value)
198
elif name == '--monitor':
200
elif name in ('-s', '--subnet'):
202
elif name in ('-h', '--help'):
204
elif name == '--version':
207
for arg in euca.args:
215
euca.validate_file(user_data_file)
216
except FileValidationError:
217
print 'Invalid user data file path'
219
user_data = read_user_data(user_data_file)
221
euca_conn = euca.make_connection()
222
except ConnectionFailed, e:
225
if block_device_map_args:
227
euca.parse_block_device_args(block_device_map_args)
229
reservation = euca_conn.run_instances(
234
security_groups=group_names,
236
addressing_type=addressing_type,
237
instance_type=instance_type,
240
ramdisk_id=ramdisk_id,
241
block_device_map=block_device_map,
242
monitoring_enabled=monitor,
245
except Exception, ex:
246
euca.display_error_and_exit('%s' % ex)
248
display_reservations(reservation)
250
print 'image_id must be specified'
254
if __name__ == '__main__':