summaryrefslogtreecommitdiffstats
path: root/charms/trusty/neutron-api-contrail/hooks/neutron_api_contrail_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'charms/trusty/neutron-api-contrail/hooks/neutron_api_contrail_utils.py')
-rw-r--r--charms/trusty/neutron-api-contrail/hooks/neutron_api_contrail_utils.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/charms/trusty/neutron-api-contrail/hooks/neutron_api_contrail_utils.py b/charms/trusty/neutron-api-contrail/hooks/neutron_api_contrail_utils.py
new file mode 100644
index 0000000..034f7fe
--- /dev/null
+++ b/charms/trusty/neutron-api-contrail/hooks/neutron_api_contrail_utils.py
@@ -0,0 +1,59 @@
+from socket import gethostbyname
+from subprocess import CalledProcessError, check_output
+
+import apt_pkg
+from apt_pkg import version_compare
+
+from charmhelpers.core.hookenv import (
+ related_units,
+ relation_get,
+ relation_ids
+)
+
+from charmhelpers.core.templating import render
+
+apt_pkg.init()
+
+def dpkg_version(pkg):
+ try:
+ return check_output(["dpkg-query", "-f", "${Version}\\n", "-W", pkg]).rstrip()
+ except CalledProcessError:
+ return None
+
+CONTRAIL_VERSION = dpkg_version("contrail-config-openstack")
+OPENSTACK_VERSION = dpkg_version("neutron-server")
+
+def contrail_api_ctx():
+ ctxs = [ { "api_server": vip if vip \
+ else gethostbyname(relation_get("private-address", unit, rid)),
+ "api_port": port }
+ for rid in relation_ids("contrail-api")
+ for unit, port, vip in
+ ((unit, relation_get("port", unit, rid), relation_get("vip", unit, rid))
+ for unit in related_units(rid))
+ if port ]
+ return ctxs[0] if ctxs else {}
+
+def identity_admin_ctx():
+ ctxs = [ { "auth_host": gethostbyname(hostname),
+ "auth_port": relation_get("service_port", unit, rid),
+ "admin_user": relation_get("service_username", unit, rid),
+ "admin_password": relation_get("service_password", unit, rid),
+ "admin_tenant_name": relation_get("service_tenant_name", unit, rid) }
+ for rid in relation_ids("identity-admin")
+ for unit, hostname in
+ ((unit, relation_get("service_hostname", unit, rid)) for unit in related_units(rid))
+ if hostname ]
+ return ctxs[0] if ctxs else {}
+
+def write_plugin_config():
+ ctx = {}
+ ctx.update(contrail_api_ctx())
+ ctx.update(identity_admin_ctx())
+ if version_compare(OPENSTACK_VERSION, "1:2015.1~") >= 0:
+ ctx["authtoken"] = True
+ if version_compare(OPENSTACK_VERSION, "2:7.0.0") >= 0:
+ ctx["authtoken_creds"] = True
+ render("ContrailPlugin.ini",
+ "/etc/neutron/plugins/opencontrail/ContrailPlugin.ini",
+ ctx, "root", "neutron", 0440)