~openstack-charmers-archive/charms/trusty/cinder/next

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/openstack/context.py

  • Committer: Edward Hope-Morley
  • Date: 2015-03-19 09:56:14 UTC
  • mfrom: (78.1.3 cinder.fix-ssl-inject)
  • Revision ID: edward.hope-morley@canonical.com-20150319095614-0uwvyycp2pmsooba
[hopem,r=wolsen]

Fixes SSL cert/key inject from config.

Closes-Bug: 1351401

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
import json
18
18
import os
 
19
import re
19
20
import time
20
21
from base64 import b64decode
21
22
from subprocess import check_call
48
49
from charmhelpers.core.sysctl import create as sysctl_create
49
50
 
50
51
from charmhelpers.core.host import (
 
52
    list_nics,
 
53
    get_nic_hwaddr,
51
54
    mkdir,
52
55
    write_file,
53
56
)
65
68
from charmhelpers.contrib.openstack.neutron import (
66
69
    neutron_plugin_attribute,
67
70
)
 
71
from charmhelpers.contrib.openstack.ip import (
 
72
    resolve_address,
 
73
    INTERNAL,
 
74
)
68
75
from charmhelpers.contrib.network.ip import (
69
76
    get_address_in_network,
 
77
    get_ipv4_addr,
70
78
    get_ipv6_addr,
71
79
    get_netmask_for_address,
72
80
    format_ipv6_addr,
73
81
    is_address_in_network,
 
82
    is_bridge_member,
74
83
)
75
84
from charmhelpers.contrib.openstack.utils import get_host_ip
76
85
 
727
736
                'endpoints': [],
728
737
                'ext_ports': []}
729
738
 
730
 
        for cn in self.canonical_names():
 
739
        cns = self.canonical_names()
 
740
        if cns:
 
741
            for cn in cns:
 
742
                self.configure_cert(cn)
 
743
        else:
 
744
            # Expect cert/key provided in config (currently assumed that ca
 
745
            # uses ip for cn)
 
746
            cn = resolve_address(endpoint_type=INTERNAL)
731
747
            self.configure_cert(cn)
732
748
 
733
749
        addresses = self.get_network_addresses()
883
899
        return ctxt
884
900
 
885
901
 
 
902
class NeutronPortContext(OSContextGenerator):
 
903
    NIC_PREFIXES = ['eth', 'bond']
 
904
 
 
905
    def resolve_ports(self, ports):
 
906
        """Resolve NICs not yet bound to bridge(s)
 
907
 
 
908
        If hwaddress provided then returns resolved hwaddress otherwise NIC.
 
909
        """
 
910
        if not ports:
 
911
            return None
 
912
 
 
913
        hwaddr_to_nic = {}
 
914
        hwaddr_to_ip = {}
 
915
        for nic in list_nics(self.NIC_PREFIXES):
 
916
            hwaddr = get_nic_hwaddr(nic)
 
917
            hwaddr_to_nic[hwaddr] = nic
 
918
            addresses = get_ipv4_addr(nic, fatal=False)
 
919
            addresses += get_ipv6_addr(iface=nic, fatal=False)
 
920
            hwaddr_to_ip[hwaddr] = addresses
 
921
 
 
922
        resolved = []
 
923
        mac_regex = re.compile(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', re.I)
 
924
        for entry in ports:
 
925
            if re.match(mac_regex, entry):
 
926
                # NIC is in known NICs and does NOT hace an IP address
 
927
                if entry in hwaddr_to_nic and not hwaddr_to_ip[entry]:
 
928
                    # If the nic is part of a bridge then don't use it
 
929
                    if is_bridge_member(hwaddr_to_nic[entry]):
 
930
                        continue
 
931
 
 
932
                    # Entry is a MAC address for a valid interface that doesn't
 
933
                    # have an IP address assigned yet.
 
934
                    resolved.append(hwaddr_to_nic[entry])
 
935
            else:
 
936
                # If the passed entry is not a MAC address, assume it's a valid
 
937
                # interface, and that the user put it there on purpose (we can
 
938
                # trust it to be the real external network).
 
939
                resolved.append(entry)
 
940
 
 
941
        return resolved
 
942
 
 
943
 
886
944
class OSConfigFlagContext(OSContextGenerator):
887
945
    """Provides support for user-defined config flags.
888
946