-
Notifications
You must be signed in to change notification settings - Fork 0
/
heavenly_cloud_service_wrapper.py
364 lines (292 loc) · 16.8 KB
/
heavenly_cloud_service_wrapper.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import uuid
import os
from cloudshell.cp.core.models import *
from data_model import *
from sdk.heavenly_cloud_service import HeavenlyCloudService
import json
from typing import List
class HeavenlyCloudServiceWrapper(object):
@staticmethod
def deploy_angel(context, cloudshell_session, cloud_provider_resource,deploy_app_action, cancellation_context):
"""
:param ResourceCommandContext context:
:param CloudShellAPISession cloudshell_session:
:param L2HeavenlyCloudShell cloud_provider_resource:
:param DeployApp deploy_app_action:
:param CancellationContext cancellation_context:
"""
if cancellation_context.is_cancelled:
HeavenlyCloudService.rollback()
return DeployAppResult(actionId=deploy_app_action.actionId, success=False,
errorMessage='Operation canceled')
# deployment_model type : HeavenlyCloudAngelDeploymentModel
deployment_model = deploy_app_action.actionParams.deployment.customModel
# generate unique name to avoid name collisions
vm_unique_name = deploy_app_action.actionParams.appName + '__' + str(uuid.uuid4())[:6]
input_user = deploy_app_action.actionParams.appResource.attributes['User']
input_password = cloudshell_session.DecryptPassword(deploy_app_action.actionParams.appResource.attributes['Password']).Value
new_pass = HeavenlyCloudService.create_new_password(cloud_provider_resource,input_user,input_password)
try:
# using cloud provider SDK, creating the instance
vm_instance = HeavenlyCloudService.create_angel_instance(input_user,new_pass,cloud_provider_resource,vm_unique_name,
deployment_model.wing_count,
deployment_model.flight_speed,
deployment_model.cloud_size,
deployment_model.cloud_image_id)
except Exception as e:
return DeployAppResult(actionId=deploy_app_action.actionId, success=False, errorMessage=e.message)
# Creating VmDetailsData
vm_details_data = HeavenlyCloudServiceWrapper.extract_vm_details(vm_instance)
# result must include the action id it results for, so server can match result to action
action_id = deploy_app_action.actionId
# optional
# deployedAppAttributes contains the attributes on the deployed app
# use to override attributes default values
deployed_app_attributes = []
deployed_app_attributes.append(Attribute('Password',input_password + '_decrypted'))
# optional
# deployedAppAdditionalData can contain dynamic data on the deployed app
# similar to AWS tags
deployed_app_additional_data_dict = {'Reservation Id': context.reservation.reservation_id , 'CreatedBy' : str(os.path.abspath(__file__))}
return DeployAppResult(actionId=action_id, success=True, vmUuid=vm_instance.id, vmName=vm_unique_name,
deployedAppAddress=vm_instance.private_ip, deployedAppAttributes=deployed_app_attributes,
deployedAppAdditionalData=deployed_app_additional_data_dict,
vmDetailsData=vm_details_data)
@staticmethod
def deploy_man(context, cloudshell_session, cloud_provider_resource, deploy_app_action, cancellation_context):
"""
:param ResourceCommandContext context:
:param CloudShellAPISession cloudshell_session:
:param L2HeavenlyCloudShell cloud_provider_resource:
:param DeployApp deploy_app_action:
:param CancellationContext cancellation_context:
"""
if cancellation_context.is_cancelled:
HeavenlyCloudService.rollback()
return DeployAppResult(actionId=deploy_app_action.actionId, success=False,
errorMessage='Operation canceled')
# deployment_model type : HeavenlyCloudAngelDeploymentModel
deployment_model = deploy_app_action.actionParams.deployment.customModel
# generate unique name to avoid name collisions
vm_unique_name = deploy_app_action.actionParams.appName + '__' + str(uuid.uuid4())[:6]
input_user = deploy_app_action.actionParams.appResource.attributes['User']
encrypted_pass = deploy_app_action.actionParams.appResource.attributes['Password']
# decrypt password using cloudshell session
input_password = cloudshell_session.DecryptPassword(encrypted_pass).Value
new_pass = HeavenlyCloudService.create_new_password(cloud_provider_resource, input_user, input_password)
try:
# using cloud provider SDK, creating the instance
vm_instance = HeavenlyCloudService.create_man_instance(input_user, new_pass, cloud_provider_resource,
vm_unique_name,
deployment_model.weight,
deployment_model.height,
deployment_model.cloud_size,
deployment_model.cloud_image_id)
except Exception as e:
return DeployAppResult(actionId=deploy_app_action.actionId, success=False, errorMessage=e.message)
# Creating VmDetailsData
vm_details_data = HeavenlyCloudServiceWrapper.extract_vm_details(vm_instance)
# result must include the action id it results for, so server can match result to action
action_id = deploy_app_action.actionId
# optional
# deployedAppAttributes contains the attributes on the deployed app
# use to override attributes default values
deployed_app_attributes = []
deployed_app_attributes.append(Attribute('Password', input_password + '_decrypted'))
# optional
# deployedAppAdditionalData can contain dynamic data on the deployed app
# similar to AWS tags
deployed_app_additional_data_dict = {'Reservation Id': context.reservation.reservation_id,
'CreatedBy': str(os.path.abspath(__file__))}
return DeployAppResult(actionId=action_id, success=True, vmUuid=vm_instance.id, vmName=vm_unique_name,
deployedAppAddress=vm_instance.private_ip, deployedAppAttributes=deployed_app_attributes,
deployedAppAdditionalData=deployed_app_additional_data_dict,
vmDetailsData=vm_details_data)
@staticmethod
def extract_vm_details(vm_instance):
"""
:param HeavenResidentInstance vm_instance:
"""
vm_instance_data = HeavenlyCloudServiceWrapper.extract_vm_instance_data(vm_instance)
vm_network_data = HeavenlyCloudServiceWrapper.extract_vm_instance_network_data(vm_instance)
return VmDetailsData(vmInstanceData=vm_instance_data, vmNetworkData=vm_network_data)
@staticmethod
def extract_vm_instance_data(vm_instance):
"""
:param HeavenResidentInstance vm_instance:
"""
data = [
VmDetailsProperty(key='Cloud Size', value='not so big'),
VmDetailsProperty(key='Instance Name', value='dummy' if not vm_instance else vm_instance.name),
VmDetailsProperty(key='Hidden stuff', value='something not for UI',hidden=True),
]
return data
@staticmethod
def extract_vm_instance_network_data(vm_instance):
"""
:param HeavenResidentInstance vm_instance:
"""
network_interfaces = []
# for each network interface
for i in range(2):
network_data = [
VmDetailsProperty(key='Device Index', value=str(i)),
VmDetailsProperty(key='MAC Address', value=str(uuid.uuid4())),
VmDetailsProperty(key='Speed', value='1KB'),
]
current_interface = VmDetailsNetworkInterface(interfaceId=i, networkId=i,
isPrimary=i == 0, # specifies whether nic is the primary interface
isPredefined=False, # specifies whether network existed before reservation
networkData=network_data,
privateIpAddress='10.0.0.' + str(i),
publicIpAddress='8.8.8.' + str(i))
network_interfaces.append(current_interface)
return network_interfaces
@staticmethod
def get_vm_details(cloud_provider_resource, cancellation_context, requests_json):
"""
:param L2HeavenlyCloudShell cloud_provider_resource:
:param CancellationContext cancellation_context:
:param str requests_json:
:param str vm_id:
"""
requests = json.loads(requests_json)
results = []
for request in requests[u'items']:
if cancellation_context.is_cancelled:
break
vm_name = request[u'deployedAppJson'][u'name']
vm_uid = request[u'deployedAppJson'][u'vmdetails'][u'uid']
address = request[u'deployedAppJson'][u'address']
vm_instance = HeavenlyCloudService.get_instance(cloud_provider_resource, vm_name, vm_uid, address)
vm_instance_data = HeavenlyCloudServiceWrapper.extract_vm_instance_data(vm_instance)
vm_network_data = HeavenlyCloudServiceWrapper.extract_vm_instance_network_data(vm_instance)
# example of reading custom data created via deployed_app_additional_data_dict at delpoy stage
# created_by = next((deployed_app_additional_data['value'] for deployed_app_additional_data in request[u'deployedAppJson'][u'vmdetails'][u'vmCustomParams'] if
# deployed_app_additional_data['name'] == 'CreatedBy'), None)
# if created_by:
# vm_instance_data.append(VmDetailsProperty(key='CreatedBy',value=created_by))
# TODO when prepare connectivity is finished uncomment
# currently we get error: Check failed because resource is connected to more than one subnet, while reservation doesn't have any subnet services
# result = VmDetailsData(vmInstanceData=vm_instance_data, vmNetworkData=vm_network_data, appName=vm_name)
result = VmDetailsData(vmInstanceData=vm_instance_data, vmNetworkData=vm_network_data, appName=vm_name)
results.append(result)
return results
@staticmethod
def power_on(cloud_provider_resource, vm_id):
"""
:param L2HeavenlyCloudShell cloud_provider_resource:
:param str vm_id:
"""
HeavenlyCloudService.power_on(cloud_provider_resource,vm_id)
@staticmethod
def power_off(cloud_provider_resource, vm_id):
"""
:param L2HeavenlyCloudShell cloud_provider_resource:
:param str vm_id:
"""
HeavenlyCloudService.power_off(cloud_provider_resource, vm_id)
@staticmethod
def remote_refresh_ip(cloud_provider_resource, cancellation_context, cloudshell_session, resource_full_name, vm_id, deployed_app_private_ip, deployed_app_public_ip):
"""
:param L2HeavenlyCloudShell cloud_provider_resource:
:param CancellationContext cancellation_context:
:param CloudShellAPISession cloudshell_session:
:param str resource_full_name:
:param str vm_id:
:param str deployed_app_private_ip:
:param str deployed_app_public_ip:
"""
curr_ip = HeavenlyCloudService.remote_refresh_ip(cloud_provider_resource, cancellation_context, resource_full_name,vm_id)
if deployed_app_private_ip != curr_ip:
cloudshell_session.UpdateResourceAddress(resource_full_name, curr_ip)
if not deployed_app_public_ip:
cloudshell_session.SetAttributeValue(resource_full_name, "Public IP", '1.1.1.1')
@staticmethod
def delete_instance(cloud_provider_resource, vm_id):
HeavenlyCloudService.delete_instance(cloud_provider_resource, vm_id)
@staticmethod
def prepare_sandbox_infra(logger, cloud_provider_resource, prepare_infa_action, create_keys_action,
prepare_subnet_actions):
"""
:param logging.Logger logger:
:param L2HeavenlyCloudShell cloud_provider_resource:
:param PrepareCloudInfra prepare_infa_action:
:param CreateKeys create_keys_action:
:param List[PrepareSubnet] prepare_subnet_actions:
:return:
:rtype:
"""
results = []
# handle PrepareInfraAction - extract sandbox CIDR and create/allocate a network in the cloud provider with
# an address range of the provided CIDR
cidr = prepare_infa_action.actionParams.cidr
logger.info("Received CIDR {0} from server".format(cidr))
results.append(PrepareCloudInfraResult(prepare_infa_action.actionId))
# handle CreateKeys - generate key pair or get it from the cloud provider and save it in a secure location
# that will be accessible from the Deploy method
results.append(CreateKeysActionResult(create_keys_action.actionId, accessKey='my_ssh_key'))
# handle prepareSubnets
@staticmethod
def connect(logger, cloud_provider_resource, set_vlan_action):
"""
:param logging.Logger logger:
:param L2HeavenlyCloudShell cloud_provider_resource:
:param SetVlan set_vlan_action:
:return:
:rtype: SetVlanResult
"""
try:
vnic_name = '' if not 'Vnic Name' in set_vlan_action.customActionAttributes else set_vlan_action.customActionAttributes['Vnic Name']
interface_id = HeavenlyCloudService.connect_vlan(cloud_provider_resource, set_vlan_action)
return SetVlanResult(actionId=set_vlan_action.actionId, success=True,infoMessage=vnic_name + ' requested', updatedInterface=interface_id)
except Exception as e:
return SetVlanResult(actionId=set_vlan_action.actionId, success=False, errorMessage=e.message)
@staticmethod
def connect_all(logger, cloud_provider_resource, set_vlan_actions):
"""
:param logging.Logger logger:
:param L2HeavenlyCloudShell cloud_provider_resource:
:param [SetVlan] set_vlan_actions:
:return:
:rtype: [SetVlanResult]
"""
# vlan_type = cloud_provider_resource.vlan_type
# vlan_type "VLAN" : cloudshell will allow VLAN ids in the 2-4096 range
# vlan_type "XVLAN" cloudshell will allow VLAN ids to be allocated in range 2-16,000,000
# the default value is VLAN
if not set_vlan_actions:
return []
actions_results = []
for set_vlan_action in set_vlan_actions:
actions_results.append(HeavenlyCloudServiceWrapper.connect(logger, cloud_provider_resource, set_vlan_action))
return actions_results
@staticmethod
def disconnect(cloud_provider_resource, remove_vlan_action):
"""
:param L2HeavenlyCloudShell cloud_provider_resource:
:param RemoveVlan remove_vlan_action:
:return:
:rtype: RemoveVlanResult
"""
try:
HeavenlyCloudService.disconnect_vlan(cloud_provider_resource, remove_vlan_action)
disconnected_interface_id = remove_vlan_action.connectorAttributes['Interface']
return RemoveVlanResult(actionId=remove_vlan_action.actionId, success=True, updatedInterface=disconnected_interface_id)
except Exception as e:
return RemoveVlanResult(actionId=remove_vlan_action.actionId, success=False,errorMessage=e.message)
@staticmethod
def disconnect_all(logger, cloud_provider_resource, remove_vlan_actions):
"""
:param logging.Logger logger:
:param L2HeavenlyCloudShell cloud_provider_resource:
:param [RemoveVlan] remove_vlan_actions:
:return:
:rtype: [RemoveVlanResult]
"""
if not remove_vlan_actions:
return []
actions_results = []
for remove_vlan_action in remove_vlan_actions:
actions_results.append(HeavenlyCloudServiceWrapper.disconnect(cloud_provider_resource, remove_vlan_action))
return actions_results