当前位置: 首页>>代码示例>>Python>>正文


Python RingBuilder.load方法代码示例

本文整理汇总了Python中swift.common.ring.RingBuilder.load方法的典型用法代码示例。如果您正苦于以下问题:Python RingBuilder.load方法的具体用法?Python RingBuilder.load怎么用?Python RingBuilder.load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在swift.common.ring.RingBuilder的用法示例。


在下文中一共展示了RingBuilder.load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_load

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
    def test_load(self):
        rb = ring.RingBuilder(8, 3, 1)
        devs = [{'id': 0, 'region': 0, 'zone': 0, 'weight': 1,
                 'ip': '127.0.0.0', 'port': 10000, 'device': 'sda1',
                 'meta': 'meta0'},
                {'id': 1, 'region': 0, 'zone': 1, 'weight': 1,
                 'ip': '127.0.0.1', 'port': 10001, 'device': 'sdb1',
                 'meta': 'meta1'},
                {'id': 2, 'region': 0, 'zone': 2, 'weight': 2,
                 'ip': '127.0.0.2', 'port': 10002, 'device': 'sdc1',
                 'meta': 'meta2'},
                {'id': 3, 'region': 0, 'zone': 3, 'weight': 2,
                 'ip': '127.0.0.3', 'port': 10003, 'device': 'sdd1'}]
        for d in devs:
            rb.add_dev(d)
        rb.rebalance()

        real_pickle = pickle.load
        try:
            #test a legit builder
            fake_pickle = Mock(return_value=rb)
            fake_open = Mock(return_value=None)
            pickle.load = fake_pickle
            builder = RingBuilder.load('fake.builder', open=fake_open)
            self.assertEquals(fake_pickle.call_count, 1)
            fake_open.assert_has_calls([mock_call('fake.builder', 'rb')])
            self.assertEquals(builder, rb)
            fake_pickle.reset_mock()
            fake_open.reset_mock()

            #test old style builder
            fake_pickle.return_value = rb.to_dict()
            pickle.load = fake_pickle
            builder = RingBuilder.load('fake.builder', open=fake_open)
            fake_open.assert_has_calls([mock_call('fake.builder', 'rb')])
            self.assertEquals(builder.devs, rb.devs)
            fake_pickle.reset_mock()
            fake_open.reset_mock()

            #test old devs but no meta
            no_meta_builder = rb
            for dev in no_meta_builder.devs:
                del(dev['meta'])
            fake_pickle.return_value = no_meta_builder
            pickle.load = fake_pickle
            builder = RingBuilder.load('fake.builder', open=fake_open)
            fake_open.assert_has_calls([mock_call('fake.builder', 'rb')])
            self.assertEquals(builder.devs, rb.devs)
            fake_pickle.reset_mock()
        finally:
            pickle.load = real_pickle
开发者ID:ganenlovecode,项目名称:swift,代码行数:53,代码来源:test_builder.py

示例2: test_remove_device

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
    def test_remove_device(self):
        for search_value in self.search_values:
            self.create_sample_ring()
            argv = ["", self.tmpfile, "remove", search_value]
            self.assertRaises(SystemExit, swift.cli.ringbuilder.main, argv)
            ring = RingBuilder.load(self.tmpfile)

            # Check that weight was set to 0
            dev = [d for d in ring.devs if d['id'] == 0][0]
            self.assertEqual(dev['weight'], 0)

            # Check that device is in list of devices to be removed
            dev = [d for d in ring._remove_devs if d['id'] == 0][0]
            self.assertEqual(dev['region'], 0)
            self.assertEqual(dev['zone'], 0)
            self.assertEqual(dev['ip'], '127.0.0.1')
            self.assertEqual(dev['port'], 6000)
            self.assertEqual(dev['device'], 'sda1')
            self.assertEqual(dev['weight'], 0)
            self.assertEqual(dev['replication_ip'], '127.0.0.1')
            self.assertEqual(dev['replication_port'], 6000)
            self.assertEqual(dev['meta'], 'some meta data')

            # Check that second device in ring is not affected
            dev = [d for d in ring.devs if d['id'] == 1][0]
            self.assertEqual(dev['weight'], 100)
            self.assertFalse([d for d in ring._remove_devs if d['id'] == 1])

            # Final check, rebalance and check ring is ok
            ring.rebalance()
            self.assertTrue(ring.validate())
开发者ID:steveruckdashel,项目名称:swift,代码行数:33,代码来源:test_ringbuilder.py

