1+ from gql import Client , gql
2+ from gql .transport .requests import RequestsHTTPTransport
3+ import os
4+
5+ RUNTIME = os .getenv ('RUNTIME' )
6+ APPLICATION = os .getenv ('APPLICATION' )
7+
8+ CF_URL = os .getenv ('CF_URL' , 'https://g.codefresh.io' )
9+ CF_API_KEY = os .getenv ('CF_API_KEY' )
10+ CF_STEP_NAME = os .getenv ('CF_STEP_NAME' , 'STEP_NAME' )
11+
12+ #######################################################################
13+
14+
15+ def main ():
16+ ingress_host = get_runtime_ingress_host ()
17+ execute_argocd_sync (ingress_host )
18+ ## Generating link to the Apps Dashboard
19+ CF_OUTPUT_URL_VAR = CF_STEP_NAME + '_CF_OUTPUT_URL'
20+ link_to_app = get_link_to_apps_dashboard ()
21+ export_variable (CF_OUTPUT_URL_VAR , link_to_app )
22+
23+
24+ #######################################################################
25+
26+ def get_query (query_name ):
27+ ## To do: get query content from a variable, failback to a file
28+ with open ('queries/' + query_name + '.graphql' , 'r' ) as file :
29+ query_content = file .read ()
30+ return gql (query_content )
31+
32+
33+ def get_runtime ():
34+ transport = RequestsHTTPTransport (
35+ url = CF_URL + '/2.0/api/graphql' ,
36+ headers = {'authorization' : CF_API_KEY },
37+ verify = True ,
38+ retries = 3 ,
39+ )
40+ client = Client (transport = transport , fetch_schema_from_transport = False )
41+ query = get_query ('getRuntime' ) ## gets gql query
42+ variables = {
43+ "runtime" : RUNTIME
44+ }
45+ runtime = client .execute (query , variable_values = variables )
46+ return runtime
47+
48+
49+ def get_runtime_ingress_host ():
50+ ingress_host = None
51+ runtime = get_runtime ()
52+ ingress_host = runtime ['runtime' ]['ingressHost' ]
53+ return ingress_host
54+
55+
56+ def get_link_to_apps_dashboard ():
57+ runtime = get_runtime ()
58+ runtime_ns = runtime ['runtime' ]['metadata' ]['namespace' ]
59+ url_to_app = CF_URL + '/2.0/applications-dashboard/' + RUNTIME + '/' + runtime_ns + '/' + APPLICATION + '/timeline'
60+ return url_to_app
61+
62+
63+ def execute_argocd_sync (ingress_host ):
64+ runtime_api_endpoint = ingress_host + '/app-proxy/api/graphql'
65+ transport = RequestsHTTPTransport (
66+ url = runtime_api_endpoint ,
67+ headers = {'authorization' : CF_API_KEY },
68+ verify = True ,
69+ retries = 3 ,
70+ )
71+ client = Client (transport = transport , fetch_schema_from_transport = False )
72+ query = get_query ('argocd_sync' ) ## gets gql query
73+ variables = {
74+ "applicationName" : APPLICATION ,
75+ "options" : {
76+ "prune" : True
77+ }
78+ }
79+ print ("Syncing app: " , variables )
80+ result = client .execute (query , variable_values = variables )
81+ print (result )
82+
83+
84+ def export_variable (var_name , var_value ):
85+ path = os .getenv ('CF_VOLUME_PATH' ) if os .getenv ('CF_VOLUME_PATH' ) != None else './'
86+ with open (path + '/env_vars_to_export' , 'a' ) as a_writer :
87+ a_writer .write (var_name + "=" + var_value + '\n ' )
88+ print ("Exporting variable: " + var_name + "=" + var_value )
89+
90+ ##############################################################
91+
92+ if __name__ == "__main__" :
93+ main ()
0 commit comments