Add multi-master support to publish.publish

This commit is contained in:
Mike Place 2016-10-19 16:42:32 +09:00
parent 7f141ba38c
commit e22a3d2be6
No known key found for this signature in database
GPG key ID: 9136F4F13705CFD3

View file

@ -13,7 +13,7 @@ import salt.crypt
import salt.payload
import salt.transport
import salt.utils.args
from salt.exceptions import SaltReqTimeoutError
from salt.exceptions import SaltReqTimeoutError, SaltInvocationError
log = logging.getLogger(__name__)
@ -46,7 +46,8 @@ def _publish(
returner='',
timeout=5,
form='clean',
wait=False):
wait=False,
via_master=None):
'''
Publish a command from the minion out to other minions, publications need
to be enabled on the Salt master and the minion needs to have permission
@ -75,7 +76,35 @@ def _publish(
arg = _parse_args(arg)
log.info('Publishing \'{0}\' to {master_uri}'.format(fun, **__opts__))
if via_master:
if 'master_uri_list' not in __opts__:
raise SaltInvocationError(message='''Could not find list of
masters in minion configuration but `via_master` was specified.''')
else:
# Find the master in the list of master_uris generated by the minion base class
matching_master_uris = [master for master
in __opts__['master_uri_list']
if '//{0}:'.format(via_master)
in master]
if not matching_master_uris:
raise SaltInvocationError('''Could not find match for {0} in
list of configured masters when using `via_master` option'''.format(
via_master, __opts__['master_uri_list']))
if len(matching_master_uris) > 1:
# If we have multiple matches, consider this a non-fatal error
# and continue with whatever we found first.
log.warning('''The `via_master` flag found
more than one possible match found for {0} when evaluating
list {1}'''.format(via_master, __opts__['master_uri_list']))
master_uri = matching_master_uris.pop()
else:
# If no preference is expressed by the user, just publish to the first master
# in the list.
master_uri = __opts__['master_uri']
log.info('Publishing \'{0}\' to {1}'.format(fun, master_uri))
auth = salt.crypt.SAuth(__opts__)
tok = auth.gen_token('salt')
load = {'cmd': 'minion_pub',
@ -89,7 +118,7 @@ def _publish(
'form': form,
'id': __opts__['id']}
channel = salt.transport.Channel.factory(__opts__)
channel = salt.transport.Channel.factory(__opts__, master_uri=master_uri)
try:
peer_data = channel.send(load)
except SaltReqTimeoutError:
@ -145,7 +174,7 @@ def _publish(
return ret
def publish(tgt, fun, arg=None, expr_form='glob', returner='', timeout=5):
def publish(tgt, fun, arg=None, expr_form='glob', returner='', timeout=5, via_master=None):
'''
Publish a command from the minion out to other minions.
@ -204,7 +233,9 @@ def publish(tgt, fun, arg=None, expr_form='glob', returner='', timeout=5):
salt '*' publish.publish test.kwarg arg="['cheese=spam','spam=cheese']"
When running via salt-call, the `via_master` flag may be set to specific which
master the publication should be sent to. Only one master may be specified. If
unset, the publication will be sent only to the first master in minion configuration.
'''
return _publish(tgt,
fun,
@ -213,7 +244,8 @@ def publish(tgt, fun, arg=None, expr_form='glob', returner='', timeout=5):
returner=returner,
timeout=timeout,
form='clean',
wait=True)
wait=True,
via_master=via_master)
def full_data(tgt, fun, arg=None, expr_form='glob', returner='', timeout=5):