Example #1
0
def _parse_remove_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    try:
        devs = []
        if len(args) > 0:
            if new_cmd_format:
                print(Commands.remove.__doc__.strip())
                exit(EXIT_ERROR)

            for arg in args:
                devs.extend(builder.search_devs(
                    parse_search_value(arg)) or [])
        else:
            devs.extend(builder.search_devs(
                parse_search_values_from_opts(opts)))

        return devs
    except ValueError as e:
        print(e)
        exit(EXIT_ERROR)
Example #2
0
def _parse_set_weight_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    try:
        devs = []
        if not new_cmd_format:
            if len(args) % 2 != 0:
                print(Commands.set_weight.__doc__.strip())
                exit(EXIT_ERROR)

            devs_and_weights = izip(islice(argvish, 0, len(argvish), 2),
                                    islice(argvish, 1, len(argvish), 2))
            for devstr, weightstr in devs_and_weights:
                devs.extend(builder.search_devs(
                    parse_search_value(devstr)) or [])
                weight = float(weightstr)
                _set_weight_values(devs, weight)
        else:
            if len(args) != 1:
                print(Commands.set_weight.__doc__.strip())
                exit(EXIT_ERROR)

            devs.extend(builder.search_devs(
                parse_search_values_from_opts(opts)) or [])
            weight = float(args[0])
            _set_weight_values(devs, weight)
    except ValueError as e:
        print(e)
        exit(EXIT_ERROR)
Example #3
0
    def list_parts():
        """
swift-ring-builder <builder_file> list_parts <search-value> [<search-value>] ..
    Returns a 2 column list of all the partitions that are assigned to any of
    the devices matching the search values given. The first column is the
    assigned partition number and the second column is the number of device
    matches for that partition. The list is ordered from most number of matches
    to least. If there are a lot of devices to match against, this command
    could take a while to run.
        """
        if len(argv) < 4:
            print Commands.list_parts.__doc__.strip()
            print
            print parse_search_value.__doc__.strip()
            exit(EXIT_ERROR)
        devs = []
        for arg in argv[3:]:
            devs.extend(builder.search_devs(parse_search_value(arg)) or [])
        if not devs:
            print 'No matching devices found'
            exit(EXIT_ERROR)
        devs = [d['id'] for d in devs]
        max_replicas = int(ceil(builder.replicas))
        matches = [array('i') for x in xrange(max_replicas)]
        for part in xrange(builder.parts):
            count = len(
                [d for d in builder.get_part_devices(part) if d['id'] in devs])
            if count:
                matches[max_replicas - count].append(part)
        print 'Partition   Matches'
        for index, parts in enumerate(matches):
            for part in parts:
                print '%9d   %7d' % (part, max_replicas - index)
        exit(EXIT_SUCCESS)
Example #4
0
    def search():
        """
swift-ring-builder <builder_file> search <search-value>
    Shows information about matching devices.
        """
        if len(argv) < 4:
            print Commands.search.__doc__.strip()
            print
            print parse_search_value.__doc__.strip()
            exit(EXIT_ERROR)
        devs = builder.search_devs(parse_search_value(argv[3]))
        if not devs:
            print 'No matching devices found'
            exit(EXIT_ERROR)
        print 'Devices:    id  region  zone      ip address  port  ' \
              'replication ip  replication port      name weight partitions ' \
              'balance meta'
        weighted_parts = builder.parts * builder.replicas / \
            sum(d['weight'] for d in builder.devs if d is not None)
        for dev in devs:
            if not dev['weight']:
                if dev['parts']:
                    balance = MAX_BALANCE
                else:
                    balance = 0
            else:
                balance = 100.0 * dev['parts'] / \
                    (dev['weight'] * weighted_parts) - 100.0
            print('         %5d %7d %5d %15s %5d %15s %17d %9s %6.02f %10s '
                  '%7.02f %s' %
                  (dev['id'], dev['region'], dev['zone'], dev['ip'],
                   dev['port'], dev['replication_ip'], dev['replication_port'],
                   dev['device'], dev['weight'], dev['parts'], balance,
                   dev['meta']))
        exit(EXIT_SUCCESS)
Example #5
0
def _parse_remove_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    try:
        devs = []
        if len(args) > 0:
            if new_cmd_format:
                print Commands.remove.__doc__.strip()
                exit(EXIT_ERROR)

            for arg in args:
                devs.extend(builder.search_devs(
                    parse_search_value(arg)) or [])
        else:
            devs.extend(builder.search_devs(
                parse_search_values_from_opts(opts)))

        return devs
    except ValueError as e:
        print e
        exit(EXIT_ERROR)
