<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
interact with a local/remote libvirt daemon
"""

# from defaults import TEMPLATES
# from distutils.spawn import find_executable
# from netaddr import IPNetwork
import os
import time
from virtualbox import VirtualBox, library, Session
from kvirt import common
import string
import yaml


KB = 1024 * 1024
MB = 1024 * KB
guestrhel532 = "RedHat"
guestrhel564 = "RedHat_64"
guestrhel632 = "RedHat"
guestrhel664 = "RedHat_64"
guestrhel764 = "RedHat_64"
guestother = "Other"
guestotherlinux = "Linux"
guestwindowsxp = "WindowsXP"
guestwindows7 = "Windows7"
guestwindows764 = "Windows7_64"
guestwindows2003 = "Windows2003"
guestwindows200364 = "Windows2003_64"
guestwindows2008 = "Windows2008_64"
guestwindows200864 = "Windows2008_64"
status = {'PoweredOff': 'down', 'PoweredOn': 'up', 'FirstOnline': 'up', 'Aborted': 'down', 'Saved': 'down'}


class Kbox:
    def __init__(self):
        try:
            self.conn = VirtualBox()
        except Exception:
            self.conn = None

    def close(self):
        conn = self.conn
        conn.close()
        self.conn = None

    def exists(self, name):
        conn = self.conn
        for vmname in conn.machines:
            if str(vmname) == name:
                return True
        return False

    def net_exists(self, name):
        conn = self.conn
        networks = []
        for network in conn.internal_networks:
            networks.append(network)
        for network in conn.nat_networks:
            networks.append(network.network_name)
        if name in networks:
            return True
        else:
            return False

    def disk_exists(self, pool, name):
        disks = self.list_disks()
        if name in disks:
            return True
        else:
            return True

    def create(self, name, virttype='vbox', title='', description='kvirt', numcpus=2, memory=512, guestid='Linux_64', pool='default', template=None, disks=[{'size': 10}], disksize=10, diskthin=True, diskinterface='virtio', nets=['default'], iso=None, vnc=False, cloudinit=True, reserveip=False, reservedns=False, start=True, keys=None, cmds=None, ips=None, netmasks=None, gateway=None, nested=True, dns=None, domain=None, tunnel=False, files=[]):
        guestid = 'Linux_64'
        default_diskinterface = diskinterface
        default_diskthin = diskthin
        default_disksize = disksize
        default_pool = pool
        default_poolpath = '/tmp'
        conn = self.conn
        vm = conn.create_machine("", name, [], guestid, "")
        vm.cpu_count = numcpus
        vm.add_storage_controller('SATA', library.StorageBus(2))
        vm.add_storage_controller('IDE', library.StorageBus(1))
        vm.memory_size = memory
        vm.description = description
        vm.set_extra_data('profile', title)
        serial = vm.get_serial_port(0)
        serial.server = True
        serial.enabled = True
        serial.path = str(common.get_free_port())
        serial.host_mode = library.PortMode.tcp
        nat_networks = [network.network_name for network in conn.nat_networks]
        internal_networks = [network for network in conn.internal_networks]
        for index, net in enumerate(nets):
            nic = vm.get_network_adapter(index)
            nic.enabled = True
            if isinstance(net, str):
                network = net
            elif isinstance(net, dict) and 'name' in net:
                network = net['name']
                ip = None
                if ips and len(ips) > index and ips[index] is not None:
                    ip = ips[index]
                    vm.set_extra_data('ip', ip)
                    nets[index]['ip'] = ip
                elif 'ip' in nets[index]:
                    ip = nets[index]['ip']
                    vm.set_extra_data('ip', ip)
                if 'mac' in nets[index]:
                    nic.mac_address = nets[index]['mac'].replace(':', '')
            if network in internal_networks:
                nic.attachment_type = library.NetworkAttachmentType.internal
                nic.internal_network = network
            elif network in nat_networks:
                nic.attachment_type = library.NetworkAttachmentType.nat_network
                nic.nat_network = network
                if ip is not None and index == 0:
                    natengine = nic.nat_engine
                    nat_network = [n for n in conn.nat_networks if n.network_name == network][0]
                    nat_network.add_port_forward_rule(False, 'ssh_%s' % name, library.NATProtocol.tcp, '', common.get_free_port(), ip, 22)
            else:
                nic.attachment_type = library.NetworkAttachmentType.nat
                if index == 0:
                    natengine = nic.nat_engine
                    natengine.add_redirect('ssh_%s' % name, library.NATProtocol.tcp, '', common.get_free_port(), '', 22)
        vm.save_settings()
        conn.register_machine(vm)
        session = Session()
        vm.lock_machine(session, library.LockType.write)
        machine = session.machine<commit_msg>virtualbox network and ansible improvements<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
interact with a local/remote libvirt daemon
"""

