@ -224,7 +224,6 @@ match_groupdict:
import binascii
import contextlib
import datetime
import errno
import math
import mmap
@ -234,11 +233,11 @@ import select
import socket
import time
import traceback
from datetime import datetime , timedelta , timezone
from ansible . module_utils . basic import AnsibleModule , missing_required_lib
from ansible . module_utils . common . sys_info import get_platform_subclass
from ansible . module_utils . common . text . converters import to_bytes , to_native
from ansible . module_utils . compat . datetime import utcnow
HAS_PSUTIL = False
@ -532,7 +531,7 @@ def main():
except Exception :
module . fail_json ( msg = " unknown active_connection_state ( %s ) defined " % _connection_state , elapsed = 0 )
start = utcnow( )
start = datetime. now ( timezone . utc )
if delay :
time . sleep ( delay )
@ -541,9 +540,9 @@ def main():
time . sleep ( timeout )
elif state in [ ' absent ' , ' stopped ' ] :
# first wait for the stop condition
end = start + datetime. timedelta( seconds = timeout )
end = start + timedelta( seconds = timeout )
while utcnow( ) < end :
while datetime. now ( timezone . utc ) < end :
if path :
try :
if not os . access ( b_path , os . F_OK ) :
@ -560,7 +559,7 @@ def main():
# Conditions not yet met, wait and try again
time . sleep ( module . params [ ' sleep ' ] )
else :
elapsed = utcnow( ) - start
elapsed = datetime. now ( timezone . utc ) - start
if port :
module . fail_json ( msg = msg or " Timeout when waiting for %s : %s to stop. " % ( host , port ) , elapsed = elapsed . seconds )
elif path :
@ -568,15 +567,15 @@ def main():
elif state in [ ' started ' , ' present ' ] :
# wait for start condition
end = start + datetime. timedelta( seconds = timeout )
while utcnow( ) < end :
end = start + timedelta( seconds = timeout )
while datetime. now ( timezone . utc ) < end :
if path :
try :
os . stat ( b_path )
except OSError as e :
# If anything except file not present, throw an error
if e . errno != 2 :
elapsed = utcnow( ) - start
elapsed = datetime. now ( timezone . utc ) - start
module . fail_json ( msg = msg or " Failed to stat %s , %s " % ( path , e . strerror ) , elapsed = elapsed . seconds )
# file doesn't exist yet, so continue
else :
@ -611,7 +610,9 @@ def main():
except IOError :
pass
elif port :
alt_connect_timeout = math . ceil ( _timedelta_total_seconds ( end - utcnow ( ) ) )
alt_connect_timeout = math . ceil (
_timedelta_total_seconds ( end - datetime . now ( timezone . utc ) ) ,
)
try :
s = socket . create_connection ( ( host , port ) , min ( connect_timeout , alt_connect_timeout ) )
except Exception :
@ -622,8 +623,12 @@ def main():
if b_compiled_search_re :
b_data = b ' '
matched = False
while utcnow ( ) < end :
max_timeout = math . ceil ( _timedelta_total_seconds ( end - utcnow ( ) ) )
while datetime . now ( timezone . utc ) < end :
max_timeout = math . ceil (
_timedelta_total_seconds (
end - datetime . now ( timezone . utc ) ,
) ,
)
readable = select . select ( [ s ] , [ ] , [ ] , max_timeout ) [ 0 ]
if not readable :
# No new data. Probably means our timeout
@ -667,7 +672,7 @@ def main():
else : # while-else
# Timeout expired
elapsed = utcnow( ) - start
elapsed = datetime. now ( timezone . utc ) - start
if port :
if search_regex :
module . fail_json ( msg = msg or " Timeout when waiting for search string %s in %s : %s " % ( search_regex , host , port ) , elapsed = elapsed . seconds )
@ -681,19 +686,19 @@ def main():
elif state == ' drained ' :
# wait until all active connections are gone
end = start + datetime. timedelta( seconds = timeout )
end = start + timedelta( seconds = timeout )
tcpconns = TCPConnectionInfo ( module )
while utcnow( ) < end :
while datetime. now ( timezone . utc ) < end :
if tcpconns . get_active_connections_count ( ) == 0 :
break
# Conditions not yet met, wait and try again
time . sleep ( module . params [ ' sleep ' ] )
else :
elapsed = utcnow( ) - start
elapsed = datetime. now ( timezone . utc ) - start
module . fail_json ( msg = msg or " Timeout when waiting for %s : %s to drain " % ( host , port ) , elapsed = elapsed . seconds )
elapsed = utcnow( ) - start
elapsed = datetime. now ( timezone . utc ) - start
module . exit_json ( state = state , port = port , search_regex = search_regex , match_groups = match_groups , match_groupdict = match_groupdict , path = path ,
elapsed = elapsed . seconds )