3
3
import logging
4
4
import os
5
5
import tempfile
6
+ import uuid
6
7
from typing import List , Optional
7
8
8
9
from gitops .common .app import App
9
10
10
- from . import settings
11
+ from . import github , settings , slack
11
12
from .app_definitions import AppDefinitions
12
13
from .git import temp_repo
13
- from .slack import post
14
14
from .types import UpdateAppResult
15
15
from .utils import get_repo_name_from_url , run
16
16
@@ -27,25 +27,37 @@ async def post_init_summary(
27
27
for typ , d in [("Adding" , added_apps ), ("Updating" , updated_apps ), ("Removing" , removed_apps )]:
28
28
if d :
29
29
deltas += f"\n \t • { typ } : { ', ' .join (f'`{ app } `' for app in sorted (d ))} "
30
- await post (
30
+ await slack . post (
31
31
f"A deployment from `{ source } ` has been initiated by *{ username } * for cluster"
32
32
f" `{ settings .CLUSTER_NAME } `, the following apps will be updated:{ deltas } \n Commit Message:"
33
33
f" { commit_message } "
34
34
)
35
35
36
36
37
- async def post_result (source : str , result : UpdateAppResult ):
37
+ async def post_result (app : App , source : str , result : UpdateAppResult ):
38
+ github_deployment_url = str (app .values .get ("github/deployment_url" , "" ))
38
39
if result ["exit_code" ] != 0 :
39
- await post (
40
+ await github .update_deployment (
41
+ github_deployment_url ,
42
+ status = github .STATUSES .failure ,
43
+ description = f"Failed to deploy app. { result ['output' ]} " ,
44
+ )
45
+ await slack .post (
40
46
f"Failed to deploy app `{ result ['app_name' ]} ` from `{ source } ` for cluster"
41
47
f" `{ settings .CLUSTER_NAME } `:\n >>>{ result ['output' ]} "
42
48
)
49
+ else :
50
+ await github .update_deployment (
51
+ github_deployment_url ,
52
+ status = github .STATUSES .in_progress ,
53
+ description = "Helm installed app into cluster. Waiting for pods to deploy." ,
54
+ )
43
55
44
56
45
57
async def post_result_summary (source : str , results : List [UpdateAppResult ]):
46
58
n_success = sum ([r ["exit_code" ] == 0 for r in results ])
47
59
n_failed = sum ([r ["exit_code" ] != 0 for r in results ])
48
- await post (
60
+ await slack . post (
49
61
f"Deployment from `{ source } ` for `{ settings .CLUSTER_NAME } ` results summary:\n "
50
62
f"\t • { n_success } succeeded\n "
51
63
f"\t • { n_failed } failed"
@@ -72,6 +84,7 @@ def __init__(
72
84
self .commit_message = commit_message
73
85
self .current_app_definitions = current_app_definitions
74
86
self .previous_app_definitions = previous_app_definitions
87
+ self .deploy_id = str (uuid .uuid4 ())
75
88
76
89
# Max parallel helm installs at a time
77
90
# Kube api may rate limit otherwise
@@ -130,10 +143,15 @@ async def uninstall_app(self, app: App) -> UpdateAppResult:
130
143
f"helm uninstall { app .name } -n { app .values ['namespace' ]} " , suppress_errors = True
131
144
)
132
145
update_result = UpdateAppResult (app_name = app .name , ** result )
133
- await post_result (self .current_app_definitions .name , update_result )
146
+ await post_result (app , self .current_app_definitions .name , update_result )
134
147
return update_result
135
148
136
149
async def update_app_deployment (self , app : App ) -> Optional [UpdateAppResult ]:
150
+ app .set_value ("deployment.labels.gitops/deploy_id" , self .deploy_id )
151
+ app .set_value ("deployment.labels.gitops/status" , github .STATUSES .in_progress )
152
+ if github_deployment_url := app .values .get ("github/deployment_url" ):
153
+ app .set_value ("deployment.annotations.github/deployment_url" , github_deployment_url )
154
+
137
155
async with self .semaphore :
138
156
logger .info (f"Deploying app { app .name !r} ." )
139
157
if app .chart .type == "git" :
@@ -176,7 +194,8 @@ async def update_app_deployment(self, app: App) -> Optional[UpdateAppResult]:
176
194
return None
177
195
178
196
update_result = UpdateAppResult (app_name = app .name , ** result )
179
- await post_result (self .current_app_definitions .name , update_result )
197
+
198
+ await post_result (app , self .current_app_definitions .name , update_result )
180
199
return update_result
181
200
182
201
def calculate_app_deltas (self ):
0 commit comments