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
Hide 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')
...
@@ -26,6 +26,7 @@ SECRET = os.getenv('SECRET')
TENANT
=
os
.
getenv
(
'TENANT'
)
TENANT
=
os
.
getenv
(
'TENANT'
)
GROUP_NAME
=
os
.
getenv
(
'GROUP_NAME'
)
GROUP_NAME
=
os
.
getenv
(
'GROUP_NAME'
)
REGION
=
os
.
getenv
(
'REGION'
)
REGION
=
os
.
getenv
(
'REGION'
)
STORAGE_NAME
=
os
.
getenv
(
'STORAGE_NAME'
)
credentials
=
ServicePrincipalCredentials
(
credentials
=
ServicePrincipalCredentials
(
client_id
=
CLIENT_ID
,
client_id
=
CLIENT_ID
,
...
@@ -192,6 +193,26 @@ def create(vm_desc):
...
@@ -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
(
poller
=
compute_client
.
virtual_machines
.
create_or_update
(
GROUP_NAME
,
GROUP_NAME
,
vm_desc
[
"name"
],
vm_desc
[
"name"
],
...
@@ -210,6 +231,7 @@ def create(vm_desc):
...
@@ -210,6 +231,7 @@ def create(vm_desc):
),
),
storage_profile
=
azure
.
mgmt
.
compute
.
models
.
StorageProfile
(
storage_profile
=
azure
.
mgmt
.
compute
.
models
.
StorageProfile
(
os_disk
=
azure
.
mgmt
.
compute
.
models
.
OSDisk
(
os_disk
=
azure
.
mgmt
.
compute
.
models
.
OSDisk
(
os_type
=
os_disk_type
,
caching
=
azure
.
mgmt
.
compute
.
models
.
CachingTypes
.
none
,
caching
=
azure
.
mgmt
.
compute
.
models
.
CachingTypes
.
none
,
create_option
=
\
create_option
=
\
azure
.
mgmt
.
compute
.
models
.
DiskCreateOptionTypes
.
from_image
,
azure
.
mgmt
.
compute
.
models
.
DiskCreateOptionTypes
.
from_image
,
...
@@ -220,13 +242,9 @@ def create(vm_desc):
...
@@ -220,13 +242,9 @@ def create(vm_desc):
os_disk_name
,
os_disk_name
,
),
),
),
),
image
=
vhd_uri
,
),
),
image_reference
=
azure
.
mgmt
.
compute
.
models
.
ImageReference
(
image_reference
=
image_reference
,
publisher
=
vm_desc
[
"os_publisher"
],
offer
=
vm_desc
[
"os_offer"
],
sku
=
vm_desc
[
"os_sku"
],
version
=
vm_desc
[
"os_version"
],
),
),
),
),
),
)
)
...
@@ -239,42 +257,46 @@ def create(vm_desc):
...
@@ -239,42 +257,46 @@ def create(vm_desc):
except
Exception
,
e
:
except
Exception
,
e
:
logging
.
error
(
"cloud not create vm '
%
s'"
%
vm_desc
[
"name"
])
logging
.
error
(
"cloud not create vm '
%
s'"
%
vm_desc
[
"name"
])
logging
.
error
(
str
(
e
))
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
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
):
os_disk_name
=
"
%
s_os_disk_template"
%
vm_pk
""" Shutdown virtual machine (need ACPI support).
Return When domain is missiing.
This job is abortable:
AbortableAsyncResult(id="<<jobid>>").abort()
"""
time_limit
=
120
@req_connection
result
=
compute_client
.
virtual_machines
.
capture
(
def
run
(
self
,
args
):
GROUP_NAME
,
from
time
import
sleep
vm_name
,
name
,
=
args
azure
.
mgmt
.
compute
.
models
.
VirtualMachineCaptureParameters
(
try
:
os_disk_name
,
domain
=
lookupByName
(
name
)
"templates"
,
domain
.
shutdown
()
False
while
True
:
)
try
:
)
Connection
.
get
()
.
lookupByName
(
name
)
result
.
wait
()
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
return
result
.
result
()
.
output
[
"resources"
][
0
][
"properties"
]
\
[
"storageProfile"
][
"osDisk"
][
"image"
][
"uri"
]
except
Exception
,
e
:
raise
@celery.task
@celery.task
@req_connection
@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