#!/usr/bin/env python3
# Copyright (c) 2008, 2011 Arista Networks, Inc.  All rights reserved.
# Arista Networks, Inc. Confidential and Proprietary.

import optparse # pylint: disable=deprecated-module
import os
import sys
# Set the 'NOPDB' environment variable before importing Tac to prevent us from
# ever dropping into PDB (see tacc/Tac/Excepthook.py).
os.environ[ 'NOPDB' ] = '1'

import Tac, WaitForWarmup # pylint: disable=wrong-import-position

usage = """
wfw [<options>] [<agent name>, ...]

   wfw stands for "wait for warmup".  When run it will wait until the agent(s)
   specified are warm and return zero, or if one or more of the agents are not
   warm by the timeout deadline, will return non-zero.  If no agent is specified
   wfw will wait until either all registered agents are warm, or the timeout,
   before returning.
"""

timeoutDefault = 600
sysnameDefault = os.environ.get( "SYSNAME", "ar" )

parser = optparse.OptionParser( usage=usage )
parser.add_option( "-v", "--verbose", action="store_true",
                   help="enable verbose output" )
parser.add_option( "-C", "--concise", action="store_true",
                   help="enable concise output (overrides -v flag)" )
parser.add_option( "-s", "--sysname", action="store",
                   # pylint: disable-next=consider-using-f-string
                   help="set the sysname (default=%s)" %( sysnameDefault ),
                   default=sysnameDefault )
parser.add_option( "-t", "--timeout", action="store",
                   # pylint: disable-next=consider-using-f-string
                   help="max seconds to wait (default=%d)" %( timeoutDefault ),
                   default=timeoutDefault )
parser.add_option( "-c", "--cellid", type="int", action="store",
                   help="cell id (default is this cell)" )
parser.add_option( "--exclude", action="append",
                   help="agent to exclude from wfw" )

( options, args ) = parser.parse_args()

startTime = Tac.now()
timeout = int( options.timeout )
agentList = None
if len( args ) > 0:
   agentList = args

if options.cellid:
   cellId = options.cellid
else:
   cellId = None

Tac.sysnameIs( options.sysname )
sysdbPort = os.environ.get( 'SYSDBPORT', None )
sysdbSockname = os.environ.get( 'SYSDBSOCKNAME', None )
if options.concise:
   verbose = "concise"
else:
   verbose = options.verbose
entMan = WaitForWarmup.emOrPyClient( sysname=options.sysname,
                                     verbose=verbose,
                                     sysdbPort=sysdbPort,
                                     sysdbSockname=sysdbSockname,
                                     timeout=timeout )
if entMan is None:
   sys.exit( 1 )

waitStartTime = Tac.now()
timeout = timeout - ( waitStartTime - startTime )
if verbose:
   if agentList:
      print("Waiting for agents to warm up:", ", ".join( agentList ))
   else:
      print("Waiting for all agents to warm up...")
try:
   WaitForWarmup.wait( entMan, agentList=agentList, timeout=timeout,
                       verbose=verbose, agentsToSkip=options.exclude,
                       cellId=cellId )
except Tac.Timeout:
   if verbose:
      print("Timed out waiting for agents to warm up") 
   sys.exit( 1 )

if verbose:
   now = Tac.now()
   # pylint: disable-next=consider-using-f-string
   print( "Agents are warm (connection time %.2f seconds, warmup time %.2f seconds)"
          % ( waitStartTime - startTime, now - waitStartTime ) )

sys.exit( 0 )
