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
c882edb7
authored
May 06, 2019
by
Belákovics Ádám
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create decorator to wrap openstack Error
parent
eee9882b
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
100 additions
and
35 deletions
+100
-35
.gitignore
+10
-1
Pipfile
+2
-0
implementation/vm/instance.py
+62
-26
interface/vm/instance.py
+5
-5
interface/vm/resources.py
+12
-0
main.py
+9
-3
No files found.
.gitignore
View file @
c882edb7
...
@@ -92,7 +92,16 @@ ipython_config.py
...
@@ -92,7 +92,16 @@ ipython_config.py
# celery beat schedule file
# celery beat schedule file
celerybeat-schedule
celerybeat-schedule
# SageMath parsed files
# SageMath parsed files ✘ cgergo@cgergo ~/mount/virtual/interface_openstack vm ● git status
On branch vm
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: .gitignore
deleted: implementation/instance.py
deleted: implementation/instance.pyc
*.sage.py
*.sage.py
# Environments
# Environments
...
...
Pipfile
View file @
c882edb7
...
@@ -7,6 +7,8 @@ verify_ssl = true
...
@@ -7,6 +7,8 @@ verify_ssl = true
[packages]
[packages]
openstacksdk = "*"
openstacksdk = "*"
novaclient = "*"
python-novaclient = "*"
[requires]
[requires]
python_version = "3.6"
python_version = "3.6"
implementation/vm/instance.py
View file @
c882edb7
from
interface.vm.instance
import
InstanceInterface
from
interface.vm.instance
import
InstanceInterface
from
interface.vm.vm
import
Instance
from
interface.vm.resources
import
Instance
from
openstack.exceptions
import
SDKException
# from novaclient import client
import
logging
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
.
get_error_message
())
new_e
=
Exception
(
e
.
get_error_message
())
new_e
.
OpenStackError
=
True
raise
new_e
return
wrap_OpenStackError
class
OSVirtualMachineManager
(
InstanceInterface
):
class
OSVirtualMachineManager
(
InstanceInterface
):
...
@@ -8,41 +27,42 @@ class OSVirtualMachineManager(InstanceInterface):
...
@@ -8,41 +27,42 @@ class OSVirtualMachineManager(InstanceInterface):
super
()
.
__init__
()
super
()
.
__init__
()
self
.
openstack
=
cloud
self
.
openstack
=
cloud
def
create_base_vm
(
self
,
resource
,
networks
):
@openstackError
raise
NotImplementedError
def
create_base_vm
(
self
,
name
,
resource
,
networks
,
block_dev_map
):
flavor
=
self
.
get_flavor
(
resource
)
new_server
=
self
.
compute
.
create_server
(
name
=
name
,
flavorRef
=
flavor
.
id
,
networks
=
networks
,
block_device_mapping
=
block_dev_map
)
return
new_server
@openstackError
def
create_vm_from_template
(
self
,
image
,
resource
,
networks
):
self
.
create_multiple_vm_from_template
(
image
,
resource
,
networks
,
1
)
def
create_vm_from_template
(
self
,
template
,
resource
,
networks
):
@openstackError
self
.
create_multiple_vm_from_template
(
template
,
resource
,
1
)
def
create_multiple_vm_from_template
(
self
,
image
,
resource
,
networks
,
number
,
**
args
):
def
create_multiple_vm_from_template
(
self
,
template
,
resource
,
networks
,
compute
=
self
.
openstack
.
compute
number
,
block_dev_map
=
None
,
flav
=
compute
.
find_flavor
(
resource
[
'name'
])
metadata
=
None
):
flav
=
self
.
openstack
.
compute
.
find_flavor
(
resource
[
'name'
])
image
=
compute
.
find_image
(
image
)
# TODO plan flavors
if
flav
is
None
:
flav
=
self
.
openstack
.
compute
.
create_flavor
(
name
=
resource
[
'name'
],
ram
=
resource
[
'ram'
],
vcpus
=
resource
[
'cpu'
],
disk
=
resource
[
'disk'
])
image
=
self
.
openstack
.
compute
.
find_image
(
template
)
if
not
image
:
if
not
image
:
raise
ValueError
(
"The template not found"
)
raise
ValueError
(
"The template not found"
)
compute
=
self
.
openstack
.
compute
new_server
=
compute
.
create_server
(
name
=
image
,
new_server
=
compute
.
create_server
(
name
=
template
,
flavorRef
=
flav
.
id
,
flavorRef
=
flav
.
id
,
imageRef
=
image
.
id
,
imageRef
=
image
.
id
,
networks
=
networks
,
networks
=
networks
,
min_count
=
number
,
min_count
=
number
,
block_device_mapping
=
block_dev_map
,
metadata
=
metadata
)
)
new_server
=
self
.
openstack
.
compute
.
wait_for_server
(
new_server
)
new_server
=
self
.
openstack
.
compute
.
wait_for_server
(
new_server
)
return
new_server
return
new_server
@openstackError
def
get_vm
(
self
,
name_or_id
=
None
):
def
get_vm
(
self
,
name_or_id
=
None
):
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"
)
...
@@ -51,55 +71,63 @@ class OSVirtualMachineManager(InstanceInterface):
...
@@ -51,55 +71,63 @@ class OSVirtualMachineManager(InstanceInterface):
raise
ValueError
(
"Could not get the vm"
)
raise
ValueError
(
"Could not get the vm"
)
return
server_instance
return
server_instance
@openstackError
def
start_vm
(
self
,
name_or_id
=
None
):
def
start_vm
(
self
,
name_or_id
=
None
):
if
name_or_id
:
if
name_or_id
:
instance
=
self
.
get_vm
(
name_or_id
)
instance
=
self
.
get_vm
(
name_or_id
)
self
.
openstack
.
compute
.
start_server
(
instance
)
self
.
openstack
.
compute
.
start_server
(
instance
)
@openstackError
def
stop_vm
(
self
,
name_or_id
=
None
):
def
stop_vm
(
self
,
name_or_id
=
None
):
if
name_or_id
:
if
name_or_id
:
instance
=
self
.
get_vm
(
name_or_id
)
instance
=
self
.
get_vm
(
name_or_id
)
self
.
openstack
.
compute
.
stop_server
(
instance
)
self
.
openstack
.
compute
.
stop_server
(
instance
)
@openstackError
def
suspend_vm
(
self
,
name_or_id
=
None
):
def
suspend_vm
(
self
,
name_or_id
=
None
):
if
name_or_id
:
if
name_or_id
:
instance
=
self
.
get_vm
(
name_or_id
)
instance
=
self
.
get_vm
(
name_or_id
)
self
.
openstack
.
compute
.
suspend_server
(
instance
)
self
.
openstack
.
compute
.
suspend_server
(
instance
)
@openstackError
def
wake_up_vm
(
self
,
name_or_id
=
None
):
def
wake_up_vm
(
self
,
name_or_id
=
None
):
if
name_or_id
:
if
name_or_id
:
instance
=
self
.
get_vm
(
name_or_id
)
instance
=
self
.
get_vm
(
name_or_id
)
self
.
openstack
.
compute
.
resume_server
(
instance
)
self
.
openstack
.
compute
.
resume_server
(
instance
)
@openstackError
def
reboot_vm
(
self
,
name_or_id
):
def
reboot_vm
(
self
,
name_or_id
):
if
name_or_id
:
if
name_or_id
:
instance
=
self
.
get_vm
(
name_or_id
)
instance
=
self
.
get_vm
(
name_or_id
)
self
.
openstack
.
compute
.
reboot_server
(
instance
,
reboot_type
=
'SOFT'
)
self
.
openstack
.
compute
.
reboot_server
(
instance
,
reboot_type
=
'SOFT'
)
@openstackError
def
reset_vm
(
self
,
name_or_id
):
def
reset_vm
(
self
,
name_or_id
):
if
name_or_id
:
if
name_or_id
:
instance
=
self
.
get_vm
(
name_or_id
)
instance
=
self
.
get_vm
(
name_or_id
)
self
.
openstack
.
compute
.
reboot_server
(
instance
,
reboot_type
=
'HARD'
)
self
.
openstack
.
compute
.
reboot_server
(
instance
,
reboot_type
=
'HARD'
)
@openstackError
def
destroy_vm
(
self
,
name_or_id
):
def
destroy_vm
(
self
,
name_or_id
):
if
name_or_id
:
if
name_or_id
:
instance
=
self
.
get_vm
(
name_or_id
)
instance
=
self
.
get_vm
(
name_or_id
)
self
.
openstack
.
compute
.
delete_server
(
instance
)
self
.
openstack
.
compute
.
delete_server
(
instance
)
@openstackError
def
get_status
(
self
,
name_or_id
):
def
get_status
(
self
,
name_or_id
):
if
name_or_id
:
if
name_or_id
:
instance
=
self
.
get_vm
(
name_or_id
)
instance
=
self
.
get_vm
(
name_or_id
)
return
instance
.
status
return
instance
.
status
@openstackError
def
list_all_vm
(
self
):
def
list_all_vm
(
self
):
return
self
.
openstack
.
compute
.
servers
()
return
self
.
openstack
.
compute
.
servers
()
@openstackError
def
get_vnc_console
(
self
,
name_or_id
):
def
get_vnc_console
(
self
,
name_or_id
):
raise
NotImplementedError
raise
NotImplementedError
def
change_password
(
self
,
name_or_id
):
@openstackError
raise
NotImplementedError
def
resize_vm
(
self
,
name_or_id
,
resource
):
def
resize_vm
(
self
,
name_or_id
,
resource
):
if
name_or_id
:
if
name_or_id
:
instance
=
self
.
get_vm
(
name_or_id
)
instance
=
self
.
get_vm
(
name_or_id
)
...
@@ -107,11 +135,19 @@ class OSVirtualMachineManager(InstanceInterface):
...
@@ -107,11 +135,19 @@ class OSVirtualMachineManager(InstanceInterface):
self
.
openstack
.
compute
.
resize_server
(
instance
,
flavor
)
self
.
openstack
.
compute
.
resize_server
(
instance
,
flavor
)
@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
:
if
name_or_id
:
instance
=
self
.
get_vm
(
name_or_id
)
instance
=
self
.
get_vm
(
name_or_id
)
self
.
openstack
.
compute
.
create_server_image
(
instance
,
template_name
,
metadata
)
self
.
openstack
.
compute
.
create_server_image
(
instance
,
template_name
,
metadata
)
# def get_vnc_console(self, name_or_id):
# with client.Client(2, "admin", "64c7ee341d03844c548c",
# "2db5309f541c4466bc80bc534cf579d7", "http://10.34.0.113/identity") as nova:
# if name_or_id:
# instance = nova.servers.get(name_or_id)
# return instance.get_vnc_console()
def
attach_volume
(
self
,
name_or_id
,
amount
):
def
attach_volume
(
self
,
name_or_id
,
amount
):
raise
NotImplementedError
raise
NotImplementedError
...
@@ -119,7 +155,7 @@ class OSVirtualMachineManager(InstanceInterface):
...
@@ -119,7 +155,7 @@ class OSVirtualMachineManager(InstanceInterface):
return
Instance
(
id
=
server
.
id
,
return
Instance
(
id
=
server
.
id
,
resource
=
server
.
flavor
.
id
,
resource
=
server
.
flavor
.
id
,
name
=
server
.
name
,
name
=
server
.
name
,
image
=
server
.
image
,
image
_id
=
server
.
image
,
disks
=
server
.
volumes
,
disks
=
server
.
volumes
,
interfaces
=
server
.
interfaces
,
interfaces
=
server
.
interfaces
,
status
=
server
.
status
,
status
=
server
.
status
,
...
...
interface/vm/instance.py
View file @
c882edb7
...
@@ -61,11 +61,11 @@ class InstanceInterface:
...
@@ -61,11 +61,11 @@ class InstanceInterface:
def
get_vnc_console
(
self
,
name_or_id
):
def
get_vnc_console
(
self
,
name_or_id
):
raise
NotImplementedError
raise
NotImplementedError
def
change_password
(
self
,
name_or_id
):
#
def change_password(self, name_or_id):
raise
NotImplementedError
#
raise NotImplementedError
#
def
get_password
(
self
,
name_or_id
):
#
def get_password(self, name_or_id):
raise
NotImplementedError
#
raise NotImplementedError
def
resize_vm
(
self
,
name_or_id
):
def
resize_vm
(
self
,
name_or_id
):
raise
NotImplementedError
raise
NotImplementedError
...
...
interface/vm/
vm
.py
→
interface/vm/
resources
.py
View file @
c882edb7
...
@@ -23,3 +23,15 @@ class Instance:
...
@@ -23,3 +23,15 @@ class Instance:
def
JSON
(
self
):
def
JSON
(
self
):
return
json
.
dump
(
self
)
return
json
.
dump
(
self
)
class
Flavor
:
def
__init__
(
self
,
name
,
id
,
ram
,
vcpus
,
disk
):
self
.
id
=
id
self
.
name
=
name
self
.
ram
=
ram
self
.
vcpus
=
vcpus
self
.
initial_disk
=
disk
class
Volume
:
pass
main.py
View file @
c882edb7
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
from
implementation.vm.instance
import
OSVirtualMachineManager
from
implementation.vm.instance
import
OSVirtualMachineManager
import
openstack
import
openstack
#
openstack.enable_logging(debug=True)
openstack
.
enable_logging
(
debug
=
True
)
conn
=
openstack
.
connect
(
cloud
=
'openstack'
)
conn
=
openstack
.
connect
(
cloud
=
'openstack'
)
...
@@ -15,10 +15,16 @@ print(resource)
...
@@ -15,10 +15,16 @@ print(resource)
# interface.stop_vm('a6bc504f-a422-4492-b429-e5dad2df12f4')
# interface.stop_vm('a6bc504f-a422-4492-b429-e5dad2df12f4')
server
=
interface
.
get_vm
(
"New test"
)
server
=
interface
.
get_vm
(
"New test"
)
print
(
server
)
print
(
interface
.
get_status
(
"Uborka Test"
))
print
(
interface
.
get_status
(
"Uborka Test"
))
#interface.create_template("New test", "test_template")
# print(interface.convert_server_to_instance(server))
interface
.
create_vm_from_template
(
"test_template"
,
resource
)
# print(interface.get_vnc_console("New test"))
# interface.create_template("New test", "test_template")
# interface.create_vm_from_template("test_template", resource, [{"uuid": "c03d0d4b-413e-4cc6-9ebe-c0b5ca0dac3a"}])
# interface.resize_vm(server, resource)
# interface.resize_vm(server, resource)
# interface.reboot_vm(server.id)
# interface.reboot_vm(server.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