5
5
import logging
6
6
import time
7
7
import sys
8
+ import json
9
+ import re
8
10
9
11
PAGE_SIZE = 10
10
12
@@ -54,7 +56,27 @@ def main():
54
56
export_variable (CF_OUTPUT_URL_VAR , link_to_app )
55
57
56
58
ingress_host = get_runtime_ingress_host ()
57
- execute_argocd_sync (ingress_host )
59
+
60
+ # Does the app exist
61
+ # if not let's wait it has been recorded
62
+ # but not too long in case of simple misspelling
63
+ is_app_real = application_exist (ingress_host )
64
+ count = 1
65
+ while count < 3 and is_app_real == False :
66
+ logging .debug ("App does not exist yet %d" , count )
67
+ time .sleep (INTERVAL )
68
+ count += 1
69
+ is_app_real = application_exist (ingress_host )
70
+
71
+ if application_exist (ingress_host ) == False :
72
+ print (f"ERROR application { APPLICATION } does not seem to exist" )
73
+ sys .exit (3 )
74
+
75
+ if application_autosync (ingress_host ) == False :
76
+ execute_argocd_sync (ingress_host )
77
+ else :
78
+ logging .info ("Skipping synchronization as Application is in auto-sync mode" )
79
+
58
80
namespace = get_runtime_ns ()
59
81
health , sync = get_app_status (ingress_host )
60
82
@@ -253,7 +275,7 @@ def execute_argocd_sync(ingress_host):
253
275
retries = 3 ,
254
276
)
255
277
client = Client (transport = transport , fetch_schema_from_transport = False )
256
- query = get_query ('argocd_sync' ) ## gets gql query
278
+ query = get_query ('argocd_sync' ) ## gets gql query
257
279
variables = {
258
280
"applicationName" : APPLICATION ,
259
281
"options" : {
@@ -273,15 +295,73 @@ def execute_argocd_sync(ingress_host):
273
295
print (f"ERROR: cannot sync Application { APPLICATION } " )
274
296
logging .debug ("Syncing App result: %s" , err )
275
297
sys .exit (1 )
276
- # finally:
277
- # print("finally block")
278
- # logging.debug("Syncing App result: %s", result)
279
- # if result.errors[0].message.contains("NOT_FOUND_ERROR"):
280
- # printf("Application %s does not exit")
281
- #
282
- # else:
283
- # # Application sync'ed properly
284
- # logging.debug("Syncing App result: %s", result)
298
+
299
+ #
300
+ # Check for application existence
301
+ # if it does not exist, it will return 403 error
302
+ #
303
+ # Return True or False
304
+ #
305
+ def application_exist (ingress_host ):
306
+ runtime_api_endpoint = ingress_host + '/app-proxy/api/graphql'
307
+ transport = RequestsHTTPTransport (
308
+ url = runtime_api_endpoint ,
309
+ headers = {'authorization' : CF_API_KEY },
310
+ verify = VERIFY ,
311
+ retries = 3 ,
312
+ )
313
+ client = Client (transport = transport , fetch_schema_from_transport = False )
314
+ query = get_query ('get_app_existence' ) ## gets gql query
315
+ variables = {
316
+ "applicationName" : APPLICATION
317
+ }
318
+ try :
319
+ result = client .execute (query , variable_values = variables )
320
+ except TransportQueryError as err :
321
+ data = json .loads (re .sub ('\' ' ,'\" ' , str (err )))
322
+ if (data ["message" ] == "Forbidden" ) and (data ["extensions" ] == 403 ):
323
+ return False
324
+ else :
325
+ print (f"ERROR: cannot test Application { APPLICATION } " )
326
+ logging .error ("Existence App result: %s" , err )
327
+ sys .exit (1 )
328
+ except Exception as err :
329
+ print (f"ERROR: cannot test Application { APPLICATION } " )
330
+ logging .error ("Existence App result: %s" , err )
331
+ sys .exit (1 )
332
+ return True
333
+
334
+ #
335
+ # Check if app is in auto-sync mode
336
+ #
337
+ # Return True or False
338
+ #
339
+ def application_autosync (ingress_host ):
340
+ runtime_api_endpoint = ingress_host + '/app-proxy/api/graphql'
341
+ transport = RequestsHTTPTransport (
342
+ url = runtime_api_endpoint ,
343
+ headers = {'authorization' : CF_API_KEY },
344
+ verify = VERIFY ,
345
+ retries = 3 ,
346
+ )
347
+ client = Client (transport = transport , fetch_schema_from_transport = False )
348
+ query = get_query ('get_app_autosync' ) ## gets gql query
349
+ variables = {
350
+ "applicationName" : APPLICATION
351
+ }
352
+ try :
353
+ result = client .execute (query , variable_values = variables )
354
+ except Exception as err :
355
+ print (f"ERROR: cannot get sync policy from Application { APPLICATION } " )
356
+ logging .debug ("Application Sync policy result: %s" , err )
357
+ sys .exit (1 )
358
+
359
+ logging .debug ("App sync Policy: " , result ['applicationProxyQuery' ]['spec' ]['syncPolicy' ]['automated' ])
360
+ if result ['applicationProxyQuery' ]['spec' ]['syncPolicy' ]['automated' ] == None :
361
+ return False
362
+ else :
363
+ return True
364
+
285
365
286
366
287
367
def export_variable (var_name , var_value ):
0 commit comments