Ejemplo n.º 1
0
Archivo: cli.py Proyecto: 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()
Ejemplo n.º 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()
Ejemplo n.º 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()
Ejemplo n.º 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()
Ejemplo n.º 5
0
 def byte_test(self):
     self.assertEqual(utils.maybe_encode(b"bar"), b"bar")
Ejemplo n.º 6
0
 def str_test(self):
     self.assertEqual(utils.maybe_encode("foo"), b"foo")
Ejemplo n.º 7
0
 def byte_test(self):
     self.assertEqual(utils.maybe_encode(b'bar'), b'bar')
Ejemplo n.º 8
0
 def str_test(self):
     self.assertEqual(utils.maybe_encode('foo'), b'foo')
Ejemplo n.º 9
0
 def byte_test(self):
     self.assertEqual(utils.maybe_encode(b'bar'), b'bar')
Ejemplo n.º 10
0
 def str_test(self):
     self.assertEqual(utils.maybe_encode('foo'), b'foo')