Example #6
0
    def search():
        """
swift-ring-builder <builder_file> search <search-value>
    Shows information about matching devices.
        """
        if len(argv) < 4:
            print Commands.search.__doc__.strip()
            print
            print parse_search_value.__doc__.strip()
            exit(EXIT_ERROR)
        devs = builder.search_devs(parse_search_value(argv[3]))
        if not devs:
            print 'No matching devices found'
            exit(EXIT_ERROR)
        print 'Devices:    id  region  zone      ip address  port  ' \
              'replication ip  replication port      name weight partitions ' \
              'balance meta'
        weighted_parts = builder.parts * builder.replicas / \
            sum(d['weight'] for d in builder.devs if d is not None)
        for dev in devs:
            if not dev['weight']:
                if dev['parts']:
                    balance = MAX_BALANCE
                else:
                    balance = 0
            else:
                balance = 100.0 * dev['parts'] / \
                    (dev['weight'] * weighted_parts) - 100.0
            print(
                '         %5d %7d %5d %15s %5d %15s %17d %9s %6.02f %10s '
                '%7.02f %s' %
                (dev['id'], dev['region'], dev['zone'], dev['ip'], dev['port'],
                 dev['replication_ip'], dev['replication_port'], dev['device'],
                 dev['weight'], dev['parts'], balance, dev['meta']))
        exit(EXIT_SUCCESS)
Example #7
0
    def list_parts():
        """
swift-ring-builder <builder_file> list_parts <search-value> [<search-value>] ..
    Returns a 2 column list of all the partitions that are assigned to any of
    the devices matching the search values given. The first column is the
    assigned partition number and the second column is the number of device
    matches for that partition. The list is ordered from most number of matches
    to least. If there are a lot of devices to match against, this command
    could take a while to run.
        """
        if len(argv) < 4:
            print Commands.list_parts.__doc__.strip()
            print
            print parse_search_value.__doc__.strip()
            exit(EXIT_ERROR)
        devs = []
        for arg in argv[3:]:
            devs.extend(builder.search_devs(parse_search_value(arg)) or [])
        if not devs:
            print 'No matching devices found'
            exit(EXIT_ERROR)
        devs = [d['id'] for d in devs]
        max_replicas = int(ceil(builder.replicas))
        matches = [array('i') for x in xrange(max_replicas)]
        for part in xrange(builder.parts):
            count = len([d for d in builder.get_part_devices(part)
                         if d['id'] in devs])
            if count:
                matches[max_replicas - count].append(part)
        print 'Partition   Matches'
        for index, parts in enumerate(matches):
            for part in parts:
                print '%9d   %7d' % (part, max_replicas - index)
        exit(EXIT_SUCCESS)
Example #8
0
def _parse_set_weight_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    try:
        devs = []
        if not new_cmd_format:
            if len(args) % 2 != 0:
                print(Commands.set_weight.__doc__.strip())
                exit(EXIT_ERROR)

            devs_and_weights = izip(islice(argvish, 0, len(argvish), 2),
                                    islice(argvish, 1, len(argvish), 2))
            for devstr, weightstr in devs_and_weights:
                devs.extend(
                    builder.search_devs(parse_search_value(devstr)) or [])
                weight = float(weightstr)
                _set_weight_values(devs, weight)
        else:
            if len(args) != 1:
                print(Commands.set_weight.__doc__.strip())
                exit(EXIT_ERROR)

            devs.extend(
                builder.search_devs(parse_search_values_from_opts(opts)) or [])
            weight = float(args[0])
            _set_weight_values(devs, weight)
    except ValueError as e:
        print(e)
        exit(EXIT_ERROR)
Example #9
0
    def remove():
        """
swift-ring-builder <builder_file> remove <search-value> [search-value ...]
    Removes the device(s) from the ring. This should normally just be used for
    a device that has failed. For a device you wish to decommission, it's best
    to set its weight to 0, wait for it to drain all its data, then use this
    remove command. This will not take effect until after running 'rebalance'.
    This is so you can make multiple device changes and rebalance them all just
    once.
        """
        if len(argv) < 4:
            print Commands.remove.__doc__.strip()
            print
            print parse_search_value.__doc__.strip()
            exit(EXIT_ERROR)

        for search_value in argv[3:]:
            devs = builder.search_devs(parse_search_value(search_value))
            if not devs:
                print(
                    "Search value \"%s\" matched 0 devices.\n"
                    "The on-disk ring builder is unchanged." % search_value)
                exit(EXIT_ERROR)
            if len(devs) > 1:
                print 'Matched more than one device:'
                for dev in devs:
                    print '    %s' % format_device(dev)
                if raw_input('Are you sure you want to remove these %s '
                             'devices? (y/N) ' % len(devs)) != 'y':
                    print 'Aborting device removals'
                    exit(EXIT_ERROR)
            for dev in devs:
                try:
                    builder.remove_dev(dev['id'])
                except exceptions.RingBuilderError as e:
                    print '-' * 79
                    print(
                        "An error occurred while removing device with id %d\n"
                        "This usually means that you attempted to remove\n"
                        "the last device in a ring. If this is the case,\n"
                        "consider creating a new ring instead.\n"
                        "The on-disk ring builder is unchanged.\n"
                        "Original exception message: %s" %
                        (dev['id'], e.message))
                    print '-' * 79
                    exit(EXIT_ERROR)

                print '%s marked for removal and will ' \
                      'be removed next rebalance.' % format_device(dev)
        builder.save(argv[1])
        exit(EXIT_SUCCESS)
