Commit f2688b1f by Szabolcs Gelencser

Refactor deploy vm operation, add diagnostics extenstion

parent c6fe99b7
<WadCfg>
<DiagnosticMonitorConfiguration overallQuotaInMB="4096">
<DiagnosticInfrastructureLogs scheduledTransferLogLevelFilter="Warning" scheduledTransferPeriod="PT1M"/>
<PerformanceCounters scheduledTransferPeriod="PT1M">
<PerformanceCounterConfiguration counterSpecifier="\Memory\PercentUsedMemory" sampleRate="PT15S" unit="Percent">
<annotation displayName="Memory percentage" locale="en-us"/>
</PerformanceCounterConfiguration>
<PerformanceCounterConfiguration counterSpecifier="\Processor\PercentProcessorTime" sampleRate="PT15S" unit="Percent">
<annotation displayName="CPU percentage guest OS" locale="en-us"/>
</PerformanceCounterConfiguration>
<PerformanceCounterConfiguration counterSpecifier="\NetworkInterface\BytesTransmitted" sampleRate="PT15S" unit="Bytes">
<annotation displayName="Network out guest OS" locale="en-us"/>
</PerformanceCounterConfiguration>
<PerformanceCounterConfiguration counterSpecifier="\NetworkInterface\BytesReceived" sampleRate="PT15S" unit="Bytes">
<annotation displayName="Network in guest OS" locale="en-us"/>
</PerformanceCounterConfiguration>
<PerformanceCounterConfiguration counterSpecifier="\NetworkInterface\PacketsTransmitted" sampleRate="PT15S" unit="Count">
<annotation displayName="Packets sent" locale="en-us"/>
</PerformanceCounterConfiguration>
<PerformanceCounterConfiguration counterSpecifier="\NetworkInterface\PacketsReceived" sampleRate="PT15S" unit="Count">
<annotation displayName="Packets received" locale="en-us"/>
</PerformanceCounterConfiguration>
</PerformanceCounters>
<Metrics resourceId="/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$GROUP_NAME/providers/Microsoft.Compute/virtualMachines/$VM_NAME">
<MetricAggregation scheduledTransferPeriod="PT1H"/>
<MetricAggregation scheduledTransferPeriod="PT1M"/>
</Metrics>
</DiagnosticMonitorConfiguration>
</WadCfg>
......@@ -90,6 +90,25 @@ def create_azure_interface(ifacepk, vlan_name):
#TODO: should delete public ip (rollback)
return {"azure_id": None, "public_ip_name": None}
@celery.task
def collect_public_ips(nics):
"""
Return public ips of interfaces of running vm
"""
nics_with_public_ip = []
for nic in nics:
try:
public_ip_address = network_client.public_ip_addresses.get(
GROUP_NAME, nic["public_ip_name"])
nics_with_public_ip.append({
"id": nic["id"],
"public_ip": public_ip_address.ip_address,
})
except:
pass
return nics_with_public_ip
@celery.task
def create(network):
......
......@@ -7,6 +7,7 @@ import socket
import json
from decorator import decorator
import lxml.etree as ET
import base64
from psutil import NUM_CPUS, virtual_memory, cpu_percent
......@@ -18,7 +19,7 @@ from vmcelery import celery, lib_connection, to_bool
from azure.common.credentials import ServicePrincipalCredentials
import azure.mgmt.compute
import azure.mgmt.network
import azure.mgmt.storage
SUBSCRIPTION_ID = os.getenv('SUBSCRIPTION_ID')
CLIENT_ID = os.getenv('CLIENT_ID')
......@@ -39,11 +40,6 @@ compute_client = azure.mgmt.compute.ComputeManagementClient(
SUBSCRIPTION_ID
)
network_client = azure.mgmt.network.NetworkManagementClient(
credentials,
SUBSCRIPTION_ID
)
sys.path.append(os.path.dirname(os.path.basename(__file__)))
vm_xml_dump = None
......@@ -296,7 +292,90 @@ def save_as_template(vm_name, vm_pk):
return result.result().output["resources"][0]["properties"]\
["storageProfile"]["osDisk"]["image"]["uri"]
except Exception, e:
raise
raise e
@celery.task
def resume(vm_name):
"""
Start existing VM
"""
poller = compute_client.virtual_machines.start(GROUP_NAME, vm_name)
try:
poller.wait()
logging.info("start vm: %s" % vm_name)
return "ok"
except Exception, e:
logging.error("cloud not start vm '%s'" % vm_name)
return None
@celery.task
def install_diagnostics_extension(vm_name):
"""
Install diagnostics extension on already running vm
"""
with open("miscellaneous/linux_diagnostics.xml", "rt") as linuxdf:
XmlCfg = linuxdf.read()
b64encodedXmlCfg = base64.b64encode(
XmlCfg.replace(
"$SUBSCRIPTION_ID",
SUBSCRIPTION_ID,
).replace(
"$GROUP_NAME",
GROUP_NAME,
).replace(
"$VM_NAME",
vm_name,
)
)
storage_client = azure.mgmt.storage.StorageManagementClient(
credentials,
SUBSCRIPTION_ID,
)
key_result = storage_client.storage_accounts.list_keys(
GROUP_NAME,
STORAGE_NAME,
)
settings = {
'StorageAccount': STORAGE_NAME,
'xmlCfg': b64encodedXmlCfg,
}
protected_settings = {
'storageAccountName': STORAGE_NAME,
'storageAccountKey': key_result.keys[0].value,
'storageAccountEndPoint': 'https://core.windows.net',
}
poller = compute_client.virtual_machine_extensions.create_or_update(
GROUP_NAME,
vm_name,
"LinuxDiagnostic",
azure.mgmt.compute.models.VirtualMachineExtension(
location=REGION,
publisher="Microsoft.OSTCExtensions",
virtual_machine_extension_type="LinuxDiagnostic",
type_handler_version="2.3",
auto_upgrade_minor_version=True,
protected_settings=protected_settings,
settings=settings,
),
)
try:
poller.wait()
logging.info("installed diagnostics on: '%s'" % vm_name)
except Exception, e:
logging.error("cloud not install diagnostics on '%s'" % vm_name)
raise e
@celery.task
def start(params):
pass
@celery.task
@req_connection
......@@ -361,17 +440,6 @@ def undefine(name):
domain = lookupByName(name)
domain.undefine()
@celery.task
@req_connection
@wrap_libvirtError
def start(name):
""" Start an already defined virtual machine."""
domain = lookupByName(name)
domain.create()
@celery.task
@req_connection
@wrap_libvirtError
......@@ -411,29 +479,6 @@ def restore(name, path):
Connection.get().restore(path)
return domain_info(name)
@celery.task
def resume(nics):
"""
Resume stopped virtual machines
return public ip of interfaces
"""
nics_with_public_ip = []
for nic in nics:
try:
public_ip_address = network_client.public_ip_addresses.get(
GROUP_NAME, nic["public_ip_name"])
nics_with_public_ip.append({
"id": nic["id"],
"public_ip": public_ip_address.ip_address,
})
except:
pass
return nics_with_public_ip
@celery.task
@req_connection
@wrap_libvirtError
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment