Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
vmdriver
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
c6fe99b7
authored
Oct 24, 2016
by
Szabolcs Gelencser
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add VM shutdown, capture template, create from template
parent
a8e2421f
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
37 deletions
+59
-37
vmdriver.py
+59
-37
No files found.
vmdriver.py
View file @
c6fe99b7
...
...
@@ -26,6 +26,7 @@ SECRET = os.getenv('SECRET')
TENANT
=
os
.
getenv
(
'TENANT'
)
GROUP_NAME
=
os
.
getenv
(
'GROUP_NAME'
)
REGION
=
os
.
getenv
(
'REGION'
)
STORAGE_NAME
=
os
.
getenv
(
'STORAGE_NAME'
)
credentials
=
ServicePrincipalCredentials
(
client_id
=
CLIENT_ID
,
...
...
@@ -192,6 +193,26 @@ def create(vm_desc):
)
)
vhd_uri
=
None
image_reference
=
None
os_disk_type
=
None
if
vm_desc
[
"vhd_uri"
]:
#TODO: select OS Disk type based on OS
os_disk_type
=
azure
.
mgmt
.
compute
.
models
.
OperatingSystemTypes
.
linux
vhd_uri
=
azure
.
mgmt
.
compute
.
models
.
VirtualHardDisk
(
vm_desc
[
"vhd_uri"
]
)
logging
.
info
(
"creating vm from template"
)
else
:
image_reference
=
azure
.
mgmt
.
compute
.
models
.
ImageReference
(
publisher
=
vm_desc
[
"os_publisher"
],
offer
=
vm_desc
[
"os_offer"
],
sku
=
vm_desc
[
"os_sku"
],
version
=
vm_desc
[
"os_version"
],
)
logging
.
info
(
"creating vm from Azure image"
)
poller
=
compute_client
.
virtual_machines
.
create_or_update
(
GROUP_NAME
,
vm_desc
[
"name"
],
...
...
@@ -210,6 +231,7 @@ def create(vm_desc):
),
storage_profile
=
azure
.
mgmt
.
compute
.
models
.
StorageProfile
(
os_disk
=
azure
.
mgmt
.
compute
.
models
.
OSDisk
(
os_type
=
os_disk_type
,
caching
=
azure
.
mgmt
.
compute
.
models
.
CachingTypes
.
none
,
create_option
=
\
azure
.
mgmt
.
compute
.
models
.
DiskCreateOptionTypes
.
from_image
,
...
...
@@ -220,13 +242,9 @@ def create(vm_desc):
os_disk_name
,
),
),
image
=
vhd_uri
,
),
image_reference
=
azure
.
mgmt
.
compute
.
models
.
ImageReference
(
publisher
=
vm_desc
[
"os_publisher"
],
offer
=
vm_desc
[
"os_offer"
],
sku
=
vm_desc
[
"os_sku"
],
version
=
vm_desc
[
"os_version"
],
),
image_reference
=
image_reference
,
),
),
)
...
...
@@ -239,42 +257,46 @@ def create(vm_desc):
except
Exception
,
e
:
logging
.
error
(
"cloud not create vm '
%
s'"
%
vm_desc
[
"name"
])
logging
.
error
(
str
(
e
))
raise
e
@celery.task
def
shutdown
(
vm_name
):
""" Shutdown a virtual machine in azure. """
poller
=
compute_client
.
virtual_machines
.
deallocate
(
GROUP_NAME
,
vm_name
)
try
:
poller
.
wait
()
logging
.
info
(
"shutdown vm:
%
s"
%
vm_name
)
return
"ok"
except
Exception
,
e
:
logging
.
error
(
"cloud not stop vm '
%
s'"
%
vm_name
)
return
None
@celery.task
def
save_as_template
(
vm_name
,
vm_pk
):
""" Save virtual machine as template in azure. """
try
:
compute_client
.
virtual_machines
.
generalize
(
GROUP_NAME
,
vm_name
)
class
shutdown
(
AbortableTask
):
""" Shutdown virtual machine (need ACPI support).
Return When domain is missiing.
This job is abortable:
AbortableAsyncResult(id="<<jobid>>").abort()
"""
time_limit
=
120
os_disk_name
=
"
%
s_os_disk_template"
%
vm_pk
@req_connection
def
run
(
self
,
args
):
from
time
import
sleep
name
,
=
args
try
:
domain
=
lookupByName
(
name
)
domain
.
shutdown
()
while
True
:
try
:
Connection
.
get
()
.
lookupByName
(
name
)
except
libvirt
.
libvirtError
as
e
:
if
e
.
get_error_code
()
==
libvirt
.
VIR_ERR_NO_DOMAIN
:
return
else
:
raise
else
:
if
self
.
is_aborted
():
logging
.
info
(
"Shutdown aborted on vm:
%
s"
,
name
)
return
sleep
(
5
)
except
libvirt
.
libvirtError
as
e
:
new_e
=
Exception
(
e
.
get_error_message
())
new_e
.
libvirtError
=
True
raise
new_e
result
=
compute_client
.
virtual_machines
.
capture
(
GROUP_NAME
,
vm_name
,
azure
.
mgmt
.
compute
.
models
.
VirtualMachineCaptureParameters
(
os_disk_name
,
"templates"
,
False
)
)
result
.
wait
()
return
result
.
result
()
.
output
[
"resources"
][
0
][
"properties"
]
\
[
"storageProfile"
][
"osDisk"
][
"image"
][
"uri"
]
except
Exception
,
e
:
raise
@celery.task
@req_connection
...
...
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