Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
RECIRCLE
/
interface-openstack
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
2
Merge Requests
4
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
dc7e976d
authored
Jul 08, 2019
by
Belákovics Ádám
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'instance_fix' into 'DEV'
Instance fix See merge request
!7
parents
a141ebcc
ee340aea
Pipeline
#746
failed with stage
in 37 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
99 additions
and
95 deletions
+99
-95
implementation/vm/instance.py
+31
-37
main.py
+68
-58
No files found.
implementation/vm/instance.py
View file @
dc7e976d
from
interface
_openstack.interface
.vm.instance
import
InstanceInterface
from
interface
_openstack.interface
.vm.resources
import
Instance
,
Flavor
from
interface.vm.instance
import
InstanceInterface
from
interface.vm.resources
import
Instance
,
Flavor
from
openstack.exceptions
import
SDKException
from
novaclient
import
client
import
logging
...
...
@@ -38,6 +38,15 @@ class OSVirtualMachineManager(InstanceInterface):
self
.
openstack
=
cloud
@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
):
devices
=
[]
b_device
=
block_dev_map
.
__dict__
...
...
@@ -102,59 +111,47 @@ class OSVirtualMachineManager(InstanceInterface):
@openstackError
def
get_vm
(
self
,
name_or_id
=
None
):
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
self
.
convert_server_to_instance
(
server_instance
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
return
self
.
convert_server_to_instance
(
instance
)
@openstackError
def
start_vm
(
self
,
name_or_id
=
None
):
if
name_or_id
:
instance
=
self
.
openstack
.
compute
.
get_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
start_server
(
instance
)
@openstackError
def
stop_vm
(
self
,
name_or_id
=
None
):
if
name_or_id
:
instance
=
self
.
openstack
.
compute
.
get_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
stop_server
(
instance
)
@openstackError
def
suspend_vm
(
self
,
name_or_id
=
None
):
if
name_or_id
:
instance
=
self
.
openstack
.
compute
.
get_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
suspend_server
(
instance
)
@openstackError
def
wake_up_vm
(
self
,
name_or_id
=
None
):
if
name_or_id
:
instance
=
self
.
openstack
.
compute
.
get_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
resume_server
(
instance
)
@openstackError
def
reboot_vm
(
self
,
name_or_id
):
if
name_or_id
:
instance
=
self
.
openstack
.
compute
.
get_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
reboot_server
(
instance
,
reboot_type
=
'SOFT'
)
@openstackError
def
reset_vm
(
self
,
name_or_id
):
if
name_or_id
:
instance
=
self
.
openstack
.
compute
.
get_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
reboot_server
(
instance
,
reboot_type
=
'HARD'
)
@openstackError
def
destroy_vm
(
self
,
name_or_id
):
if
name_or_id
:
instance
=
self
.
openstack
.
compute
.
get_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
delete_server
(
instance
)
@openstackError
def
get_status
(
self
,
name_or_id
):
if
name_or_id
:
instance
=
self
.
openstack
.
compute
.
get_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
return
instance
.
status
@openstackError
...
...
@@ -166,16 +163,14 @@ class OSVirtualMachineManager(InstanceInterface):
@openstackError
def
resize_vm
(
self
,
name_or_id
,
resource
):
if
name_or_id
:
instance
=
self
.
openstack
.
get_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
flavor
=
self
.
openstack
.
compute
.
find_flavor
(
resource
[
'name'
])
self
.
openstack
.
compute
.
resize_server
(
instance
,
flavor
)
@openstackError
def
create_template
(
self
,
name_or_id
,
template_name
,
metadata
=
None
):
if
name_or_id
:
instance
=
self
.
openstack
.
compute
.
get_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
create_server_image
(
instance
,
template_name
,
metadata
)
...
...
@@ -188,24 +183,23 @@ class OSVirtualMachineManager(InstanceInterface):
@openstackError
def
attach_volume
(
self
,
server_id
,
volume_id
,
device
=
None
):
self
.
openstack
.
compute
.
create_volume_attachment
(
server
_id
,
{
"volumeId"
:
volume
_id
,
}
)
self
.
openstack
.
compute
.
create_volume_attachment
(
volumeId
=
volume
_id
,
server
=
server
_id
,
)
@openstackError
def
detach_volume
(
self
,
server_id
,
volume_id
,
device
=
None
):
self
.
openstack
.
compute
.
delete_volume_attachment
(
server_id
,
{
"volumeId"
:
volume_id
,
"device"
:
device
})
self
.
openstack
.
compute
.
delete_volume_attachment
(
volume_attachment
=
volume_id
,
server
=
server_id
)
def
convert_server_to_instance
(
self
,
server
):
if
not
server
.
image
:
image_id
=
None
else
:
image_id
=
server
.
image
_id
image_id
=
server
.
image
[
"id"
]
return
Instance
(
id
=
server
.
id
,
flavor
=
server
.
flavor
_id
,
flavor
=
server
.
flavor
[
"id"
]
,
name
=
server
.
name
,
image_id
=
image_id
,
disks
=
server
.
attached_volumes
,
...
...
main.py
View file @
dc7e976d
...
...
@@ -40,8 +40,18 @@ for vm in interface.list_all_vm():
print
(
vm
.
JSON
())
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())
...
...
@@ -72,65 +82,65 @@ print(interface.create_base_vm("BaseTest", "2", networks, block_dev_map).JSON())
# Network test
# ==============================================================================
os
=
openstack
.
connect
(
cloud
=
'openstack'
)
router_manager
=
OpenstackRouterManager
(
os
)
network_manager
=
OpenstackNetworkManager
(
os
)
subnet_manager
=
OpenstackSubnetManager
(
os
)
port_manager
=
OpenstackPortManager
(
os
)
floating_ip_manager
=
OpenstackFloatingIPManager
(
os
)
'''
Network setup steps:
- Create inner network
- Create subnet for inner network
- Create router
- Add public network to router as gateway
- Add inner subnet to router as interface
- Add interface (or port?) to vm
- Get the port of the vm
- Create floating ip to public network
- Associate floating ip and vm port
'''
#router = router_manager.create()
router
=
router_manager
.
get
(
'3ee87bcd-b1b5-4e14-86a1-ce691bd3e5a2'
)
print
(
'Router:'
)
print
(
'
\t
%
s'
%
router
)
#network = network_manager.create()
public_network
=
network_manager
.
get
(
'3b349c93-8b2c-4abd-b8ce-22b7db4cca98'
)
inner_network
=
network_manager
.
get
(
'8df0d7ab-7d56-4496-aca0-317af66b3ebc'
)
print
(
'Network:'
)
print
(
'
\t
Public:
%
s'
%
public_network
)
print
(
'
\t
Inner:
%
s'
%
inner_network
)
#subnet = subnet_manager.create(network.id, 4, '10.34.0.0/16')
public_subnet
=
subnet_manager
.
get
(
'8a7e05f8-6358-48f7-9104-990d350425a0'
)
# create(inner_network.id, 4, '10.0.0.0/24')
inner_subnet
=
subnet_manager
.
get
(
'34911571-c929-4433-b4e6-305fcb585a91'
)
print
(
'Subnet:'
)
print
(
'
\t
Public:
%
s'
%
public_subnet
)
print
(
'
\t
Inner:
%
s'
%
inner_subnet
)
vm_port
=
port_manager
.
get
(
'85202d6d-7aa0-44bc-a5eb-78b0519066c5'
)
# inner
print
(
'Port:'
)
print
(
'
\t
VM:
%
s'
%
vm_port
)
#floating_ip = floating_ip_manager.create(public_network.id)
floating_ip
=
floating_ip_manager
.
get
(
'fe1e329f-aedb-4b3e-a649-9fec8eb3ece5'
)
print
(
'Floating IP:'
)
print
(
'
\t
Floating IP:
%
s'
%
floating_ip
)
#
os = openstack.connect(cloud='openstack')
#
#
router_manager = OpenstackRouterManager(os)
#
network_manager = OpenstackNetworkManager(os)
#
subnet_manager = OpenstackSubnetManager(os)
#
port_manager = OpenstackPortManager(os)
#
floating_ip_manager = OpenstackFloatingIPManager(os)
#
#
'''
#
Network setup steps:
#
- Create inner network
#
- Create subnet for inner network
#
- Create router
#
- Add public network to router as gateway
#
- Add inner subnet to router as interface
#
- Add interface (or port?) to vm
#
- Get the port of the vm
#
- Create floating ip to public network
#
- Associate floating ip and vm port
#
'''
#
#
#
router = router_manager.create()
#
router = router_manager.get('3ee87bcd-b1b5-4e14-86a1-ce691bd3e5a2')
#
#
print('Router:')
#
print('\t%s' % router)
#
#
#
network = network_manager.create()
#
public_network = network_manager.get('3b349c93-8b2c-4abd-b8ce-22b7db4cca98')
#
inner_network = network_manager.get('8df0d7ab-7d56-4496-aca0-317af66b3ebc')
#
#
print('Network:')
#
print('\tPublic: %s' % public_network)
#
print('\tInner: %s' % inner_network)
#
#
#
subnet = subnet_manager.create(network.id, 4, '10.34.0.0/16')
#
public_subnet = subnet_manager.get('8a7e05f8-6358-48f7-9104-990d350425a0')
#
#
create(inner_network.id, 4, '10.0.0.0/24')
#
inner_subnet = subnet_manager.get('34911571-c929-4433-b4e6-305fcb585a91')
#
#
print('Subnet:')
#
print('\tPublic: %s' % public_subnet)
#
print('\tInner: %s' % inner_subnet)
#
#
vm_port = port_manager.get('85202d6d-7aa0-44bc-a5eb-78b0519066c5') # inner
#
#
print('Port:')
#
print('\tVM: %s' % vm_port)
#
#
#
floating_ip = floating_ip_manager.create(public_network.id)
#
floating_ip = floating_ip_manager.get('fe1e329f-aedb-4b3e-a649-9fec8eb3ece5')
#
#
print('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_gateway(router.id, public_network.id)) # TODO: fix it!
#
# print(floating_ip_manager.update(floating_ip.id, {
# 'port_id': vm_port.id
# }))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment