-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecstatic.py
More file actions
executable file
·218 lines (162 loc) · 5.3 KB
/
ecstatic.py
File metadata and controls
executable file
·218 lines (162 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env python
import os
import json
import logging
import requests
import sys
import boto3
import botocore
from pprint import pformat
from dotenv import load_dotenv
load_dotenv()
ecs = boto3.client('ecs')
#
# setup logger
#
logging.basicConfig()
logger = logging.getLogger( __name__ )
logger.setLevel( os.getenv( 'ECSTATIC_LOG_LEVEL', 'INFO' ) )
logger.info( 'logger level set to {}'
.format( logging.getLevelName( logger.getEffectiveLevel() ) )
)
#
# functions
#
def update_all_cluster_agents():
clusters = ecs.list_clusters().get('clusterArns')
if not clusters:
logger.info( 'no ecs clusters found' )
return
for c in clusters:
update_cluster_agents( [ c ] )
return
def update_cluster_agents( clusters ):
dc = ecs.describe_clusters(
clusters = clusters,
include = [ 'TAGS' ]
)
for cluster in dc.get('clusters'):
cluster_arn = cluster.get('clusterArn')
cluster_name = cluster.get('clusterName')
logger.info(
'checking cluster, arn: {}'
.format( cluster_arn )
)
lci = ecs.list_container_instances(
cluster = cluster_arn
)
cia = lci.get('containerInstanceArns')
if not cia:
logger.info(
'skipping cluster, no containers instances, arn: {}'
.format( cluster_arn )
)
continue
dci = ecs.describe_container_instances(
cluster = cluster_arn,
containerInstances = cia
)
# if this changes to false, then update requests will not be made for the cluster
cluster_healthy = True
for ci in dci.get('containerInstances'):
ec2_instance_id = ci.get('ec2InstanceId')
if ci.get('agentConnected') == 'False' :
logger.warning(
'agent is not connected, ec2_instance_id: {} {}'
.format( ec2_instance_id, pformat( ci ) )
)
send_message_to_slack(
':boom: *agent is not connected*, ec2_instance_id: {}, cluster_arn: {}'
.format( ec2_instance_id, cluster_arn )
)
cluster_healthy = False
continue
if ci.get('agentUpdateStatus') == 'FAILED' :
logger.warning(
'agent update failed, ec2_instance_id: {} {}'
.format( ec2_instance_id, pformat( ci ) )
)
send_message_to_slack(
':boom: *agent update failed*, ec2_instance_id: {}, cluster_arn: {}'
.format( ec2_instance_id, cluster_arn )
)
cluster_healthy = False
continue
# if the cluster has unhealthy instances, then skip update requests
if not cluster_healthy:
logger.warning(
'skipping updates, cluster has unhealthy instances, ec2_instance_id: {}, cluster_arn: {}'
.format( ec2_instance_id, cluster_arn )
)
return
# request agent updates
for ci in dci.get('containerInstances'):
ec2_instance_id = ci.get('ec2InstanceId')
containter_arn = ci.get('containerInstanceArn')
agent_version = ci.get('versionInfo').get('agentVersion')
docker_version = ci.get('versionInfo').get('dockerVersion')
try:
logger.info(
'attempting container agent update, cluster_name: {}, ec2_instance_id: {}'
.format( cluster_name, ec2_instance_id, pformat( ci ) )
)
ecs.update_container_agent(
cluster = cluster_arn,
containerInstance = containter_arn
)
logger.info(
'agent update requested, delaying additional updates until next run, ec2_instance_id: {}, cluster_name: {}'
.format( ec2_instance_id, cluster_name )
)
logger.debug( 'agent update requested, ci: {}'.format( pformat( ci ) ) )
send_message_to_slack(
'agent update requested, delaying additional updates until next run, ec2_instance_id: {}, cluster_arn: {}'
.format( ec2_instance_id, cluster_arn )
)
return
except ecs.exceptions.UpdateInProgressException as err:
logger.info( 'agent update in progress, ec2_instance_id:', ec2_instance_id )
continue
except ecs.exceptions.NoUpdateAvailableException as err:
logger.info(
'agent is current, ec2_instance_id: {}, agent_version: {}, docker_version: {}'
.format( ec2_instance_id, agent_version, docker_version )
)
continue
except Exception as e:
logger.error(
'! unexpected error: {} {}'
.format( sys.exc_info()[0], pformat( e ) )
)
def send_message_to_slack( message ):
webhook_url = os.getenv( 'ECSTATIC_WEBHOOK_URL' )
if not webhook_url:
logger.info( 'skipping slack notification, ECSTATIC_WEBHOOK_URL is undefined' )
return
logger.debug(
'sending message to slack, ECSTATIC_WEBHOOK_URL: {}'
.format( webhook_url )
)
slack_data = { 'text': '@ecstatic: {}'.format( message ) }
response = requests.post(
webhook_url, data=json.dumps( slack_data ),
headers = { 'Content-Type': 'application/json' }
)
if response.status_code != 200:
logger.warn(
'Request to slack returned an error, status_code: {}, text: {}'
.format( response.status_code, response.text )
)
#
# lambda handlers
#
def lambda_handler( event, context ):
update_all_cluster_agents()
return { 'message' : 'success' }
#
# main
#
def main():
update_all_cluster_agents()
if __name__ == "__main__":
main()