Example #10
0
    def remove():
        """
swift-ring-builder <builder_file> remove <search-value> [search-value ...]
    Removes the device(s) from the ring. This should normally just be used for
    a device that has failed. For a device you wish to decommission, it's best
    to set its weight to 0, wait for it to drain all its data, then use this
    remove command. This will not take effect until after running 'rebalance'.
    This is so you can make multiple device changes and rebalance them all just
    once.
        """
        if len(argv) < 4:
            print Commands.remove.__doc__.strip()
            print
            print parse_search_value.__doc__.strip()
            exit(EXIT_ERROR)

        for search_value in argv[3:]:
            devs = builder.search_devs(parse_search_value(search_value))
            if not devs:
                print("Search value \"%s\" matched 0 devices.\n"
                      "The on-disk ring builder is unchanged." % search_value)
                exit(EXIT_ERROR)
            if len(devs) > 1:
                print 'Matched more than one device:'
                for dev in devs:
                    print '    %s' % format_device(dev)
                if raw_input('Are you sure you want to remove these %s '
                             'devices? (y/N) ' % len(devs)) != 'y':
                    print 'Aborting device removals'
                    exit(EXIT_ERROR)
            for dev in devs:
                try:
                    builder.remove_dev(dev['id'])
                except exceptions.RingBuilderError as e:
                    print '-' * 79
                    print(
                        "An error occurred while removing device with id %d\n"
                        "This usually means that you attempted to remove\n"
                        "the last device in a ring. If this is the case,\n"
                        "consider creating a new ring instead.\n"
                        "The on-disk ring builder is unchanged.\n"
                        "Original exception message: %s" %
                        (dev['id'], e)
                    )
                    print '-' * 79
                    exit(EXIT_ERROR)

                print '%s marked for removal and will ' \
                      'be removed next rebalance.' % format_device(dev)
        builder.save(argv[1])
        exit(EXIT_SUCCESS)
Example #11
0
def _parse_set_info_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    if not new_cmd_format:
        if len(args) % 2 != 0:
            print(Commands.search.__doc__.strip())
            exit(EXIT_ERROR)

        searches_and_changes = izip(islice(argvish, 0, len(argvish), 2),
                                    islice(argvish, 1, len(argvish), 2))

        for search_value, change_value in searches_and_changes:
            devs = builder.search_devs(parse_search_value(search_value))
            change = {}

            change_value = calculate_change_value(change_value, change,
                                                  'ip', 'port')

            if change_value.startswith('R'):
                change_value = change_value[1:]
                change_value = calculate_change_value(change_value, change,
                                                      'replication_ip',
                                                      'replication_port')
            if change_value.startswith('/'):
                i = 1
                while i < len(change_value) and change_value[i] != '_':
                    i += 1
                change['device'] = change_value[1:i]
                change_value = change_value[i:]
            if change_value.startswith('_'):
                change['meta'] = change_value[1:]
                change_value = ''
            if change_value or not change:
                raise ValueError('Invalid set info change value: %s' %
                                 repr(argvish[1]))
            _set_info_values(devs, change, opts)
    else:
        devs = builder.search_devs(parse_search_values_from_opts(opts))
        change = parse_change_values_from_opts(opts)
        _set_info_values(devs, change, opts)
Example #12
0
def _parse_set_info_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    if not new_cmd_format:
        if len(args) % 2 != 0:
            print(Commands.search.__doc__.strip())
            exit(EXIT_ERROR)

        searches_and_changes = izip(islice(argvish, 0, len(argvish), 2),
                                    islice(argvish, 1, len(argvish), 2))

        for search_value, change_value in searches_and_changes:
            devs = builder.search_devs(parse_search_value(search_value))
            change = {}

            change_value = calculate_change_value(change_value, change,
                                                  'ip', 'port')

            if change_value.startswith('R'):
                change_value = change_value[1:]
                change_value = calculate_change_value(change_value, change,
                                                      'replication_ip',
                                                      'replication_port')
            if change_value.startswith('/'):
                i = 1
                while i < len(change_value) and change_value[i] != '_':
                    i += 1
                change['device'] = change_value[1:i]
                change_value = change_value[i:]
            if change_value.startswith('_'):
                change['meta'] = change_value[1:]
                change_value = ''
            if change_value or not change:
                raise ValueError('Invalid set info change value: %s' %
                                 repr(argvish[1]))
            _set_info_values(devs, change, opts)
    else:
        devs = builder.search_devs(parse_search_values_from_opts(opts))
        change = parse_change_values_from_opts(opts)
        _set_info_values(devs, change, opts)
Example #13
0
def _parse_search_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    try:
        search_values = {}
        if len(args) > 0:
            if new_cmd_format or len(args) != 1:
                print(Commands.search.__doc__.strip())
                exit(EXIT_ERROR)
            search_values = parse_search_value(args[0])
        else:
            search_values = parse_search_values_from_opts(opts)
        return search_values
    except ValueError as e:
        print(e)
        exit(EXIT_ERROR)
Example #14
0
def _parse_search_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    try:
        search_values = {}
        if len(args) > 0:
            if new_cmd_format or len(args) != 1:
                print(Commands.search.__doc__.strip())
                exit(EXIT_ERROR)
            search_values = parse_search_value(args[0])
        else:
            search_values = parse_search_values_from_opts(opts)
        return search_values
    except ValueError as e:
        print(e)
        exit(EXIT_ERROR)
