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
|
#!/usr/bin/env python
import sys
import json
from charmhelpers.contrib.openstack.utils import configure_installation_source
from charmhelpers.core.hookenv import (
Hooks,
UnregisteredHookError,
config,
local_unit,
log,
relation_get
)
from charmhelpers.core.host import (
restart_on_change,
service_restart
)
from charmhelpers.fetch import (
apt_install,
apt_upgrade,
configure_sources
)
from contrail_control_utils import (
fix_nodemgr,
fix_permissions,
provision_control,
units,
unprovision_control,
write_control_config,
write_nodemgr_config,
write_vnc_api_config
)
PACKAGES = [ "contrail-control", "contrail-utils", "contrail-nodemgr" ]
hooks = Hooks()
config = config()
def add_control():
# check relation dependencies
if not config_get("control-provisioned") \
and config_get("contrail-api-ready") \
and config_get("contrail-discovery-ready") \
and config_get("contrail-ifmap-ready") \
and config_get("identity-admin-ready"):
provision_control()
config["control-provisioned"] = True
@hooks.hook("config-changed")
def config_changed():
pass
def config_get(key):
try:
return config[key]
except KeyError:
return None
@hooks.hook("contrail-api-relation-changed")
def contrail_api_changed():
if not relation_get("port"):
log("Relation not ready")
return
write_vnc_api_config()
config["contrail-api-ready"] = True
add_control()
@hooks.hook("contrail-api-relation-departed")
@hooks.hook("contrail-api-relation-broken")
def contrail_api_departed():
if not units("contrail-api"):
remove_control()
config["contrail-api-ready"] = False
write_vnc_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_control()
@hooks.hook("contrail-discovery-relation-departed")
@hooks.hook("contrail-discovery-relation-broken")
def contrail_discovery_departed():
if not units("contrail-discovery"):
remove_control()
config["contrail-discovery-ready"] = False
contrail_discovery_relation()
@restart_on_change({"/etc/contrail/contrail-control.conf": ["contrail-control"],
"/etc/contrail/control-node.conf": ["contrail-control"],
"/etc/contrail/contrail-control-nodemgr.conf": ["contrail-control-nodemgr"]})
def contrail_discovery_relation():
write_control_config()
write_nodemgr_config()
@hooks.hook("contrail-ifmap-relation-changed")
def contrail_ifmap_changed():
creds = relation_get("creds")
creds = json.loads(creds) if creds else {}
if local_unit() not in creds:
log("Relation not ready")
return
contrail_ifmap_relation()
config["contrail-ifmap-ready"] = True
add_control()
@hooks.hook("contrail-ifmap-relation-departed")
@hooks.hook("contrail-ifmap-relation-broken")
def contrail_ifmap_departed():
if not units("contrail-ifmap"):
remove_control()
config["contrail-ifmap-ready"] = False
contrail_ifmap_relation()
@restart_on_change({"/etc/contrail/contrail-control.conf": ["contrail-control"],
"/etc/contrail/control-node.conf": ["contrail-control"]})
def contrail_ifmap_relation():
write_control_config()
@hooks.hook("identity-admin-relation-changed")
def identity_admin_changed():
if not relation_get("service_hostname"):
log("Relation not ready")
return
write_vnc_api_config()
config["identity-admin-ready"] = True
add_control()
@hooks.hook("identity-admin-relation-departed")
@hooks.hook("identity-admin-relation-broken")
def identity_admin_departed():
if not units("identity-admin"):
remove_control()
config["identity-admin-ready"] = False
write_vnc_api_config()
@hooks.hook()
def install():
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_nodemgr()
def main():
try:
hooks.execute(sys.argv)
except UnregisteredHookError as e:
log("Unknown hook {} - skipping.".format(e))
def remove_control():
if config_get("control-provisioned"):
unprovision_control()
config["control-provisioned"] = False
@hooks.hook("upgrade-charm")
def upgrade_charm():
write_control_config()
write_nodemgr_config()
service_restart("supervisor-control")
if __name__ == "__main__":
main()
|