示例#1
0
def import_to_db(reactor, key_type, value_type, input_format, input_file, etcd_address, dry_run,
                 dry_output, verbosity):
    db_current = yield get_all_keys(reactor, key_type, value_type, etcd_address)
    db_import = yield get_input_content(input_format, input_file, value_type)
    db_diff = yield get_db_diff(db_current, db_import, key_type, value_type)

    transaction = []

    if dry_run:
        pretty_print(db_diff, key_type, value_type, dry_output)
    else:
        for k, v in db_diff['to_update'].items():
            if not isinstance(k, bytes):
                k = k.encode()
            if not isinstance(v, bytes):
                v = v.encode()
            transaction.append(OpSet(k, v))

        for key in db_diff['to_delete']:
            if not isinstance(key, bytes):
                key = key.encode()
            transaction.append(OpDel(key))

        etcd = Client(reactor, etcd_address)
        yield etcd.submit(Transaction(success=transaction))
        if verbosity == 'verbose':
            pretty_print(db_diff, key_type, value_type, dry_output)
        elif verbosity == 'compact':
            print('{} updated.'.format(len(db_diff['to_update'])))
            print('{} deleted.'.format(len(db_diff['to_delete'])))
示例#2
0
    def _reserve(self, key, value, **kw):
        for name, val in kw.items():
            if name == 'ttl':
                ttl = val
                break
        reserved = False
        err = 'reservation-failed'
        owner = None

        # Create a lease
        lease = yield self.client.lease(ttl)

        # Create a transaction
        txn = Transaction(compare=[CompVersion(key, '==', 0)],
                          success=[OpSet(key, bytes(value), lease=lease)],
                          failure=[OpGet(key)])
        newly_acquired = False
        try:
            result = yield self.client.submit(txn)
        except Failed as failed:
            log.debug('key-already-present', key=key)
            if len(failed.responses) > 0:
                response = failed.responses[0]
                if response.kvs is not None and len(response.kvs) > 0:
                    kv = response.kvs[0]
                    log.debug('key-already-present', value=kv.value)
                    if kv.value == value:
                        reserved = True
                        log.debug('key-already-reserved',
                                  key=kv.key,
                                  value=kv.value)
        else:
            newly_acquired = True
            log.debug('key-was-absent', key=key, result=result)

        # Check if reservation succeeded
        resp = yield self.client.get(key)
        if resp.kvs is not None and len(resp.kvs) == 1:
            owner = resp.kvs[0].value
            if owner == value:
                if newly_acquired:
                    log.debug('key-reserved',
                              key=key,
                              value=value,
                              ttl=ttl,
                              lease_id=lease.lease_id)
                    reserved = True
                    # Add key to reservation list
                    self.key_reservations[key] = lease
                else:
                    log.debug("reservation-still-held")
            else:
                log.debug('reservation-held-by-another', value=owner)

        if reserved:
            err = None
        returnValue((owner, err))
示例#3
0
def main(reactor):

    etcd = Client(reactor)

    #
    # Example 1
    #
    for val in [b'val1', b'val2']:

        yield etcd.set(b'test1', val)

        txn = Transaction(
            compare=[
                # compute conjunction of all terms to
                # determine "success" vs "failure"
                CompValue(b'test1', '==', b'val1')
            ],
            success=[
                # if true ("success"), run these ops
                OpSet(b'test1', b'val2'),
                OpSet(b'test2', b'success')
            ],
            failure=[
                # if not true ("failure"), run these ops
                OpSet(b'test2', b'failure'),
                OpGet(b'test1')
            ])

        try:
            result = yield etcd.submit(txn)
        except Failed as failed:
            print('transaction FAILED:')
            for response in failed.responses:
                print(response)
        else:
            print('transaction SUCCESS:')
            for response in result.responses:
                print(response)

        for key in [b'test1', b'test2']:
            value = yield etcd.get(key)
            print('{}: {}'.format(key, value))

    #
    # Example 2
    #
    rev = yield etcd.set(b'mykey1', os.urandom(8))
    print(rev)

    result = yield etcd.get(b'mykey1')
    kv = result.kvs[0]
    print(kv)

    for version in [kv.version, kv.version - 1]:
        txn = Transaction(
            compare=[
                # value equality comparison
                CompValue(b'mykey1', '==', kv.value),

                # version, and different comparison operators
                CompVersion(b'mykey1', '==', version),
                CompVersion(b'mykey1', '!=', version + 1),
                CompVersion(b'mykey1', '<', version + 1),
                CompVersion(b'mykey1', '>', version - 1),

                # created revision comparison
                CompCreated(b'mykey1', '==', kv.create_revision),

                # modified revision comparison
                CompModified(b'mykey1', '==', kv.mod_revision),
            ],
            success=[
                OpSet(b'mykey2', b'success'),
                OpSet(b'mykey3', os.urandom(8))
            ],
            failure=[
                OpSet(b'mykey2', b'failure'),
                OpSet(b'mykey3', os.urandom(8))
            ])

        try:
            result = yield etcd.submit(txn)
        except Failed as failed:
            print('transaction FAILED:')
            for response in failed.responses:
                print(response)
        else:
            print('transaction SUCCESS:')
            for response in result.responses:
                print(response)

        result = yield etcd.get(KeySet(b'mykey2', b'mykey4'))
        for kv in result.kvs:
            print(kv)