Example #15
0
    def set_weight():
        """
swift-ring-builder <builder_file> set_weight <search-value> <weight>
    [<search-value> <weight] ...

    Resets the devices' weights. No partitions will be reassigned to or from
    the device until after running 'rebalance'. This is so you can make
    multiple device changes and rebalance them all just once.
        """
        if len(argv) < 5 or len(argv) % 2 != 1:
            print Commands.set_weight.__doc__.strip()
            print
            print parse_search_value.__doc__.strip()
            exit(EXIT_ERROR)

        devs_and_weights = izip(islice(argv, 3, len(argv), 2),
                                islice(argv, 4, len(argv), 2))
        for devstr, weightstr in devs_and_weights:
            devs = builder.search_devs(parse_search_value(devstr))
            weight = float(weightstr)
            if not devs:
                print("Search value \"%s\" matched 0 devices.\n"
                      "The on-disk ring builder is unchanged.\n"
                      % devstr)
                exit(EXIT_ERROR)
            if len(devs) > 1:
                print 'Matched more than one device:'
                for dev in devs:
                    print '    %s' % format_device(dev)
                if raw_input('Are you sure you want to update the weight for '
                             'these %s devices? (y/N) ' % len(devs)) != 'y':
                    print 'Aborting device modifications'
                    exit(EXIT_ERROR)
            for dev in devs:
                builder.set_dev_weight(dev['id'], weight)
                print '%s weight set to %s' % (format_device(dev),
                                               dev['weight'])
        builder.save(argv[1])
        exit(EXIT_SUCCESS)
Example #16
0
    def set_weight():
        """
swift-ring-builder <builder_file> set_weight <search-value> <weight>
    [<search-value> <weight] ...

    Resets the devices' weights. No partitions will be reassigned to or from
    the device until after running 'rebalance'. This is so you can make
    multiple device changes and rebalance them all just once.
        """
        if len(argv) < 5 or len(argv) % 2 != 1:
            print Commands.set_weight.__doc__.strip()
            print
            print parse_search_value.__doc__.strip()
            exit(EXIT_ERROR)

        devs_and_weights = izip(islice(argv, 3, len(argv), 2),
                                islice(argv, 4, len(argv), 2))
        for devstr, weightstr in devs_and_weights:
            devs = builder.search_devs(parse_search_value(devstr))
            weight = float(weightstr)
            if not devs:
                print(
                    "Search value \"%s\" matched 0 devices.\n"
                    "The on-disk ring builder is unchanged.\n" % devstr)
                exit(EXIT_ERROR)
            if len(devs) > 1:
                print 'Matched more than one device:'
                for dev in devs:
                    print '    %s' % format_device(dev)
                if raw_input('Are you sure you want to update the weight for '
                             'these %s devices? (y/N) ' % len(devs)) != 'y':
                    print 'Aborting device modifications'
                    exit(EXIT_ERROR)
            for dev in devs:
                builder.set_dev_weight(dev['id'], weight)
                print '%s weight set to %s' % (format_device(dev),
                                               dev['weight'])
        builder.save(argv[1])
        exit(EXIT_SUCCESS)
Example #17
0
def _parse_set_info_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    if not new_cmd_format:
        if len(args) % 2 != 0:
            print(Commands.search.__doc__.strip())
            exit(EXIT_ERROR)

        searches_and_changes = izip(islice(argvish, 0, len(argvish), 2),
                                    islice(argvish, 1, len(argvish), 2))

        for search_value, change_value in searches_and_changes:
            devs = builder.search_devs(parse_search_value(search_value))
            change = {}
            ip = ''
            if len(change_value) and change_value[0].isdigit():
                i = 1
                while (i < len(change_value)
                       and change_value[i] in '0123456789.'):
                    i += 1
                ip = change_value[:i]
                change_value = change_value[i:]
            elif len(change_value) and change_value[0] == '[':
                i = 1
                while i < len(change_value) and change_value[i] != ']':
                    i += 1
                i += 1
                ip = change_value[:i].lstrip('[').rstrip(']')
                change_value = change_value[i:]
            if ip:
                change['ip'] = validate_and_normalize_ip(ip)
            if change_value.startswith(':'):
                i = 1
                while i < len(change_value) and change_value[i].isdigit():
                    i += 1
                change['port'] = int(change_value[1:i])
                change_value = change_value[i:]
            if change_value.startswith('R'):
                change_value = change_value[1:]
                replication_ip = ''
                if len(change_value) and change_value[0].isdigit():
                    i = 1
                    while (i < len(change_value)
                           and change_value[i] in '0123456789.'):
                        i += 1
                    replication_ip = change_value[:i]
                    change_value = change_value[i:]
                elif len(change_value) and change_value[0] == '[':
                    i = 1
                    while i < len(change_value) and change_value[i] != ']':
                        i += 1
                    i += 1
                    replication_ip = \
                        change_value[:i].lstrip('[').rstrip(']')
                    change_value = change_value[i:]
                if replication_ip:
                    change['replication_ip'] = \
                        validate_and_normalize_ip(replication_ip)
                if change_value.startswith(':'):
                    i = 1
                    while i < len(change_value) and change_value[i].isdigit():
                        i += 1
                    change['replication_port'] = int(change_value[1:i])
                    change_value = change_value[i:]
            if change_value.startswith('/'):
                i = 1
                while i < len(change_value) and change_value[i] != '_':
                    i += 1
                change['device'] = change_value[1:i]
                change_value = change_value[i:]
            if change_value.startswith('_'):
                change['meta'] = change_value[1:]
                change_value = ''
            if change_value or not change:
                raise ValueError('Invalid set info change value: %s' %
                                 repr(argvish[1]))
            _set_info_values(devs, change)
    else:
        devs = builder.search_devs(parse_search_values_from_opts(opts))
        change = parse_change_values_from_opts(opts)
        _set_info_values(devs, change)
