~tribaal/charms/trusty/landscape-client/fix-unit-tests

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
#!/usr/bin/python

import sys
import socket
import os
import base64

from charmhelpers.core.hookenv import (
    Hooks, UnregisteredHookError, log)
from shutil import rmtree
from subprocess import CalledProcessError

from common import (
    write_json_file, JujuBroker, LandscapeBroker, chown,
    get_juju_info_filenames)
from ceph import (
    get_ceph_client_path, write_ceph_client_keyring, write_ceph_client_config)


CERT_FILE = "/etc/ssl/certs/landscape_server_ca.crt"

JUJU_BROKER = JujuBroker()

hooks = Hooks()


def get_landscape_broker():
    return LandscapeBroker()


@hooks.hook("registration-relation-joined", "registration-relation-changed")
def registration_relation(juju_broker=JUJU_BROKER, landscape_broker=None):
    if landscape_broker is None:
        landscape_broker = get_landscape_broker()

    data = juju_broker.get_relation_config()
    if "ping-url" in data:
        config_changed(relation_data=data, juju_broker=juju_broker,
                       landscape_broker=landscape_broker)


@hooks.hook("config-changed")
def config_changed(relation_data=None, juju_broker=JUJU_BROKER,
                   landscape_broker=None):
    """
    @param relation_data: data from the context of a relation, it should
                          match the data in the config
    """
    if landscape_broker is None:
        landscape_broker = get_landscape_broker()

    juju_broker.log("In config-changed for %s" % landscape_broker.local_unit)

    if relation_data is None:
        relation_data = {}
    service_config = juju_broker.get_service_config()
    # Only update client config with service configs that have been set
    service_config = dict(
        (key, value) for key, value in service_config.iteritems()
        if value is not None)
    relation_data.update(service_config)
    if relation_data.get("ssl-public-key", "").startswith("base64:"):
        _write_certificate(relation_data["ssl-public-key"][7:], CERT_FILE)
        relation_data["ssl-public-key"] = CERT_FILE
    if "account-name" in relation_data:
        return landscape_broker.update_client_config(relation_data)


@hooks.hook("upgrade-charm")
def upgrade_charm(landscape_broker=None):
    """Idempotently upgrades state from older charms."""
    if landscape_broker is None:
        landscape_broker = get_landscape_broker()

    landscape_broker.config.reload()
    return migrate_old_juju_info(landscape_broker.config)


@hooks.hook("container-relation-joined", "ceph-client-relation-joined")
def container_relation_joined(juju_broker=JUJU_BROKER, landscape_broker=None):
    if landscape_broker is None:
        landscape_broker = get_landscape_broker()
    juju_broker.log("In container-relation-joined for %s"
                    "" % landscape_broker.local_unit)
    landscape_broker.config.reload()
    relation_conf = juju_broker.get_relation_config()
    remote_unit_name = juju_broker.environment.get("JUJU_REMOTE_UNIT")
    # We use the remote unit for the unit name, since we want to associate this
    # client with the unit it's managing, not its own unit.
    juju_info = {
        "environment-uuid": juju_broker.environment.get("JUJU_ENV_UUID"),
        "unit-name": remote_unit_name,
        "api-addresses": juju_broker.environment.get("JUJU_API_ADDRESSES"),
        "private-address": relation_conf.get("private-address")}

    # Let's not use any "/" in a filename.
    unit_name = remote_unit_name.replace("/", "-")
    juju_file = "%s.json" % unit_name

    juju_info_dir = landscape_broker.juju_directory

    try:
        os.mkdir(juju_info_dir)
    except OSError:
        pass  # The file already exists.

    juju_info_file = os.path.join(juju_info_dir, juju_file)

    write_json_file(juju_info_file, juju_info)
    exit_code = landscape_broker.update_client_config(
        {"computer-title": socket.gethostname()})

    return exit_code


