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
b24c1117
authored
Oct 11, 2016
by
Szabolcs Gelencser
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement create task for Azure VM creation.
parent
13928bea
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
49 deletions
+80
-49
vmdriver.py
+80
-49
No files found.
vmdriver.py
View file @
b24c1117
...
...
@@ -16,6 +16,27 @@ from vm import VMInstance, VMDisk, VMNetwork
from
vmcelery
import
celery
,
lib_connection
,
to_bool
from
azure.common.credentials
import
ServicePrincipalCredentials
import
azure.mgmt.compute
SUBSCRIPTION_ID
=
os
.
getenv
(
'SUBSCRIPTION_ID'
)
CLIENT_ID
=
os
.
getenv
(
'CLIENT_ID'
)
SECRET
=
os
.
getenv
(
'SECRET'
)
TENANT
=
os
.
getenv
(
'TENANT'
)
GROUP_NAME
=
os
.
getenv
(
'GROUP_NAME'
)
REGION
=
os
.
getenv
(
'REGION'
)
credentials
=
ServicePrincipalCredentials
(
client_id
=
CLIENT_ID
,
secret
=
SECRET
,
tenant
=
TENANT
,
)
compute_client
=
azure
.
mgmt
.
compute
.
ComputeManagementClient
(
credentials
,
SUBSCRIPTION_ID
)
sys
.
path
.
append
(
os
.
path
.
dirname
(
os
.
path
.
basename
(
__file__
)))
vm_xml_dump
=
None
...
...
@@ -30,7 +51,6 @@ state_dict = {0: 'NOSTATE',
7
:
'PMSUSPENDED'
}
# class Singleton(type):
#
# """ Singleton class."""
...
...
@@ -154,48 +174,66 @@ def define(vm):
@celery.task
@req_connection
@wrap_libvirtError
def
create
(
vm_desc
):
""" Create and start non-permanent virtual machine from xml.
""" Create and start a virtual machine in azure. """
os_disk_name
=
"
%
s_os_disk"
%
vm_desc
[
"name"
]
nic_references
=
[]
for
nic
in
vm_desc
[
"nics"
]:
nic_references
.
append
(
azure
.
mgmt
.
compute
.
models
.
NetworkInterfaceReference
(
id
=
nic
,
)
)
poller
=
compute_client
.
virtual_machines
.
create_or_update
(
GROUP_NAME
,
vm_desc
[
"name"
],
azure
.
mgmt
.
compute
.
models
.
VirtualMachine
(
location
=
REGION
,
os_profile
=
azure
.
mgmt
.
compute
.
models
.
OSProfile
(
admin_username
=
vm_desc
[
"user"
],
admin_password
=
vm_desc
[
"pw"
],
computer_name
=
vm_desc
[
"name"
],
),
hardware_profile
=
azure
.
mgmt
.
compute
.
models
.
HardwareProfile
(
vm_size
=
vm_desc
[
"vm_size"
]
),
network_profile
=
azure
.
mgmt
.
compute
.
models
.
NetworkProfile
(
network_interfaces
=
nic_references
,
),
storage_profile
=
azure
.
mgmt
.
compute
.
models
.
StorageProfile
(
os_disk
=
azure
.
mgmt
.
compute
.
models
.
OSDisk
(
caching
=
azure
.
mgmt
.
compute
.
models
.
CachingTypes
.
none
,
create_option
=
\
azure
.
mgmt
.
compute
.
models
.
DiskCreateOptionTypes
.
from_image
,
name
=
os_disk_name
,
vhd
=
azure
.
mgmt
.
compute
.
models
.
VirtualHardDisk
(
uri
=
'https://
%
s.blob.core.windows.net/vhds/
%
s.vhd'
%
(
vm_desc
[
"datastore_name"
],
os_disk_name
,
),
),
),
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"
],
),
),
),
)
Return the domain info dict.
flags can be:
VIR_DOMAIN_NONE = 0
VIR_DOMAIN_START_PAUSED = 1
VIR_DOMAIN_START_AUTODESTROY = 2
VIR_DOMAIN_START_BYPASS_CACHE = 4
VIR_DOMAIN_START_FORCE_BOOT = 8
"""
vm
=
VMInstance
.
deserialize
(
vm_desc
)
# Setting proper hypervisor
vm
.
vm_type
=
os
.
getenv
(
"HYPERVISOR_TYPE"
,
"test"
)
if
vm
.
vm_type
==
"test"
:
vm
.
arch
=
"i686"
vm_xml_dump
=
vm
.
dump_xml
()
logging
.
info
(
vm_xml_dump
)
# Emulating DOMAIN_START_PAUSED FLAG behaviour on test driver
if
vm
.
vm_type
==
"test"
:
Connection
.
get
()
.
createXML
(
vm_xml_dump
,
libvirt
.
VIR_DOMAIN_NONE
)
domain
=
lookupByName
(
vm
.
name
)
domain
.
suspend
()
# Real driver create
else
:
Connection
.
get
()
.
createXML
(
vm_xml_dump
,
libvirt
.
VIR_DOMAIN_START_PAUSED
)
logging
.
info
(
"Virtual machine
%
s is created from xml"
,
vm
.
name
)
# context
try
:
sock
=
socket
.
create_connection
((
'127.0.0.1'
,
1235
),
3
)
data
=
{
'boot_token'
:
vm
.
boot_token
,
'socket'
:
'/var/lib/libvirt/serial/
%
s'
%
vm
.
name
}
sock
.
sendall
(
json
.
dumps
(
data
))
sock
.
close
()
except
socket
.
error
:
logging
.
error
(
'Unable to connect to context server'
)
return
vm_xml_dump
poller
.
wait
(
)
azure_id
=
poller
.
result
()
.
id
logging
.
info
(
"created vm with id:
%
s"
%
azure_id
)
return
azure_id
except
Exception
,
e
:
logging
.
error
(
"cloud not create vm '
%
s'"
%
vm_desc
[
"name"
])
logging
.
error
(
str
(
e
)
)
return
None
class
shutdown
(
AbortableTask
):
...
...
@@ -347,18 +385,11 @@ def restore(name, path):
@celery.task
@req_connection
@wrap_libvirtError
def
resume
(
name
):
""" Resume stopped virtual machines.
Return the domain info dict.
"""
domain
=
lookupByName
(
name
)
domain
.
resume
()
return
_parse_info
(
domain
.
info
())
#TODO: implement
return
@celery.task
...
...
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