9
9
def node_name (node_name ):
10
10
return node_name .split ('.production' ).pop (0 )
11
11
12
- def strip_isodate (raw_output ):
13
- return re .sub (r'ISODate\((.*?)\)' , r'\1' , raw_output )
12
+ def strip_dates (raw_output ):
13
+ stripped_isodates = re .sub (r'ISODate\((.*?)\)' , r'\1' , raw_output )
14
+ return re .sub (r'Timestamp\((.*?)\)' , r'"\1"' , stripped_isodates )
14
15
15
16
def mongo_command (command ):
16
17
return "mongo --quiet --eval 'printjson(%s)'" % command
17
18
18
- def run_mongo_command (command ):
19
- return json .loads (
20
- strip_isodate (
21
- run (mongo_command (command ))))
19
+ def run_mongo_command (command , command_warn_only = False ):
20
+ if command_warn_only :
21
+ with settings (warn_only = True ):
22
+ response = run (mongo_command (command ))
23
+ else :
24
+ response = run (mongo_command (command ))
25
+
26
+ if response .return_code == 252 :
27
+ dict_response = {"return_code" : 252 }
28
+ else :
29
+ dict_response = json .loads (strip_dates (response ))
30
+
31
+ return dict_response
22
32
23
33
@task (default = True )
24
34
@roles ('class-mongo' )
@@ -39,20 +49,32 @@ def status():
39
49
with hide ('output' ):
40
50
status = run_mongo_command ("rs.status()" )
41
51
print ("Status at %s" % status ['date' ])
52
+ parsed_statuses = []
53
+ parsed_statuses .append (['Name' , 'Health' , 'State' , 'Heartbeat' ])
54
+
42
55
for member_status in status ['members' ]:
43
56
name = node_name (member_status ['name' ])
44
57
health = "OK" if member_status ['health' ] == 1 else "ERROR"
45
58
state = member_status ['stateStr' ]
46
59
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
+ for status in parsed_statuses :
63
+ print ("| {0:<22} | {1:<8} | {2:<10} | {3:<22} |" .format (status [0 ], status [1 ], status [2 ], status [3 ]))
48
64
49
65
@task
50
66
def step_down_primary (seconds = '100' ):
51
67
"""Step down as primary for a given number of seconds (default: 100)"""
52
68
with hide ('output' ):
53
- result = run_mongo_command ("rs.stepDown(%s)" % seconds )
54
- if result ['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' in result and result ['ok' ] == 0 :
57
75
print ("Failed to step down: %s\n Try running mongo.find_primary" % result ['errmsg' ])
76
+ return 0
58
77
78
+ if 'return_code' in result and result ['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