Merge pull request #19293 from DSRCompany/raet_presence_requests

Support joined, allowed, alived and reaped runners.
This commit is contained in:
Thomas S Hatch 2014-12-30 12:27:27 -07:00
commit 0487834e72
3 changed files with 288 additions and 34 deletions

View File

@ -1116,7 +1116,9 @@ class SaltRaetPresenter(ioflo.base.deeding.Deed):
Ioinits = {'opts': '.salt.opts',
'presence_req': '.salt.presence.event_req',
'lane_stack': '.salt.lane.manor.stack',
'alloweds': '.salt.var.presence.alloweds', # odict
'aliveds': '.salt.var.presence.aliveds', # odict
'reapeds': '.salt.var.presence.reapeds', # odict
'availables': '.salt.var.presence.availables', # set
}
@ -1129,12 +1131,43 @@ class SaltRaetPresenter(ioflo.base.deeding.Deed):
if y_name not in self.lane_stack.value.nameRemotes: # subscriber not a remote
pass # drop msg don't answer
else:
if 'data' in msg and 'state' in msg['data']:
state = msg['data']['state']
else:
state = None
# create answer message
if state is None or state == 'available' or state == 'present':
present = odict()
for name in self.availables.value:
minion = self.aliveds.value[name]
present[name] = minion.ha[0] if minion else None
data = {'present': present}
elif state == 'joined':
joined = odict()
# For now use alloweds here.
# TODO: update to really return joineds
for name in self.alloweds.value:
joined[name] = self.alloweds.value[name].ha[0]
data = {'joined': joined}
elif state == 'allowed':
allowed = odict()
for name in self.alloweds.value:
allowed[name] = self.alloweds.value[name].ha[0]
data = {'allowed': allowed}
elif state == 'alived':
alived = odict()
for name in self.aliveds.value:
alived[name] = self.aliveds.value[name].ha[0]
data = {'alived': alived}
elif state == 'reaped':
reaped = odict()
for name in self.reapeds.value:
reaped[name] = self.reapeds.value[name].ha[0]
data = {'reaped': reaped}
else:
# error: wrong/unknown state requested
log.error('Lane Router Received invalid message: {0}'.format(msg))
return
tag = tagify('present', 'presence')
route = {'dst': (None, None, 'event_fire'),
'src': (None, self.lane_stack.value.local.name, None)}

View File

