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 | #!/usr/bin/env python3
from subprocess import (
CalledProcessError,
check_call,
check_output
)
import sys
import yaml
from charmhelpers.core.hookenv import (
Hooks,
UnregisteredHookError,
config,
resource_get,
log,
status_set,
relation_get,
application_version_set
)
from charmhelpers.fetch import (
apt_install,
apt_upgrade
)
from contrail_analytics_utils import (
fix_hostname,
write_analytics_config,
launch_docker_image,
units,
dpkg_version,
is_already_launched
)
PACKAGES = [ "python", "python-yaml", "python-apt", "docker.io" ]
hooks = Hooks()
config = config()
@hooks.hook("config-changed")
def config_changed():
set_status()
return None
def config_get(key):
try:
return config[key]
except KeyError:
return None
def set_status():
try:
if is_already_launched():
version = dpkg_version("contrail-analytics")
application_version_set(version)
result = check_output(["/usr/bin/docker",
"inspect",
"-f",
"{{.State.Running}}",
"contrail-analytics"
])
except CalledProcessError:
status_set("waiting", "Waiting for container to be launched")
return
if result:
status_set("active", "Unit ready")
else:
status_set("blocked", "Control container is not running")
def load_docker_image():
img_path = resource_get("contrail-analytics")
check_call(["/usr/bin/docker",
"load",
"-i",
img_path,
])
@hooks.hook()
def install():
fix_hostname()
apt_upgrade(fatal=True, dist=True)
apt_install(PACKAGES, fatal=True)
load_docker_image()
#launch_docker_image()
@hooks.hook("contrail-control-relation-joined")
def contrail_control_joined():
print "NUM CONTROL UNITS: ", len(units("contrail-control"))
if len(units("contrail-control")) == config.get("control_units"):
config["control-ready"] = True
write_analytics_config()
@hooks.hook("contrail-analyticsdb-relation-joined")
def contrail_analyticsdb_joined():
print "NUM ANALYTICSDB UNITS: ", len(units("contrail-analyticsdb"))
if len(units("contrail-analyticsdb")) == config.get("analyticsdb_units"):
config["analyticsdb-ready"] = True
write_analytics_config()
@hooks.hook("contrail-lb-relation-joined")
def contrail_lb_joined():
config["lb-ready"] = True
write_analytics_config()
@hooks.hook("contrail-control-relation-departed")
def contrail_control_departed():
config["control-ready"] = False
@hooks.hook("contrail-analyticsdb-relation-departed")
def contrail_analyticsdb_departed():
config["analyticsdb-ready"] = False
@hooks.hook("contrail-lb-relation-departed")
def contrail_lb_departed():
config["lb-ready"] = False
@hooks.hook("identity-admin-relation-changed")
def identity_admin_changed():
if not relation_get("service_hostname"):
log("Keystone relation not ready")
return
config["identity-admin-ready"] = True
write_analytics_config()
@hooks.hook("identity-admin-relation-departed")
@hooks.hook("identity-admin-relation-broken")
def identity_admin_broken():
config["identity-admin-ready"] = False
@hooks.hook("update-status")
def update_status():
set_status()
#status_set("active", "Unit ready")
def main():
try:
hooks.execute(sys.argv)
except UnregisteredHookError as e:
log("Unknown hook {} - skipping.".format(e))
if __name__ == "__main__":
main()
|