Example #18
0
    def set_info():
        """
swift-ring-builder <builder_file> set_info
    <search-value> <ip>:<port>[R<r_ip>:<r_port>]/<device_name>_<meta>
    [<search-value> <ip>:<port>[R<r_ip>:<r_port>]/<device_name>_<meta>] ...

    Where <r_ip> and <r_port> are replication ip and port.

    For each search-value, resets the matched device's information.
    This information isn't used to assign partitions, so you can use
    'write_ring' afterward to rewrite the current ring with the newer
    device information. Any of the parts are optional in the final
    <ip>:<port>/<device_name>_<meta> parameter; just give what you
    want to change. For instance set_info d74 _"snet: 5.6.7.8" would
    just update the meta data for device id 74.
        """
        if len(argv) < 5 or len(argv) % 2 != 1:
            print Commands.set_info.__doc__.strip()
            print
            print parse_search_value.__doc__.strip()
            exit(EXIT_ERROR)

        searches_and_changes = izip(islice(argv, 3, len(argv), 2),
                                    islice(argv, 4, len(argv), 2))

        for search_value, change_value in searches_and_changes:
            devs = builder.search_devs(parse_search_value(search_value))
            change = []
            if len(change_value) and change_value[0].isdigit():
                i = 1
                while (i < len(change_value) and
                       change_value[i] in '0123456789.'):
                    i += 1
                change.append(('ip', change_value[:i]))
                change_value = change_value[i:]
            elif len(change_value) and change_value[0] == '[':
                i = 1
                while i < len(change_value) and change_value[i] != ']':
                    i += 1
                i += 1
                change.append(('ip', change_value[:i].lstrip('[').rstrip(']')))
                change_value = change_value[i:]
            if change_value.startswith(':'):
                i = 1
                while i < len(change_value) and change_value[i].isdigit():
                    i += 1
                change.append(('port', int(change_value[1:i])))
                change_value = change_value[i:]
            if change_value.startswith('R'):
                change_value = change_value[1:]
                if len(change_value) and change_value[0].isdigit():
                    i = 1
                    while (i < len(change_value) and
                           change_value[i] in '0123456789.'):
                        i += 1
                    change.append(('replication_ip', change_value[:i]))
                    change_value = change_value[i:]
                elif len(change_value) and change_value[0] == '[':
                    i = 1
                    while i < len(change_value) and change_value[i] != ']':
                        i += 1
                    i += 1
                    change.append(('replication_ip',
                                   change_value[:i].lstrip('[').rstrip(']')))
                    change_value = change_value[i:]
                if change_value.startswith(':'):
                    i = 1
                    while i < len(change_value) and change_value[i].isdigit():
                        i += 1
                    change.append(('replication_port', int(change_value[1:i])))
                    change_value = change_value[i:]
            if change_value.startswith('/'):
                i = 1
                while i < len(change_value) and change_value[i] != '_':
                    i += 1
                change.append(('device', change_value[1:i]))
                change_value = change_value[i:]
            if change_value.startswith('_'):
                change.append(('meta', change_value[1:]))
                change_value = ''
            if change_value or not change:
                raise ValueError('Invalid set info change value: %s' %
                                 repr(argv[4]))
            if not devs:
                print("Search value \"%s\" matched 0 devices.\n"
                      "The on-disk ring builder is unchanged.\n"
                      % search_value)
                exit(EXIT_ERROR)
            if len(devs) > 1:
                print 'Matched more than one device:'
                for dev in devs:
                    print '    %s' % format_device(dev)
                if raw_input('Are you sure you want to update the info for '
                             'these %s devices? (y/N) ' % len(devs)) != 'y':
                    print 'Aborting device modifications'
                    exit(EXIT_ERROR)
            for dev in devs:
                orig_dev_string = format_device(dev)
                test_dev = dict(dev)
                for key, value in change:
                    test_dev[key] = value
                for check_dev in builder.devs:
                    if not check_dev or check_dev['id'] == test_dev['id']:
                        continue
                    if check_dev['ip'] == test_dev['ip'] and \
                            check_dev['port'] == test_dev['port'] and \
                            check_dev['device'] == test_dev['device']:
                        print 'Device %d already uses %s:%d/%s.' % \
                              (check_dev['id'], check_dev['ip'],
                               check_dev['port'], check_dev['device'])
                        exit(EXIT_ERROR)
                for key, value in change:
                    dev[key] = value
                print 'Device %s is now %s' % (orig_dev_string,
                                               format_device(dev))
        builder.save(argv[1])
        exit(EXIT_SUCCESS)
