Commit dc7e976d by Belákovics Ádám

Merge branch 'instance_fix' into 'DEV'

Instance fix

See merge request !7
parents a141ebcc ee340aea
Pipeline #746 failed with stage
in 37 seconds
from interface_openstack.interface.vm.instance import InstanceInterface from interface.vm.instance import InstanceInterface
from interface_openstack.interface.vm.resources import Instance, Flavor from interface.vm.resources import Instance, Flavor
from openstack.exceptions import SDKException from openstack.exceptions import SDKException
from novaclient import client from novaclient import client
import logging import logging
...@@ -38,6 +38,15 @@ class OSVirtualMachineManager(InstanceInterface): ...@@ -38,6 +38,15 @@ class OSVirtualMachineManager(InstanceInterface):
self.openstack = cloud self.openstack = cloud
@openstackError @openstackError
def _get_openstack_server(self, name_or_id):
if not name_or_id:
raise ValueError("Name or id doesn't given")
server_instance = self.openstack.compute.get_server(name_or_id)
if not server_instance:
raise ValueError("Could not get the vm")
return server_instance
@openstackError
def create_base_vm(self, name, flavor, networks, block_dev_map): def create_base_vm(self, name, flavor, networks, block_dev_map):
devices = [] devices = []
b_device = block_dev_map.__dict__ b_device = block_dev_map.__dict__
...@@ -102,59 +111,47 @@ class OSVirtualMachineManager(InstanceInterface): ...@@ -102,59 +111,47 @@ class OSVirtualMachineManager(InstanceInterface):
@openstackError @openstackError
def get_vm(self, name_or_id=None): def get_vm(self, name_or_id=None):
if not name_or_id: instance = self._get_openstack_server(name_or_id)
raise ValueError("Name or id doesn't given") return self.convert_server_to_instance(instance)
server_instance = self.openstack.compute.get_server(name_or_id)
if not server_instance:
raise ValueError("Could not get the vm")
return self.convert_server_to_instance(server_instance)
@openstackError @openstackError
def start_vm(self, name_or_id=None): def start_vm(self, name_or_id=None):
if name_or_id: instance = self._get_openstack_server(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.start_server(instance) self.openstack.compute.start_server(instance)
@openstackError @openstackError
def stop_vm(self, name_or_id=None): def stop_vm(self, name_or_id=None):
if name_or_id: instance = self._get_openstack_server(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.stop_server(instance) self.openstack.compute.stop_server(instance)
@openstackError @openstackError
def suspend_vm(self, name_or_id=None): def suspend_vm(self, name_or_id=None):
if name_or_id: instance = self._get_openstack_server(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.suspend_server(instance) self.openstack.compute.suspend_server(instance)
@openstackError @openstackError
def wake_up_vm(self, name_or_id=None): def wake_up_vm(self, name_or_id=None):
if name_or_id: instance = self._get_openstack_server(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.resume_server(instance) self.openstack.compute.resume_server(instance)
@openstackError @openstackError
def reboot_vm(self, name_or_id): def reboot_vm(self, name_or_id):
if name_or_id: instance = self._get_openstack_server(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.reboot_server(instance, reboot_type='SOFT') self.openstack.compute.reboot_server(instance, reboot_type='SOFT')
@openstackError @openstackError
def reset_vm(self, name_or_id): def reset_vm(self, name_or_id):
if name_or_id: instance = self._get_openstack_server(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.reboot_server(instance, reboot_type='HARD') self.openstack.compute.reboot_server(instance, reboot_type='HARD')
@openstackError @openstackError
def destroy_vm(self, name_or_id): def destroy_vm(self, name_or_id):
if name_or_id: instance = self._get_openstack_server(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.delete_server(instance) self.openstack.compute.delete_server(instance)
@openstackError @openstackError
def get_status(self, name_or_id): def get_status(self, name_or_id):
if name_or_id: instance = self._get_openstack_server(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
return instance.status return instance.status
@openstackError @openstackError
...@@ -166,16 +163,14 @@ class OSVirtualMachineManager(InstanceInterface): ...@@ -166,16 +163,14 @@ class OSVirtualMachineManager(InstanceInterface):
@openstackError @openstackError
def resize_vm(self, name_or_id, resource): def resize_vm(self, name_or_id, resource):
if name_or_id: instance = self._get_openstack_server(name_or_id)
instance = self.openstack.get_server(name_or_id)
flavor = self.openstack.compute.find_flavor(resource['name']) flavor = self.openstack.compute.find_flavor(resource['name'])
self.openstack.compute.resize_server(instance, flavor) self.openstack.compute.resize_server(instance, flavor)
@openstackError @openstackError
def create_template(self, name_or_id, template_name, metadata=None): def create_template(self, name_or_id, template_name, metadata=None):
if name_or_id: instance = self._get_openstack_server(name_or_id)
instance = self.openstack.compute.get_server(name_or_id)
self.openstack.compute.create_server_image(instance, self.openstack.compute.create_server_image(instance,
template_name, template_name,
metadata) metadata)
...@@ -188,24 +183,23 @@ class OSVirtualMachineManager(InstanceInterface): ...@@ -188,24 +183,23 @@ class OSVirtualMachineManager(InstanceInterface):
@openstackError @openstackError
def attach_volume(self, server_id, volume_id, device=None): def attach_volume(self, server_id, volume_id, device=None):
self.openstack.compute.create_volume_attachment(server_id, self.openstack.compute.create_volume_attachment(volumeId=volume_id,
{"volumeId": volume_id, server=server_id,
}) )
@openstackError @openstackError
def detach_volume(self, server_id, volume_id, device=None): def detach_volume(self, server_id, volume_id, device=None):
self.openstack.compute.delete_volume_attachment(server_id, self.openstack.compute.delete_volume_attachment(
{"volumeId": volume_id, volume_attachment=volume_id,
"device": device server=server_id)
})
def convert_server_to_instance(self, server): def convert_server_to_instance(self, server):
if not server.image: if not server.image:
image_id = None image_id = None
else: else:
image_id = server.image_id image_id = server.image["id"]
return Instance(id=server.id, return Instance(id=server.id,
flavor=server.flavor_id, flavor=server.flavor["id"],
name=server.name, name=server.name,
image_id=image_id, image_id=image_id,
disks=server.attached_volumes, disks=server.attached_volumes,
......
...@@ -40,8 +40,18 @@ for vm in interface.list_all_vm(): ...@@ -40,8 +40,18 @@ for vm in interface.list_all_vm():
print(vm.JSON()) print(vm.JSON())
print("*" * 50) print("*" * 50)
print(interface.create_base_vm("BaseTest", "2", networks, block_dev_map).JSON()) # print(interface.create_base_vm("BaseTest", "2", networks, block_dev_map).JSON())
print("*********TEST SERVER**********")
print(interface.get_vm("fb91a363-e23b-4fb6-8770-357509b753da").JSON())
print(interface.openstack.get_server("fb91a363-e23b-4fb6-8770-357509b753da"))
print("*********TEST SUSPEND**********")
interface.suspend_vm("fb91a363-e23b-4fb6-8770-357509b753da")
interface.detach_volume("8e94c162-f8ed-4872-b9e5-50bf33040b5b",
"83d4f11a-f6a8-4f50-8a95-8f5016604780")
# print(interface.convert_server_to_instance(server).JSON()) # print(interface.convert_server_to_instance(server).JSON())
...@@ -72,65 +82,65 @@ print(interface.create_base_vm("BaseTest", "2", networks, block_dev_map).JSON()) ...@@ -72,65 +82,65 @@ print(interface.create_base_vm("BaseTest", "2", networks, block_dev_map).JSON())
# Network test # Network test
# ============================================================================== # ==============================================================================
os = openstack.connect(cloud='openstack') # os = openstack.connect(cloud='openstack')
#
router_manager = OpenstackRouterManager(os) # router_manager = OpenstackRouterManager(os)
network_manager = OpenstackNetworkManager(os) # network_manager = OpenstackNetworkManager(os)
subnet_manager = OpenstackSubnetManager(os) # subnet_manager = OpenstackSubnetManager(os)
port_manager = OpenstackPortManager(os) # port_manager = OpenstackPortManager(os)
floating_ip_manager = OpenstackFloatingIPManager(os) # floating_ip_manager = OpenstackFloatingIPManager(os)
#
''' # '''
Network setup steps: # Network setup steps:
- Create inner network # - Create inner network
- Create subnet for inner network # - Create subnet for inner network
- Create router # - Create router
- Add public network to router as gateway # - Add public network to router as gateway
- Add inner subnet to router as interface # - Add inner subnet to router as interface
- Add interface (or port?) to vm # - Add interface (or port?) to vm
- Get the port of the vm # - Get the port of the vm
- Create floating ip to public network # - Create floating ip to public network
- Associate floating ip and vm port # - Associate floating ip and vm port
''' # '''
#
#router = router_manager.create() # #router = router_manager.create()
router = router_manager.get('3ee87bcd-b1b5-4e14-86a1-ce691bd3e5a2') # router = router_manager.get('3ee87bcd-b1b5-4e14-86a1-ce691bd3e5a2')
#
print('Router:') # print('Router:')
print('\t%s' % router) # print('\t%s' % router)
#
#network = network_manager.create() # #network = network_manager.create()
public_network = network_manager.get('3b349c93-8b2c-4abd-b8ce-22b7db4cca98') # public_network = network_manager.get('3b349c93-8b2c-4abd-b8ce-22b7db4cca98')
inner_network = network_manager.get('8df0d7ab-7d56-4496-aca0-317af66b3ebc') # inner_network = network_manager.get('8df0d7ab-7d56-4496-aca0-317af66b3ebc')
#
print('Network:') # print('Network:')
print('\tPublic: %s' % public_network) # print('\tPublic: %s' % public_network)
print('\tInner: %s' % inner_network) # print('\tInner: %s' % inner_network)
#
#subnet = subnet_manager.create(network.id, 4, '10.34.0.0/16') # #subnet = subnet_manager.create(network.id, 4, '10.34.0.0/16')
public_subnet = subnet_manager.get('8a7e05f8-6358-48f7-9104-990d350425a0') # public_subnet = subnet_manager.get('8a7e05f8-6358-48f7-9104-990d350425a0')
# create(inner_network.id, 4, '10.0.0.0/24') # # create(inner_network.id, 4, '10.0.0.0/24')
inner_subnet = subnet_manager.get('34911571-c929-4433-b4e6-305fcb585a91') # inner_subnet = subnet_manager.get('34911571-c929-4433-b4e6-305fcb585a91')
#
print('Subnet:') # print('Subnet:')
print('\tPublic: %s' % public_subnet) # print('\tPublic: %s' % public_subnet)
print('\tInner: %s' % inner_subnet) # print('\tInner: %s' % inner_subnet)
#
vm_port = port_manager.get('85202d6d-7aa0-44bc-a5eb-78b0519066c5') # inner # vm_port = port_manager.get('85202d6d-7aa0-44bc-a5eb-78b0519066c5') # inner
#
print('Port:') # print('Port:')
print('\tVM: %s' % vm_port) # print('\tVM: %s' % vm_port)
#
#floating_ip = floating_ip_manager.create(public_network.id) # #floating_ip = floating_ip_manager.create(public_network.id)
floating_ip = floating_ip_manager.get('fe1e329f-aedb-4b3e-a649-9fec8eb3ece5') # floating_ip = floating_ip_manager.get('fe1e329f-aedb-4b3e-a649-9fec8eb3ece5')
#
print('Floating IP:') # print('Floating IP:')
print('\tFloating IP: %s' % floating_ip) # print('\tFloating IP: %s' % floating_ip)
#
#
# print(router_manager.add_interface(router.id, inner_subnet.id)) # TODO: fix error msg # print(router_manager.add_interface(router.id, inner_subnet.id)) # TODO: fix error msg
# print(router_manager.add_gateway(router.id, public_network.id)) # TODO: fix it! # print(router_manager.add_gateway(router.id, public_network.id)) # TODO: fix it!
#
# print(floating_ip_manager.update(floating_ip.id, { # print(floating_ip_manager.update(floating_ip.id, {
# 'port_id': vm_port.id # 'port_id': vm_port.id
# })) # }))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment