1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
import apt_pkg
from base64 import b64decode
import json
import os
import requests
from six.moves.urllib.parse import urlparse
from socket import gethostbyname
from charmhelpers.core.hookenv import (
config,
log,
WARNING,
ERROR,
relation_ids,
related_units,
)
from charmhelpers.core.host import (
write_file,
)
from charmhelpers.core.templating import render
apt_pkg.init()
config = config()
def update_service_ips():
try:
endpoints = _get_endpoints()
except Exception as e:
log("Couldn't detect services ips: " + str(e),
level=WARNING)
return False
changed = {}
def _check_key(key):
val = endpoints.get(key)
if val and val != config.get(key):
config[key] = val
changed[key] = val
_check_key("compute_service_ip")
_check_key("image_service_ip")
_check_key("network_service_ip")
if changed:
config.save()
return True
return False
def _get_endpoints():
auth_info = config.get("auth_info")
if auth_info:
auth_info = json.loads(auth_info)
if not auth_info or not auth_info.get("keystone_ip"):
raise Exception("auth_info is not ready.")
api_ver = int(auth_info["keystone_api_version"])
if api_ver == 2:
req_data = {
"auth": {
"tenantName": auth_info["keystone_admin_tenant"],
"passwordCredentials": {
"username": auth_info["keystone_admin_user"],
"password": auth_info["keystone_admin_password"]}}}
else:
req_data = {
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"name": auth_info["keystone_admin_user"],
"domain": {"id": "default"},
"password": auth_info["keystone_admin_password"]
}
}
}
}
}
url = "{proto}://{ip}:{port}/{tokens}".format(
proto=auth_info["keystone_protocol"],
ip=auth_info["keystone_ip"],
port=auth_info["keystone_public_port"],
tokens=auth_info["keystone_api_tokens"])
r = requests.post(url, headers={'Content-type': 'application/json'},
data=json.dumps(req_data), verify=False)
content = json.loads(r.content)
result = dict()
catalog = (content["access"]["serviceCatalog"] if api_ver == 2 else
content["token"]["catalog"])
for service in catalog:
if api_ver == 2:
# NOTE: 0 means first region. do we need to search for region?
url = service["endpoints"][0]["publicURL"]
else:
for endpoint in service["endpoints"]:
if endpoint["interface"] == "public":
url = endpoint["url"]
break
host = gethostbyname(urlparse(url).hostname)
result[service["type"] + "_service_ip"] = host
return result
def write_configs():
# don't need to write any configs for nova. only for neutron.
units = [unit for rid in relation_ids("neutron-api")
for unit in related_units(rid)]
if not units:
return
ctx = _get_context()
# store files in standard path
ca_path = "/etc/contrail/ssl/certs/ca-cert.pem"
ssl_ca = ctx["ssl_ca"]
_save_file(ca_path, ssl_ca)
ctx["ssl_ca_path"] = ca_path
render("ContrailPlugin.ini",
"/etc/neutron/plugins/opencontrail/ContrailPlugin.ini",
ctx, "root", "neutron", 0o440)
def _get_context():
ctx = {}
ip = config.get("api_vip")
if not ip:
ip = config.get("api_ip")
ctx["api_server"] = ip
ctx["api_port"] = config.get("api_port")
ssl_ca = _decode_cert("ssl_ca")
ctx["ssl_ca"] = ssl_ca
ctx["ssl_enabled"] = (ssl_ca is not None and len(ssl_ca) > 0)
log("CTX: " + str(ctx))
auth_info = config.get("auth_info")
if auth_info:
ctx.update(json.loads(auth_info))
return ctx
def _decode_cert(key):
val = config.get(key)
if not val:
return None
try:
return b64decode(val)
except Exception as e:
log("Couldn't decode certificate from config['{}']: {}".format(
key, str(e)), level=ERROR)
return None
def _save_file(path, data):
if data:
fdir = os.path.dirname(path)
if not os.path.exists(fdir):
os.makedirs(fdir)
write_file(path, data, perms=0o444)
elif os.path.exists(path):
os.remove(path)
|