Skip to content

Commit

Permalink
Use implicit volume creation
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Apr 18, 2019
1 parent c8b73ea commit fb47c03
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 14 deletions.
71 changes: 58 additions & 13 deletions IM/connectors/OpenStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ def updateVMInfo(self, vm, auth_data):

self.addRouterInstance(vm, node.driver)
self.setIPsFromInstance(vm, node)
self.attach_volumes(vm, node)
else:
self.log_warn("Error updating the instance %s. VM not found." % vm.id)
return (False, "Error updating the instance %s. VM not found." % vm.id)
Expand Down Expand Up @@ -783,12 +782,63 @@ def get_networks(self, driver, radl):

return nets

@staticmethod
def get_volumes(driver, image, radl):
"""
Create the required volumes (in the RADL) for the VM.
Arguments:
- vm(:py:class:`IM.VirtualMachine`): VM to modify.
"""
system = radl.systems[0]

boot_disk = {
'boot_index': 0,
'device_name': 'vda',
'source_type': "image",
'delete_on_termination': False,
'uuid': image.id
}
res = [boot_disk]

cont = 1
while ((system.getValue("disk." + str(cont) + ".size") or
system.getValue("disk." + str(cont) + ".image.url")) and
system.getValue("disk." + str(cont) + ".device")):
disk_url = system.getValue("disk." + str(cont) + ".image.url")
disk_device = system.getValue("disk." + str(cont) + ".device")
disk_device = "vd%s" % disk_device[-1]
disk_fstype = system.getValue("disk." + str(cont) + ".fstype")
if not disk_fstype:
disk_fstype = 'ext3'

disk_size = None
if disk_url:
volume = driver.ex_get_volume(os.path.basename(disk_url))
disk_url = volume.id
else:
disk_size = system.getFeature("disk." + str(cont) + ".size").getValue('G')

disk = {
'boot_index': cont,
'device_name': disk_device,
'source_type': "blank",
'guest_format': disk_fstype,
'destination_type': "volume",
'delete_on_termination': True,
'volume_size': disk_size
}
res.append(disk)
cont += 1

return res

def launch(self, inf, radl, requested_radl, num_vm, auth_data):
driver = self.get_driver(auth_data)

system = radl.systems[0]
image_id = self.get_image_id(system.getValue("disk.0.image.url"))
image = NodeImage(id=image_id, name=None, driver=driver)
image = driver.get_image(image_id)

instance_type = self.get_instance_type(driver.list_sizes(), system)
if not instance_type:
Expand All @@ -800,6 +850,8 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
if not name:
name = "userimage"

blockdevicemappings = self.get_volumes(driver, image, radl)

with inf._lock:
self.create_networks(driver, radl, inf)

Expand All @@ -808,9 +860,10 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
sgs = self.create_security_groups(driver, inf, radl)

args = {'size': instance_type,
'image': image,
'networks': nets,
'image': image,
'ex_security_groups': sgs,
'ex_blockdevicemappings': blockdevicemappings,
'name': "%s-%s" % (name, int(time.time() * 100))}

tags = self.get_instance_tags(system)
Expand Down Expand Up @@ -864,6 +917,7 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data):
vm.destroy = False
res.append((True, vm))
except Exception as ex:
self.log_exception("Error creating node: %s." % ex.args[0])
res.append((False, "%s" % ex.args[0]))

i += 1
Expand Down Expand Up @@ -958,7 +1012,7 @@ def add_elastic_ip_from_pool(self, vm, node, fixed_ip=None, pool_name=None):
Arguments:
- vm(:py:class:`IM.VirtualMachine`): VM information.
- node(:py:class:`libcloud.compute.base.Node`): node object to attach the volumes.
- node(:py:class:`libcloud.compute.base.Node`): node object to attach the ip.
- fixed_ip(str, optional): specifies a fixed IP to add to the instance.
- pool_name(str, optional): specifies a pool to get the elastic IP
Returns: a :py:class:`OpenStack_1_1_FloatingIpAddress` added or None if some problem occur.
Expand Down Expand Up @@ -1137,15 +1191,6 @@ def finalize(self, vm, last, auth_data):

driver = self.get_driver(auth_data)

# Delete the EBS volumes
try:
res, msg = self.delete_volumes(driver, vm)
except Exception as ex:
res = False
msg = "%s" % ex.args[0]
success.append(res)
msgs.append(msg)

if last:
# Delete the SG if this is the last VM
try:
Expand Down
20 changes: 19 additions & 1 deletion test/unit/connectors/OpenStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,31 @@ def test_20_launch(self, save_data, get_driver):
driver.ex_list_routers.return_value = [router]
driver.ex_add_router_subnet.return_value = True

image = MagicMock()
image.id = 'imageid'
driver.get_image.return_value = image

inf = InfrastructureInfo()
inf.auth = auth
res = ost_cloud.launch_with_retry(inf, radl, radl, 1, auth, 2, 1)
success, _ = res[0]
self.assertTrue(success, msg="ERROR: launching a VM.")
self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())
self.assertEqual(driver.create_node.call_args_list[0][1]['networks'], [net1])
mappings = [
{'source_type': 'image',
'uuid': 'imageid',
'boot_index': 0,
'delete_on_termination': False,
'device_name': 'vda'},
{'guest_format': 'ext3',
'boot_index': 1,
'volume_size': 1,
'device_name': 'vdb',
'source_type': 'blank',
'destination_type': 'volume',
'delete_on_termination': True}
]
self.assertEqual(driver.create_node.call_args_list[0][1]['ex_blockdevicemappings'], mappings)

# test with proxy auth data
auth = Authentication([{'id': 'ost', 'type': 'OpenStack', 'proxy': 'proxy',
Expand Down

0 comments on commit fb47c03

Please sign in to comment.