示例3: test_set_info

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
    def test_set_info(self):
        for search_value in self.search_values:

            self.create_sample_ring()
            argv = ["", self.tmpfile, "set_info", search_value,
                    "127.0.1.1:8000/sda1_other meta data"]
            self.assertRaises(SystemExit, swift.cli.ringbuilder.main, argv)

            # Check that device was created with given data
            ring = RingBuilder.load(self.tmpfile)
            dev = [d for d in ring.devs if d['id'] == 0][0]
            self.assertEqual(dev['ip'], '127.0.1.1')
            self.assertEqual(dev['port'], 8000)
            self.assertEqual(dev['device'], 'sda1')
            self.assertEqual(dev['meta'], 'other meta data')

            # Check that second device in ring is not affected
            dev = [d for d in ring.devs if d['id'] == 1][0]
            self.assertEqual(dev['ip'], '127.0.0.2')
            self.assertEqual(dev['port'], 6001)
            self.assertEqual(dev['device'], 'sda2')
            self.assertEqual(dev['meta'], '')

            # Final check, rebalance and check ring is ok
            ring.rebalance()
            self.assertTrue(ring.validate())
开发者ID:steveruckdashel,项目名称:swift,代码行数:28,代码来源:test_ringbuilder.py

示例4: test_validate

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
 def test_validate(self):
     self.create_sample_ring()
     ring = RingBuilder.load(self.tmpfile)
     ring.rebalance()
     ring.save(self.tmpfile)
     argv = ["", self.tmpfile, "validate"]
     self.assertRaises(SystemExit, swift.cli.ringbuilder.main, argv)
开发者ID:steveruckdashel,项目名称:swift,代码行数:9,代码来源:test_ringbuilder.py