Example #19
0
def _parse_set_info_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    if not new_cmd_format:
        if len(args) % 2 != 0:
            print(Commands.search.__doc__.strip())
            exit(EXIT_ERROR)

        searches_and_changes = izip(islice(argvish, 0, len(argvish), 2), islice(argvish, 1, len(argvish), 2))

        for search_value, change_value in searches_and_changes:
            devs = builder.search_devs(parse_search_value(search_value))
            change = {}
            ip = ""
            if len(change_value) and change_value[0].isdigit():
                i = 1
                while i < len(change_value) and change_value[i] in "0123456789.":
                    i += 1
                ip = change_value[:i]
                change_value = change_value[i:]
            elif len(change_value) and change_value[0] == "[":
                i = 1
                while i < len(change_value) and change_value[i] != "]":
                    i += 1
                i += 1
                ip = change_value[:i].lstrip("[").rstrip("]")
                change_value = change_value[i:]
            if ip:
                change["ip"] = validate_and_normalize_ip(ip)
            if change_value.startswith(":"):
                i = 1
                while i < len(change_value) and change_value[i].isdigit():
                    i += 1
                change["port"] = int(change_value[1:i])
                change_value = change_value[i:]
            if change_value.startswith("R"):
                change_value = change_value[1:]
                replication_ip = ""
                if len(change_value) and change_value[0].isdigit():
                    i = 1
                    while i < len(change_value) and change_value[i] in "0123456789.":
                        i += 1
                    replication_ip = change_value[:i]
                    change_value = change_value[i:]
                elif len(change_value) and change_value[0] == "[":
                    i = 1
                    while i < len(change_value) and change_value[i] != "]":
                        i += 1
                    i += 1
                    replication_ip = change_value[:i].lstrip("[").rstrip("]")
                    change_value = change_value[i:]
                if replication_ip:
                    change["replication_ip"] = validate_and_normalize_ip(replication_ip)
                if change_value.startswith(":"):
                    i = 1
                    while i < len(change_value) and change_value[i].isdigit():
                        i += 1
                    change["replication_port"] = int(change_value[1:i])
                    change_value = change_value[i:]
            if change_value.startswith("/"):
                i = 1
                while i < len(change_value) and change_value[i] != "_":
                    i += 1
                change["device"] = change_value[1:i]
                change_value = change_value[i:]
            if change_value.startswith("_"):
                change["meta"] = change_value[1:]
                change_value = ""
            if change_value or not change:
                raise ValueError("Invalid set info change value: %s" % repr(argvish[1]))
            _set_info_values(devs, change)
    else:
        devs = builder.search_devs(parse_search_values_from_opts(opts))
        change = parse_change_values_from_opts(opts)
        _set_info_values(devs, change)
Example #20
0
 def test_parse_search_value(self):
     res = parse_search_value('r0')
     self.assertEqual(res, {'region': 0})
     res = parse_search_value('r1')
     self.assertEqual(res, {'region': 1})
     res = parse_search_value('r1z2')
     self.assertEqual(res, {'region': 1, 'zone': 2})
     res = parse_search_value('d1')
     self.assertEqual(res, {'id': 1})
     res = parse_search_value('z1')
     self.assertEqual(res, {'zone': 1})
     res = parse_search_value('-127.0.0.1')
     self.assertEqual(res, {'ip': '127.0.0.1'})
     res = parse_search_value('127.0.0.1')
     self.assertEqual(res, {'ip': '127.0.0.1'})
     res = parse_search_value('-[127.0.0.1]:10001')
     self.assertEqual(res, {'ip': '127.0.0.1', 'port': 10001})
     res = parse_search_value(':10001')
     self.assertEqual(res, {'port': 10001})
     res = parse_search_value('R127.0.0.10')
     self.assertEqual(res, {'replication_ip': '127.0.0.10'})
     res = parse_search_value('R[127.0.0.10]:20000')
     self.assertEqual(res, {
         'replication_ip': '127.0.0.10',
         'replication_port': 20000
     })
     res = parse_search_value('R:20000')
     self.assertEqual(res, {'replication_port': 20000})
     res = parse_search_value('/sdb1')
     self.assertEqual(res, {'device': 'sdb1'})
     res = parse_search_value('_meta1')
     self.assertEqual(res, {'meta': 'meta1'})
     self.assertRaises(ValueError, parse_search_value, 'OMGPONIES')
