~bjornt/charms/trusty/quantum-gateway-hardcode-eth1/trunk

« back to all changes in this revision

Viewing changes to hooks/quantum_contexts.py

[gnuoy,r=james-page,t=james-page] Add support for specifying external port by mac address

Also resync charm-helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
import os
3
3
import uuid
4
4
import socket
 
5
from charmhelpers.core.host import (
 
6
    list_nics,
 
7
    get_nic_hwaddr
 
8
)
5
9
from charmhelpers.core.hookenv import (
6
10
    config,
7
11
    relation_ids,
23
27
from charmhelpers.contrib.hahelpers.cluster import(
24
28
    eligible_leader
25
29
)
 
30
import re
26
31
 
27
32
DB_USER = "quantum"
28
33
QUANTUM_DB = "quantum"
121
126
 
122
127
class ExternalPortContext(OSContextGenerator):
123
128
    def __call__(self):
124
 
        if config('ext-port'):
125
 
            return {"ext_port": config('ext-port')}
126
 
        else:
 
129
        if not config('ext-port'):
127
130
            return None
 
131
        hwaddrs = {}
 
132
        for nic in list_nics(['eth', 'bond']):
 
133
            hwaddrs[get_nic_hwaddr(nic)] = nic
 
134
        mac_regex = re.compile(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', re.I)
 
135
        for entry in config('ext-port').split():
 
136
            entry = entry.strip()
 
137
            if re.match(mac_regex, entry):
 
138
                if entry in hwaddrs:
 
139
                    return {"ext_port": hwaddrs[entry]}
 
140
            else:
 
141
                return {"ext_port": entry}
 
142
        return None
128
143
 
129
144
 
130
145
class QuantumGatewayContext(OSContextGenerator):