@hooks.hook("container-relation-departed")
def container_relation_departed(landscape_broker=None):
    """Stop managing the removed unit with Landscape.

    If the relation is removed, the registration will be cleared, the
    computer title unset and the juju info file removed, so that we
    won't try to register again when the config-changed hook is fired.
    """
    if landscape_broker is None:
        landscape_broker = get_landscape_broker()
    landscape_broker.clear_registration()

    # Delete all of the juju-info files.
    files_to_delete = get_juju_info_filenames(landscape_broker.juju_directory)
    for to_delete in files_to_delete:
        os.remove(to_delete)
    landscape_broker.update_client_config(
        {"computer-title": ""}, try_to_register=False)


@hooks.hook("ceph-client-relation-changed")
def ceph_relation_changed(juju_broker=JUJU_BROKER, landscape_broker=None):
    if landscape_broker is None:
        landscape_broker = get_landscape_broker()
    relation_config = juju_broker.get_relation_config()
    # The key is only reported once the ceph ring has quorum (there are enough
    # nodes in the service). There's nothing to do until the key is provided.
    if "key" not in relation_config:
        juju_broker.log(
            "Ceph key not reported in relation config, nothing to do.")
        return 0

    ceph_client_dir = get_ceph_client_path(landscape_broker)
    if not os.path.exists(ceph_client_dir):
        os.mkdir(ceph_client_dir)
        chown(ceph_client_dir)

    # Create ceph client keyring and config for landscape-client use.
    try:
        keyring_file = write_ceph_client_keyring(
            landscape_broker, "landscape-client", relation_config["key"])
    except CalledProcessError as err:
        juju_broker.log(
            "Writing ceph keyring file failed with code %d" % err.returncode)
        return 1

    try:
        write_ceph_client_config(
            landscape_broker, relation_config["auth"], keyring_file,
            relation_config["private-address"])
    except IOError as err:
        juju_broker.log("Writing ceph configuration file failed: %s" % err)
        return 1
    else:
        juju_broker.log("Ceph configuration written to file.")
        return 0


@hooks.hook("ceph-client-relation-broken")
def ceph_relation_broken(landscape_broker=None):
    if landscape_broker is None:
        landscape_broker = get_landscape_broker()
    rmtree(get_ceph_client_path(landscape_broker), ignore_errors=True)


@hooks.hook("ceph-client-relation-departed")
def ceph_relation_departed(landscape_broker=None):
    if landscape_broker is None:
        landscape_broker = get_landscape_broker()
    landscape_broker.clear_registration()


def migrate_old_juju_info(client_config):
    """
    Migrates data from the old meta-data.d directory into the new
    juju-info.json file.
    """
    fallback_meta_data_dir = os.path.join(
        client_config.data_path, "meta-data.d")
    meta_data_dir = client_config.get("meta_data_path", fallback_meta_data_dir)
    if not os.path.exists(meta_data_dir):
        return 0
    juju_info_file = os.path.join(client_config.data_path, "juju-info.json")
    juju_key_map = {"juju-env-uuid": "environment-uuid",
                    "juju-unit-name": "unit-name",
                    "juju-api-addresses": "api-addresses",
                    "juju-private-address": "private-address"}
    juju_info = dict.fromkeys(juju_key_map.values())
    files_to_delete = []
    for filename, info_key in juju_key_map.iteritems():
        file_path = os.path.join(meta_data_dir, filename)
        if not os.path.exists(file_path):
            continue
        with open(file_path, "r") as juju_info_part_file:
            juju_info[info_key] = juju_info_part_file.read().strip()
        files_to_delete.append(file_path)
    missing_values = [value is None for value in juju_info.itervalues()]
    # If they're all missing it probably means we've already converted.
    if all(missing_values):
        return 0
    # ... otherwise there's something unexpected going on.
    if any(missing_values):
        return "Incomplete Juju information found in %s" % meta_data_dir
    write_json_file(juju_info_file, juju_info)
    for file_to_delete in files_to_delete:
        os.remove(file_to_delete)
    return 0


def _write_certificate(certificate, filename):
    """
    @param certificate Text of the certificate, base64 encoded.
    @param filename Full path to file to write
    """
    with open(filename, "w") as file:
        file.write(base64.b64decode(certificate))


if __name__ == '__main__':
    try:
        sys.exit(hooks.execute(sys.argv))
    except UnregisteredHookError as e:
        log('Unknown hook {} - skipping.'.format(e))