Example #21
0
    def set_info():
        """
swift-ring-builder <builder_file> set_info
    <search-value> <ip>:<port>[R<r_ip>:<r_port>]/<device_name>_<meta>
    [<search-value> <ip>:<port>[R<r_ip>:<r_port>]/<device_name>_<meta>] ...

    Where <r_ip> and <r_port> are replication ip and port.

    For each search-value, resets the matched device's information.
    This information isn't used to assign partitions, so you can use
    'write_ring' afterward to rewrite the current ring with the newer
    device information. Any of the parts are optional in the final
    <ip>:<port>/<device_name>_<meta> parameter; just give what you
    want to change. For instance set_info d74 _"snet: 5.6.7.8" would
    just update the meta data for device id 74.
        """
        if len(argv) < 5 or len(argv) % 2 != 1:
            print Commands.set_info.__doc__.strip()
            print
            print parse_search_value.__doc__.strip()
            exit(EXIT_ERROR)

        searches_and_changes = izip(islice(argv, 3, len(argv), 2),
                                    islice(argv, 4, len(argv), 2))

        for search_value, change_value in searches_and_changes:
            devs = builder.search_devs(parse_search_value(search_value))
            change = []
            if len(change_value) and change_value[0].isdigit():
                i = 1
                while (i < len(change_value)
                       and change_value[i] in '0123456789.'):
                    i += 1
                change.append(('ip', change_value[:i]))
                change_value = change_value[i:]
            elif len(change_value) and change_value[0] == '[':
                i = 1
                while i < len(change_value) and change_value[i] != ']':
                    i += 1
                i += 1
                change.append(('ip', change_value[:i].lstrip('[').rstrip(']')))
                change_value = change_value[i:]
            if change_value.startswith(':'):
                i = 1
                while i < len(change_value) and change_value[i].isdigit():
                    i += 1
                change.append(('port', int(change_value[1:i])))
                change_value = change_value[i:]
            if change_value.startswith('R'):
                change_value = change_value[1:]
                if len(change_value) and change_value[0].isdigit():
                    i = 1
                    while (i < len(change_value)
                           and change_value[i] in '0123456789.'):
                        i += 1
                    change.append(('replication_ip', change_value[:i]))
                    change_value = change_value[i:]
                elif len(change_value) and change_value[0] == '[':
                    i = 1
                    while i < len(change_value) and change_value[i] != ']':
                        i += 1
                    i += 1
                    change.append(('replication_ip',
                                   change_value[:i].lstrip('[').rstrip(']')))
                    change_value = change_value[i:]
                if change_value.startswith(':'):
                    i = 1
                    while i < len(change_value) and change_value[i].isdigit():
                        i += 1
                    change.append(('replication_port', int(change_value[1:i])))
                    change_value = change_value[i:]
            if change_value.startswith('/'):
                i = 1
                while i < len(change_value) and change_value[i] != '_':
                    i += 1
                change.append(('device', change_value[1:i]))
                change_value = change_value[i:]
            if change_value.startswith('_'):
                change.append(('meta', change_value[1:]))
                change_value = ''
            if change_value or not change:
                raise ValueError('Invalid set info change value: %s' %
                                 repr(argv[4]))
            if not devs:
                print(
                    "Search value \"%s\" matched 0 devices.\n"
                    "The on-disk ring builder is unchanged.\n" % search_value)
                exit(EXIT_ERROR)
            if len(devs) > 1:
                print 'Matched more than one device:'
                for dev in devs:
                    print '    %s' % format_device(dev)
                if raw_input('Are you sure you want to update the info for '
                             'these %s devices? (y/N) ' % len(devs)) != 'y':
                    print 'Aborting device modifications'
                    exit(EXIT_ERROR)
            for dev in devs:
                orig_dev_string = format_device(dev)
                test_dev = dict(dev)
                for key, value in change:
                    test_dev[key] = value
                for check_dev in builder.devs:
                    if not check_dev or check_dev['id'] == test_dev['id']:
                        continue
                    if check_dev['ip'] == test_dev['ip'] and \
                            check_dev['port'] == test_dev['port'] and \
                            check_dev['device'] == test_dev['device']:
                        print 'Device %d already uses %s:%d/%s.' % \
                              (check_dev['id'], check_dev['ip'],
                               check_dev['port'], check_dev['device'])
                        exit(EXIT_ERROR)
                for key, value in change:
                    dev[key] = value
                print 'Device %s is now %s' % (orig_dev_string,
                                               format_device(dev))
        builder.save(argv[1])
        exit(EXIT_SUCCESS)
