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
841e3e1f
authored
Apr 27, 2020
by
Arnau Comas Codina
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored openstackError for shared using along modules
parent
07df1fec
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
39 deletions
+69
-39
implementation/utils/decorators.py
+45
-0
implementation/vm/instance.py
+24
-39
No files found.
implementation/utils/decorators.py
0 → 100644
View file @
841e3e1f
import
logging
from
openstack.exceptions
import
(
SDKException
,
BadRequestException
)
class
ErrorException
(
Exception
):
""" Base class for exceptions in this module.
Attributes:
OpenStackError -- boolean
DuplicateError -- boolean
"""
def
__init__
(
self
,
e
):
super
()
.
__init__
(
e
)
self
.
OpenStackError
=
True
self
.
DuplicateError
=
False
if
hasattr
(
e
,
'details'
):
if
'duplicate'
in
e
.
details
:
self
.
DuplicateError
=
True
def
OpenStackError
(
func
):
""" Decorator to wrap openstack error in simple Exception.
Return decorated function
"""
def
wrap_OpenStackError
(
*
args
,
**
kw
):
try
:
try
:
return
func
(
*
args
,
**
kw
)
except
SDKException
as
e
:
raise
ErrorException
(
e
)
except
BadRequestException
as
e
:
import
pdb
;
pdb
.
set_trace
()
raise
ErrorException
(
e
)
except
Exception
as
e
:
raise
e
return
wrap_OpenStackError
implementation/vm/instance.py
View file @
841e3e1f
from
openstack.exceptions
import
SDKException
from
openstack.exceptions
import
SDKException
from
novaclient
import
client
from
novaclient
import
client
import
logging
from
interface_openstack.implementation.utils.connection
import
OpenStackConnection
from
interface_openstack.implementation.utils.connection
import
OpenStackConnection
from
interface_openstack.implementation.utils.decorators
import
OpenStackError
import
secrets
import
secrets
from
interface_openstack.interface.vm.instance
import
InstanceInterface
from
interface_openstack.interface.vm.instance
import
InstanceInterface
from
interface_openstack.interface.vm.resources
import
Instance
,
Flavor
from
interface_openstack.interface.vm.resources
import
Instance
,
Flavor
from
interface_openstack.interface.image.image
import
Image
from
interface_openstack.interface.image.image
import
Image
def
openstackError
(
func
):
def
wrap_OpenStackError
(
*
args
,
**
kw
):
""" Decorator to wrap openstack error in simple Exception.
Return decorated function
"""
try
:
return
func
(
*
args
,
**
kw
)
except
SDKException
as
e
:
logging
.
error
(
e
)
new_e
=
Exception
(
e
)
new_e
.
OpenStackError
=
True
raise
new_e
return
wrap_OpenStackError
class
OSVirtualMachineManager
(
InstanceInterface
,
OpenStackConnection
):
class
OSVirtualMachineManager
(
InstanceInterface
,
OpenStackConnection
):
@
opens
tackError
@
OpenS
tackError
def
_get_openstack_server
(
self
,
name_or_id
):
def
_get_openstack_server
(
self
,
name_or_id
):
if
not
name_or_id
:
if
not
name_or_id
:
raise
ValueError
(
"Name or id doesn't given"
)
raise
ValueError
(
"Name or id doesn't given"
)
...
@@ -35,7 +20,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
...
@@ -35,7 +20,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
raise
ValueError
(
"Could not get the vm"
)
raise
ValueError
(
"Could not get the vm"
)
return
server_instance
return
server_instance
@
opens
tackError
@
OpenS
tackError
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__
...
@@ -49,12 +34,12 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
...
@@ -49,12 +34,12 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
)
)
return
self
.
convert_server_to_instance
(
new_server
)
return
self
.
convert_server_to_instance
(
new_server
)
@
opens
tackError
@
OpenS
tackError
def
create_vm_from_template
(
self
,
name
,
image
,
flavor
,
networks
):
def
create_vm_from_template
(
self
,
name
,
image
,
flavor
,
networks
):
return
self
.
create_multiple_vm_from_template
(
name
,
image
,
flavor
,
return
self
.
create_multiple_vm_from_template
(
name
,
image
,
flavor
,
networks
,
1
)[
0
]
networks
,
1
)[
0
]
@
opens
tackError
@
OpenS
tackError
def
create_multiple_vm_from_template
(
self
,
name
,
image
,
flavor
,
networks
,
def
create_multiple_vm_from_template
(
self
,
name
,
image
,
flavor
,
networks
,
number
,
**
args
):
number
,
**
args
):
compute
=
self
.
openstack
.
compute
compute
=
self
.
openstack
.
compute
...
@@ -77,7 +62,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
...
@@ -77,7 +62,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
return
server_ids
return
server_ids
@
opens
tackError
@
OpenS
tackError
def
create_flavor
(
self
,
name
,
ram
,
vcpus
,
initial_disk
):
def
create_flavor
(
self
,
name
,
ram
,
vcpus
,
initial_disk
):
flavor
=
self
.
openstack
.
compute
.
create_flavor
(
name
=
name
,
flavor
=
self
.
openstack
.
compute
.
create_flavor
(
name
=
name
,
ram
=
ram
,
ram
=
ram
,
...
@@ -86,18 +71,18 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
...
@@ -86,18 +71,18 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
return
Flavor
(
flavor
.
name
,
flavor
.
id
,
flavor
.
ram
,
return
Flavor
(
flavor
.
name
,
flavor
.
id
,
flavor
.
ram
,
flavor
.
vcpus
,
flavor
.
disk
)
flavor
.
vcpus
,
flavor
.
disk
)
@
opens
tackError
@
OpenS
tackError
def
get_flavor
(
self
,
flavor_id
):
def
get_flavor
(
self
,
flavor_id
):
flavor
=
self
.
openstack
.
compute
.
find_flavor
(
flavor_id
)
flavor
=
self
.
openstack
.
compute
.
find_flavor
(
flavor_id
)
return
Flavor
(
flavor
.
name
,
flavor
.
id
,
flavor
.
ram
,
return
Flavor
(
flavor
.
name
,
flavor
.
id
,
flavor
.
ram
,
flavor
.
vcpus
,
flavor
.
disk
)
flavor
.
vcpus
,
flavor
.
disk
)
@
opens
tackError
@
OpenS
tackError
def
delete_flavor
(
self
,
flavor_id
):
def
delete_flavor
(
self
,
flavor_id
):
flavor
=
self
.
openstack
.
compute
.
find_flavor
(
flavor_id
)
flavor
=
self
.
openstack
.
compute
.
find_flavor
(
flavor_id
)
self
.
openstack
.
compute
.
delete_flavor
(
flavor
)
self
.
openstack
.
compute
.
delete_flavor
(
flavor
)
@
opens
tackError
@
OpenS
tackError
def
list_flavors
(
self
):
def
list_flavors
(
self
):
flavors
=
[]
flavors
=
[]
for
flavor
in
self
.
openstack
.
compute
.
flavors
():
for
flavor
in
self
.
openstack
.
compute
.
flavors
():
...
@@ -105,66 +90,66 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
...
@@ -105,66 +90,66 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
flavor
.
vcpus
,
flavor
.
disk
))
flavor
.
vcpus
,
flavor
.
disk
))
return
flavors
return
flavors
@
opens
tackError
@
OpenS
tackError
def
get_vm
(
self
,
name_or_id
=
None
):
def
get_vm
(
self
,
name_or_id
=
None
):
instance
=
self
.
_get_openstack_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
return
self
.
convert_server_to_instance
(
instance
)
return
self
.
convert_server_to_instance
(
instance
)
@
opens
tackError
@
OpenS
tackError
def
start_vm
(
self
,
name_or_id
=
None
):
def
start_vm
(
self
,
name_or_id
=
None
):
instance
=
self
.
_get_openstack_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
start_server
(
instance
)
self
.
openstack
.
compute
.
start_server
(
instance
)
@
opens
tackError
@
OpenS
tackError
def
stop_vm
(
self
,
name_or_id
=
None
):
def
stop_vm
(
self
,
name_or_id
=
None
):
instance
=
self
.
_get_openstack_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
stop_server
(
instance
)
self
.
openstack
.
compute
.
stop_server
(
instance
)
@
opens
tackError
@
OpenS
tackError
def
suspend_vm
(
self
,
name_or_id
=
None
):
def
suspend_vm
(
self
,
name_or_id
=
None
):
instance
=
self
.
_get_openstack_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
suspend_server
(
instance
)
self
.
openstack
.
compute
.
suspend_server
(
instance
)
@
opens
tackError
@
OpenS
tackError
def
wake_up_vm
(
self
,
name_or_id
=
None
):
def
wake_up_vm
(
self
,
name_or_id
=
None
):
instance
=
self
.
_get_openstack_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
resume_server
(
instance
)
self
.
openstack
.
compute
.
resume_server
(
instance
)
@
opens
tackError
@
OpenS
tackError
def
reboot_vm
(
self
,
name_or_id
):
def
reboot_vm
(
self
,
name_or_id
):
instance
=
self
.
_get_openstack_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
reboot_server
(
instance
,
reboot_type
=
'SOFT'
)
self
.
openstack
.
compute
.
reboot_server
(
instance
,
reboot_type
=
'SOFT'
)
@
opens
tackError
@
OpenS
tackError
def
reset_vm
(
self
,
name_or_id
):
def
reset_vm
(
self
,
name_or_id
):
instance
=
self
.
_get_openstack_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
reboot_server
(
instance
,
reboot_type
=
'HARD'
)
self
.
openstack
.
compute
.
reboot_server
(
instance
,
reboot_type
=
'HARD'
)
@
opens
tackError
@
OpenS
tackError
def
destroy_vm
(
self
,
name_or_id
):
def
destroy_vm
(
self
,
name_or_id
):
instance
=
self
.
_get_openstack_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
self
.
openstack
.
compute
.
delete_server
(
instance
)
self
.
openstack
.
compute
.
delete_server
(
instance
)
@
opens
tackError
@
OpenS
tackError
def
get_status
(
self
,
name_or_id
):
def
get_status
(
self
,
name_or_id
):
instance
=
self
.
_get_openstack_server
(
name_or_id
)
instance
=
self
.
_get_openstack_server
(
name_or_id
)
return
instance
.
status
return
instance
.
status
@
opens
tackError
@
OpenS
tackError
def
list_all_vm
(
self
):
def
list_all_vm
(
self
):
servers
=
[]
servers
=
[]
for
server
in
self
.
openstack
.
compute
.
servers
():
for
server
in
self
.
openstack
.
compute
.
servers
():
servers
.
append
(
self
.
convert_server_to_instance
(
server
))
servers
.
append
(
self
.
convert_server_to_instance
(
server
))
return
servers
return
servers
@
opens
tackError
@
OpenS
tackError
def
resize_vm
(
self
,
name_or_id
,
resource
):
def
resize_vm
(
self
,
name_or_id
,
resource
):
instance
=
self
.
_get_openstack_server
(
name_or_id
)
instance
=
self
.
_get_openstack_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
)
@
opens
tackError
@
OpenS
tackError
def
create_image
(
self
,
instance_id
,
metadata
=
None
):
def
create_image
(
self
,
instance_id
,
metadata
=
None
):
image_name
=
secrets
.
token_hex
(
30
)
image_name
=
secrets
.
token_hex
(
30
)
self
.
openstack
.
compute
.
create_server_image
(
instance_id
,
self
.
openstack
.
compute
.
create_server_image
(
instance_id
,
...
@@ -179,13 +164,13 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
...
@@ -179,13 +164,13 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
instance
=
nova
.
servers
.
get
(
server_id
)
instance
=
nova
.
servers
.
get
(
server_id
)
return
instance
.
get_vnc_console
(
"novnc"
)
return
instance
.
get_vnc_console
(
"novnc"
)
@
opens
tackError
@
OpenS
tackError
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
(
volumeId
=
volume_id
,
self
.
openstack
.
compute
.
create_volume_attachment
(
volumeId
=
volume_id
,
server
=
server_id
,
server
=
server_id
,
)
)
@
opens
tackError
@
OpenS
tackError
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
(
self
.
openstack
.
compute
.
delete_volume_attachment
(
volume_attachment
=
volume_id
,
volume_attachment
=
volume_id
,
...
@@ -205,7 +190,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
...
@@ -205,7 +190,7 @@ class OSVirtualMachineManager(InstanceInterface, OpenStackConnection):
launched_at
=
server
.
launched_at
,
launched_at
=
server
.
launched_at
,
terminated_at
=
server
.
terminated_at
,
terminated_at
=
server
.
terminated_at
,
addresses
=
server
.
addresses
)
addresses
=
server
.
addresses
)
def
os_image_to_rc_image
(
self
,
os_image
):
def
os_image_to_rc_image
(
self
,
os_image
):
return
Image
(
return
Image
(
os_image
.
id
,
os_image
.
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