@ -134,10 +134,95 @@ def up(): # pylint: disable=C0103
return ret
def list_state(subset=None, show_ipv4=False, state=None):
'''
Print a list of all minions that are up according to Salt's presence
detection (no commands will be sent to minions)
subset : None
Pass in a CIDR range to filter minions by IP address.
show_ipv4 : False
Also show the IP address each minion is connecting from.
state : 'available'
Show minions being in specific state that is one of 'available', 'joined',
'allowed', 'alived' or 'reaped'.
CLI Example:
.. code-block:: bash
salt-run manage.list_state
'''
conf_file = __opts__['conf_file']
opts = salt.config.client_config(conf_file)
if opts['transport'] == 'raet':
event = salt.utils.raetevent.PresenceEvent(__opts__, __opts__['sock_dir'], state=state)
data = event.get_event(wait=60, tag=salt.utils.event.tagify('present', 'presence'))
key = 'present' if state is None else state
if not data or key not in data:
minions = []
else:
minions = data[key]
if subset:
minions = [m for m in minions if m in subset]
else:
# Always return 'present' for 0MQ for now
# TODO: implement other states spport for 0MQ
ckminions = salt.utils.minions.CkMinions(__opts__)
minions = ckminions.connected_ids(show_ipv4=show_ipv4, subset=subset)
connected = dict(minions) if show_ipv4 else sorted(minions)
return connected
def list_not_state(subset=None, show_ipv4=False, state=None):
'''
Print a list of all minions that are NOT up according to Salt's presence
detection (no commands will be sent to minions)
subset : None
Pass in a CIDR range to filter minions by IP address.
show_ipv4 : False
Also show the IP address each minion is connecting from.
state : 'available'
Show minions being in specific state that is one of 'available', 'joined',
'allowed', 'alived' or 'reaped'.
CLI Example:
.. code-block:: bash
salt-run manage.list_not_state
'''
connected = list_state(subset=None, show_ipv4=show_ipv4, state=state)
key = salt.key.get_key(__opts__)
keys = key.list_keys()
# TODO: Need better way to handle key/node name difference for raet
# In raet case node name is '<name>_<kind>' meanwhile the key name
# is just '<name>'. So append '_minion' to the name to match.
appen_kind = isinstance(key, salt.key.RaetKey)
not_connected = []
for minion in keys[key.ACC]:
if appen_kind:
minion += '_minion'
if minion not in connected and (subset is None or minion in subset):
not_connected.append(minion)
return not_connected
def present(subset=None, show_ipv4=False):
'''
Print a list of all minions that are up according to Salt's presence
detection (no commands will be sent)
detection (no commands will be sent to minions)
subset : None
Pass in a CIDR range to filter minions by IP address.
@ -151,20 +236,7 @@ def present(subset=None, show_ipv4=False):
salt-run manage.present
'''
conf_file = __opts__['conf_file']
opts = salt.config.client_config(conf_file)
if opts['transport'] == 'raet':
event = salt.utils.raetevent.PresenceEvent(__opts__, __opts__['sock_dir'])
data = event.get_event(wait=60, tag=salt.utils.event.tagify('present', 'presence'))
present = data['present'] if data else []
minions = [m for m in present if m in subset] if subset else present
else:
ckminions = salt.utils.minions.CkMinions(__opts__)
minions = ckminions.connected_ids(show_ipv4=show_ipv4, subset=subset)
connected = dict(minions) if show_ipv4 else sorted(minions)
return connected
return list_state(subset=subset, show_ipv4=show_ipv4)
def not_present(subset=None, show_ipv4=False):
@ -184,24 +256,167 @@ def not_present(subset=None, show_ipv4=False):
salt-run manage.not_present
'''
connected = present(subset=None, show_ipv4=show_ipv4)
return list_not_state(subset=subset, show_ipv4=show_ipv4)
key = salt.key.get_key(__opts__)
keys = key.list_keys()
# TODO: Need better way to handle key/node name difference for raet
# In raet case node name is '<name>_<kind>' meanwhile the key name
# is just '<name>'. So append '_minion' to the name to match.
appen_kind = isinstance(key, salt.key.RaetKey)
def joined(subset=None, show_ipv4=False):
'''
Print a list of all minions that are up according to Salt's presence
detection (no commands will be sent to minions)
not_connected = []
for minion in keys[key.ACC]:
if appen_kind:
minion += '_minion'
if minion not in connected and (subset is None or minion in subset):
not_connected.append(minion)
subset : None
Pass in a CIDR range to filter minions by IP address.
return not_connected
show_ipv4 : False
Also show the IP address each minion is connecting from.
CLI Example:
.. code-block:: bash
salt-run manage.joined
'''
return list_state(subset=subset, show_ipv4=show_ipv4, state='joined')
def not_joined(subset=None, show_ipv4=False):
'''
Print a list of all minions that are NOT up according to Salt's presence
detection (no commands will be sent)
subset : None
Pass in a CIDR range to filter minions by IP address.
show_ipv4 : False
Also show the IP address each minion is connecting from.
CLI Example:
.. code-block:: bash
salt-run manage.not_joined
'''
return list_not_state(subset=subset, show_ipv4=show_ipv4, state='joined')
def allowed(subset=None, show_ipv4=False):
'''
Print a list of all minions that are up according to Salt's presence
detection (no commands will be sent to minions)
subset : None
Pass in a CIDR range to filter minions by IP address.
show_ipv4 : False
Also show the IP address each minion is connecting from.
CLI Example:
.. code-block:: bash
salt-run manage.allowed
'''
return list_state(subset=subset, show_ipv4=show_ipv4, state='allowed')
def not_allowed(subset=None, show_ipv4=False):
'''
Print a list of all minions that are NOT up according to Salt's presence
detection (no commands will be sent)
subset : None
Pass in a CIDR range to filter minions by IP address.
show_ipv4 : False
Also show the IP address each minion is connecting from.
CLI Example:
.. code-block:: bash
salt-run manage.not_allowed
'''
return list_not_state(subset=subset, show_ipv4=show_ipv4, state='allowed')
def alived(subset=None, show_ipv4=False):
'''
Print a list of all minions that are up according to Salt's presence
detection (no commands will be sent to minions)
subset : None
Pass in a CIDR range to filter minions by IP address.
show_ipv4 : False
Also show the IP address each minion is connecting from.
CLI Example:
.. code-block:: bash
salt-run manage.alived
'''
return list_state(subset=subset, show_ipv4=show_ipv4, state='alived')
def not_alived(subset=None, show_ipv4=False):
'''
Print a list of all minions that are NOT up according to Salt's presence
detection (no commands will be sent)
subset : None
Pass in a CIDR range to filter minions by IP address.
show_ipv4 : False
Also show the IP address each minion is connecting from.
CLI Example:
.. code-block:: bash
salt-run manage.not_alived
'''
return list_not_state(subset=subset, show_ipv4=show_ipv4, state='alived')
def reaped(subset=None, show_ipv4=False):
'''
Print a list of all minions that are up according to Salt's presence
detection (no commands will be sent to minions)
subset : None
Pass in a CIDR range to filter minions by IP address.
show_ipv4 : False
Also show the IP address each minion is connecting from.
CLI Example:
.. code-block:: bash
salt-run manage.reaped
'''
return list_state(subset=subset, show_ipv4=show_ipv4, state='reaped')
def not_reaped(subset=None, show_ipv4=False):
'''
Print a list of all minions that are NOT up according to Salt's presence
detection (no commands will be sent)
subset : None
Pass in a CIDR range to filter minions by IP address.
show_ipv4 : False
Also show the IP address each minion is connecting from.
CLI Example:
.. code-block:: bash
salt-run manage.not_reaped
'''
return list_not_state(subset=subset, show_ipv4=show_ipv4, state='reaped')
def safe_accept(target, expr_form='glob'):

View File

@ -277,6 +277,10 @@ class RunnerEvent(MasterEvent):
class PresenceEvent(MasterEvent):
def __init__(self, opts, sock_dir, listen=True, state=None):
self.state = state
super(PresenceEvent, self).__init__(opts=opts, sock_dir=sock_dir, listen=listen)
def connect_pub(self):
'''
Establish the publish connection
@ -286,6 +290,8 @@ class PresenceEvent(MasterEvent):
route = {'dst': (None, self.ryn, 'presence_req'),
'src': (None, self.stack.local.name, None)}
msg = {'route': route}
if self.state:
msg['data'] = {'state': self.state}
self.stack.transmit(msg, self.stack.nameRemotes[self.ryn].uid)
self.stack.serviceAll()
self.connected = True