summaryrefslogtreecommitdiffstats
path: root/deploy/environments/execution_environment.py
blob: af0e130dd74219bc4bc574e50fdb3d71dbb053dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Nu
###############################################################################
# Copyright (c) 2015 Ericsson AB and others.
# szilard.cserey@ericsson.com
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
###############################################################################


from lxml import etree
from dha_adapters.libvirt_adapter import LibvirtAdapter

from common import (
    exec_cmd,
    log,
    delete,
)


class ExecutionEnvironment(object):

    def __init__(self, storage_dir, dha_file, root_dir):
        self.storage_dir = storage_dir
        self.dha = LibvirtAdapter(dha_file)
        self.root_dir = root_dir
        self.parser = etree.XMLParser(remove_blank_text=True)
        self.fuel_node_id = self.dha.get_fuel_node_id()

    def delete_vm(self, node_id):
        vm_name = self.dha.get_node_property(node_id, 'libvirtName')
        r, c = exec_cmd('virsh dumpxml %s' % vm_name, False)
        if c:
            return
        self.undefine_vm_delete_disk(r, vm_name)

    def undefine_vm_delete_disk(self, printout, vm_name):
        disk_files = []
        xml_dump = etree.fromstring(printout, self.parser)
        disks = xml_dump.xpath('/domain/devices/disk')
        for disk in disks:
            sources = disk.xpath('source')
            for source in sources:
                source_file = source.get('file')
                if source_file:
                    disk_files.append(source_file)
        log('Deleting VM %s with disks %s' % (vm_name, disk_files))
        exec_cmd('virsh destroy %s' % vm_name, False)
        exec_cmd('virsh undefine %s' % vm_name, False)
        for file in disk_files:
            delete(file)

    def overwrite_xml(self, vm_xml, vm_definition_overwrite):
        if not vm_definition_overwrite:
            return
        for key, value in vm_definition_overwrite.iteritems():
            if key == 'attribute_equlas':
                continue
            if key == 'value':
                vm_xml.text = str(value)
                return
            if key == 'attribute':
                for attr_key, attr_value in value.iteritems():
                    vm_xml.set(attr_key, str(attr_value))
                return

            if isinstance(value, dict):
                only_when_attribute = value.get('attribute_equlas')
            for xml_element in vm_xml.xpath(key):
                if only_when_attribute:
                    for attr_key, attr_value in \
                            only_when_attribute.iteritems():
                        if attr_value != xml_element.get(attr_key):
                            continue
                self.overwrite_xml(xml_element, value)

    def define_vm(self, vm_name, temp_vm_file, disk_path,
                  vm_definition_overwrite):
        log('Creating VM %s with disks %s' % (vm_name, disk_path))
        with open(temp_vm_file) as f:
            vm_xml = etree.parse(f)
        names = vm_xml.xpath('/domain/name')
        for name in names:
            name.text = vm_name
        uuids = vm_xml.xpath('/domain/uuid')
        for uuid in uuids:
            uuid.getparent().remove(uuid)
        self.overwrite_xml(vm_xml.xpath('/domain')[0],
                           vm_definition_overwrite)
        disks = vm_xml.xpath('/domain/devices/disk')
        for disk in disks:
            if (disk.get('type') == 'file' and
                    disk.get('device') == 'disk'):
                sources = disk.xpath('source')
                for source in sources:
                    disk.remove(source)
                source = etree.Element('source')
                source.set('file', disk_path)
                disk.append(source)
        with open(temp_vm_file, 'w') as f:
            vm_xml.write(f, pretty_print=True, xml_declaration=True)
        exec_cmd('virsh define %s' % temp_vm_file)