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, --key 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, --key 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
if (len(counts) > 1):
181
min_count = int(counts[0])
182
max_count = int(counts[1])
184
min_count = max_count = int(counts[0])
186
print "Invalid value for --instance-count: ", value
188
elif name in ('-t', '--instance-type'):
189
instance_type = value
190
elif name in ('-g', '--group'):
191
group_names.append(value)
192
elif name in ('-d', '--user-data'):
194
elif name in ('-f', '--user-data-file'):
195
user_data_file = value
196
elif name == '--addressing':
197
addressing_type = value
198
elif name in ('-z', '--availability-zone'):
200
elif name in ('-b', '--block-device-mapping'):
201
block_device_map_args.append(value)
202
elif name == '--monitor':
204
elif name in ('-s', '--subnet'):
206
elif name in ('-h', '--help'):
208
elif name == '--version':
211
for arg in euca.args:
219
euca.validate_file(user_data_file)
220
except FileValidationError:
221
print 'Invalid user data file path'
223
user_data = read_user_data(user_data_file)
225
euca_conn = euca.make_connection()
226
except ConnectionFailed, e:
229
if block_device_map_args:
231
euca.parse_block_device_args(block_device_map_args)
233
reservation = euca_conn.run_instances(
238
security_groups=group_names,
240
addressing_type=addressing_type,
241
instance_type=instance_type,
244
ramdisk_id=ramdisk_id,
245
block_device_map=block_device_map,
246
monitoring_enabled=monitor,
249
except Exception, ex:
250
euca.display_error_and_exit('%s' % ex)
252
display_reservations(reservation)
254
print 'image_id must be specified'
258
if __name__ == '__main__':