mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
Fixes consul.agent_service_register which was broken for registering
service checks. The function "agent_service_register" in the consul.py module failed to register service health checks and did not follow the consul.io documentation for doing so. This patch fixes the code to successfully register service checks, check for dict keys case-insensitive (because the absense of keys with the correct case would fail silently), and more closely follow the documentation at consul.io, see: https://www.consul.io/docs/agent/http/agent.html#agent_service_register Here is an example salt state for registering a service and a couple of health checks, similar to the example in the consul.io documents: example-service-registration: module.run: - name: consul.agent_service_register - consul_url: "http://localhost:8500" - kwargs: id: redis1 name: "redis" tags: [master, v1] address: "127.0.0.1" port: 8000 EnableTagOverride: false check: script: "/usr/local/bin/check_redis.py" http: "http://localhost:5000/health" interval: "15s" NOTE: the saltstack documentation needs to be updated. It wasn't correct anyway.
This commit is contained in:
parent
d9c20c0456
commit
f16a30786b
1 changed files with 42 additions and 27 deletions
|
@ -12,6 +12,7 @@ from __future__ import absolute_import
|
|||
# Import 3rd-party libs
|
||||
# pylint: disable=import-error,no-name-in-module,redefined-builtin
|
||||
from salt.ext.six.moves.urllib.parse import urljoin as _urljoin
|
||||
from salt.ext.six import iteritems
|
||||
import salt.ext.six.moves.http_client
|
||||
# pylint: enable=import-error,no-name-in-module
|
||||
|
||||
|
@ -973,48 +974,62 @@ def agent_service_register(consul_url=None, **kwargs):
|
|||
ret['res'] = False
|
||||
return ret
|
||||
|
||||
if 'name' in kwargs:
|
||||
data['Name'] = kwargs['name']
|
||||
lc_kwargs = dict()
|
||||
for k, v in iteritems(kwargs):
|
||||
lc_kwargs[k.lower()] = v
|
||||
|
||||
if 'name' in lc_kwargs:
|
||||
data['Name'] = lc_kwargs['name']
|
||||
else:
|
||||
raise SaltInvocationError('Required argument "name" is missing.')
|
||||
|
||||
if 'address' in kwargs:
|
||||
data['Address'] = kwargs['address']
|
||||
if 'address' in lc_kwargs:
|
||||
data['Address'] = lc_kwargs['address']
|
||||
|
||||
if 'port' in kwargs:
|
||||
data['Port'] = kwargs['port']
|
||||
if 'port' in lc_kwargs:
|
||||
data['Port'] = lc_kwargs['port']
|
||||
|
||||
if 'id' in kwargs:
|
||||
data['ID'] = kwargs['id']
|
||||
if 'id' in lc_kwargs:
|
||||
data['ID'] = lc_kwargs['id']
|
||||
|
||||
if 'tags' in kwargs:
|
||||
_tags = kwargs['tags']
|
||||
if 'tags' in lc_kwargs:
|
||||
_tags = lc_kwargs['tags']
|
||||
if not isinstance(_tags, list):
|
||||
_tags = [_tags]
|
||||
data['Tags'] = _tags
|
||||
|
||||
check_elements = ('check_script', 'check_http', 'check_ttl')
|
||||
if True in [True for item in check_elements if item in kwargs]:
|
||||
data['Check'] = {}
|
||||
if 'enabletagoverride' in lc_kwargs:
|
||||
data['EnableTagOverride'] = lc_kwargs['enabletagoverride']
|
||||
|
||||
if 'check_script' in kwargs:
|
||||
if 'interval' not in kwargs:
|
||||
if 'check' in lc_kwargs:
|
||||
dd = dict()
|
||||
for k, v in iteritems(lc_kwargs['check']):
|
||||
dd[k.lower()] = v
|
||||
interval_required = False
|
||||
check_dd = dict()
|
||||
|
||||
if 'script' in dd:
|
||||
interval_required = True
|
||||
check_dd['Script'] = dd['script']
|
||||
if 'http' in dd:
|
||||
interval_required = True
|
||||
check_dd['HTTP'] = dd['http']
|
||||
if 'ttl' in dd:
|
||||
check_dd['TTL'] = dd['ttl']
|
||||
if 'interval' in dd:
|
||||
check_dd['Interval'] = dd['interval']
|
||||
|
||||
if interval_required:
|
||||
if 'Interval' not in check_dd:
|
||||
ret['message'] = 'Required parameter "interval" is missing.'
|
||||
ret['res'] = False
|
||||
return ret
|
||||
data['Check']['Script'] = kwargs['check_script']
|
||||
data['Check']['Interval'] = kwargs['check_interval']
|
||||
else:
|
||||
if 'Interval' in check_dd:
|
||||
del check_dd['Interval'] # not required, so ignore it
|
||||
|
||||
if 'check_ttl' in kwargs:
|
||||
data['Check']['TTL'] = kwargs['check_ttl']
|
||||
|
||||
if 'check_http' in kwargs:
|
||||
if 'interval' not in kwargs:
|
||||
ret['message'] = 'Required parameter "interval" is missing.'
|
||||
ret['res'] = False
|
||||
return ret
|
||||
data['Check']['HTTP'] = kwargs['check_http']
|
||||
data['Check']['Interval'] = kwargs['check_interval']
|
||||
if len(check_dd) > 0:
|
||||
data['Check'] = check_dd # if empty, ignore it
|
||||
|
||||
function = 'agent/service/register'
|
||||
res = _query(consul_url=consul_url,
|
||||
|
|
Loading…
Add table
Reference in a new issue