aboutsummaryrefslogtreecommitdiffstats
path: root/charms/trusty/contrail-analytics/hooks/contrail_analytics_hooks.py
blob: c05a53f33cc28e8f48c9fe64b4ca4fe221afcc89 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python

from socket import gethostbyname
import sys
import shutil

from apt_pkg import version_compare
import yaml

from charmhelpers.contrib.openstack.utils import configure_installation_source

from charmhelpers.core.hookenv import (
    Hooks,
    UnregisteredHookError,
    config,
    local_unit,
    log,
    relation_get,
    relation_ids,
    relation_set,
    unit_get
)

from charmhelpers.core.host import (
    restart_on_change,
    service_restart
)

from charmhelpers.fetch import (
    apt_install,
    apt_upgrade,
    configure_sources
)

from contrail_analytics_utils import (
    CONTRAIL_VERSION,
    cassandra_units,
    fix_hostname,
    fix_nodemgr,
    fix_permissions,
    fix_services,
    kafka_units,
    provision_analytics,
    units,
    unprovision_analytics,
    write_alarm_config,
    write_analytics_api_config,
    write_collector_config,
    write_nodemgr_config,
    write_query_engine_config,
    write_snmp_collector_config,
    write_topology_config,
    write_vnc_api_config,
    write_keystone_auth_config
)

PACKAGES = [ "contrail-analytics", "contrail-utils", "contrail-nodemgr",
             "python-jinja2" ]

hooks = Hooks()
config = config()

def add_analytics():
    # check relation dependencies
    if not config.get("analytics-configured") \
       and config.get("cassandra-ready") \
       and config.get("kafka-ready") \
       and config.get("zookeeper-ready") \
       and config.get("contrail-api-ready") \
       and config.get("contrail-discovery-ready") \
       and config.get("identity-admin-ready"):
        # provision analytics on 3.0.2.0+
        if version_compare(CONTRAIL_VERSION, "3.0.2.0-34") >= 0:
            provision_analytics()
        config["analytics-configured"] = True

@hooks.hook("cassandra-relation-changed")
def cassandra_changed():
    if not relation_get("native_transport_port"):
        log("Relation not ready")
        return
    if not config.get("cassandra-ready"):
        units = len(cassandra_units())
        required = config["cassandra-units"]
        if units < required:
            log("{} cassandra unit(s) ready, require {} more".format(units, required - units))
            return
    config["cassandra-ready"] = True
    cassandra_relation()
    add_analytics()

@hooks.hook("cassandra-relation-departed")
@hooks.hook("cassandra-relation-broken")
def cassandra_departed():
    if not units("cassandra"):
        remove_analytics()
        config["cassandra-ready"] = False
    cassandra_relation()

@restart_on_change({"/etc/contrail/contrail-collector.conf": ["contrail-collector"],
                    "/etc/contrail/contrail-query-engine.conf": ["contrail-query-engine"],
                    "/etc/contrail/contrail-analytics-api.conf": ["contrail-analytics-api"]})
def cassandra_relation():
    write_collector_config()
    write_query_engine_config()
    write_analytics_api_config()

@hooks.hook("config-changed")
def config_changed():
    vip = config.get("vip")
    for rid in relation_ids("contrail-analytics-api"):
        relation_set(relation_id=rid, vip=vip)

@hooks.hook("contrail-analytics-api-relation-joined")
def contrail_analytics_api_joined():
    relation_set(port=8081, vip=config.get("vip"))

@hooks.hook("contrail-api-relation-changed")
def contrail_api_changed():
    if not relation_get("port"):
        log("Relation not ready")
        return
    contrail_api_relation()
    config["contrail-api-ready"] = True
    add_analytics()

@hooks.hook("contrail-api-relation-departed")
@hooks.hook("contrail-api-relation-broken")
def contrail_api_departed():
    if not units("contrail-api"):
        remove_analytics()
        config["contrail-api-ready"] = False
    contrail_api_relation()

@restart_on_change({"/etc/contrail/contrail-snmp-collector.conf": ["contrail-snmp-collector"],
                    "/etc/contrail/vnc_api_lib.ini": ["contrail-topology"]})
def contrail_api_relation():
    write_snmp_collector_config()
    write_vnc_api_config()
    write_analytics_api_config()

@hooks.hook("contrail-discovery-relation-changed")
def contrail_discovery_changed():
    if not relation_get("port"):
        log("Relation not ready")
        return
    contrail_discovery_relation()
    config["contrail-discovery-ready"] = True
    add_analytics()

@hooks.hook("contrail-discovery-relation-departed")
@hooks.hook("contrail-discovery-relation-broken")
def contrail_discovery_departed():
    if not units("contrail-discovery"):
        remove_analytics()
        config["contrail-discovery-ready"] = False
    contrail_discovery_relation()

