Skip to content

Commit f471605

Browse files
authored
Initial version (#599)
* Initial version * Adding debug info * v1.0.1 Renaming variable --------- Signed-off-by: Laurent Rochette <[email protected]>
1 parent e045baa commit f471605

File tree

7 files changed

+366
-0
lines changed

7 files changed

+366
-0
lines changed
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM python:3.11.2-slim-buster
2+
WORKDIR /app
3+
COPY requirements.txt requirements.txt
4+
RUN pip3 install -r requirements.txt
5+
COPY queries queries/
6+
COPY argocd_app_status.py argocd_app_status.py
7+
CMD [ "python3", "argocd_app_status.py"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from gql import Client, gql
2+
from gql.transport.requests import RequestsHTTPTransport
3+
import os
4+
import logging
5+
6+
RUNTIME = os.getenv('RUNTIME')
7+
APPLICATION = os.getenv('APPLICATION')
8+
9+
CF_URL = os.getenv('CF_URL', 'https://g.codefresh.io')
10+
CF_API_KEY = os.getenv('CF_API_KEY')
11+
CF_STEP_NAME= os.getenv('CF_STEP_NAME', 'STEP_NAME')
12+
LOG_LEVEL = os.getenv('LOG_LEVEL', "info")
13+
14+
#######################################################################
15+
16+
17+
def main():
18+
log_format = "%(asctime)s:%(levelname)s:%(name)s.%(funcName)s: %(message)s"
19+
logging.basicConfig(format = log_format, level = LOG_LEVEL.upper())
20+
21+
get_app_status()
22+
23+
## Generating link to the Apps Dashboard
24+
CF_OUTPUT_URL_VAR = CF_STEP_NAME + '_CF_OUTPUT_URL'
25+
link_to_app = get_link_to_apps_dashboard()
26+
export_variable(CF_OUTPUT_URL_VAR, link_to_app)
27+
28+
29+
#######################################################################
30+
31+
def get_query(query_name):
32+
## To do: get query content from a variable, failback to a file
33+
with open('queries/'+query_name+'.graphql', 'r') as file:
34+
query_content = file.read()
35+
return gql(query_content)
36+
37+
def get_runtime():
38+
transport = RequestsHTTPTransport(
39+
url = CF_URL + '/2.0/api/graphql',
40+
headers={'authorization': CF_API_KEY},
41+
verify=True,
42+
retries=3,
43+
)
44+
client = Client(transport=transport, fetch_schema_from_transport=False)
45+
query = get_query('getRuntime') ## gets gql query
46+
variables = {
47+
"runtime": RUNTIME
48+
}
49+
runtime = client.execute(query, variable_values=variables)
50+
return runtime
51+
52+
def get_runtime_ns():
53+
runtime = get_runtime()
54+
runtime_ns = runtime['runtime']['metadata']['namespace']
55+
logging.debug("Runtime Namespace: %", runtime_ns)
56+
return runtime_ns
57+
58+
def get_link_to_apps_dashboard():
59+
runtime_ns= get_runtime_ns()
60+
url_to_app = CF_URL+'/2.0/applications-dashboard/'+RUNTIME+'/'+runtime_ns+'/'+APPLICATION+'/timeline'
61+
return url_to_app
62+
63+
def get_app_status():
64+
gql_api_endpoint = CF_URL + '/2.0/api/graphql'
65+
transport = RequestsHTTPTransport(
66+
url=gql_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('get_app_status') ## gets gql query
73+
variables = {
74+
"runtime": RUNTIME,
75+
"name": APPLICATION,
76+
"namespace": get_runtime_ns()
77+
}
78+
result = client.execute(query, variable_values=variables)
79+
80+
health = result['application']['healthStatus']
81+
sync = result['application']['syncStatus']
82+
export_variable('HEALTH_STATUS', health)
83+
export_variable('SYNC_STATUS', sync)
84+
85+
86+
def export_variable(var_name, var_value):
87+
path = os.getenv('CF_VOLUME_PATH') if os.getenv('CF_VOLUME_PATH') != None else './'
88+
with open(path+'/env_vars_to_export', 'a') as a_writer:
89+
a_writer.write(var_name + "=" + var_value+'\n')
90+
91+
if os.getenv('CF_VOLUME_PATH') == None: os.mkdir('/meta')
92+
with open('/meta/env_vars_to_export', 'a') as a_writer:
93+
a_writer.write(var_name + "=" + var_value+'\n')
94+
95+
logging.info("Exporting variable: %s=%s", var_name, var_value)
96+
97+
##############################################################
98+
99+
if __name__ == "__main__":
100+
main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
HEALTH_STATUS=HEALTHY
2+
SYNC_STATUS=SYNCED
3+
STEP_NAME_CF_OUTPUT_URL=https://g.codefresh.io/2.0/applications-dashboard/csdp/csdp/color-dev/timeline
4+
HEALTH_STATUS=HEALTHY
5+
SYNC_STATUS=SYNCED
6+
STEP_NAME_CF_OUTPUT_URL=https://g.codefresh.io/2.0/applications-dashboard/csdp/csdp/color-dev/timeline
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
query getRuntime(
2+
$runtime: String!) {
3+
runtime(name: $runtime) {
4+
metadata {
5+
name
6+
namespace
7+
}
8+
ingressHost
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
fragment ApplicationShortOperationStateFragment on ApplicationOperationState {
2+
message
3+
phase
4+
type
5+
syncResult {
6+
revision
7+
source {
8+
repoURL
9+
targetRevision
10+
path
11+
chart
12+
__typename
13+
}
14+
resources {
15+
status
16+
hookPhase
17+
__typename
18+
}
19+
__typename
20+
}
21+
__typename
22+
}
23+
24+
query ApplicationsStatusesQuery(
25+
$runtime: String!
26+
$name: String!
27+
$namespace: String
28+
) {
29+
application(runtime: $runtime, name: $name, namespace: $namespace) {
30+
metadata {
31+
runtime
32+
name
33+
runtime
34+
namespace
35+
created
36+
uid
37+
labels {
38+
key
39+
value
40+
__typename
41+
}
42+
cluster
43+
__typename
44+
}
45+
healthStatus
46+
syncStatus
47+
syncPolicy
48+
isHelmApp
49+
repoURL
50+
revision
51+
updatedAt
52+
size
53+
desiredManifest
54+
appsRelations {
55+
referencedBy {
56+
kind
57+
__typename
58+
}
59+
__typename
60+
}
61+
specSource {
62+
repoURL
63+
targetRevision
64+
path
65+
chart
66+
__typename
67+
}
68+
destination {
69+
name
70+
server
71+
namespace
72+
__typename
73+
}
74+
status {
75+
minHistoryId
76+
__typename
77+
}
78+
source {
79+
gitManifest
80+
path
81+
revision
82+
gitSource {
83+
metadata {
84+
name
85+
__typename
86+
}
87+
self {
88+
repoURL
89+
revision
90+
path
91+
__typename
92+
}
93+
permissions {
94+
read
95+
write
96+
user {
97+
id
98+
__typename
99+
}
100+
__typename
101+
}
102+
__typename
103+
}
104+
__typename
105+
}
106+
errors {
107+
__typename
108+
object {
109+
metadata {
110+
kind
111+
name
112+
namespace
113+
runtime
114+
group
115+
version
116+
__typename
117+
}
118+
__typename
119+
}
120+
level
121+
title
122+
message
123+
lastSeen
124+
... on SyncError {
125+
code
126+
context {
127+
repoURL
128+
revision
129+
path
130+
fileUrl
131+
commitAuthor
132+
commitUrl
133+
commitMessage
134+
commitDate
135+
__typename
136+
}
137+
__typename
138+
}
139+
}
140+
operationState {
141+
...ApplicationShortOperationStateFragment
142+
__typename
143+
}
144+
sync {
145+
status
146+
revision
147+
comparedTo {
148+
source {
149+
repoURL
150+
targetRevision
151+
path
152+
chart
153+
__typename
154+
}
155+
__typename
156+
}
157+
__typename
158+
}
159+
__typename
160+
}
161+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
gql==3.4.0
2+
graphql-core==3.2.3
3+
requests==2.28.2
4+
requests-toolbelt==0.10.1
5+
urllib3==1.26.15
6+
multidict==6.0.4
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
kind: step-type
2+
metadata:
3+
name: argocd-app-status
4+
version: 1.0.1
5+
isPublic: true
6+
description: Get Argo CD App status and return SYNC_STATUS and HEALTH_STATUS
7+
sources:
8+
- 'https://github.com/codefresh-io/steps/tree/master/incubating/argocd-app-status'
9+
stage: incubating
10+
maintainers:
11+
- name: Laurent Rochette
12+
13+
categories:
14+
- GitOps
15+
official: true
16+
tags: []
17+
icon:
18+
type: svg
19+
url: https://cdn.jsdelivr.net/gh/codefresh-io/steps/graduated/argocd-sync/argo.svg
20+
background: "#f4f4f4"
21+
examples:
22+
- description: Get Argo CD app status
23+
workflow:
24+
argocd_app_status:
25+
title: Get Argo CD app status
26+
type: argocd-app-status
27+
arguments:
28+
RUNTIME: my-runtime
29+
APPLICATION: my-app
30+
31+
spec:
32+
arguments: |-
33+
{
34+
"definitions": {},
35+
"$schema": "http://json-schema.org/draft-07/schema#",
36+
"type": "object",
37+
"additionalProperties": true,
38+
"patterns": [],
39+
"required": [
40+
"RUNTIME",
41+
"APPLICATION"
42+
],
43+
"properties": {
44+
"RUNTIME": {
45+
"type": "string",
46+
"description": "The name of the GitOps Runtime managing the Argo CD Application"
47+
},
48+
"APPLICATION": {
49+
"type": "string",
50+
"description": "The name of the Argo CD Application to be checked"
51+
},
52+
"IMAGE_NAME": {
53+
"type": "string",
54+
"default": "quay.io/codefreshplugins/argocd-app-status",
55+
"description": "OPTIONAL - To overwrite the image name to use"
56+
},
57+
"IMAGE_TAG": {
58+
"type": "string",
59+
"default": "1.0.1",
60+
"description": "OPTIONAL - To overwrite the tag to use"
61+
}
62+
}
63+
}
64+
stepsTemplate: |-
65+
argocd_app_status:
66+
image: '[[.Arguments.IMAGE_NAME]]:[[.Arguments.IMAGE_TAG]]'
67+
environment:
68+
[[ range $key, $val := .Arguments ]]
69+
- '[[ $key ]]=[[ $val ]]'
70+
[[- end ]]
71+
commands:
72+
- cd /app
73+
- python3 argocd_app_status.py
74+
delimiters:
75+
left: '[['
76+
right: ']]'

0 commit comments

Comments
 (0)