~adam-collard/charms/trusty/swift-proxy/lib-in-python-package

« back to all changes in this revision

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

  • Committer: Liam Young
  • Date: 2015-04-16 10:26:51 UTC
  • Revision ID: liam.young@canonical.com-20150416102651-0krkkgyz310fa1uv
[gnuoy,trivial] Pre-release charmhelper sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
47
47
)
48
48
 
49
49
from charmhelpers.core.sysctl import create as sysctl_create
 
50
from charmhelpers.core.strutils import bool_from_string
50
51
 
51
52
from charmhelpers.core.host import (
52
53
    list_nics,
67
68
)
68
69
from charmhelpers.contrib.openstack.neutron import (
69
70
    neutron_plugin_attribute,
 
71
    parse_data_port_mappings,
70
72
)
71
73
from charmhelpers.contrib.openstack.ip import (
72
74
    resolve_address,
82
84
    is_bridge_member,
83
85
)
84
86
from charmhelpers.contrib.openstack.utils import get_host_ip
85
 
 
86
87
CA_CERT_PATH = '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt'
87
88
ADDRESS_TYPES = ['admin', 'internal', 'public']
88
89
 
319
320
 
320
321
 
321
322
class IdentityServiceContext(OSContextGenerator):
322
 
    interfaces = ['identity-service']
323
323
 
324
 
    def __init__(self, service=None, service_user=None):
 
324
    def __init__(self, service=None, service_user=None, rel_name='identity-service'):
325
325
        self.service = service
326
326
        self.service_user = service_user
 
327
        self.rel_name = rel_name
 
328
        self.interfaces = [self.rel_name]
327
329
 
328
330
    def __call__(self):
329
 
        log('Generating template context for identity-service', level=DEBUG)
 
331
        log('Generating template context for ' + self.rel_name, level=DEBUG)
330
332
        ctxt = {}
331
333
 
332
334
        if self.service and self.service_user:
340
342
 
341
343
            ctxt['signing_dir'] = cachedir
342
344
 
343
 
        for rid in relation_ids('identity-service'):
 
345
        for rid in relation_ids(self.rel_name):
344
346
            for unit in related_units(rid):
345
347
                rdata = relation_get(rid=rid, unit=unit)
346
348
                serv_host = rdata.get('service_host')
806
808
 
807
809
        return ovs_ctxt
808
810
 
 
811
    def nuage_ctxt(self):
 
812
        driver = neutron_plugin_attribute(self.plugin, 'driver',
 
813
                                          self.network_manager)
 
814
        config = neutron_plugin_attribute(self.plugin, 'config',
 
815
                                          self.network_manager)
 
816
        nuage_ctxt = {'core_plugin': driver,
 
817
                      'neutron_plugin': 'vsp',
 
818
                      'neutron_security_groups': self.neutron_security_groups,
 
819
                      'local_ip': unit_private_ip(),
 
820
                      'config': config}
 
821
 
 
822
        return nuage_ctxt
 
823
 
809
824
    def nvp_ctxt(self):
810
825
        driver = neutron_plugin_attribute(self.plugin, 'driver',
811
826
                                          self.network_manager)
889
904
            ctxt.update(self.n1kv_ctxt())
890
905
        elif self.plugin == 'Calico':
891
906
            ctxt.update(self.calico_ctxt())
 
907
        elif self.plugin == 'vsp':
 
908
            ctxt.update(self.nuage_ctxt())
892
909
 
893
910
        alchemy_flags = config('neutron-alchemy-flags')
894
911
        if alchemy_flags:
1162
1179
            sysctl_create(sysctl_dict,
1163
1180
                          '/etc/sysctl.d/50-{0}.conf'.format(charm_name()))
1164
1181
        return {'sysctl': sysctl_dict}
 
1182
 
 
1183
 
 
1184
class NeutronAPIContext(OSContextGenerator):
 
1185
    '''
 
1186
    Inspects current neutron-plugin-api relation for neutron settings. Return
 
1187
    defaults if it is not present.
 
1188
    '''
 
1189
    interfaces = ['neutron-plugin-api']
 
1190
 
 
1191
    def __call__(self):
 
1192
        self.neutron_defaults = {
 
1193
            'l2_population': {
 
1194
                'rel_key': 'l2-population',
 
1195
                'default': False,
 
1196
            },
 
1197
            'overlay_network_type': {
 
1198
                'rel_key': 'overlay-network-type',
 
1199
                'default': 'gre',
 
1200
            },
 
1201
            'neutron_security_groups': {
 
1202
                'rel_key': 'neutron-security-groups',
 
1203
                'default': False,
 
1204
            },
 
1205
            'network_device_mtu': {
 
1206
                'rel_key': 'network-device-mtu',
 
1207
                'default': None,
 
1208
            },
 
1209
            'enable_dvr': {
 
1210
                'rel_key': 'enable-dvr',
 
1211
                'default': False,
 
1212
            },
 
1213
            'enable_l3ha': {
 
1214
                'rel_key': 'enable-l3ha',
 
1215
                'default': False,
 
1216
            },
 
1217
        }
 
