<commit_before>#!/usr/bin/python
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

import os
import sys
import socket
import netaddr
import argparse
import netifaces
import subprocess
import ConfigParser

from fabric.api import local, run
from fabric.state import env
from fabric.context_managers import settings, lcd

from contrail_provisioning.common.base import ContrailSetup
from contrail_provisioning.compute.network import ComputeNetworkSetup
from contrail_provisioning.compute.templates import vrouter_nodemgr_param
from contrail_provisioning.compute.templates import contrail_vrouter_agent_conf
from contrail_provisioning.compute.templates import contrail_vrouter_nodemgr_template


class ExtList (list):
    def findex (self, fun):
        for i, x in enumerate (self):
            if fun (x):
                return i
        raise LookupError, 'No matching element in list'


class ComputeBaseSetup(ContrailSetup, ComputeNetworkSetup):
    def __init__(self, compute_args, args_str=None):
        super(ComputeBaseSetup, self).__init__()
        self._args = compute_args

    def enable_kernel_core(self): 
        self.enable_kernel_core()
        local ('for s in abrt-vmcore abrtd kdump; do chkconfig ${s} on; done')

    def fixup_config_files(self):
        self.add_dev_tun_in_cgroup_device_acl()
        self.fixup_vrouter_nodemgr_param()
        self.fixup_contrail_vrouter_agent()
        self.fixup_contrail_vrouter_nodemgr()

    def setup_lbaas_prereq(self):
        if self.pdist in ['centos']:
           local('groupadd -f nogroup')
           local("sed -i s/'Defaults    requiretty'/'#Defaults    requiretty'/g /etc/sudoers")

    def add_dev_tun_in_cgroup_device_acl(self):
        # add /dev/net/tun in cgroup_device_acl needed for type=ethernet interfaces
        with settings(warn_only = True):
            ret = local("sudo grep -q '^cgroup_device_acl' /etc/libvirt/qemu.conf")
            if ret.return_code == 1:
                if  self.pdist in ['centos', 'redhat']:
                    local('sudo echo "clear_emulator_capabilities = 1" >> /etc/libvirt/qemu.conf')
                    local('sudo echo \'user = "root"\' >> /etc/libvirt/qemu.conf')
                    local('sudo echo \'group = "root"\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \'cgroup_device_acl = [\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \'    "/dev/null", "/dev/full", "/dev/zero",\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \'    "/dev/random", "/dev/urandom",\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \'    "/dev/ptmx", "/dev/kvm", "/dev/kqemu",\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \'    "/dev/rtc", "/dev/hpet","/dev/net/tun",\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \']\' >> /etc/libvirt/qemu.conf')
                self._fixed_qemu_conf = True
            # add "alias bridge off" in /etc/modprobe.conf for Centos
            if  self.pdist in ['centos', 'redhat']:
                local('sudo echo "alias bridge off" > /etc/modprobe.conf')

    def fixup_vrouter_nodemgr_param(self):
        template_vals = {'__contrail_discovery_ip__': self._args.contrail_internal_vip or self._args.cfgm_ip
                        }
        self._template_substitute_write(vrouter_nodemgr_param.template,
                                        template_vals, self._temp_dir_name + '/vrouter_nodemgr_param')
        local("sudo mv %s/vrouter_nodemgr_param /etc/contrail/vrouter_nodemgr_param" %(self._temp_dir_name))

    def fixup_contrail_vrouter_nodemgr(self):
        template_vals = {'__contrail_discovery_ip__' : self._args.cfgm_ip,
                         '__contrail_discovery_port__': '5998'
                       }
        self._template_substitute_write(contrail_vrouter_nodemgr_template.template,
                                        template_vals, self._temp_dir_name + '/contrail-vrouter-nodemgr.conf')
        local("sudo mv %s/contrail-vrouter-nodemgr.conf /etc/contrail/contrail-vrouter-nodemgr.conf" %(self._temp_dir_name))

    def fixup_contrail_vrouter_agent(self):
        keystone_ip = self._args.keystone_ip
        compute_ip = self._args.self_ip
        discovery_ip = self._args.contrail_internal_vip or self._args.cfgm_ip
        ncontrols = self._args.ncontrols
        physical_interface = self._args.physical_interface
        non_mgmt_ip = self._args.non_mgmt_ip
        non_mgmt_gw = self._args.non_mgmt_gw
        self.vhost_ip = compute_ip
        vgw_public_subnet = self._args.vgw_public_subnet
        vgw_public_vn_name = self._args.vgw_public_vn_name
        vgw_intf_list = self._args.vgw_intf_list
        vgw_gateway_routes = self._args.vgw_gateway_routes
        self.multi_net= False
        if non_mgmt_ip :
            self.multi_net= True
            self.vhost_ip= non_mgmt_ip

        self.dev = None
        compute_dev = None
        if physical_interface:
            if physical_interface in netifaces.interfaces ():
                self.dev = physical_interface
            else:
                 raise KeyError, 'Interface %s in present' % (
                         physical_interface)
        else:
            # deduce the phy interface from ip, if configured
            self.dev = self.get_device_by_ip (self.vhost_ip)
            if self.multi_net:
                compute_dev = self.get_device_by_ip (compute_ip)

        self.mac = None
        if self.dev and self.dev != 'vhost0' :
            self.mac = netifaces.ifaddresses (self.dev)[netifaces.AF_LINK][0][
                        'addr']
            if self.mac:
                with open ('%s/default_pmac' % self._temp_dir_name, 'w') as f:
                    f.write (self.mac)
                with settings(warn_only = True):
                    local("sudo mv %s/default_pmac /etc/contrail/default_pmac" % (self._temp_dir_name))
            else:
                raise KeyError, 'Interface %s Mac %s' % (str (self.dev), str (self.mac))
            self.netmask = netifaces.ifaddresses(self.dev)[netifaces.AF_INET][0][
                            'netmask']
            if self.multi_net:
                self.gateway= non_mgmt_gw
            else:
                self.gateway = self.find_gateway(self.dev)
            cidr = str (netaddr.IPNetwork('%s/%s' % (self.vhost_ip, self.netmask)))

            if vgw_public_subnet:
                with lcd(self._temp_dir_name):
                    # Manipulating the string to use in agent_param
                    vgw_public_subnet_str=[]
                    for i in vgw_public_subnet[1:-1].split(";"):
                        j=i[1:-1].split(",")
                        j=";".join(j)
                        vgw_public_subnet_str.append(j)
                    vgw_public_subnet_str=str(tuple(vgw_public_subnet_str)).replace("'","")
                    vgw_public_subnet_str=vgw_public_subnet_str.replace(" ","")
                    vgw_intf_list_str=str(tuple(vgw_intf_list[1:-1].split(";"))).replace(" ","")

                    local("sudo sed 's@dev=.*@dev=%s@g;s@vgw_subnet_ip=.*@vgw_subnet_ip=%s@g;s@vgw_intf=.*@vgw_intf=%s@g' /etc/contrail/agent_param.tmpl > agent_param.new" % 
                          (self.dev,vgw_public_subnet_str,vgw_intf_list_str))
                    local("sudo mv agent_param.new /etc/contrail/agent_param")
                    local("openstack-config --set /etc/nova/nova.conf DEFAULT firewall_driver nova.virt.firewall.NoopFirewallDriver")
            else:
                with lcd(self._temp_dir_name):<commit_msg>Partial-Bug: #1444660 Remove unused /etc/contrail/default_pmac MAC Address is anyway directly read from $(cat /sys/class/net/$dev/address)"<commit_after>#!/usr/bin/python
