@@ -313,14 +313,44 @@ def jobs(self):
313313 yield job_id , intervals , SNMPBot .do_snmp , job_info
314314
315315
316+ def wait_for_grafolean (backend_url ):
317+ while True :
318+ url = '{}/status/info' .format (backend_url )
319+ log .info ("Checking Grafolean status..." )
320+ try :
321+ r = requests .get (url )
322+ r .raise_for_status ()
323+ status_info = r .json ()
324+ if status_info ['db_migration_needed' ] == False and status_info ['user_exists' ] == True :
325+ log .info ("Grafolean backend is ready." )
326+ return
327+ except :
328+ pass
329+ log .info ("Grafolean backend not available / initialized yet, waiting." )
330+ time .sleep (10 )
331+
332+
316333if __name__ == "__main__" :
317334 dotenv .load_dotenv ()
318335
319336 backend_url = os .environ .get ('BACKEND_URL' )
320- bot_token = os .environ .get ('BOT_TOKEN' )
321- if not backend_url or not bot_token :
322- raise Exception ("Please specify BACKEND_URL and BOT_TOKEN env vars." )
323337 jobs_refresh_interval = int (os .environ .get ('JOBS_REFRESH_INTERVAL' , 120 ))
324338
339+ if not backend_url :
340+ raise Exception ("Please specify BACKEND_URL and BOT_TOKEN / BOT_TOKEN_FROM_FILE env vars." )
341+
342+ wait_for_grafolean (backend_url )
343+
344+ bot_token = os .environ .get ('BOT_TOKEN' )
345+ if not bot_token :
346+ # bot token can also be specified via contents of a file:
347+ bot_token_from_file = os .environ .get ('BOT_TOKEN_FROM_FILE' )
348+ if bot_token_from_file :
349+ with open (bot_token_from_file , 'rt' ) as f :
350+ bot_token = f .read ()
351+
352+ if not bot_token :
353+ raise Exception ("Please specify BOT_TOKEN / BOT_TOKEN_FROM_FILE env var." )
354+
325355 c = SNMPBot (backend_url , bot_token , jobs_refresh_interval )
326356 c .execute ()
0 commit comments