1218
        ctxt = self.get_neutron_options({})
 
1219
        for rid in relation_ids('neutron-plugin-api'):
 
1220
            for unit in related_units(rid):
 
1221
                rdata = relation_get(rid=rid, unit=unit)
 
1222
                if 'l2-population' in rdata:
 
1223
                    ctxt.update(self.get_neutron_options(rdata))
 
1224
 
 
1225
        return ctxt
 
1226
 
 
1227
    def get_neutron_options(self, rdata):
 
1228
        settings = {}
 
1229
        for nkey in self.neutron_defaults.keys():
 
1230
            defv = self.neutron_defaults[nkey]['default']
 
1231
            rkey = self.neutron_defaults[nkey]['rel_key']
 
1232
            if rkey in rdata.keys():
 
1233
                if type(defv) is bool:
 
1234
                    settings[nkey] = bool_from_string(rdata[rkey])
 
1235
                else:
 
1236
                    settings[nkey] = rdata[rkey]
 
1237
            else:
 
1238
                settings[nkey] = defv
 
1239
        return settings
 
1240
 
 
1241
 
 
1242
class ExternalPortContext(NeutronPortContext):
 
1243
 
 
1244
    def __call__(self):
 
1245
        ctxt = {}
 
1246
        ports = config('ext-port')
 
1247
        if ports:
 
1248
            ports = [p.strip() for p in ports.split()]
 
1249
            ports = self.resolve_ports(ports)
 
1250
            if ports:
 
1251
                ctxt = {"ext_port": ports[0]}
 
1252
                napi_settings = NeutronAPIContext()()
 
1253
                mtu = napi_settings.get('network_device_mtu')
 
1254
                if mtu:
 
1255
                    ctxt['ext_port_mtu'] = mtu
 
1256
 
 
1257
        return ctxt
 
1258
 
 
1259
 
 
1260
class DataPortContext(NeutronPortContext):
 
1261
 
 
1262
    def __call__(self):
 
1263
        ports = config('data-port')
 
1264
        if ports:
 
1265
            portmap = parse_data_port_mappings(ports)
 
1266
            ports = portmap.values()
 
1267
            resolved = self.resolve_ports(ports)
 
1268
            normalized = {get_nic_hwaddr(port): port for port in resolved
 
1269
                          if port not in ports}
 
1270
            normalized.update({port: port for port in resolved
 
1271
                               if port in ports})
 
1272
            if resolved:
 
1273
                return {bridge: normalized[port] for bridge, port in
 
1274
                        six.iteritems(portmap) if port in normalized.keys()}
 
1275
 
 
1276
        return None
 
1277
 
 
1278
 
 
1279
class PhyNICMTUContext(DataPortContext):
 
1280
 
 
1281
    def __call__(self):
 
1282
        ctxt = {}
 
1283
        mappings = super(PhyNICMTUContext, self).__call__()
 
1284
        if mappings and mappings.values():
 
1285
            ports = mappings.values()
 
1286
            napi_settings = NeutronAPIContext()()
 
1287
            mtu = napi_settings.get('network_device_mtu')
 
1288
            if mtu:
 
1289
                ctxt["devs"] = '\\n'.join(ports)
 
1290
                ctxt['mtu'] = mtu
 
1291
 
 
1292
        return ctxt
 
1293
 
 
1294
 
 
1295
class NetworkServiceContext(OSContextGenerator):
 
1296
 
 
1297
    def __init__(self, rel_name='quantum-network-service'):
 
1298
        self.rel_name = rel_name
 
1299
        self.interfaces = [rel_name]
 
1300
 
 
1301
    def __call__(self):
 
1302
        for rid in relation_ids(self.rel_name):
 
1303
            for unit in related_units(rid):
 
1304
                rdata = relation_get(rid=rid, unit=unit)
 
1305
                ctxt = {
 
1306
                    'keystone_host': rdata.get('keystone_host'),
 
1307
                    'service_port': rdata.get('service_port'),
 
1308
                    'auth_port': rdata.get('auth_port'),
 
1309
                    'service_tenant': rdata.get('service_tenant'),
 
1310
                    'service_username': rdata.get('service_username'),
 
1311
                    'service_password': rdata.get('service_password'),
 
1312
                    'quantum_host': rdata.get('quantum_host'),
 
1313
                    'quantum_port': rdata.get('quantum_port'),
 
1314
                    'quantum_url': rdata.get('quantum_url'),
 
1315
                    'region': rdata.get('region'),
 
1316
                    'service_protocol':
 
1317
                    rdata.get('service_protocol') or 'http',
 
1318
                    'auth_protocol':
 
1319
                    rdata.get('auth_protocol') or 'http',
 
1320
                }
 
1321
                if context_complete(ctxt):
 
1322
                    return ctxt
 
1323
        return {}