示例5: add_to_ring

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
 def add_to_ring(self, builder_type, body, lasthash, start_response, env):
     """ Handle a add device post """
     with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
         self.verify_current_hash(self.bf_path[builder_type], lasthash)
         builder = RingBuilder.load(self.bf_path[builder_type])
         ring_modified = False
         try:
             for device in body['devices']:
                 sleep()  # so we don't starve/block
                 if not self._is_existing_dev(builder, device['ip'],
                                              int(device['port']),
                                              device['device']):
                     self._add_device(builder, int(device['zone']),
                                      device['ip'], int(device['port']),
                                      device['device'],
                                      float(device['weight']),
                                      device['meta'])
                     ring_modified = True
         except (AttributeError, KeyError, ValueError, TypeError) as err:
             return self.return_response(False, lasthash,
                                         "Malformed request.",
                                         start_response, env)
         if ring_modified:
             newmd5 = self.write_builder(builder,
                                         self.bf_path[builder_type])
             return self.return_response(True, newmd5, None,
                                         start_response, env)
         else:
             return self.return_response(False, lasthash,
                                         'Ring remains unchanged.',
                                         start_response, env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:33,代码来源:middleware.py

示例6: change_meta

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
    def change_meta(self, builder_type, dev_meta, lasthash, start_response,
                    env):
        """ Change meta info for devices

        :param builder_type: the builder_type to use when loading the builder
        :param dev_meta: a dict of device id and meta info
        :param lasthash: the hash to use when verifying state
        """
        with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
            self.verify_current_hash(self.bf_path[builder_type], lasthash)
            builder = RingBuilder.load(self.bf_path[builder_type])
            try:
                modified = False
                for dev_id in dev_meta:
                    sleep()  # so we don't starve/block
                    for device in builder.devs:
                        if not device:
                            continue
                        if device['id'] == int(dev_id):
                            modified = True
                            device['meta'] = '%s' % dev_meta[dev_id]
                if modified:
                    newmd5 = self.write_builder(builder,
                                                self.bf_path[builder_type])
                    return self.return_response(True, newmd5, None,
                                                start_response, env)
                else:
                    return self.return_response(False, lasthash,
                                                'Invalid dev id %s.' % dev_id,
                                                start_response, env)
            except ValueError as err:
                return self.return_response(False, lasthash, str(err),
                                            start_response, env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:35,代码来源:middleware.py

示例7: change_weight

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
    def change_weight(self, builder_type, dev_weights, lasthash,
                      start_response, env):
        """ Change weight of devices

        :param builder_type: the builder_type to use when loading the builder
        :param dev_weights: a dict of device id and weight
        :param lasthash: the hash to use when verifying state
        """
        with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
            self.verify_current_hash(self.bf_path[builder_type], lasthash)
            builder = RingBuilder.load(self.bf_path[builder_type])
            for dev_id in dev_weights:
                sleep()  # so we don't starve/block
                try:
                    builder.set_dev_weight(int(dev_id),
                                           float(dev_weights[dev_id]))
                except (IndexError, TypeError):
                    return self.return_response(False, lasthash,
                                                'Invalid dev id %s.' % dev_id,
                                                start_response, env)
                except ValueError as err:
                    return self.return_response(False, lasthash, str(err),
                                                start_response, env)
            newmd5 = self.write_builder(builder, self.bf_path[builder_type])
            return self.return_response(True, newmd5, None, start_response,
                                        env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:28,代码来源:middleware.py

示例8: remove_devs

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
    def remove_devs(self, builder_type, devices, lasthash, start_response,
                    env):
        """ remove devices from the builder

        :params builder_type: the builder_type to use when loading the builder
        :params devices: list of device ids to be removed.
        :params lasthash: the hash to use when verifying state
        """
        with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
            self.verify_current_hash(self.bf_path[builder_type], lasthash)
            builder = RingBuilder.load(self.bf_path[builder_type])
            if not isinstance(devices, list):
                return self.return_response(False, lasthash,
                                            'Malformed request.',
                                            start_response, env)
            for dev_id in devices:
                sleep()  # so we don't starve/block
                try:
                    builder.remove_dev(int(dev_id))
                except (IndexError, TypeError):
                    return self.return_response(False, lasthash,
                                                'Invalid dev id %s.' % dev_id,
                                                start_response, env)
                except RingBuilderError as err:
                    return self.return_response(False, lasthash,
                                                'Error removing %s - %s.' %
                                                (dev_id, err),
                                                start_response, env)
                except ValueError as err:
                    return self.return_response(False, lasthash, str(err),
                                                start_response, env)
            newmd5 = self.write_builder(builder, self.bf_path[builder_type])
            return self.return_response(True, newmd5, None, start_response,
                                        env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:36,代码来源:middleware.py

示例9: test_create_ring

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
 def test_create_ring(self):
     argv = ["", self.tmpfile, "create", "6", "3.14159265359", "1"]
     self.assertRaises(SystemExit, swift.cli.ringbuilder.main, argv)
     ring = RingBuilder.load(self.tmpfile)
     self.assertEqual(ring.part_power, 6)
     self.assertEqual(ring.replicas, 3.14159265359)
     self.assertEqual(ring.min_part_hours, 1)
开发者ID:steveruckdashel,项目名称:swift,代码行数:9,代码来源:test_ringbuilder.py

示例10: rebalance

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
    def rebalance(self, builder_type, lasthash, start_response, env):
        """ rebalance a ring

            note: rebalance doesn't yield.
        """
        with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
            self.verify_current_hash(self.bf_path[builder_type], lasthash)
            builder = RingBuilder.load(self.bf_path[builder_type])
            devs_changed = builder.devs_changed
            try:
                last_balance = builder.get_balance()
                parts, balance = builder.rebalance()
            except RingBuilderError, err:
                self.logger.exception(_("Error during ring validation."))
                return self.return_response(False, None, err.message,
                                            start_response, env)
            if not parts:
                msg = 'Either none need to be assigned or none can be due ' \
                      'to min_part_hours [%s].' % builder.min_part_hours
                self.logger.error(_(msg))
                return self.return_response(False, None, msg, start_response,
                                            env)
            if not devs_changed and abs(last_balance - balance) < 1:
                msg = 'Refusing to save rebalance. Did not change at least 1%.'
                self.logger.error(_(msg))
                return self.return_response(False, None, msg, start_response,
                                            env)
            try:
                builder.validate()
            except RingValidationError, err:
                self.logger.exception(_("Error during ring validation."))
                return self.return_response(False, None, err.message,
                                            start_response, env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:35,代码来源:middleware.py

示例11: __init__

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
 def __init__(self, builder_file, swiftdir='/etc/swift',
              backup_dir_name='backups', verbose=False):
     self.verbose = verbose
     self.builder_file = builder_file
     self.swiftdir = swiftdir
     self.backup_dir = pathjoin(self.swiftdir, backup_dir_name)
     self.builder = RingBuilder.load(builder_file)
开发者ID:pandemicsyn,项目名称:swiftscout,代码行数:9,代码来源:drivescout.py

示例12: test_load

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
    def test_load(self):
        rb = ring.RingBuilder(8, 3, 1)
        devs = [
            {"id": 0, "zone": 0, "weight": 1, "ip": "127.0.0.0", "port": 10000, "device": "sda1", "meta": "meta0"},
            {"id": 1, "zone": 1, "weight": 1, "ip": "127.0.0.1", "port": 10001, "device": "sdb1", "meta": "meta1"},
            {"id": 2, "zone": 2, "weight": 2, "ip": "127.0.0.2", "port": 10002, "device": "sdc1", "meta": "meta2"},
            {"id": 3, "zone": 3, "weight": 2, "ip": "127.0.0.3", "port": 10003, "device": "sdd1"},
        ]
        for d in devs:
            rb.add_dev(d)
        rb.rebalance()

        real_pickle = pickle.load
        try:
            # test a legit builder
            fake_pickle = Mock(return_value=rb)
            fake_open = Mock(return_value=None)
            pickle.load = fake_pickle
            builder = RingBuilder.load("fake.builder", open=fake_open)
            self.assertEquals(fake_pickle.call_count, 1)
            fake_open.assert_has_calls([mock_call("fake.builder", "rb")])
            self.assertEquals(builder, rb)
            fake_pickle.reset_mock()
            fake_open.reset_mock()

            # test old style builder
            fake_pickle.return_value = rb.to_dict()
            pickle.load = fake_pickle
            builder = RingBuilder.load("fake.builder", open=fake_open)
            fake_open.assert_has_calls([mock_call("fake.builder", "rb")])
            self.assertEquals(builder.devs, rb.devs)
            fake_pickle.reset_mock()
            fake_open.reset_mock()

            # test old devs but no meta
            no_meta_builder = rb
            for dev in no_meta_builder.devs:
                del (dev["meta"])
            fake_pickle.return_value = no_meta_builder
            pickle.load = fake_pickle
            builder = RingBuilder.load("fake.builder", open=fake_open)
            fake_open.assert_has_calls([mock_call("fake.builder", "rb")])
            self.assertEquals(builder.devs, rb.devs)
            fake_pickle.reset_mock()
        finally:
            pickle.load = real_pickle
开发者ID:ChristopherMacGown,项目名称:swift,代码行数:48,代码来源:test_builder.py

示例13: main

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
def main(arguments=None):
    global argv, backup_dir, builder, builder_file, ring_file
    if arguments:
        argv = arguments
    else:
        argv = sys_argv

    if len(argv) < 2:
        print "swift-ring-builder %(MAJOR_VERSION)s.%(MINOR_VERSION)s\n" % \
              globals()
        print Commands.default.__doc__.strip()
        print
        cmds = [c for c, f in Commands.__dict__.iteritems()
                if f.__doc__ and c[0] != '_' and c != 'default']
        cmds.sort()
        for cmd in cmds:
            print Commands.__dict__[cmd].__doc__.strip()
            print
        print parse_search_value.__doc__.strip()
        print
        for line in wrap(' '.join(cmds), 79, initial_indent='Quick list: ',
                         subsequent_indent='            '):
            print line
        print('Exit codes: 0 = operation successful\n'
              '            1 = operation completed with warnings\n'
              '            2 = error')
        exit(EXIT_SUCCESS)

    builder_file, ring_file = parse_builder_ring_filename_args(argv)

    if exists(builder_file):
        builder = RingBuilder.load(builder_file)
    elif len(argv) < 3 or argv[2] not in('create', 'write_builder'):
        print 'Ring Builder file does not exist: %s' % argv[1]
        exit(EXIT_ERROR)

    backup_dir = pathjoin(dirname(argv[1]), 'backups')
    try:
        mkdir(backup_dir)
    except OSError as err:
        if err.errno != EEXIST:
            raise

    if len(argv) == 2:
        command = "default"
    else:
        command = argv[2]
    if argv[0].endswith('-safe'):
        try:
            with lock_parent_directory(abspath(argv[1]), 15):
                Commands.__dict__.get(command, Commands.unknown.im_func)()
        except exceptions.LockTimeout:
            print "Ring/builder dir currently locked."
            exit(2)
    else:
        Commands.__dict__.get(command, Commands.unknown.im_func)()
开发者ID:701,项目名称:swift,代码行数:58,代码来源:ringbuilder.py

示例14: list_devices

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
    def list_devices(self, builder_type, start_response, env):
        """ list ALL devices in the ring

        :params builder_type: the builder_type to use when loading the builder
        :returns: list of boolean status, md5sum of the current ring, and all
                  builder.devs
        """
        with lock_file(self.bf_path[builder_type], timeout=1, unlink=False):
            builder = RingBuilder.load(self.bf_path[builder_type])
            current_md5sum = self._get_md5sum(self.bf_path[builder_type])
            return self.return_response(True, current_md5sum, builder.devs,
                                        start_response, env)
开发者ID:pandemicsyn,项目名称:rbm,代码行数:14,代码来源:middleware.py

示例15: test_validate_corrupted_file

# 需要导入模块: from swift.common.ring import RingBuilder [as 别名]
# 或者: from swift.common.ring.RingBuilder import load [as 别名]
    def test_validate_corrupted_file(self):
        self.create_sample_ring()
        ring = RingBuilder.load(self.tmpfile)
        ring.rebalance()
        self.assertTrue(ring.validate())  # ring is valid until now
        ring.save(self.tmpfile)
        argv = ["", self.tmpfile, "validate"]

        # corrupt the file
        with open(self.tmpfile, 'wb') as f:
            f.write(os.urandom(1024))
        try:
            swift.cli.ringbuilder.main(argv)
        except SystemExit as e:
            self.assertEquals(e.code, 2)
开发者ID:steveruckdashel,项目名称:swift,代码行数:17,代码来源:test_ringbuilder.py


注:本文中的swift.common.ring.RingBuilder.load方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。