~ubuntu-branches/ubuntu/natty/euca2ools/natty-201106142030

« back to all changes in this revision

Viewing changes to .pc/print-instances-cleanup.patch/bin/euca-run-instances

  • Committer: Scott Moser
  • Date: 2010-11-17 21:23:16 UTC
  • Revision ID: smoser@ubuntu.com-20101117212316-is94e55u3jv46y9l
move to quilt 3.0 source format, re-apply all relevant delta

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Software License Agreement (BSD License)
 
5
#
 
6
# Copyright (c) 2009, Eucalyptus Systems, Inc.
 
7
# All rights reserved.
 
8
#
 
9
# Redistribution and use of this software in source and binary forms, with or
 
10
# without modification, are permitted provided that the following conditions
 
11
# are met:
 
12
#
 
13
#   Redistributions of source code must retain the above
 
14
#   copyright notice, this list of conditions and the
 
15
#   following disclaimer.
 
16
#
 
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.
 
21
#
 
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.
 
33
#
 
34
# Author: Neil Soman neil@eucalyptus.com
 
35
 
 
36
import getopt
 
37
import sys
 
38
import os
 
39
from euca2ools import Euca2ool, Util, ConnectionFailed
 
40
 
 
41
usage_string = \
 
42
    """
 
43
Starts instances.
 
44
 
 
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 
 
50
 
 
51
REQUIRED PARAMETERS
 
52
        
 
53
image_id                                identifier for the image to run. 
 
54
 
 
55
OPTIONAL PARAMETERS
 
56
 
 
57
-n, --instance-count                    Number of instances to run.
 
58
        
 
59
-g, --group                             Security group to run the instance under.
 
60
 
 
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.
 
63
 
 
64
-f, --user-data-file                    User data for instances as a filename.
 
65
 
 
66
--addressing                            Addressing mode (e.g., private).
 
67
 
 
68
-t, --instance-type                     VM Image type to run the instance(s) as (default: m1.small).
 
69
 
 
70
-z, --availability-zone                 Availability zone to run the instance(s) in.
 
71
 
 
72
--kernel                                Id of the kernel to be used to launch instance(s).
 
73
 
 
74
--ramdisk                               Id of the ramdisk to be used to launch instance(s).
 
75
 
 
76
-b, --block-device-mapping              Block device mapping for the instance(s). Option may be used multiple times.
 
77
 
 
78
--monitor                               Enable monitoring for instance.
 
79
 
 
80
-s, --subnet                            Amazon VPC subnet ID for the instance.
 
81
 
 
82
"""
 
83
 
 
84
 
 
85
def usage(status=1):
 
86
    print usage_string
 
87
    Util().usage()
 
88
    sys.exit(status)
 
89
 
 
90
 
 
91
def version():
 
92
    print Util().version()
 
93
    sys.exit()
 
94
 
 
95
 
 
96
def display_reservations(reservation):
 
97
    reservation_string = '%s\t%s' % (reservation.id,
 
98
            reservation.owner_id)
 
99
    group_delim = '\t'
 
100
    for group in reservation.groups:
 
101
        reservation_string += '%s%s' % (group_delim, group.id)
 
102
        group_delim = ', '
 
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' % (
 
106
            instance.id,
 
107
            instance.image_id,
 
108
            instance.public_dns_name,
 
109
            instance.private_dns_name,
 
110
            instance.state,
 
111
            instance.key_name,
 
112
            instance.launch_time,
 
113
            instance.kernel,
 
114
            instance.ramdisk,
 
115
            )
 
116
        print 'INSTANCE\t%s' % instance_string
 
117
 
 
118
 
 
119
def read_user_data(user_data_filename):
 
120
    USER_DATA_CHUNK_SIZE = 512
 
121
    user_data = ''
 
122
    user_data_file = open(user_data_filename, 'r')
 
123
    while 1:
 
124
        data = user_data_file.read(USER_DATA_CHUNK_SIZE)
 
125
        if not data:
 
126
            break
 
127
        user_data += data
 
128
    user_data_file.close()
 
129
    return user_data
 
130
 
 
131
 
 
132
def main():
 
