Esempio n. 1
0
File: cli.py Progetto: 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()
Esempio 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()
Esempio 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()
Esempio 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()
 def byte_test(self):
     self.assertEqual(utils.maybe_encode(b"bar"), b"bar")
 def str_test(self):
     self.assertEqual(utils.maybe_encode("foo"), b"foo")
Esempio n. 7
0
 def byte_test(self):
     self.assertEqual(utils.maybe_encode(b'bar'), b'bar')
Esempio n. 8
0
 def str_test(self):
     self.assertEqual(utils.maybe_encode('foo'), b'foo')
Esempio n. 9
0
 def byte_test(self):
     self.assertEqual(utils.maybe_encode(b'bar'), b'bar')
Esempio n. 10
0
 def str_test(self):
     self.assertEqual(utils.maybe_encode('foo'), b'foo')