Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc5eaf4b

Browse files
committed
Merge pull requestalphagov#44 from alphagov/fix-mongo-status
Fix and improve Mongo commands
2 parents9abd2ab +7fcd91f commitc5eaf4b

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

‎mongo.py‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@
99
defnode_name(node_name):
1010
returnnode_name.split('.production').pop(0)
1111

12-
defstrip_isodate(raw_output):
13-
returnre.sub(r'ISODate\((.*?)\)',r'\1',raw_output)
12+
defstrip_dates(raw_output):
13+
stripped_isodates=re.sub(r'ISODate\((.*?)\)',r'\1',raw_output)
14+
returnre.sub(r'Timestamp\((.*?)\)',r'"\1"',stripped_isodates)
1415

1516
defmongo_command(command):
1617
return"mongo --quiet --eval 'printjson(%s)'"%command
1718

18-
defrun_mongo_command(command):
19-
returnjson.loads(
20-
strip_isodate(
21-
run(mongo_command(command))))
19+
defrun_mongo_command(command,command_warn_only=False):
20+
ifcommand_warn_only:
21+
withsettings(warn_only=True):
22+
response=run(mongo_command(command))
23+
else:
24+
response=run(mongo_command(command))
25+
26+
ifresponse.return_code==252:
27+
dict_response= {"return_code":252}
28+
else:
29+
dict_response=json.loads(strip_dates(response))
30+
31+
returndict_response
2232

2333
@task(default=True)
2434
@roles('class-mongo')
@@ -39,20 +49,32 @@ def status():
3949
withhide('output'):
4050
status=run_mongo_command("rs.status()")
4151
print("Status at %s"%status['date'])
52+
parsed_statuses= []
53+
parsed_statuses.append(['Name','Health','State','Heartbeat'])
54+
4255
formember_statusinstatus['members']:
4356
name=node_name(member_status['name'])
4457
health="OK"ifmember_status['health']==1else"ERROR"
4558
state=member_status['stateStr']
4659
heartbeat=member_status.get('lastHeartbeat','')
47-
print("%s %s % -10s %s"% (name,health,state,heartbeat))
60+
parsed_statuses.append([name,health,state,heartbeat])
61+
62+
forstatusinparsed_statuses:
63+
print("| {0:<22} | {1:<8} | {2:<10} | {3:<22} |".format(status[0],status[1],status[2],status[3]))
4864

4965
@task
5066
defstep_down_primary(seconds='100'):
5167
"""Step down as primary for a given number of seconds (default: 100)"""
5268
withhide('output'):
53-
result=run_mongo_command("rs.stepDown(%s)"%seconds)
54-
ifresult['ok']==1:
55-
print("Stepped down for %s seconds"%seconds)
56-
else:
69+
# Mongo returns an exit code of 252 when the primary steps down, as well as disconnecting
70+
# the current console session. This means that we have to run the command with Fabric's
71+
# warn_only enabled, or Fabric will error.
72+
result=run_mongo_command("rs.stepDown(%s)"%seconds,command_warn_only=True)
73+
74+
if'ok'inresultandresult['ok']==0:
5775
print("Failed to step down: %s\nTry running mongo.find_primary"%result['errmsg'])
76+
return0
5877

78+
if'return_code'inresultandresult['return_code']==252:
79+
print"Received a 252 exit code from Mongo. There may have been disconnection warnings"
80+
print"during the step down from primary that caused this. Verify the primary with mongo.find_primary."

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp