~gnuoy/charms/trusty/cinder/keystoneauth

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/storage/linux/ceph.py

  • Committer: James Page
  • Date: 2016-03-02 12:06:54 UTC
  • Revision ID: james.page@ubuntu.com-20160302120654-240q0neokigi4z61
Resync charm-helpers

Change-Id: I629ed87fa03b8deeacb147adc016625b3b47f009

Show diffs side-by-side

added added

removed removed

Lines of Context:
120
120
    """
121
121
    A custom error to inform the caller that a pool creation failed.  Provides an error message
122
122
    """
 
123
 
123
124
    def __init__(self, message):
124
125
        super(PoolCreationError, self).__init__(message)
125
126
 
129
130
    An object oriented approach to Ceph pool creation. This base class is inherited by ReplicatedPool and ErasurePool.
130
131
    Do not call create() on this base class as it will not do anything.  Instantiate a child class and call create().
131
132
    """
 
133
 
132
134
    def __init__(self, service, name):
133
135
        self.service = service
134
136
        self.name = name
180
182
        :return: int.  The number of pgs to use.
181
183
        """
182
184
        validator(value=pool_size, valid_type=int)
183
 
        osds = get_osds(self.service)
184
 
        if not osds:
 
185
        osd_list = get_osds(self.service)
 
186
        if not osd_list:
185
187
            # NOTE(james-page): Default to 200 for older ceph versions
186
188
            # which don't support OSD query from cli
187
189
            return 200
188
190
 
 
191
        osd_list_length = len(osd_list)
189
192
        # Calculate based on Ceph best practices
190
 
        if osds < 5:
 
193
        if osd_list_length < 5:
191
194
            return 128
192
 
        elif 5 < osds < 10:
 
195
        elif 5 < osd_list_length < 10:
193
196
            return 512
194
 
        elif 10 < osds < 50:
 
197
        elif 10 < osd_list_length < 50:
195
198
            return 4096
196
199
        else:
197
 
            estimate = (osds * 100) / pool_size
 
200
            estimate = (osd_list_length * 100) / pool_size
198
201
            # Return the next nearest power of 2
199
202
            index = bisect.bisect_right(powers_of_two, estimate)
200
203
            return powers_of_two[index]
201
204
 
202
205
 
203
206
class ReplicatedPool(Pool):
204
 
    def __init__(self, service, name, replicas=2):
 
207
    def __init__(self, service, name, pg_num=None, replicas=2):
205
208
        super(ReplicatedPool, self).__init__(service=service, name=name)
206
209
        self.replicas = replicas
 
210
        if pg_num is None:
 
211
            self.pg_num = self.get_pgs(self.replicas)
 
212
        else:
 
213
            self.pg_num = pg_num
207
214
 
208
215
    def create(self):
209
216
        if not pool_exists(self.service, self.name):
210
217
            # Create it
211
 
            pgs = self.get_pgs(self.replicas)
212
 
            cmd = ['ceph', '--id', self.service, 'osd', 'pool', 'create', self.name, str(pgs)]
 
218
            cmd = ['ceph', '--id', self.service, 'osd', 'pool', 'create',
 
219
                   self.name, str(self.pg_num)]
213
220
            try:
214
221
                check_call(cmd)
215
222
            except CalledProcessError:
241
248
 
242
249
            pgs = self.get_pgs(int(erasure_profile['k']) + int(erasure_profile['m']))
243
250
            # Create it
244
 
            cmd = ['ceph', '--id', self.service, 'osd', 'pool', 'create', self.name, str(pgs),
 
251
            cmd = ['ceph', '--id', self.service, 'osd', 'pool', 'create', self.name, str(pgs), str(pgs),
245
252
                   'erasure', self.erasure_code_profile]
246
253
            try:
247
254
                check_call(cmd)
322
329
    :return: None.  Can raise CalledProcessError
323
330
    """
324
331
    # Set a byte quota on a RADOS pool in ceph.
325
 
    cmd = ['ceph', '--id', service, 'osd', 'pool', 'set-quota', pool_name, 'max_bytes', max_bytes]
 
332
    cmd = ['ceph', '--id', service, 'osd', 'pool', 'set-quota', pool_name,
 
333
           'max_bytes', str(max_bytes)]
326
334
    try:
327
335
        check_call(cmd)
328
336
    except CalledProcessError:
343
351
        raise
344
352
 
345
353
 
346
 
def create_erasure_profile(service, profile_name, erasure_plugin_name='jerasure', failure_domain='host',
 
354
def remove_erasure_profile(service, profile_name):
 
355
    """
 
356
    Create a new erasure code profile if one does not already exist for it.  Updates
 
357
    the profile if it exists. Please see http://docs.ceph.com/docs/master/rados/operations/erasure-code-profile/
 
358
    for more details
 
359
    :param service: six.string_types. The Ceph user name to run the command under
 
360
    :param profile_name: six.string_types
 
361
    :return: None.  Can raise CalledProcessError
 
362
    """
 
363
    cmd = ['ceph', '--id', service, 'osd', 'erasure-code-profile', 'rm',
 
364
           profile_name]
 
365
    try:
 
366
        check_call(cmd)
 
367
    except CalledProcessError:
 
368
        raise
 
369
 
 
370
 
 
371
def create_erasure_profile(service, profile_name, erasure_plugin_name='jerasure',
 
372
                           failure_domain='host',
347
373
                           data_chunks=2, coding_chunks=1,
348
374
                           locality=None, durability_estimator=None):
349
375
    """