~ahasenack/charms/precise/landscape-client/fix-registration-relation

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
import os
from subprocess import check_output

from common import LandscapeBroker, chown


CEPH_CLIENT_DIR = "ceph-client"
CEPH_CLIENT_KEYRING = "ceph.landscape-client.keyring"
CEPH_CLIENT_CONF = "ceph.landscape-client.conf"

CONF_TEMPLATE = """
[global]
 auth supported = %(auth)s
 keyring = %(keyring)s
 mon host = %(mon-host)s
"""


def get_ceph_client_path(landscape_broker):
    """Return the path for ceph client conf and keyring."""
    return os.path.join(landscape_broker.config.data_path, CEPH_CLIENT_DIR)


def write_ceph_client_keyring(landscape_broker, name, key):
    """Creates a ceph client keyring with the specified name and key.

    @param name: name of the key
    @param key: the key to include in the keyring
    """
    keyring_file = os.path.join(
        get_ceph_client_path(landscape_broker), CEPH_CLIENT_KEYRING)
    check_output(
        ["ceph-authtool", keyring_file, "--create-keyring",
         "--name=client.%s" % name, "--add-key=%s" % key])
    chown(keyring_file)
    return keyring_file


def write_ceph_client_config(landscape_broker, auth, keyring, mon_address):
    """Write ceph client configuration file.

    @param auth: authentication type
    @param keyring: absolute path of the keyring file
    @param mon_address: address of the ceph monitor
    """
    ceph_client_conf = os.path.join(
        get_ceph_client_path(landscape_broker), CEPH_CLIENT_CONF)
    with open(ceph_client_conf, "w") as file:
        file.write(
            CONF_TEMPLATE % {
                "auth": auth,
                "keyring": keyring,
                "mon-host": "%s:6789" % mon_address})
    chown(ceph_client_conf)
    return ceph_client_conf