예제 #1
0
파일: cli.py 프로젝트: 40a/consulate
def kv_backup(consul, args):
    """Backup the Consul KV database

    :param consulate.api_old.Consul consul: The Consul instance
    :param argparser.namespace args: The cli args

    """
    handle = open(args.file, 'w') if args.file else sys.stdout
    if args.key:
        args.key = args.key.strip('/')
        prefixlen = len(args.key.split('/'))
        records = [('/'.join(k.split('/')[prefixlen:]), f, v)
                   for k, f, v in consul.kv.records(args.key)]
    else:
        records = consul.kv.records()
    if args.base64:
        if utils.PYTHON3:
            records = [(k, f, str(base64.b64encode(utils.maybe_encode(v)),
                                  'ascii'))
                       for k, f, v in records]
        else:
            records = [(k, f, base64.b64encode(v) if v else v)
                       for k, f, v in records]
    try:
        if args.pretty:
            handle.write(json.dumps(records, sort_keys=True, indent=2,
                                    separators=(',', ': ')) + '\n')
        else:
            handle.write(json.dumps(records) + '\n')
    except exceptions.ConnectionError:
        connection_error()
예제 #2
0
def kv_backup(consul, args):
    """Backup the Consul KV database

    :param consulate.api_old.Consul consul: The Consul instance
    :param argparser.namespace args: The cli args

    """
    handle = open(args.file, 'w') if args.file else sys.stdout
    if args.key:
        args.key = args.key.strip('/')
        prefixlen = len(args.key.split('/'))
        records = [('/'.join(k.split('/')[prefixlen:]), f, v)
                   for k, f, v in consul.kv.records(args.key)]
    else:
        records = consul.kv.records()
    if args.base64:
        if utils.PYTHON3:
            records = [(k, f,
                        str(base64.b64encode(utils.maybe_encode(v)), 'ascii'))
                       for k, f, v in records]
        else:
            records = [(k, f, base64.b64encode(v) if v else v)
                       for k, f, v in records]
    try:
        if args.pretty:
            handle.write(
                json.dumps(
                    records, sort_keys=True, indent=2, separators=(',',
                                                                   ': ')) +
                '\n')
        else:
            handle.write(json.dumps(records) + '\n')
    except exceptions.ConnectionError:
        connection_error()
예제 #3
0
def kv_backup(consul, args):
    """Backup the Consul KV database

    :param consulate.api_old.Consul consul: The Consul instance
    :param argparser.namespace args: The cli args

    """
    handle = open(args.file, "w") if args.file else sys.stdout
    records = consul.kv.records()
    if args.base64:
        if utils.PYTHON3:
            records = [(k, f, str(base64.b64encode(utils.maybe_encode(v)), "ascii")) for k, f, v in records]
        else:
            records = [(k, f, base64.b64encode(v) if v else v) for k, f, v in records]
    try:
        handle.write(json.dumps(records) + "\n")
    except exceptions.ConnectionError:
        connection_error()
예제 #4
0
def kv_backup(consul, args):
    """Backup the Consul KV database

    :param consulate.api_old.Consul consul: The Consul instance
    :param argparser.namespace args: The cli args

    """
    handle = open(args.file, 'w') if args.file else sys.stdout
    records = consul.kv.records()
    if args.base64:
        if utils.PYTHON3:
            records = [(k, f, str(base64.b64encode(utils.maybe_encode(v)),
                                  'ascii'))
                       for k,f,v in records]
        else:
            records = [(k, f, base64.b64encode(v) if v else v) for k,f,v in records]
    try:
        handle.write(json.dumps(records) + '\n')
    except exceptions.ConnectionError:
        connection_error()
예제 #5
0
 def byte_test(self):
     self.assertEqual(utils.maybe_encode(b"bar"), b"bar")
예제 #6
0
 def str_test(self):
     self.assertEqual(utils.maybe_encode("foo"), b"foo")
예제 #7
0
 def byte_test(self):
     self.assertEqual(utils.maybe_encode(b'bar'), b'bar')
예제 #8
0
 def str_test(self):
     self.assertEqual(utils.maybe_encode('foo'), b'foo')
예제 #9
0
 def byte_test(self):
     self.assertEqual(utils.maybe_encode(b'bar'), b'bar')
예제 #10
0
 def str_test(self):
     self.assertEqual(utils.maybe_encode('foo'), b'foo')