[gnuoy,trivial] Charmhelper sync (+1'd by mojo)
This commit is contained in:
parent
7c5831be1c
commit
0b0d832c33
@ -50,6 +50,8 @@ from charmhelpers.core.sysctl import create as sysctl_create
|
|||||||
from charmhelpers.core.strutils import bool_from_string
|
from charmhelpers.core.strutils import bool_from_string
|
||||||
|
|
||||||
from charmhelpers.core.host import (
|
from charmhelpers.core.host import (
|
||||||
|
get_bond_master,
|
||||||
|
is_phy_iface,
|
||||||
list_nics,
|
list_nics,
|
||||||
get_nic_hwaddr,
|
get_nic_hwaddr,
|
||||||
mkdir,
|
mkdir,
|
||||||
@ -923,7 +925,6 @@ class NeutronContext(OSContextGenerator):
|
|||||||
|
|
||||||
|
|
||||||
class NeutronPortContext(OSContextGenerator):
|
class NeutronPortContext(OSContextGenerator):
|
||||||
NIC_PREFIXES = ['eth', 'bond']
|
|
||||||
|
|
||||||
def resolve_ports(self, ports):
|
def resolve_ports(self, ports):
|
||||||
"""Resolve NICs not yet bound to bridge(s)
|
"""Resolve NICs not yet bound to bridge(s)
|
||||||
@ -935,7 +936,18 @@ class NeutronPortContext(OSContextGenerator):
|
|||||||
|
|
||||||
hwaddr_to_nic = {}
|
hwaddr_to_nic = {}
|
||||||
hwaddr_to_ip = {}
|
hwaddr_to_ip = {}
|
||||||
for nic in list_nics(self.NIC_PREFIXES):
|
for nic in list_nics():
|
||||||
|
# Ignore virtual interfaces (bond masters will be identified from
|
||||||
|
# their slaves)
|
||||||
|
if not is_phy_iface(nic):
|
||||||
|
continue
|
||||||
|
|
||||||
|
_nic = get_bond_master(nic)
|
||||||
|
if _nic:
|
||||||
|
log("Replacing iface '%s' with bond master '%s'" % (nic, _nic),
|
||||||
|
level=DEBUG)
|
||||||
|
nic = _nic
|
||||||
|
|
||||||
hwaddr = get_nic_hwaddr(nic)
|
hwaddr = get_nic_hwaddr(nic)
|
||||||
hwaddr_to_nic[hwaddr] = nic
|
hwaddr_to_nic[hwaddr] = nic
|
||||||
addresses = get_ipv4_addr(nic, fatal=False)
|
addresses = get_ipv4_addr(nic, fatal=False)
|
||||||
@ -961,7 +973,8 @@ class NeutronPortContext(OSContextGenerator):
|
|||||||
# trust it to be the real external network).
|
# trust it to be the real external network).
|
||||||
resolved.append(entry)
|
resolved.append(entry)
|
||||||
|
|
||||||
return resolved
|
# Ensure no duplicates
|
||||||
|
return list(set(resolved))
|
||||||
|
|
||||||
|
|
||||||
class OSConfigFlagContext(OSContextGenerator):
|
class OSConfigFlagContext(OSContextGenerator):
|
||||||
@ -1280,15 +1293,19 @@ class DataPortContext(NeutronPortContext):
|
|||||||
def __call__(self):
|
def __call__(self):
|
||||||
ports = config('data-port')
|
ports = config('data-port')
|
||||||
if ports:
|
if ports:
|
||||||
|
# Map of {port/mac:bridge}
|
||||||
portmap = parse_data_port_mappings(ports)
|
portmap = parse_data_port_mappings(ports)
|
||||||
ports = portmap.values()
|
ports = portmap.keys()
|
||||||
|
# Resolve provided ports or mac addresses and filter out those
|
||||||
|
# already attached to a bridge.
|
||||||
resolved = self.resolve_ports(ports)
|
resolved = self.resolve_ports(ports)
|
||||||
|
# FIXME: is this necessary?
|
||||||
normalized = {get_nic_hwaddr(port): port for port in resolved
|
normalized = {get_nic_hwaddr(port): port for port in resolved
|
||||||
if port not in ports}
|
if port not in ports}
|
||||||
normalized.update({port: port for port in resolved
|
normalized.update({port: port for port in resolved
|
||||||
if port in ports})
|
if port in ports})
|
||||||
if resolved:
|
if resolved:
|
||||||
return {bridge: normalized[port] for bridge, port in
|
return {bridge: normalized[port] for port, bridge in
|
||||||
six.iteritems(portmap) if port in normalized.keys()}
|
six.iteritems(portmap) if port in normalized.keys()}
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
@ -255,17 +255,30 @@ def network_manager():
|
|||||||
return 'neutron'
|
return 'neutron'
|
||||||
|
|
||||||
|
|
||||||
def parse_mappings(mappings):
|
def parse_mappings(mappings, key_rvalue=False):
|
||||||
|
"""By default mappings are lvalue keyed.
|
||||||
|
|
||||||
|
If key_rvalue is True, the mapping will be reversed to allow multiple
|
||||||
|
configs for the same lvalue.
|
||||||
|
"""
|
||||||
parsed = {}
|
parsed = {}
|
||||||
if mappings:
|
if mappings:
|
||||||
mappings = mappings.split()
|
mappings = mappings.split()
|
||||||
for m in mappings:
|
for m in mappings:
|
||||||
p = m.partition(':')
|
p = m.partition(':')
|
||||||
key = p[0].strip()
|
|
||||||
if p[1]:
|
if key_rvalue:
|
||||||
parsed[key] = p[2].strip()
|
key_index = 2
|
||||||
|
val_index = 0
|
||||||
|
# if there is no rvalue skip to next
|
||||||
|
if not p[1]:
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
parsed[key] = ''
|
key_index = 0
|
||||||
|
val_index = 2
|
||||||
|
|
||||||
|
key = p[key_index].strip()
|
||||||
|
parsed[key] = p[val_index].strip()
|
||||||
|
|
||||||
return parsed
|
return parsed
|
||||||
|
|
||||||
@ -283,25 +296,25 @@ def parse_bridge_mappings(mappings):
|
|||||||
def parse_data_port_mappings(mappings, default_bridge='br-data'):
|
def parse_data_port_mappings(mappings, default_bridge='br-data'):
|
||||||
"""Parse data port mappings.
|
"""Parse data port mappings.
|
||||||
|
|
||||||
Mappings must be a space-delimited list of bridge:port mappings.
|
Mappings must be a space-delimited list of port:bridge mappings.
|
||||||
|
|
||||||
Returns dict of the form {bridge:port}.
|
Returns dict of the form {port:bridge} where port may be an mac address or
|
||||||
|
interface name.
|
||||||
"""
|
"""
|
||||||
_mappings = parse_mappings(mappings)
|
|
||||||
|
# NOTE(dosaboy): we use rvalue for key to allow multiple values to be
|
||||||
|
# proposed for <port> since it may be a mac address which will differ
|
||||||
|
# across units this allowing first-known-good to be chosen.
|
||||||
|
_mappings = parse_mappings(mappings, key_rvalue=True)
|
||||||
if not _mappings or list(_mappings.values()) == ['']:
|
if not _mappings or list(_mappings.values()) == ['']:
|
||||||
if not mappings:
|
if not mappings:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
# For backwards-compatibility we need to support port-only provided in
|
# For backwards-compatibility we need to support port-only provided in
|
||||||
# config.
|
# config.
|
||||||
_mappings = {default_bridge: mappings.split()[0]}
|
_mappings = {mappings.split()[0]: default_bridge}
|
||||||
|
|
||||||
bridges = _mappings.keys()
|
|
||||||
ports = _mappings.values()
|
|
||||||
if len(set(bridges)) != len(bridges):
|
|
||||||
raise Exception("It is not allowed to have more than one port "
|
|
||||||
"configured on the same bridge")
|
|
||||||
|
|
||||||
|
ports = _mappings.keys()
|
||||||
if len(set(ports)) != len(ports):
|
if len(set(ports)) != len(ports):
|
||||||
raise Exception("It is not allowed to have the same port configured "
|
raise Exception("It is not allowed to have the same port configured "
|
||||||
"on more than one bridge")
|
"on more than one bridge")
|
||||||
|
@ -417,25 +417,80 @@ def pwgen(length=None):
|
|||||||
return(''.join(random_chars))
|
return(''.join(random_chars))
|
||||||
|
|
||||||
|
|
||||||
def list_nics(nic_type):
|
def is_phy_iface(interface):
|
||||||
|
"""Returns True if interface is not virtual, otherwise False."""
|
||||||
|
if interface:
|
||||||
|
sys_net = '/sys/class/net'
|
||||||
|
if os.path.isdir(sys_net):
|
||||||
|
for iface in glob.glob(os.path.join(sys_net, '*')):
|
||||||
|
if '/virtual/' in os.path.realpath(iface):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if interface == os.path.basename(iface):
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def get_bond_master(interface):
|
||||||
|
"""Returns bond master if interface is bond slave otherwise None.
|
||||||
|
|
||||||
|
NOTE: the provided interface is expected to be physical
|
||||||
|
"""
|
||||||
|
if interface:
|
||||||
|
iface_path = '/sys/class/net/%s' % (interface)
|
||||||
|
if os.path.exists(iface_path):
|
||||||
|
if '/virtual/' in os.path.realpath(iface_path):
|
||||||
|
return None
|
||||||
|
|
||||||
|
master = os.path.join(iface_path, 'master')
|
||||||
|
if os.path.exists(master):
|
||||||
|
master = os.path.realpath(master)
|
||||||
|
# make sure it is a bond master
|
||||||
|
if os.path.exists(os.path.join(master, 'bonding')):
|
||||||
|
return os.path.basename(master)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def list_nics(nic_type=None):
|
||||||
'''Return a list of nics of given type(s)'''
|
'''Return a list of nics of given type(s)'''
|
||||||
if isinstance(nic_type, six.string_types):
|
if isinstance(nic_type, six.string_types):
|
||||||
int_types = [nic_type]
|
int_types = [nic_type]
|
||||||
else:
|
else:
|
||||||
int_types = nic_type
|
int_types = nic_type
|
||||||
|
|
||||||
interfaces = []
|
interfaces = []
|
||||||
for int_type in int_types:
|
if nic_type:
|
||||||
cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
|
for int_type in int_types:
|
||||||
|
cmd = ['ip', 'addr', 'show', 'label', int_type + '*']
|
||||||
|
ip_output = subprocess.check_output(cmd).decode('UTF-8')
|
||||||
|
ip_output = ip_output.split('\n')
|
||||||
|
ip_output = (line for line in ip_output if line)
|
||||||
|
for line in ip_output:
|
||||||
|
if line.split()[1].startswith(int_type):
|
||||||
|
matched = re.search('.*: (' + int_type +
|
||||||
|
r'[0-9]+\.[0-9]+)@.*', line)
|
||||||
|
if matched:
|
||||||
|
iface = matched.groups()[0]
|
||||||
|
else:
|
||||||
|
iface = line.split()[1].replace(":", "")
|
||||||
|
|
||||||
|
if iface not in interfaces:
|
||||||
|
interfaces.append(iface)
|
||||||
|
else:
|
||||||
|
cmd = ['ip', 'a']
|
||||||
ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
|
ip_output = subprocess.check_output(cmd).decode('UTF-8').split('\n')
|
||||||
ip_output = (line for line in ip_output if line)
|
ip_output = (line.strip() for line in ip_output if line)
|
||||||
|
|
||||||
|
key = re.compile('^[0-9]+:\s+(.+):')
|
||||||
for line in ip_output:
|
for line in ip_output:
|
||||||
if line.split()[1].startswith(int_type):
|
matched = re.search(key, line)
|
||||||
matched = re.search('.*: (' + int_type + r'[0-9]+\.[0-9]+)@.*', line)
|
if matched:
|
||||||
if matched:
|
iface = matched.group(1)
|
||||||
interface = matched.groups()[0]
|
iface = iface.partition("@")[0]
|
||||||
else:
|
if iface not in interfaces:
|
||||||
interface = line.split()[1].replace(":", "")
|
interfaces.append(iface)
|
||||||
interfaces.append(interface)
|
|
||||||
|
|
||||||
return interfaces
|
return interfaces
|
||||||
|
|
||||||
|
62
hooks/charmhelpers/core/hugepage.py
Normal file
62
hooks/charmhelpers/core/hugepage.py
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Copyright 2014-2015 Canonical Limited.
|
||||||
|
#
|
||||||
|
# This file is part of charm-helpers.
|
||||||
|
#
|
||||||
|
# charm-helpers is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Lesser General Public License version 3 as
|
||||||
|
# published by the Free Software Foundation.
|
||||||
|
#
|
||||||
|
# charm-helpers is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Lesser General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Lesser General Public License
|
||||||
|
# along with charm-helpers. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
from charmhelpers.core import fstab
|
||||||
|
from charmhelpers.core import sysctl
|
||||||
|
from charmhelpers.core.host import (
|
||||||
|
add_group,
|
||||||
|
add_user_to_group,
|
||||||
|
fstab_mount,
|
||||||
|
mkdir,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def hugepage_support(user, group='hugetlb', nr_hugepages=256,
|
||||||
|
max_map_count=65536, mnt_point='/run/hugepages/kvm',
|
||||||
|
pagesize='2MB', mount=True):
|
||||||
|
"""Enable hugepages on system.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
user (str) -- Username to allow access to hugepages to
|
||||||
|
group (str) -- Group name to own hugepages
|
||||||
|
nr_hugepages (int) -- Number of pages to reserve
|
||||||
|
max_map_count (int) -- Number of Virtual Memory Areas a process can own
|
||||||
|
mnt_point (str) -- Directory to mount hugepages on
|
||||||
|
pagesize (str) -- Size of hugepages
|
||||||
|
mount (bool) -- Whether to Mount hugepages
|
||||||
|
"""
|
||||||
|
group_info = add_group(group)
|
||||||
|
gid = group_info.gr_gid
|
||||||
|
add_user_to_group(user, group)
|
||||||
|
sysctl_settings = {
|
||||||
|
'vm.nr_hugepages': nr_hugepages,
|
||||||
|
'vm.max_map_count': max_map_count,
|
||||||
|
'vm.hugetlb_shm_group': gid,
|
||||||
|
}
|
||||||
|
sysctl.create(yaml.dump(sysctl_settings), '/etc/sysctl.d/10-hugepage.conf')
|
||||||
|
mkdir(mnt_point, owner='root', group='root', perms=0o755, force=False)
|
||||||
|
lfstab = fstab.Fstab()
|
||||||
|
fstab_entry = lfstab.get_entry_by_attr('mountpoint', mnt_point)
|
||||||
|
if fstab_entry:
|
||||||
|
lfstab.remove_entry(fstab_entry)
|
||||||
|
entry = lfstab.Entry('nodev', mnt_point, 'hugetlbfs',
|
||||||
|
'mode=1770,gid={},pagesize={}'.format(gid, pagesize), 0, 0)
|
||||||
|
lfstab.add_entry(entry)
|
||||||
|
if mount:
|
||||||
|
fstab_mount(mnt_point)
|
Loading…
Reference in New Issue
Block a user