-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamazon-test.py
221 lines (196 loc) · 7.21 KB
/
amazon-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import time
from calplus.v1.compute.drivers.amazon import AmazonDriver
from calplus.v1.block_storage.drivers.amazon import AmazonVolumeDriver
from calplus.v1.network.drivers.amazon import AmazonNetDriver
import argparse
amazon_config_driver = {
'driver_name': 'AMAZON1',
'aws_access_key_id': '9d0e9902b3984d22a179b28df675ad80',
'aws_secret_access_key': '8404f669a8a74195a15ad46171a3b548',
'region_name': 'RegionOne',
'endpoint_url': 'http://controller:8788',
'limit': {
}
}
class AmazonTest():
def __init__(self):
self.amazon_driver = AmazonDriver(amazon_config_driver)
self.amazon_volume_driver = AmazonVolumeDriver(amazon_config_driver)
self.amazon_net_driver = AmazonNetDriver(amazon_config_driver)
def create_vm_small_flavor(self, block_device_mapping=None):
started_time = int(round(time.time() * 1000))
image_id = 'ami-9e6af129'
# if block_device_mapping is not None:
# image_id = 'ami-9e6af129'
self.amazon_driver = AmazonDriver(amazon_config_driver)
result = self.amazon_driver.create(
'm1.small',
'subnet-94e5201d',
'cal', # keypair
block_device_mapping,
image_id,
)
print (result[0])
if(result[0]):
ended_time = int(round(time.time() * 1000))
print 'SMALL_FLAVOR CREATED_TIME (ms): ', ended_time - started_time
# self.delete_server(result[0].id)
return result[0]
def create_vm_medium_flavor(self, block_device_mapping=None):
started_time = int(round(time.time() * 1000))
image_id = 'ami-9e6af129'
# if block_device_mapping is not None:
# image_id = None
self.amazon_driver = AmazonDriver(amazon_config_driver)
result = self.amazon_driver.create(
block_device_mapping,
image_id,
'm1.medium',
'subnet-94e5201d',
'cal' # keypair
)
print (result[0])
if(result[0]):
ended_time = int(round(time.time() * 1000))
print 'MEDIUM_FLAVOR CREATED_TIME (ms): ', ended_time - started_time
# self.delete_server(result[0].id)
return result[0]
def create_vm_large_flavor(self, block_device_mapping=None):
started_time = int(round(time.time() * 1000))
image_id = 'ami-9e6af129'
# if block_device_mapping is not None:
# image_id = None
self.amazon_driver = AmazonDriver(amazon_config_driver)
result = self.amazon_driver.create(
block_device_mapping,
image_id,
'm1.large',
'subnet-94e5201d',
'cal' # keypair
)
print (result[0])
if(result[0]):
ended_time = int(round(time.time() * 1000))
print 'LARGE_FLAVOR CREATED_TIME (ms): ', ended_time - started_time
# self.delete_server(result[0].id)
return result[0]
def delete_server(self, id):
time.sleep(10)
print 'DELETED', id
self.amazon_driver.delete(id)
time.sleep(10)
def create_volume(self):
configs = {
'size': 15,
'name': 'Amazon-Cal-Vol-Test',
'snapshot_id': 'snap-e6bfaec3'
}
return self.amazon_volume_driver.create(configs)
def create_snapshot(self):
configs = {
'description': 'snapshot for testing CAL with EC2',
'name': 'cal-snapshot'
}
return self.amazon_volume_driver.create_snapshot(configs)
def associate_public_ip(self, allocation_id, instance_id):
started_time = int(round(time.time() * 1000))
configs = {
'allocation_id': allocation_id,
'instance_id': instance_id
}
result = self.amazon_driver.associate_public_ip(configs)
ended_time = int(round(time.time() * 1000))
print 'Associate Public IP in (ms): ', ended_time - started_time
return result
def is_valid_pub_ip(self, pub_ip):
search_opts = {
'Filters': [
{
'Name': 'network-interface.addresses.association.public-ip',
'Values': [
pub_ip,
]
},
]
}
instances = self.amazon_driver.list(**search_opts)
return False if len(instances) else True
def processArguments():
parser = argparse.ArgumentParser()
parser.add_argument('--flavor', dest='FLAVOR', type=str, default="")
parser.add_argument('--volume', dest='VOLUME', type=str, default="false")
parser.add_argument('--size', dest='SIZE', type=str, default="20")
parser.add_argument('--assign-ip', dest='ASSIGN', type=str, default="false")
args = parser.parse_args()
return args
def run():
args = processArguments()
amazon_test = AmazonTest()
if args.FLAVOR == 'small':
print 'small type vm'
amazon_test.create_vm_small_flavor()
elif args.FLAVOR == 'medium':
print 'medium type vm'
amazon_test.create_vm_medium_flavor()
elif args.FLAVOR == 'large':
print 'large type vm'
amazon_test.create_vm_large_flavor()
if args.VOLUME == 'true':
started_time = int(round(time.time() * 1000))
print 'create volume'
# volume = amazon_test.create_volume()
print 'create instance'
block_device_mapping = [
{
'DeviceName': '/dev/vdb',
'VirtualName': 'ephemeral0',
'Ebs': {
'DeleteOnTermination': False,
'SnapshotId': 'snap-e6bfaec3',
'VolumeSize': int(args.SIZE),
'VolumeType': '',
'Encrypted': False,
'KmsKeyId': ''
},
'NoDevice': ''
},
]
vm = amazon_test.create_vm_small_flavor(block_device_mapping)
ended_time = int(round(time.time() * 1000))
print 'CREATE VOLUME WITH SIZE ' + args.SIZE + 'GB IN (ms): ', ended_time - started_time
f = open('amazon_saved_vm.txt', 'w+')
f.write(str(vm.id)) # write to file
if args.ASSIGN != 'false':
print 'Associate Floating IP'
f = open("amazon_saved_vm.txt", "rw+")
vm_id = f.readline()
print vm_id
search_opts = {
'Filters': [
{
'Name': 'domain',
'Values': [
'vpc',
]
}
]
}
ips = amazon_test.amazon_net_driver.list_public_ip(**search_opts)
has_pub_ip = False
for ip in ips:
is_valid = amazon_test.is_valid_pub_ip(ip['public_ip'])
if is_valid:
has_pub_ip = True
amazon_test.associate_public_ip(ip['id'], vm_id)
break
if has_pub_ip == False:
print "Has no public IP"
# else:
# print 'all type vm'
# amazon_test.create_vm_small_flavor()
# time.sleep(3)
# amazon_test.create_vm_medium_flavor()
# time.sleep(3)
# amazon_test.create_vm_large_flavor()
if __name__ == "__main__":
run()