133
    euca = None
 
134
    try:
 
135
        euca = Euca2ool('k:n:t:g:d:f:z:b:', [
 
136
            'key=',
 
137
            'kernel=',
 
138
            'ramdisk=',
 
139
            'instance-count=',
 
140
            'instance-type=',
 
141
            'group=',
 
142
            'user-data=',
 
143
            'user-data-file=',
 
144
            'addressing=',
 
145
            'availability-zone=',
 
146
            'block-device-mapping=',
 
147
            'monitor',
 
148
            'subnet_id=',
 
149
            ])
 
150
    except Exception, e:
 
151
        print e
 
152
        usage()
 
153
 
 
154
    image_id = None
 
155
    keyname = None
 
156
    kernel_id = None
 
157
    ramdisk_id = None
 
158
    min_count = 1
 
159
    max_count = 1
 
160
    instance_type = 'm1.small'
 
161
    group_names = []
 
162
    user_data = None
 
163
    user_data_file = None
 
164
    addressing_type = None
 
165
    zone = None
 
166
    block_device_map_args = []
 
167
    block_device_map = None
 
168
    monitor = False
 
169
    subnet_id = None
 
170
    for (name, value) in euca.opts:
 
171
        if name in ('-k', '--key'):
 
172
            keyname = value
 
173
        elif name == '--kernel':
 
174
            kernel_id = value
 
175
        elif name == '--ramdisk':
 
176
            ramdisk_id = value
 
177
        elif name in ('-n', '--instance-count'):
 
178
            counts = value.split('-')
 
179
            try:
 
180
                if (len(counts) > 1):
 
181
                    min_count = int(counts[0])
 
182
                    max_count = int(counts[1])
 
183
                else:
 
184
                    min_count = max_count = int(counts[0])
 
185
            except ValueError:
 
186
                print "Invalid value for --instance-count: ", value
 
187
                sys.exit(1)
 
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'):
 
193
            user_data = value
 
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'):
 
199
            zone = value
 
200
        elif name in ('-b', '--block-device-mapping'):
 
201
            block_device_map_args.append(value)
 
202
        elif name == '--monitor':
 
203
            monitor = True
 
204
        elif name in ('-s', '--subnet'):
 
205
            subnet_id = value
 
206
        elif name in ('-h', '--help'):
 
207
            usage(0)
 
208
        elif name == '--version':
 
209
            version()
 
210
 
 
211
    for arg in euca.args:
 
212
        image_id = arg
 
213
        break
 
214
 
 
215
    if image_id:
 
216
        if not user_data:
 
217
            if user_data_file:
 
218
                try:
 
219
                    euca.validate_file(user_data_file)
 
220
                except FileValidationError:
 
221
                    print 'Invalid user data file path'
 
222
                    sys.exit(1)
 
223
                user_data = read_user_data(user_data_file)
 
224
        try:
 
225
            euca_conn = euca.make_connection()
 
226
        except ConnectionFailed, e:
 
227
            print e.message
 
228
            sys.exit(1)
 
229
        if block_device_map_args:
 
230
            block_device_map = \
 
231
                euca.parse_block_device_args(block_device_map_args)
 
232
        try:
 
233
            reservation = euca_conn.run_instances(
 
234
                image_id=image_id,
 
235
                min_count=min_count,
 
236
                max_count=max_count,
 
237
                key_name=keyname,
 
238
                security_groups=group_names,
 
239
                user_data=user_data,
 
240
                addressing_type=addressing_type,
 
241
                instance_type=instance_type,
 
242
                placement=zone,
 
243
                kernel_id=kernel_id,
 
244
                ramdisk_id=ramdisk_id,
 
245
                block_device_map=block_device_map,
 
246
                monitoring_enabled=monitor,
 
247
                subnet_id=subnet_id
 
248
                )
 
249
        except Exception, ex:
 
250
            euca.display_error_and_exit('%s' % ex)
 
251
 
 
252
        display_reservations(reservation)
 
253
    else:
 
254
        print 'image_id must be specified'
 
255
        usage()
 
256
 
 
257
 
 
258
if __name__ == '__main__':
 
259
    main()
 
260