1
Description: combine duplicate print-instaces code
2
The code had 2 locations where it prints instances,
3
describe-instances and run-instances. The implementations are different
4
when they should be the same.
6
The bug that is fixed here is that in describe-instances, there is a
7
trailing space on each field ("running ", not "running").
8
But overall, this patch reduces code.
10
Originally from 1.2-0ubuntu8 package version
11
Author: Scott Moser <scott.moser@canonical.com>
12
Last-Update: 2010-11-17
13
Bug: https://launchpad.net/bugs/531453
15
--- a/bin/euca-describe-instances
16
+++ b/bin/euca-describe-instances
20
from euca2ools import Euca2ool, InstanceValidationError, Util, \
22
+ ConnectionFailed, print_instances
27
reservation_string += '%s%s' % (group_delim, group.id)
29
print 'RESERVATION\t%s' % reservation_string
30
- for instance in instances:
32
- instance_string = '%s\t%s\t%s\t%s\t%s' % (instance.id,
33
- instance.image_id, instance.public_dns_name,
34
- instance.private_dns_name, instance.state)
35
- if instance.key_name:
36
- instance_string += ' \t%s' % instance.key_name
37
- if instance.ami_launch_index:
38
- instance_string += ' \t%s' \
39
- % instance.ami_launch_index
40
- if instance.product_codes:
42
- for p in instance.product_codes:
44
- instance_string += ' \t%s' % p
47
- instance_string += ',%s' % p
48
- if instance.instance_type:
49
- instance_string += ' \t%s' % instance.instance_type
50
- if instance.launch_time:
51
- instance_string += ' \t%s' % instance.launch_time
52
- if instance.placement:
53
- instance_string += ' \t%s' % instance.placement
55
- instance_string += ' \t%s' % instance.kernel
56
- if instance.ramdisk:
57
- instance_string += ' \t%s' % instance.ramdisk
58
- print 'INSTANCE\t%s' % instance_string
60
+ print_instances(instances)
64
--- a/euca2ools/euca2ools/__init__.py
65
+++ b/euca2ools/euca2ools/__init__.py
66
@@ -1456,3 +1456,23 @@
67
dict[keylist[i]] = values[i]
70
+def print_instances(instances, nil=""):
71
+ members=( "id", "image_id", "public_dns_name", "private_dns_name",
72
+ "state", "key_name", "ami_launch_index", "product_codes",
73
+ "instance_type", "launch_time", "placement", "kernel",
76
+ for instance in instances:
77
+ # in old describe-instances, there was a check for 'if instance:'
78
+ # I (smoser) have carried this over, but dont know how instance
80
+ if not instance: continue
82
+ for member in members:
83
+ val = getattr(instance,member,nil)
84
+ # product_codes is a list
85
+ if val is None: val = nil
86
+ if hasattr(val,'__iter__'):
89
+ print "INSTANCE\t%s" % '\t'.join(items)
90
--- a/bin/euca-run-instances
91
+++ b/bin/euca-run-instances
96
-from euca2ools import Euca2ool, Util, ConnectionFailed
97
+from euca2ools import Euca2ool, Util, ConnectionFailed, print_instances
102
reservation_string += '%s%s' % (group_delim, group.id)
104
print 'RESERVATION\t%s' % reservation_string
105
- for instance in reservation.instances:
106
- instance_string = '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s' % (
109
- instance.public_dns_name,
110
- instance.private_dns_name,
113
- instance.launch_time,
117
- print 'INSTANCE\t%s' % instance_string
119
+ print_instances(reservation.instances)
121
def read_user_data(user_data_filename):
122
USER_DATA_CHUNK_SIZE = 512