~tribaal/charms/trusty/landscape-client/additional-logging

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Adam Collard
  • Date: 2013-11-19 12:39:44 UTC
  • mto: This revision was merged to the branch mainline in revision 33.
  • Revision ID: adam.collard@canonical.com-20131119123944-vqv3t6323n6tsbqb
Move logic for ceph-client-relation-changed into hooks.py
 * Make it testable
 * Test it
 * Bump revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
import sys
4
4
import os
5
5
import base64
 
6
from subprocess import CalledProcessError
6
7
 
7
 
from common import write_json_file, JujuBroker, LandscapeBroker
 
8
from common import write_json_file, JujuBroker, LandscapeBroker, chown
 
9
from ceph import (
 
10
    get_ceph_client_path, write_ceph_client_keyring, write_ceph_client_config)
8
11
 
9
12
 
10
13
def _write_certificate(certificate, filename):
103
106
    return exit_code
104
107
 
105
108
 
 
109
def ceph_relation_changed(juju_broker, landscape_broker):
 
110
    relation_config = juju_broker.get_relation_config()
 
111
    # The key is only reported once the ceph ring has quorum (there are enough
 
112
    # nodes in the service). There's nothing to do until the key is provided.
 
113
    if "key" not in relation_config:
 
114
        juju_broker.log(
 
115
            "Ceph key not reported in relation config, nothing to do.")
 
116
        return 0
 
117
 
 
118
    ceph_client_dir = get_ceph_client_path(landscape_broker)
 
119
    if not os.path.exists(ceph_client_dir):
 
120
        os.mkdir(ceph_client_dir)
 
121
        chown(ceph_client_dir)
 
122
 
 
123
    # Create ceph client keyring and config for landscape-client use.
 
124
    try:
 
125
        keyring_file = write_ceph_client_keyring(
 
126
            landscape_broker, "landscape-client", relation_config["key"])
 
127
    except CalledProcessError as err:
 
128
        juju_broker.log(
 
129
            "Writing ceph keyring file failed with code %d" % err.returncode)
 
130
        return 1
 
131
 
 
132
    try:
 
133
        write_ceph_client_config(
 
134
            landscape_broker, relation_config["auth"], keyring_file,
 
135
            relation_config["private-address"])
 
136
    except IOError as err:
 
137
        juju_broker.log("Writing ceph configuration file failed: %s" % err)
 
138
        return 1
 
139
    else:
 
140
        juju_broker.log("Ceph configuration written to file.")
 
141
        return 0
 
142
 
 
143
 
106
144
HOOKS = {
107
145
    "config-changed": config_changed,
108
146
    "upgrade-charm": upgrade_charm,
114
152
    "container-relation-departed": lambda juju_broker, landscape_broker:
115
153
        landscape_broker.clear_registration(),
116
154
    "ceph-client-relation-joined": container_relation_joined,
 
155
    "ceph-client-relation-changed": ceph_relation_changed,
117
156
    "ceph-client-relation-departed": lambda juju_broker, landscape_broker:
118
157
        landscape_broker.clear_registration()}
119
158