#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

import os
import sys
import socket
import netaddr
import argparse
import netifaces
import subprocess
import ConfigParser

from fabric.api import local, run
from fabric.state import env
from fabric.context_managers import settings, lcd

from contrail_provisioning.common.base import ContrailSetup
from contrail_provisioning.compute.network import ComputeNetworkSetup
from contrail_provisioning.compute.templates import vrouter_nodemgr_param
from contrail_provisioning.compute.templates import contrail_vrouter_agent_conf
from contrail_provisioning.compute.templates import contrail_vrouter_nodemgr_template


class ExtList (list):
    def findex (self, fun):
        for i, x in enumerate (self):
            if fun (x):
                return i
        raise LookupError, 'No matching element in list'


class ComputeBaseSetup(ContrailSetup, ComputeNetworkSetup):
    def __init__(self, compute_args, args_str=None):
        super(ComputeBaseSetup, self).__init__()
        self._args = compute_args

    def enable_kernel_core(self): 
        self.enable_kernel_core()
        local ('for s in abrt-vmcore abrtd kdump; do chkconfig ${s} on; done')

    def fixup_config_files(self):
        self.add_dev_tun_in_cgroup_device_acl()
        self.fixup_vrouter_nodemgr_param()
        self.fixup_contrail_vrouter_agent()
        self.fixup_contrail_vrouter_nodemgr()

    def setup_lbaas_prereq(self):
        if self.pdist in ['centos']:
           local('groupadd -f nogroup')
           local("sed -i s/'Defaults    requiretty'/'#Defaults    requiretty'/g /etc/sudoers")

    def add_dev_tun_in_cgroup_device_acl(self):
        # add /dev/net/tun in cgroup_device_acl needed for type=ethernet interfaces
        with settings(warn_only = True):
            ret = local("sudo grep -q '^cgroup_device_acl' /etc/libvirt/qemu.conf")
            if ret.return_code == 1:
                if  self.pdist in ['centos', 'redhat']:
                    local('sudo echo "clear_emulator_capabilities = 1" >> /etc/libvirt/qemu.conf')
                    local('sudo echo \'user = "root"\' >> /etc/libvirt/qemu.conf')
                    local('sudo echo \'group = "root"\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \'cgroup_device_acl = [\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \'    "/dev/null", "/dev/full", "/dev/zero",\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \'    "/dev/random", "/dev/urandom",\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \'    "/dev/ptmx", "/dev/kvm", "/dev/kqemu",\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \'    "/dev/rtc", "/dev/hpet","/dev/net/tun",\' >> /etc/libvirt/qemu.conf')
                local('sudo echo \']\' >> /etc/libvirt/qemu.conf')
                self._fixed_qemu_conf = True
            # add "alias bridge off" in /etc/modprobe.conf for Centos
            if  self.pdist in ['centos', 'redhat']:
                local('sudo echo "alias bridge off" > /etc/modprobe.conf')

    def fixup_vrouter_nodemgr_param(self):
        template_vals = {'__contrail_discovery_ip__': self._args.contrail_internal_vip or self._args.cfgm_ip
                        }
        self._template_substitute_write(vrouter_nodemgr_param.template,
                                        template_vals, self._temp_dir_name + '/vrouter_nodemgr_param')
        local("sudo mv %s/vrouter_nodemgr_param /etc/contrail/vrouter_nodemgr_param" %(self._temp_dir_name))

    def fixup_contrail_vrouter_nodemgr(self):
        template_vals = {'__contrail_discovery_ip__' : self._args.cfgm_ip,
                         '__contrail_discovery_port__': '5998'
                       }
        self._template_substitute_write(contrail_vrouter_nodemgr_template.template,
                                        template_vals, self._temp_dir_name + '/contrail-vrouter-nodemgr.conf')
        local("sudo mv %s/contrail-vrouter-nodemgr.conf /etc/contrail/contrail-vrouter-nodemgr.conf" %(self._temp_dir_name))

    def fixup_contrail_vrouter_agent(self):
        keystone_ip = self._args.keystone_ip
        compute_ip = self._args.self_ip
        discovery_ip = self._args.contrail_internal_vip or self._args.cfgm_ip
        ncontrols = self._args.ncontrols
        physical_interface = self._args.physical_interface
        non_mgmt_ip = self._args.non_mgmt_ip
        non_mgmt_gw = self._args.non_mgmt_gw
        self.vhost_ip = compute_ip
        vgw_public_subnet = self._args.vgw_public_subnet
        vgw_public_vn_name = self._args.vgw_public_vn_name
        vgw_intf_list = self._args.vgw_intf_list
        vgw_gateway_routes = self._args.vgw_gateway_routes
        self.multi_net= False
        if non_mgmt_ip :
            self.multi_net= True
            self.vhost_ip= non_mgmt_ip

        self.dev = None
        compute_dev = None
        if physical_interface:
            if physical_interface in netifaces.interfaces ():
                self.dev = physical_interface
            else:
                 raise KeyError, 'Interface %s in present' % (
                         physical_interface)
        else:
            # deduce the phy interface from ip, if configured
            self.dev = self.get_device_by_ip (self.vhost_ip)
            if self.multi_net:
                compute_dev = self.get_device_by_ip (compute_ip)

        self.mac = None
        if self.dev and self.dev != 'vhost0' :
            self.mac = netifaces.ifaddresses (self.dev)[netifaces.AF_LINK][0][
                        'addr']
            if not self.mac:
                raise KeyError, 'Interface %s Mac %s' % (str (self.dev), str (self.mac))
            self.netmask = netifaces.ifaddresses(self.dev)[netifaces.AF_INET][0][
                            'netmask']
            if self.multi_net:
                self.gateway= non_mgmt_gw
            else:
                self.gateway = self.find_gateway(self.dev)
            cidr = str (netaddr.IPNetwork('%s/%s' % (self.vhost_ip, self.netmask)))

            if vgw_public_subnet:
                with lcd(self._temp_dir_name):
                    # Manipulating the string to use in agent_param
                    vgw_public_subnet_str=[]
                    for i in vgw_public_subnet[1:-1].split(";"):
                        j=i[1:-1].split(",")
                        j=";".join(j)
                        vgw_public_subnet_str.append(j)
                    vgw_public_subnet_str=str(tuple(vgw_public_subnet_str)).replace("'","")
                    vgw_public_subnet_str=vgw_public_subnet_str.replace(" ","")
                    vgw_intf_list_str=str(tuple(vgw_intf_list[1:-1].split(";"))).replace(" ","")

                    local("sudo sed 's@dev=.*@dev=%s@g;s@vgw_subnet_ip=.*@vgw_subnet_ip=%s@g;s@vgw_intf=.*@vgw_intf=%s@g' /etc/contrail/agent_param.tmpl > agent_param.new" % 
                          (self.dev,vgw_public_subnet_str,vgw_intf_list_str))
                    local("sudo mv agent_param.new /etc/contrail/agent_param")
                    local("openstack-config --set /etc/nova/nova.conf DEFAULT firewall_driver nova.virt.firewall.NoopFirewallDriver")
            else:
                with lcd(self._temp_dir_name):
{'fraction_non_alphanumeric': 0.10848585690515807, 'fraction_numerical': 0.002995008319467554, 'mean_word_length': 2.2523809523809524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1545455', 'n_tokens_mistral': 4981, 'n_tokens_neox': 4700, 'n_words': 847}