~ubuntu-branches/ubuntu/precise/euca2ools/precise

« back to all changes in this revision

Viewing changes to .pc/run-instances-usage-and-manpage-keypair.patch/bin/euca-run-instances

  • Committer: Bazaar Package Importer
  • Author(s): Scott Moser
  • Date: 2011-08-11 17:17:12 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20110811171712-fvtblni2lfvv6q03
Tags: 2.0.0~bzr451-0ubuntu1
* Move to new snapshot of upstream bzr in anticipation of 2.0.0 release
* update standards version

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, --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 
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, --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.
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
 
            if len(counts) > 1:
180
 
                min_count = int(counts[0])
181
 
                max_count = int(counts[1])
182
 
            else:
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'):
189
 
            user_data = value
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'):
195
 
            zone = value
196
 
        elif name in ('-b', '--block-device-mapping'):
197
 
            block_device_map_args.append(value)
198
 
        elif name == '--monitor':
199
 
            monitor = True
200
 
        elif name in ('-s', '--subnet'):
201
 
            subnet_id = value
202
 
        elif name in ('-h', '--help'):
203
 
            usage(0)
204
 
        elif name == '--version':
205
 
            version()
206
 
 
207
 
    for arg in euca.args:
208
 
        image_id = arg
209
 
        break
210
 
 
211
 
    if image_id:
212
 
        if not user_data:
213
 
            if user_data_file:
214
 
                try:
215
 
                    euca.validate_file(user_data_file)
216
 
                except FileValidationError:
217
 
                    print 'Invalid user data file path'
218
 
                    sys.exit(1)
219
 
                user_data = read_user_data(user_data_file)
220
 
        try:
221
 
            euca_conn = euca.make_connection()
222
 
        except ConnectionFailed, e:
223
 
            print e.message
224
 
            sys.exit(1)
225
 
        if block_device_map_args:
226
 
            block_device_map = \
227
 
                euca.parse_block_device_args(block_device_map_args)
228
 
        try:
229
 
            reservation = euca_conn.run_instances(
230
 
                image_id=image_id,
231
 
                min_count=min_count,
232
 
                max_count=max_count,
233
 
                key_name=keyname,
234
 
                security_groups=group_names,
235
 
                user_data=user_data,
236
 
                addressing_type=addressing_type,
237
 
                instance_type=instance_type,
238
 
                placement=zone,
239
 
                kernel_id=kernel_id,
240
 
                ramdisk_id=ramdisk_id,
241
 
                block_device_map=block_device_map,
242
 
                monitoring_enabled=monitor,
243
 
                subnet_id=subnet_id
244
 
                )
245
 
        except Exception, ex:
246
 
            euca.display_error_and_exit('%s' % ex)
247
 
 
248
 
        display_reservations(reservation)
249
 
    else:
250
 
        print 'image_id must be specified'
251
 
        usage()
252
 
 
253
 
 
254
 
if __name__ == '__main__':
255
 
    main()
256