~openstack-charmers-archive/charms/precise/ceph-osd/old-1501

« back to all changes in this revision

Viewing changes to hooks/ceph.py

  • Committer: James Page
  • Date: 2012-12-17 10:31:03 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: james.page@canonical.com-20121217103103-2hbmznsgm3syxckt
Resync with ceph charm, updates for raring

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
import time
13
13
import utils
14
14
import os
 
15
import apt_pkg as apt
15
16
 
16
 
QUORUM = ['leader', 'peon']
 
17
LEADER = 'leader'
 
18
PEON = 'peon'
 
19
QUORUM = [LEADER, PEON]
17
20
 
18
21
 
19
22
def is_quorum():
40
43
        return False
41
44
 
42
45
 
 
46
def is_leader():
 
47
    asok = "/var/run/ceph/ceph-mon.{}.asok".format(utils.get_unit_hostname())
 
48
    cmd = [
 
49
        "ceph",
 
50
        "--admin-daemon",
 
51
        asok,
 
52
        "mon_status"
 
53
        ]
 
54
    if os.path.exists(asok):
 
55
        try:
 
56
            result = json.loads(subprocess.check_output(cmd))
 
57
        except subprocess.CalledProcessError:
 
58
            return False
 
59
        except ValueError:
 
60
            # Non JSON response from mon_status
 
61
            return False
 
62
        if result['state'] == LEADER:
 
63
            return True
 
64
        else:
 
65
            return False
 
66
    else:
 
67
        return False
 
68
 
 
69
 
43
70
def wait_for_quorum():
44
71
    while not is_quorum():
45
72
        time.sleep(3)
81
108
 
82
109
    subprocess.call(cmd)
83
110
 
 
111
 
84
112
_bootstrap_keyring = "/var/lib/ceph/bootstrap-osd/ceph.keyring"
85
113
 
86
114
 
88
116
    return os.path.exists(_bootstrap_keyring)
89
117
 
90
118
 
 
119
def wait_for_bootstrap():
 
120
    while (not is_bootstrapped()):
 
121
        time.sleep(3)
 
122
 
 
123
 
91
124
def import_osd_bootstrap_key(key):
92
125
    if not os.path.exists(_bootstrap_keyring):
93
126
        cmd = [
100
133
        subprocess.check_call(cmd)
101
134
 
102
135
# OSD caps taken from ceph-create-keys
103
 
_osd_bootstrap_caps = [
104
 
    'allow command osd create ...',
105
 
    'allow command osd crush set ...',
106
 
   r'allow command auth add * osd allow\ * mon allow\ rwx',
107
 
    'allow command mon getmap'
108
 
    ]
 
136
_osd_bootstrap_caps = {
 
137
    'mon': [
 
138
        'allow command osd create ...',
 
139
        'allow command osd crush set ...',
 
140
       r'allow command auth add * osd allow\ * mon allow\ rwx',
 
141
        'allow command mon getmap'
 
142
        ]
 
143
    }
109
144
 
110
145
 
111
146
def get_osd_bootstrap_key():
 
147
    return get_named_key('bootstrap-osd', _osd_bootstrap_caps)
 
148
 
 
149
 
 
150
_radosgw_keyring = "/etc/ceph/keyring.rados.gateway"
 
151
 
 
152
 
 
153
def import_radosgw_key(key):
 
154
    if not os.path.exists(_radosgw_keyring):
 
155
        cmd = [
 
156
            'ceph-authtool',
 
157
            _radosgw_keyring,
 
158
            '--create-keyring',
 
159
            '--name=client.radosgw.gateway',
 
160
            '--add-key={}'.format(key)
 
161
            ]
 
162
        subprocess.check_call(cmd)
 
163
 
 
164
# OSD caps taken from ceph-create-keys
 
165
_radosgw_caps = {
 
166
    'mon': ['allow r'],
 
167
    'osd': ['allow rwx']
 
168
    }
 
169
 
 
170
 
 
171
def get_radosgw_key():
 
172
    return get_named_key('radosgw.gateway', _radosgw_caps)
 
173
 
 
174
 
 
175
_default_caps = {
 
176
    'mon': ['allow r'],
 
177
    'osd': ['allow rwx']
 
178
    }
 
179
 
 
180
 
 
181
def get_named_key(name, caps=None):
 
182
    caps = caps or _default_caps
112
183
    cmd = [
113
184
        'ceph',
114
185
        '--name', 'mon.',
116
187
        '/var/lib/ceph/mon/ceph-{}/keyring'.format(
117
188
                                        utils.get_unit_hostname()
118
189
                                        ),
119
 
        'auth', 'get-or-create', 'client.bootstrap-osd',
120
 
        'mon', '; '.join(_osd_bootstrap_caps)
 
190
        'auth', 'get-or-create', 'client.{}'.format(name),
121
191
        ]
 
192
    # Add capabilities
 
193
    for subsystem, subcaps in caps.iteritems():
 
194
        cmd.extend([
 
195
            subsystem,
 
196
            '; '.join(subcaps),
 
197
            ])
122
198
    output = subprocess.check_output(cmd).strip()  # IGNORE:E1103
123
199
    # get-or-create appears to have different output depending
124
200
    # on whether its 'get' or 'create'
132
208
            if 'key' in element:
133
209
                key = element.split(' = ')[1].strip()  # IGNORE:E1103
134
210
    return key
 
211
 
 
212
 
 
213
def get_ceph_version():
 
214
    apt.init()
 
215
    cache = apt.Cache()
 
216
    pkg = cache['ceph']
 
217
    if pkg.current_ver:
 
218
        return apt.upstream_version(pkg.current_ver.ver_str)
 
219
    else:
 
220
        return None
 
221
 
 
222
 
 
223
def version_compare(a, b):
 
224
    return apt.version_compare(a, b)