# from defaults import TEMPLATES
# from distutils.spawn import find_executable
# from netaddr import IPNetwork
import os
import time
from virtualbox import VirtualBox, library, Session
from kvirt import common
import string
import yaml


KB = 1024 * 1024
MB = 1024 * KB
guestrhel532 = "RedHat"
guestrhel564 = "RedHat_64"
guestrhel632 = "RedHat"
guestrhel664 = "RedHat_64"
guestrhel764 = "RedHat_64"
guestother = "Other"
guestotherlinux = "Linux"
guestwindowsxp = "WindowsXP"
guestwindows7 = "Windows7"
guestwindows764 = "Windows7_64"
guestwindows2003 = "Windows2003"
guestwindows200364 = "Windows2003_64"
guestwindows2008 = "Windows2008_64"
guestwindows200864 = "Windows2008_64"
status = {'PoweredOff': 'down', 'PoweredOn': 'up', 'FirstOnline': 'up', 'Aborted': 'down', 'Saved': 'down'}


class Kbox:
    def __init__(self):
        try:
            self.conn = VirtualBox()
        except Exception:
            self.conn = None

    def close(self):
        conn = self.conn
        conn.close()
        self.conn = None

    def exists(self, name):
        conn = self.conn
        for vmname in conn.machines:
            if str(vmname) == name:
                return True
        return False

    def net_exists(self, name):
        conn = self.conn
        networks = []
        for network in conn.internal_networks:
            networks.append(network)
        for network in conn.nat_networks:
            networks.append(network.network_name)
        if name in networks:
            return True
        else:
            return False

    def disk_exists(self, pool, name):
        disks = self.list_disks()
        if name in disks:
            return True
        else:
            return True

    def create(self, name, virttype='vbox', title='', description='kvirt', numcpus=2, memory=512, guestid='Linux_64', pool='default', template=None, disks=[{'size': 10}], disksize=10, diskthin=True, diskinterface='virtio', nets=['default'], iso=None, vnc=False, cloudinit=True, reserveip=False, reservedns=False, start=True, keys=None, cmds=None, ips=None, netmasks=None, gateway=None, nested=True, dns=None, domain=None, tunnel=False, files=[]):
        guestid = 'Linux_64'
        default_diskinterface = diskinterface
        default_diskthin = diskthin
        default_disksize = disksize
        default_pool = pool
        default_poolpath = '/tmp'
        conn = self.conn
        vm = conn.create_machine("", name, [], guestid, "")
        vm.cpu_count = numcpus
        vm.add_storage_controller('SATA', library.StorageBus(2))
        vm.add_storage_controller('IDE', library.StorageBus(1))
        vm.memory_size = memory
        vm.description = description
        vm.set_extra_data('profile', title)
        serial = vm.get_serial_port(0)
        serial.server = True
        serial.enabled = True
        serial.path = str(common.get_free_port())
        serial.host_mode = library.PortMode.tcp
        nat_networks = [network.network_name for network in conn.nat_networks]
        internal_networks = [network for network in conn.internal_networks]
        for index, net in enumerate(nets):
            ip = None
            nic = vm.get_network_adapter(index)
            nic.adapter_type = library.NetworkAdapterType.virtio
            nic.enabled = True
            if isinstance(net, str):
                network = net
            elif isinstance(net, dict) and 'name' in net:
                network = net['name']
                if ips and len(ips) > index and ips[index] is not None:
                    ip = ips[index]
                    vm.set_extra_data('ip', ip)
                    nets[index]['ip'] = ip
                elif 'ip' in nets[index]:
                    ip = nets[index]['ip']
                    vm.set_extra_data('ip', ip)
                if 'mac' in nets[index]:
                    nic.mac_address = nets[index]['mac'].replace(':', '')
            if network in internal_networks:
                nic.attachment_type = library.NetworkAttachmentType.internal
                nic.internal_network = network
            elif network in nat_networks:
                nic.attachment_type = library.NetworkAttachmentType.nat_network
                nic.nat_network = network
                if ip is not None and index == 0:
                    natengine = nic.nat_engine
                    nat_network = [n for n in conn.nat_networks if n.network_name == network][0]
                    nat_network.add_port_forward_rule(False, 'ssh_%s' % name, library.NATProtocol.tcp, '', common.get_free_port(), ip, 22)
            else:
                nic.attachment_type = library.NetworkAttachmentType.nat
                if index == 0:
                    natengine = nic.nat_engine
                    natengine.add_redirect('ssh_%s' % name, library.NATProtocol.tcp, '', common.get_free_port(), '', 22)
        vm.save_settings()
        conn.register_machine(vm)
        session = Session()
        vm.lock_machine(session, library.LockType.write)
        machine = session.machine
{'fraction_non_alphanumeric': 0.10411598715578477, 'fraction_numerical': 0.02062858810937044, 'mean_word_length': 2.295286950945816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6474167', 'n_tokens_mistral': 3174, 'n_tokens_neox': 2930, 'n_words': 729}