summaryrefslogtreecommitdiffstats
path: root/deploy/environments/libvirt_environment.py
blob: c9fa41fd79c437337d0ecf823b935d645788afd0 (plain)
1
2
3
4
5
6
7

@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.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /
###############################################################################
# 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
import glob
from execution_environment import ExecutionEnvironment
import tempfile

from common import (
    exec_cmd,
    log,
    check_dir_exists,
    check_file_exists,
    delete,
)


class LibvirtEnvironment(ExecutionEnvironment):

    def __init__(self, storage_dir, dha_file, dea, root_dir):
        super(LibvirtEnvironment, self).__init__(
            storage_dir, dha_file, root_dir)
        self.dea = dea
        self.network_dir = '%s/%s' % (self.root_dir,
                                      self.dha.get_virt_net_conf_dir())
        self.node_ids = self.dha.get_all_node_ids()
        self.net_names = self.collect_net_names()

    def create_storage(self, node_id, disk_path, disk_sizes):
        role = self.dea.get_node_main_role(node_id, self.fuel_node_id)
        disk_size = disk_sizes[role]
        exec_cmd('qemu-img create -f qcow2 %s %s' % (disk_path, disk_size))

    def create_vms(self):
        temp_dir = tempfile.mkdtemp()
        disk_sizes = self.dha.get_disks()
        for node_id in self.node_ids:
            vm_name = self.dha.get_node_property(node_id, 'libvirtName')
            vm_template = '%s/%s' % (self.root_dir,
                                     self.dha.get_node_property(
                                         node_id, 'libvirtTemplate'))
            check_file_exists(vm_template)
            disk_path = '%s/%s.raw' % (self.storage_dir, vm_name)
            self.create_storage(node_id, disk_path, disk_sizes)
            temp_vm_file = '%s/%s' % (temp_dir, vm_name)
            exec_cmd('cp %s %s' % (vm_template, temp_vm_file))
            vm_definition_overwrite = self.dha.get_vm_definition(
                 self.dea.get_node_main_role(node_id, self.fuel_node_id))
            self.define_vm(vm_name, temp_vm_file, disk_path,
                           vm_definition_overwrite)
        delete(temp_dir)

    def start_vms(self):
        for node_id in self.node_ids:
            self.dha.node_power_on(node_id)

    def create_networks(self):
        for net_file in glob.glob('%s/*' % self.network_dir):
            exec_cmd('virsh net-define %s' % net_file)
        for net in self.net_names:
            log('Creating network %s' % net)
            exec_cmd('virsh net-autostart %s' % net)
            exec_cmd('virsh net-start %s' % net)

    def delete_networks(self):
        for net in self.net_names:
            log('Deleting network %s' % net)
            exec_cmd('virsh net-destroy %s' % net, False)
            exec_cmd('virsh net-undefine %s' % net, False)

    def get_net_name(self, net_file):
        with open(net_file) as f:
            net_xml = etree.parse(f)
            name_list = net_xml.xpath('/network/name')
            for name in name_list:
                net_name = name.text
        return net_name

    def collect_net_names(self):
        net_list = []
        for net_file in glob.glob('%s/*' % self.network_dir):
            name = self.get_net_name(net_file)
            net_list.append(name)
        return net_list

    def delete_vms(self):
        for node_id in self.node_ids:
            self.delete_vm(node_id)


    def setup_environment(self):
        check_dir_exists(self.network_dir)
        self.cleanup_environment()
        self.create_networks()
        self.create_vms()
        self.start_vms()

    def cleanup_environment(self):
        self.delete_vms()
        self.delete_networks()