Python consul.Consul() Examples

The following are 30 code examples of consul.Consul(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module consul , or try the search function .
Example #1
Source File: test_std_acl.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_acl_operator_area(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        payload = {
            "PeerDatacenter": "dc1",
            "RetryJoin": ["10.1.2.3", "10.1.2.4", "10.1.2.5"],
            "UseTLS": False
        }
        area1 = c.operator.area.create(payload)
        c.operator.area.get(area1['ID'])
        payload["PeerDatacenter"] = "dc1"
        c.operator.area.update(payload, area1['ID'])
        c.operator.area.list()
        payload2 = ["10.1.2.3", "10.1.2.4", "10.1.2.5"]
        c.operator.area.join(payload2, area1['ID'])
        c.operator.area.members()
        c.operator.area.delete(area1['ID'])
        c.operator.area.list() 
Example #2
Source File: utils.py    From opencraft with GNU Affero General Public License v3.0 6 votes vote down vote up
def get(self, key, index=False, **kwargs):
        """
        Get's a key value from Consul's Key-Value store after casting it to
        the proper identified data-type.

        :param key: The key its value to be fetched
        :param index: If True then the return value will be a tuple of (index, value)
                      where index is the current Consul index, suitable for making subsequent
                      calls to wait for changes since this query was last run.
        :param kwargs: Consul.kv.get specific options
        :return: The value or the the tuple of (index, value) of the specified key.
        """
        key = self.prefix + key
        data_index, data = self._client.kv.get(key, **kwargs)

        stored_value = data['Value'] if data else None
        value = self._cast_value(stored_value)

        if index:
            return data_index, value

        return value 
Example #3
Source File: test_std.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_kv_acquire_release(self, consul_port):
        c = consul.Consul(port=consul_port)

        pytest.raises(
            consul.ConsulException, c.kv.put, 'foo', 'bar', acquire='foo')

        s1 = c.session.create()
        s2 = c.session.create()

        assert c.kv.put('foo', '1', acquire=s1) is True
        assert c.kv.put('foo', '2', acquire=s2) is False
        assert c.kv.put('foo', '1', acquire=s1) is True
        assert c.kv.put('foo', '1', release='foo') is False
        assert c.kv.put('foo', '2', release=s2) is False
        assert c.kv.put('foo', '2', release=s1) is True

        c.session.destroy(s1)
        c.session.destroy(s2) 
Example #4
Source File: chronograph.py    From openprocurement.auction with Apache License 2.0 6 votes vote down vote up
def __init__(self, server_name, config,
                 limit_auctions=500,
                 limit_free_memory=0.15,
                 logger=getLogger(__name__),
                 *args, **kwargs):
        super(AuctionScheduler, self).__init__(*args, **kwargs)
        self.server_name = server_name
        self.config = config
        self.execution_stopped = False
        self.use_consul = self.config.get('main', {}).get('use_consul', True)
        if self.use_consul:
            self.consul = consul.Consul()
        self.logger = logger
        self._limit_pool_lock = self._create_lock()
        self._limit_auctions = self.config['main'].get('limit_auctions',
                                                       int(limit_auctions))
        self._limit_free_memory = self.config['main'].get(
            'limit_free_memory', float(limit_free_memory)
        )
        self._count_auctions = 0
        self.exit = False
        self.processes = {} 
Example #5
Source File: test_std.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_kv_encoding(self, consul_port):
        c = consul.Consul(port=consul_port)

        # test binary
        c.kv.put('foo', struct.pack('i', 1000))
        index, data = c.kv.get('foo')
        assert struct.unpack('i', data['Value']) == (1000,)

        # test unicode
        c.kv.put('foo', u'bar')
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('bar')

        # test empty-string comes back as `None`
        c.kv.put('foo', '')
        index, data = c.kv.get('foo')
        assert data['Value'] is None

        # test None
        c.kv.put('foo', None)
        index, data = c.kv.get('foo')
        assert data['Value'] is None

        # check unencoded values raises assert * Python3 don't need
        # pytest.raises(AssertionError, c.kv.put, 'foo', {1: 2}) 
Example #6
Source File: test_std.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_service_dereg_issue_156(self, consul_port):
        # https://github.com/cablehead/python-consul/issues/156
        service_name = 'app#127.0.0.1#3000'
        c = consul.Consul(port=consul_port)
        c.agent.service.register(service_name)

        time.sleep(80 / 1000.0)

        index, nodes = c.health.service(service_name)
        assert [node['Service']['ID'] for node in nodes] == [service_name]

        # Clean up tasks
        assert c.agent.service.deregister(service_name) is True

        time.sleep(40 / 1000.0)

        index, nodes = c.health.service(service_name)
        assert [node['Service']['ID'] for node in nodes] == [] 
Example #7
Source File: test_std_token.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_agent_connect(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        root_ca = c.agent.connect.root_certificates()
        assert root_ca['Roots'][0]['Name'] == 'Consul CA Root Cert'
        # assert root_ca['Roots'][0]['Active'] fixme
        c.agent.service.register('web')
        c.agent.service.register('db')
        c.connect.intentions.create(source_name='web',
                                    source_type='consul',
                                    destination_name='db',
                                    action='allow')
        authorize_ca = c.agent.connect.authorize('db',
                                                 'spiffe://' +
                                                 root_ca['TrustDomain'] +
                                                 '/ns/default/dc'
                                                 '/dc1/svc/web',
                                                 root_ca['ActiveRootID'])
        assert authorize_ca['Authorized']
        db_ca = c.agent.connect.leaf_certificates('web')
        # fixme why none?
        assert db_ca == (None, None) 
Example #8
Source File: test_std_token.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_config(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        payload = {
            "Kind": "service-defaults",
            "Name": "web",
            "Protocol": "http",
        }
        assert c.config.put(payload)

        config = c.config.get(kind=payload['Kind'], name=payload['Name'])
        assert config['Kind'] == 'service-defaults'
        assert config['Name'] == 'web'
        assert config['Protocol'] == 'http'

        configs = c.config.list(kind=payload['Kind'])
        assert configs[0]['Kind'] == 'service-defaults'
        assert configs[0]['Name'] == 'web'
        assert configs[0]['Protocol'] == 'http'

        isdel = c.config.delete(kind=payload['Kind'], name=payload['Name'])

        assert isdel
        config = c.config.get(kind=payload['Kind'], name=payload['Name'])
        assert config[1] is None 
Example #9
Source File: test_std.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_agent_register_check_no_service_id(self, consul_port):
        c = consul.Consul(port=consul_port)
        index, nodes = c.health.service("foo1")
        assert nodes == []

        pytest.raises(consul.std.base.ConsulException,
                      c.agent.check.register,
                      'foo', Check.ttl('100ms'),
                      service_id='foo1')

        time.sleep(40 / 1000.0)

        assert c.agent.checks() == {}

        # Cleanup tasks
        c.agent.check.deregister('foo')

        time.sleep(40 / 1000.0) 
Example #10
Source File: test_std_token.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_session_delete_ttl_renew(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)

        s = c.session.create(behavior='delete', ttl=20)

        # attempt to renew an unknown session
        pytest.raises(consul.NotFound, c.session.renew, '1' * 36)

        session = c.session.renew(s)
        assert session['Behavior'] == 'delete'
        assert session['TTL'] == '20s'

        # trying out the behavior
        assert c.kv.put('foo', '1', acquire=s) is True
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('1')

        c.session.destroy(s)
        index, data = c.kv.get('foo')
        assert data is None 
Example #11
Source File: test_std.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_session_delete_ttl_renew(self, consul_port):
        c = consul.Consul(port=consul_port)

        s = c.session.create(behavior='delete', ttl=20)

        # attempt to renew an unknown session
        pytest.raises(consul.NotFound, c.session.renew, '1' * 36)

        session = c.session.renew(s)
        assert session['Behavior'] == 'delete'
        assert session['TTL'] == '20s'

        # trying out the behavior
        assert c.kv.put('foo', '1', acquire=s) is True
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('1')

        c.session.destroy(s)
        index, data = c.kv.get('foo')
        assert data is None 
Example #12
Source File: test_std_token.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_kv_recurse(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        index, data = c.kv.get('foo/', recurse=True)
        assert data is None

        c.kv.put('foo/', None)
        index, data = c.kv.get('foo/', recurse=True)
        assert len(data) == 1

        c.kv.put('foo/bar1', '1')
        c.kv.put('foo/bar2', '2')
        c.kv.put('foo/bar3', '3')
        index, data = c.kv.get('foo/', recurse=True)
        assert [x['Key'] for x in data] == [
            'foo/', 'foo/bar1', 'foo/bar2', 'foo/bar3']
        assert [x['Value'] for x in data] == [
            None, six.b('1'), six.b('2'), six.b('3')]
        c.kv.delete('foo')
        c.kv.delete('foo/')
        c.kv.delete('foo/bar1')
        c.kv.delete('foo/bar2')
        c.kv.delete('foo/bar3') 
Example #13
Source File: test_std_token.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_kv_acquire_release(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)

        pytest.raises(
            consul.ConsulException, c.kv.put, 'foo', 'bar', acquire='foo')

        s1 = c.session.create()
        s2 = c.session.create()

        assert c.kv.put('foo', '1', acquire=s1) is True
        assert c.kv.put('foo', '2', acquire=s2) is False
        assert c.kv.put('foo', '1', acquire=s1) is True
        assert c.kv.put('foo', '1', release='foo') is False
        assert c.kv.put('foo', '2', release=s2) is False
        assert c.kv.put('foo', '2', release=s1) is True

        c.session.destroy(s1)
        c.session.destroy(s2) 
Example #14
Source File: test_std_acl.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_acl_translate(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)

        payload = """
        agent "" {
            policy = "write"
        }
        """

        translate = c.acl.create_translate(
            payload=payload, token=acl_consul.token)
        assert translate == b'agent_prefix "" {\n  policy = "write"\n}'

        # fixme
        pytest.raises(consul.ConsulException,
                      c.acl.get_translate,
                      c.acl.self()['AccessorID'],
                      acl_consul.token) 
Example #15
Source File: test_std_token.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_service_dereg_issue_156(self, acl_consul):
        # https://github.com/cablehead/python-consul/issues/156
        service_name = 'app#127.0.0.1#3000'
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        c.agent.service.register(service_name)

        time.sleep(80 / 1000.0)

        index, nodes = c.health.service(service_name)
        assert [node['Service']['ID'] for node in nodes] == [service_name]

        # Clean up tasks
        assert c.agent.service.deregister(service_name) is True

        time.sleep(40 / 1000.0)

        index, nodes = c.health.service(service_name)
        assert [node['Service']['ID'] for node in nodes] == [] 
Example #16
Source File: test_std.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_operator_autopilot(self, consul_port):
        time.sleep(2)  # http code 429 Too Many Requests
        c = consul.Consul(port=consul_port)
        assert c.operator.autopilot.configuration()['MaxTrailingLogs'] == 250
        assert not c.operator.autopilot.health()['FailureTolerance']
        payload = {
            "CleanupDeadServers": True,
            "LastContactThreshold": "200ms",
            "MaxTrailingLogs": 251,
            "ServerStabilizationTime": "10s",
            "RedundancyZoneTag": "",
            "DisableUpgradeMigration": False,
            "UpgradeVersionTag": "",
            "CreateIndex": 4,
            "ModifyIndex": 4
        }
        assert c.operator.autopilot.update(payload)

        config = c.operator.autopilot.configuration()

        assert config['MaxTrailingLogs'] == 251 
Example #17
Source File: test_std.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_health_checks(self, consul_port):
        c = consul.Consul(port=consul_port)

        c.agent.service.register(
            'foobar', service_id='foobar', check=Check.ttl('10s'))

        time.sleep(40 / 1000.00)

        index, checks = c.health.checks('foobar')

        assert [check['ServiceID'] for check in checks] == ['foobar']
        assert [check['CheckID'] for check in checks] == ['service:foobar']

        c.agent.service.deregister('foobar')

        time.sleep(40 / 1000.0)

        index, checks = c.health.checks('foobar')
        assert len(checks) == 0 
Example #18
Source File: test_std.py    From python-consul2 with MIT License 6 votes vote down vote up
def test_agent_node_maintenance(self, consul_port):
        c = consul.Consul(port=consul_port)

        c.agent.maintenance('true', "test")

        time.sleep(40 / 1000.0)

        checks_pre = c.agent.checks()
        assert '_node_maintenance' in checks_pre.keys()
        assert 'test' == checks_pre['_node_maintenance']['Notes']

        c.agent.maintenance('false')

        time.sleep(40 / 1000.0)

        checks_post = c.agent.checks()
        assert '_node_maintenance' not in checks_post.keys() 
Example #19
Source File: chronograph_http.py    From openprocurement.auction with Apache License 2.0 5 votes vote down vote up
def get_active_locks():
    client = Consul()
    return dumps(client.kv.get('auction_', recurse=True)[1]) 
Example #20
Source File: grpc_client.py    From xos with Apache License 2.0 5 votes vote down vote up
def _get_endpoint_from_consul(self, service_name):
        """
        Look up an appropriate grpc endpoint (host, port) from
        consul, under the service name specified by service-name
        """
        host = self.consul_endpoint.split(':')[0].strip()
        port = int(self.consul_endpoint.split(':')[1].strip())

        while True:
            log.debug('consul-lookup', host=host, port=port)
            consul = Consul(host=host, port=port)
            _, services = consul.catalog.service(service_name)
            log.debug('consul-response', services=services)
            if services:
                break
            log.warning('no-service', consul_host=host, consul_port=port,
                        service_name=service_name)
            yield asleep(1.0)

        # pick local addresses when resolving a service via consul
        # see CORD-815 (https://jira.opencord.org/browse/CORD-815)

        service = services[randint(0, len(services) - 1)]
        endpoint = '{}:{}'.format(service['ServiceAddress'],
                                  service['ServicePort'])
        returnValue(endpoint) 
Example #21
Source File: test_std_token.py    From python-consul2 with MIT License 5 votes vote down vote up
def test_agent_checks_service_id(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        c.agent.service.register('foo1')

        time.sleep(40 / 1000.0)

        index, nodes = c.health.service('foo1')
        assert [node['Service']['ID'] for node in nodes] == ['foo1']

        c.agent.check.register('foo', Check.ttl('100ms'), service_id='foo1')

        time.sleep(40 / 1000.0)

        index, nodes = c.health.service('foo1')
        assert set([
            check['ServiceID'] for node in nodes
            for check in node['Checks']]) == {'foo1', ''}
        assert set([
            check['CheckID'] for node in nodes
            for check in node['Checks']]) == {'foo', 'serfHealth'}

        # Clean up tasks
        assert c.agent.check.deregister('foo') is True

        time.sleep(40 / 1000.0)

        assert c.agent.service.deregister('foo1') is True

        time.sleep(40 / 1000.0) 
Example #22
Source File: test_std_token.py    From python-consul2 with MIT License 5 votes vote down vote up
def test_kv_delete_cas(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)

        c.kv.put('foo', 'bar')
        index, data = c.kv.get('foo')

        assert c.kv.delete('foo', cas=data['ModifyIndex'] - 1) is False
        assert c.kv.get('foo') == (index, data)

        assert c.kv.delete('foo', cas=data['ModifyIndex']) is True
        index, data = c.kv.get('foo')
        assert data is None 
Example #23
Source File: test_std_token.py    From python-consul2 with MIT License 5 votes vote down vote up
def test_event(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)

        assert c.event.fire("fooname", "foobody")
        index, events = c.event.list()
        assert [x['Name'] == 'fooname' for x in events]
        assert [x['Payload'] == 'foobody' for x in events] 
Example #24
Source File: test_std_token.py    From python-consul2 with MIT License 5 votes vote down vote up
def test_transaction(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        value = base64.b64encode(b"1").decode("utf8")
        d = {"KV": {"Verb": "set", "Key": "asdf", "Value": value}}
        r = c.txn.put([d])
        assert r["Errors"] is None

        d = {"KV": {"Verb": "get", "Key": "asdf"}}
        r = c.txn.put([d])
        assert r["Results"][0]["KV"]["Value"] == value 
Example #25
Source File: test_std_token.py    From python-consul2 with MIT License 5 votes vote down vote up
def test_kv_put_flags(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        c.kv.put('foo', 'bar')
        index, data = c.kv.get('foo')
        assert data['Flags'] == 0

        assert c.kv.put('foo', 'bar', flags=50) is True
        index, data = c.kv.get('foo')
        assert data['Flags'] == 50 
Example #26
Source File: utils.py    From opencraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def put(self, key, value, **kwargs):
        """
        Will put the given value of the key/prefixed-key in Consul's Key-Value
        store. It'll dump lists and dictionaries first before storing them
        :param key: The key its value to be updated.
        :param value: The value given to the specified key.
        :param kwargs: Consul.kv.put specific options
        :return: Either True or False. If False is returned, then the update has not taken place.
        """
        consul_key = self.prefix + key
        value = json.dumps(value) if self._is_json_serializable(value) else str(value)

        return self._client.kv.put(consul_key, value, **kwargs) 
Example #27
Source File: test_std_token.py    From python-consul2 with MIT License 5 votes vote down vote up
def test_kv_encoding(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)

        # test binary
        c.kv.put('foo', struct.pack('i', 1000))
        index, data = c.kv.get('foo')
        assert struct.unpack('i', data['Value']) == (1000,)

        # test unicode
        c.kv.put('foo', u'bar')
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('bar')

        # test empty-string comes back as `None`
        c.kv.put('foo', '')
        index, data = c.kv.get('foo')
        assert data['Value'] is None

        # test None
        c.kv.put('foo', None)
        index, data = c.kv.get('foo')
        assert data['Value'] is None

        c.kv.delete('foo')
        # check unencoded values raises assert * Python3 don't need
        # pytest.raises(AssertionError, c.kv.put, 'foo', {1: 2}) 
Example #28
Source File: test_std_token.py    From python-consul2 with MIT License 5 votes vote down vote up
def test_kv_put_cas(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
        assert c.kv.put('foo', 'bar', cas=50) is False
        assert c.kv.put('foo', 'bar', cas=0) is True
        index, data = c.kv.get('foo')

        assert c.kv.put('foo', 'bar2', cas=data['ModifyIndex'] - 1) is False
        assert c.kv.put('foo', 'bar2', cas=data['ModifyIndex']) is True
        index, data = c.kv.get('foo')
        assert data['Value'] == six.b('bar2') 
Example #29
Source File: test_std_token.py    From python-consul2 with MIT License 5 votes vote down vote up
def test_kv_keys_only(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)

        assert c.kv.put('bar', '4') is True
        assert c.kv.put('base/foo', '1') is True
        assert c.kv.put('base/base/foo', '5') is True

        index, data = c.kv.get('base/', keys=True, separator='/')
        assert data == ['base/base/', 'base/foo'] 
Example #30
Source File: test_std_token.py    From python-consul2 with MIT License 5 votes vote down vote up
def test_agent_service_maintenance(self, acl_consul):
        c = consul.Consul(port=acl_consul.port, token=acl_consul.token)

        c.agent.service.register('foo', check=Check.ttl('100ms'))

        time.sleep(40 / 1000.0)

        c.agent.service.maintenance('foo', 'true', "test")

        time.sleep(40 / 1000.0)

        checks_pre = c.agent.checks()
        assert '_service_maintenance:foo' in checks_pre.keys()
        assert 'test' == checks_pre['_service_maintenance:foo']['Notes']

        c.agent.service.maintenance('foo', 'false')

        time.sleep(40 / 1000.0)

        checks_post = c.agent.checks()
        assert '_service_maintenance:foo' not in checks_post.keys()

        # Cleanup
        c.agent.service.deregister('foo')

        time.sleep(40 / 1000.0)