Example #22
0
def _parse_set_info_values(argvish):

    new_cmd_format, opts, args = validate_args(argvish)

    # We'll either parse the all-in-one-string format or the
    # --options format,
    # but not both. If both are specified, raise an error.
    if not new_cmd_format:
        if len(args) % 2 != 0:
            print(Commands.search.__doc__.strip())
            exit(EXIT_ERROR)

        searches_and_changes = izip(islice(argvish, 0, len(argvish), 2),
                                    islice(argvish, 1, len(argvish), 2))

        for search_value, change_value in searches_and_changes:
            devs = builder.search_devs(parse_search_value(search_value))
            change = {}
            ip = ''
            if change_value and change_value[0].isdigit():
                i = 1
                while (i < len(change_value) and
                       change_value[i] in '0123456789.'):
                    i += 1
                ip = change_value[:i]
                change_value = change_value[i:]
            elif change_value and change_value.startswith('['):
                i = 1
                while i < len(change_value) and change_value[i] != ']':
                    i += 1
                i += 1
                ip = change_value[:i].lstrip('[').rstrip(']')
                change_value = change_value[i:]
            if ip:
                change['ip'] = validate_and_normalize_ip(ip)
            if change_value.startswith(':'):
                i = 1
                while i < len(change_value) and change_value[i].isdigit():
                    i += 1
                change['port'] = int(change_value[1:i])
                change_value = change_value[i:]
            if change_value.startswith('R'):
                change_value = change_value[1:]
                replication_ip = ''
                if change_value and change_value[0].isdigit():
                    i = 1
                    while (i < len(change_value) and
                           change_value[i] in '0123456789.'):
                        i += 1
                    replication_ip = change_value[:i]
                    change_value = change_value[i:]
                elif change_value and change_value.startswith('['):
                    i = 1
                    while i < len(change_value) and change_value[i] != ']':
                        i += 1
                    i += 1
                    replication_ip = \
                        change_value[:i].lstrip('[').rstrip(']')
                    change_value = change_value[i:]
                if replication_ip:
                    change['replication_ip'] = \
                        validate_and_normalize_ip(replication_ip)
                if change_value.startswith(':'):
                    i = 1
                    while i < len(change_value) and change_value[i].isdigit():
                        i += 1
                    change['replication_port'] = int(change_value[1:i])
                    change_value = change_value[i:]
            if change_value.startswith('/'):
                i = 1
                while i < len(change_value) and change_value[i] != '_':
                    i += 1
                change['device'] = change_value[1:i]
                change_value = change_value[i:]
            if change_value.startswith('_'):
                change['meta'] = change_value[1:]
                change_value = ''
            if change_value or not change:
                raise ValueError('Invalid set info change value: %s' %
                                 repr(argvish[1]))
            _set_info_values(devs, change)
    else:
        devs = builder.search_devs(parse_search_values_from_opts(opts))
        change = parse_change_values_from_opts(opts)
        _set_info_values(devs, change)
Example #23
0
 def test_parse_search_value(self):
     res = parse_search_value('r0')
     self.assertEqual(res, {'region': 0})
     res = parse_search_value('r1')
     self.assertEqual(res, {'region': 1})
     res = parse_search_value('r1z2')
     self.assertEqual(res, {'region': 1, 'zone': 2})
     res = parse_search_value('d1')
     self.assertEqual(res, {'id': 1})
     res = parse_search_value('z1')
     self.assertEqual(res, {'zone': 1})
     res = parse_search_value('-127.0.0.1')
     self.assertEqual(res, {'ip': '127.0.0.1'})
     res = parse_search_value('127.0.0.1')
     self.assertEqual(res, {'ip': '127.0.0.1'})
     res = parse_search_value('-[127.0.0.1]:10001')
     self.assertEqual(res, {'ip': '127.0.0.1', 'port': 10001})
     res = parse_search_value(':10001')
     self.assertEqual(res, {'port': 10001})
     res = parse_search_value('R127.0.0.10')
     self.assertEqual(res, {'replication_ip': '127.0.0.10'})
     res = parse_search_value('R[127.0.0.10]:20000')
     self.assertEqual(res, {'replication_ip': '127.0.0.10',
                            'replication_port': 20000})
     res = parse_search_value('R:20000')
     self.assertEqual(res, {'replication_port': 20000})
     res = parse_search_value('/sdb1')
     self.assertEqual(res, {'device': 'sdb1'})
     res = parse_search_value('_meta1')
     self.assertEqual(res, {'meta': 'meta1'})
     self.assertRaises(ValueError, parse_search_value, 'OMGPONIES')
Example #24
0
 def test_parse_search_value(self):
     res = parse_search_value("r0")
     self.assertEqual(res, {"region": 0})
     res = parse_search_value("r1")
     self.assertEqual(res, {"region": 1})
     res = parse_search_value("r1z2")
     self.assertEqual(res, {"region": 1, "zone": 2})
     res = parse_search_value("d1")
     self.assertEqual(res, {"id": 1})
     res = parse_search_value("z1")
     self.assertEqual(res, {"zone": 1})
     res = parse_search_value("-127.0.0.1")
     self.assertEqual(res, {"ip": "127.0.0.1"})
     res = parse_search_value("-[127.0.0.1]:10001")
     self.assertEqual(res, {"ip": "127.0.0.1", "port": 10001})
     res = parse_search_value(":10001")
     self.assertEqual(res, {"port": 10001})
     res = parse_search_value("R127.0.0.10")
     self.assertEqual(res, {"replication_ip": "127.0.0.10"})
     res = parse_search_value("R[127.0.0.10]:20000")
     self.assertEqual(res, {"replication_ip": "127.0.0.10", "replication_port": 20000})
     res = parse_search_value("R:20000")
     self.assertEqual(res, {"replication_port": 20000})
     res = parse_search_value("/sdb1")
     self.assertEqual(res, {"device": "sdb1"})
     res = parse_search_value("_meta1")
     self.assertEqual(res, {"meta": "meta1"})
     self.assertRaises(ValueError, parse_search_value, "OMGPONIES")