def __init__(self, addr=DEFAULT_ADDR, host=DEFAULT_HOST, port=DEFAULT_PORT, datacenter=None, token=DEFAULT_TOKEN, scheme=DEFAULT_SCHEME, adapter=None, verify=True, cert=None, timeout=None): """Create a new instance of the Consul class""" base_uri = self._base_uri(addr=addr, scheme=scheme, host=host, port=port) self._adapter = adapter() if adapter else adapters.Request( timeout=timeout, verify=verify, cert=cert) self._acl = api.ACL(base_uri, self._adapter, datacenter, token) self._agent = api.Agent(base_uri, self._adapter, datacenter, token) self._catalog = api.Catalog(base_uri, self._adapter, datacenter, token) self._event = api.Event(base_uri, self._adapter, datacenter, token) self._health = api.Health(base_uri, self._adapter, datacenter, token) self._coordinate = api.Coordinate(base_uri, self._adapter, datacenter, token) self._kv = api.KV(base_uri, self._adapter, datacenter, token) self._session = api.Session(base_uri, self._adapter, datacenter, token) self._status = api.Status(base_uri, self._adapter, datacenter, token) self._lock = api.Lock(base_uri, self._adapter, self._session, datacenter, token)
def __init__(self, host=DEFAULT_HOST, port=DEFAULT_PORT, datacenter=None, token=None, scheme=DEFAULT_SCHEME, adapter=None): """Create a new instance of the Consul class""" base_uri = self._base_uri(scheme, host, port) self._adapter = adapter() if adapter else adapters.Request() self._acl = api.ACL(base_uri, self._adapter, datacenter, token) self._agent = api.Agent(base_uri, self._adapter, datacenter, token) self._catalog = api.Catalog(base_uri, self._adapter, datacenter, token) self._event = api.Event(base_uri, self._adapter, datacenter, token) self._health = api.Health(base_uri, self._adapter, datacenter, token) self._kv = api.KV(base_uri, self._adapter, datacenter, token) self._session = api.Session(base_uri, self._adapter, datacenter, token) self._status = api.Status(base_uri, self._adapter, datacenter, token)
def setUp(self, status, health, kv, catalog, agent, adapter): self.host = '127.0.0.1' self.port = 8500 self.dc = str(uuid.uuid4()) self.token = str(uuid.uuid4()) self.acl = None self.adapter = adapter self.agent = agent self.catalog = catalog self.events = None self.kv = kv self.health = health self.status = status self.base_uri = '{0}://{1}:{2}/v1'.format(api.Session.SCHEME, self.host, self.port) self.session = api.Session(self.host, self.port, self.dc, self.token)
def main(): args = parse_cli_args() session = api.Session(args.api_host, args.api_port, args.datacenter, args.token) if args.command == 'register': check = args.path if args.ctype == 'check' else None interval = '%ss' % args.interval if args.ctype == 'check' else None ttl = '%ss' % args.duration if args.ctype == 'ttl' else None tags = args.tags.split(',') if args.tags else None session.agent.service.register(args.name, args.service_id, args.port, tags, check, interval, ttl) elif args.command == 'kv': if args.action == 'backup': with open(args.backup_file, 'wb') as handle: handle.write( json.dumps(session.kv.records(), ensure_ascii=False, indent=2)) elif args.action == 'restore': with open(args.restore_file, 'rb') as handle: data = json.load(handle) for row in data: session.kv.set_record(row[0], row[1], row[2].encode('utf-8')) elif args.action == 'del': del session.kv[args.key] elif args.action == 'get': sys.stdout.write("%s\n" % session.kv.get(args.key)) elif args.action == 'set': session.kv[args.key] = args.value