@restart_on_change({"/etc/contrail/contrail-collector.conf": ["contrail-collector"],
                    "/etc/contrail/contrail-query-engine.conf": ["contrail-query-engine"],
                    "/etc/contrail/contrail-analytics-api.conf": ["contrail-analytics-api"],
                    "/etc/contrail/contrail-alarm-gen.conf": ["contrail-alarm-gen"],
                    "/etc/contrail/contrail-topology.conf": ["contrail-topology"],
                    "/etc/contrail/contrail-snmp-collector.conf": ["contrail-snmp-collector"],
                    "/etc/contrail/contrail-analytics-nodemgr.conf": ["contrail-analytics-nodemgr"]})
def contrail_discovery_relation():
    write_collector_config()
    write_query_engine_config()
    write_analytics_api_config()
    write_alarm_config()
    write_topology_config()
    write_snmp_collector_config()
    write_nodemgr_config()

@hooks.hook("http-services-relation-joined")
def http_services_joined():
    name = local_unit().replace("/", "-")
    addr = gethostbyname(unit_get("private-address"))
    services = [ { "service_name": "contrail-analytics-api",
                   "service_host": "0.0.0.0",
                   "service_port": 8081,
                   "service_options": [ "mode http", "balance leastconn", "option httpchk GET /analytics HTTP/1.0" ],
                   "servers": [ [ name, addr, 8081, "check" ] ] } ]
    relation_set(services=yaml.dump(services))

@hooks.hook("identity-admin-relation-changed")
def identity_admin_changed():
    if not relation_get("service_hostname"):
        log("Relation not ready")
        return
    identity_admin_relation()
    config["identity-admin-ready"] = True
    add_analytics()

@hooks.hook("identity-admin-relation-departed")
@hooks.hook("identity-admin-relation-broken")
def identity_admin_departed():
    if not units("identity-admin"):
        remove_analytics()
        config["identity-admin-ready"] = False
    identity_admin_relation()

@restart_on_change({"/etc/contrail/contrail-snmp-collector.conf": ["contrail-snmp-collector"],
                    "/etc/contrail/vnc_api_lib.ini": ["contrail-topology"],
                    "/etc/contrail/contrail-keystone-auth.conf": ["contrail-keystone-auth"]})
def identity_admin_relation():
    write_snmp_collector_config()
    write_vnc_api_config()
    write_keystone_auth_config()

@hooks.hook()
def install():
    fix_hostname()
    shutil.copy('files/contrail', '/etc/apt/preferences.d')
    configure_installation_source(config["openstack-origin"])
    configure_sources(True, "install-sources", "install-keys")
    apt_upgrade(fatal=True, dist=True)
    apt_install(PACKAGES, fatal=True)
    fix_permissions()
    fix_services()
    fix_nodemgr()

@hooks.hook("kafka-relation-changed")
def kafka_changed():
    if not relation_get("port"):
        log("Relation not ready")
        return
    if not config.get("kafka-ready"):
        units = len(kafka_units())
        required = config["kafka-units"]
        if units < required:
            log("{} kafka unit(s) ready, require {} more".format(units, required - units))
            return
    config["kafka-ready"] = True
    kafka_relation()
    add_analytics()

@hooks.hook("kafka-relation-departed")
@hooks.hook("kafka-relation-broken")
def kafka_departed():
    if not units("kafka"):
        remove_analytics()
        config["kafka-ready"] = False
    kafka_relation()

@restart_on_change({"/etc/contrail/contrail-collector.conf": ["contrail-collector"],
                    "/etc/contrail/contrail-alarm-gen.conf": ["contrail-alarm-gen"]})
def kafka_relation():
    write_collector_config()
    write_alarm_config()

def main():
    try:
        hooks.execute(sys.argv)
    except UnregisteredHookError as e:
        log("Unknown hook {} - skipping.".format(e))

def remove_analytics():
    if config.get("analytics-configured"):
        # unprovision analytics on 3.0.2.0+
        if version_compare(CONTRAIL_VERSION, "3.0.2.0-34") >= 0:
            unprovision_analytics()
        config["analytics-configured"] = False

@hooks.hook("upgrade-charm")
def upgrade_charm():
    write_collector_config()
    write_query_engine_config()
    write_analytics_api_config()
    write_alarm_config()
    write_topology_config()
    write_snmp_collector_config()
    write_vnc_api_config()
    write_nodemgr_config()
    service_restart("supervisor-analytics")

@hooks.hook("zookeeper-relation-changed")
def zookeeper_changed():
    if not relation_get("port"):
        log("Relation not ready")
        return
    zookeeper_relation()
    config["zookeeper-ready"] = True
    add_analytics()

@hooks.hook("zookeeper-relation-departed")
@hooks.hook("zookeeper-relation-broken")
def zookeeper_departed():
    if not units("zookeeper"):
        remove_analytics()
        config["zookeeper-ready"] = False
    zookeeper_relation()

@restart_on_change({"/etc/contrail/contrail-collector.conf": ["contrail-collector"],
                    "/etc/contrail/contrail-alarm-gen.conf": ["contrail-alarm-gen"],
                    "/etc/contrail/contrail-topology.conf": ["contrail-topology"],
                    "/etc/contrail/contrail-snmp-collector.conf": ["contrail-snmp-collector"]})
def zookeeper_relation():
    write_collector_config()
    write_alarm_config()
    write_topology_config()
    write_snmp_collector_config()

if __name__ == "__main__":
    main()