Skip to content

Commit e4d1865

Browse files
committed
Enh: CLI GOSSIP add a ping method to directly test a node
1 parent d1bec37 commit e4d1865

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

data/core-configuration/packs/core-cli-gossip/cli/cli.py

+48
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
NODE_STATES.LEAVE : 'cyan',
3232
NODE_STATES.UNKNOWN: 'grey',
3333
}
34+
3435
NODE_STATE_PREFIXS = {NODE_STATES.ALIVE : CHARACTERS.check,
3536
NODE_STATES.DEAD : CHARACTERS.cross,
3637
NODE_STATES.SUSPECT: CHARACTERS.double_exclamation,
@@ -203,6 +204,45 @@ def do_join(seed=''):
203204
cprint('FAILED', color='red')
204205

205206

207+
def do_ping(node):
208+
# The information is available only if the agent is started
209+
wait_for_agent_started(visual_wait=True)
210+
211+
try:
212+
node_obj = get_opsbro_json('/agent/query/guess/%s' % node)
213+
except get_request_errors() as exp:
214+
logger.error('Cannot query the node: %s' % exp)
215+
sys.exit(2)
216+
if node_obj is None:
217+
cprint('FAILED: cannot find the node %s' % node, color='red')
218+
sys.exit(2)
219+
node_uuid = node_obj['uuid']
220+
221+
try:
222+
ping_result = get_opsbro_json('/agent/ping/%s' % node_uuid)
223+
except get_request_errors() as exp:
224+
logger.error('Cannot launch the node ping: %s' % exp)
225+
sys.exit(2)
226+
if 'error' in ping_result:
227+
cprint('FAILED: %s' % ping_result['error'], color='red')
228+
sys.exit(2)
229+
node_state = ping_result['state']
230+
display_name = node_obj['display_name']
231+
if not display_name:
232+
display_name = node_obj['name']
233+
234+
state_color = NODE_STATE_COLORS.get(node_state)
235+
state_char = NODE_STATE_PREFIXS.get(node_state)
236+
cprint(' %s ' % state_char, color=state_color, end='')
237+
cprint('%-15s ' % display_name, color='magenta', end='')
238+
cprint('is: ', end='')
239+
cprint(node_state, color=state_color)
240+
241+
# If not alive, it's an error
242+
if node_state != NODE_STATES.ALIVE:
243+
sys.exit(2)
244+
245+
206246
def do_zone_change(name=''):
207247
if not name:
208248
cprint("Need a zone name")
@@ -628,6 +668,14 @@ def do_wait_members(name='', display_name='', group='', count=1, timeout=30):
628668
],
629669
},
630670

671+
do_ping : {
672+
'keywords' : ['gossip', 'ping'],
673+
'description': 'Ping another node of the cluster',
674+
'args' : [
675+
{'name': 'node', 'default': '', 'description': 'uuid, name or display name of the node to ping'},
676+
],
677+
},
678+
631679
do_leave : {
632680
'keywords' : ['gossip', 'leave'],
633681
'description': 'Put in leave a cluster node',

opsbro/gossip.py

+20
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,26 @@ def get_reload_zone_key(zone_name):
19171917
return jsoner.dumps(encrypter.load_or_reload_key_for_zone_if_need(zone_name))
19181918

19191919

1920+
@http_export('/agent/query/guess/:name_or_uuid', method='GET', protected=True)
1921+
def get_query_by_name(name_or_uuid):
1922+
response.content_type = 'application/json'
1923+
for node in self.nodes.values():
1924+
if node['uuid'] == name_or_uuid or node['name'] == name_or_uuid or node['display_name'] == name_or_uuid:
1925+
return node
1926+
return None
1927+
1928+
1929+
@http_export('/agent/ping/:node_uuid', method='GET', protected=True)
1930+
def get_ping_node(node_uuid):
1931+
response.content_type = 'application/json'
1932+
node = self.get(node_uuid)
1933+
if node is None:
1934+
return {'error': 'No such node %s' % node_uuid}
1935+
self.__do_ping(node)
1936+
logger.info('NODE STATE: %s' % node['state'])
1937+
return {'state': node['state']}
1938+
1939+
19201940
@http_export('/agent/event/:event_type', method='GET', protected=True)
19211941
def get_event(event_type):
19221942
response.content_type = 'application/json'

0 commit comments

Comments
 (0)