~tribaal/charms/trusty/landscape-client/pythonize-install

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Christopher Glass
  • Date: 2014-05-19 07:33:02 UTC
  • Revision ID: christopher.glass@canonical.com-20140519073302-ple6fbqw15uuyyzx
* removed useless backup file.
* Make hooks use dependency injection for brokers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
CERT_FILE = "/etc/ssl/certs/landscape_server_ca.crt"
22
22
 
 
23
JUJU_BROKER = JujuBroker()
 
24
LANDSCAPE_BROKER = LandscapeBroker()
 
25
 
23
26
hooks = Hooks()
24
27
 
25
28
 
49
52
 
50
53
 
51
54
@hooks.hook("registration-relation-joined","registration-relation-changed")
52
 
def registration_relation():
53
 
    juju_broker = _get_juju_broker()
 
55
def registration_relation(juju_broker=JUJU_BROKER,
 
56
                          landscape_broker=LANDSCAPE_BROKER):
54
57
    data = juju_broker.get_relation_config()
55
58
    if "ping-url" in data:
56
 
        config_changed(relation_data=data)
 
59
        config_changed(relation_data=data, juju_broker=juju_broker,
 
60
                       landscape_broker=landscape_broker)
57
61
 
58
62
 
59
63
@hooks.hook("config-changed")
60
 
def config_changed(relation_data=None):
 
64
def config_changed(relation_data=None, juju_broker=JUJU_BROKER,
 
65
                   landscape_broker=LANDSCAPE_BROKER):
61
66
    """
62
67
    @param relation_data: data from the context of a relation, it should
63
68
                          match the data in the config
64
69
    """
65
 
    juju_broker = _get_juju_broker()
66
 
    landscape_broker = _get_landscape_broker()
67
70
    if relation_data is None:
68
71
        relation_data = {}
69
72
    service_config = juju_broker.get_service_config()
80
83
 
81
84
 
82
85
@hooks.hook("upgrade-charm")
83
 
def upgrade_charm():
 
86
def upgrade_charm(landscape_broker=LANDSCAPE_BROKER):
84
87
    """Idempotently upgrades state from older charms."""
85
 
    landscape_broker = _get_landscape_broker()
86
88
    landscape_broker.config.reload()
87
89
    return migrate_old_juju_info(landscape_broker.config)
88
90
 
89
91
 
90
92
@hooks.hook("container-relation-joined", "ceph-client-relation-joined")
91
 
def container_relation_joined():
92
 
    landscape_broker = _get_landscape_broker()
93
 
    juju_broker = _get_juju_broker()
 
93
def container_relation_joined(juju_broker=JUJU_BROKER,
 
94
                              landscape_broker=LANDSCAPE_BROKER):
94
95
    landscape_broker.config.reload()
95
96
    relation_conf = juju_broker.get_relation_config()
96
97
    # We use the remote unit for the unit name, since we want to associate this
110
111
 
111
112
 
112
113
@hooks.hook("container-relation-departed")
113
 
def container_relation_departed():
 
114
def container_relation_departed(landscape_broker=LANDSCAPE_BROKER):
114
115
    """Stop managing the removed unit with Landscape.
115
116
 
116
117
    If the relation is removed, the registration will be cleared, the
117
118
    computer title unset and the juju info file removed, so that we
118
119
    won't try to register again when the config-changed hook is fired.
119
120
    """
120
 
    landscape_broker = _get_landscape_broker()
121
121
    landscape_broker.clear_registration(),
122
122
    juju_info_path = os.path.join(
123
123
        landscape_broker.config.data_path, "juju-info.json")
127
127
 
128
128
 
129
129
@hooks.hook("ceph-client-relation-changed")
130
 
def ceph_relation_changed():
131
 
    landscape_broker = _get_landscape_broker()
132
 
    juju_broker = _get_juju_broker()
 
130
def ceph_relation_changed(juju_broker=JUJU_BROKER,
 
131
                          landscape_broker=LANDSCAPE_BROKER):
133
132
    relation_config = juju_broker.get_relation_config()
134
133
    # The key is only reported once the ceph ring has quorum (there are enough
135
134
    # nodes in the service). There's nothing to do until the key is provided.
165
164
 
166
165
 
167
166
@hooks.hook("ceph-client-relation-broken")
168
 
def ceph_relation_broken():
169
 
    landscape_broker = _get_landscape_broker()
 
167
def ceph_relation_broken(landscape_broker=LANDSCAPE_BROKER):
170
168
    rmtree(get_ceph_client_path(landscape_broker), ignore_errors=True)
171
169
 
172
170
 
173
171
@hooks.hook("ceph-client-relation-departed")
174
 
def ceph_relation_departed():
175
 
    landscape_broker = _get_landscape_broker()
 
172
def ceph_relation_departed(landscape_broker=LANDSCAPE_BROKER):
176
173
    landscape_broker.clear_registration()
177
174
 
178
175
 
262
259
        file.write(base64.b64decode(certificate))
263
260
 
264
261
 
265
 
def _get_juju_broker():
266
 
    return JujuBroker()
267
 
 
268
 
 
269
 
def _get_landscape_broker():
270
 
    return LandscapeBroker()
271
 
 
272
 
 
273
262
if __name__ == '__main__':
274
263
    try:
275
264
        hooks.execute(sys.argv)