-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
276 lines (233 loc) · 6.87 KB
/
main.py
File metadata and controls
276 lines (233 loc) · 6.87 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/usr/bin/env python3
import logging
import argparse
import asyncio
from typing import List
from aiobotocore.config import AioConfig
from types_aiobotocore_logs.client import CloudWatchLogsClient
from aiodocker.docker import DockerContainer
from constants import (
MAX_CONNECTION_RETRIES,
MAX_MESSAGE_SIZE_BYTES,
MAX_BATCH_SIZE,
SEND_LOGS_INTERVAL
)
from helpers import (
ensure_cw_log_group_and_stream,
periodic_log_push,
init_aws_session,
init_docker_client,
run_docker_container,
check_container_status,
process_log_messages
)
logger = logging.getLogger(__name__)
def parse_arguments() -> argparse.Namespace:
"""
Parse command line arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument(
'--docker-image',
type=str,
help='Docker image name',
required=True
)
parser.add_argument(
'--bash-command',
type=str,
help='Bash command to run in the Docker container',
required=True
)
parser.add_argument(
'--aws-cloudwatch-group',
type=str,
help='AWS CloudWatch group',
required=True
)
parser.add_argument(
'--aws-cloudwatch-stream',
type=str,
help='AWS CloudWatch stream',
required=True
)
parser.add_argument(
'--aws-access-key-id',
type=str,
help='AWS access key ID',
required=True
)
parser.add_argument(
'--aws-secret-access-key',
type=str,
help='AWS secret access key',
required=True
)
parser.add_argument(
'--aws-region',
type=str,
help='AWS region',
required=True
)
parser.add_argument(
"--log-level",
type=str,
default="info",
choices=[
"debug",
"info",
"warning",
"error",
"critical"
],
help="Set the logging level"
)
args = parser.parse_args()
return args
def setup_logging_level(log_level: str) -> None:
"""
Set the logging level
"""
logging.basicConfig(
level=log_level.upper(),
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
async def log_stream(
container: DockerContainer,
message_buffer: List[bytes]
) -> None:
"""
Stream container logs
It will stop when the container stops
"""
async for line in container.log(follow=True, stdout=True, stderr=True):
logger.debug(line)
message_buffer.append(line.encode('utf-8'))
if not await check_container_status(container):
logger.info("Container has stopped. Finishing log processing.")
break
async def stream_and_push_logs(
container: DockerContainer,
cw_client: CloudWatchLogsClient,
log_group_name: str,
log_stream_name: str
) -> None:
"""
Stream container logs and push them to CloudWatch in batches
"""
message_buffer = []
batch_buffer = []
push_task = None
log_process_task = None
try:
# Stream the logs from the container
log_stream_task = asyncio.create_task(
log_stream(
container, message_buffer
)
)
# Process the log messages in batches
log_process_task = asyncio.create_task(
process_log_messages(
message_buffer=message_buffer,
batch_buffer=batch_buffer,
message_size_bytes=MAX_MESSAGE_SIZE_BYTES,
interval=SEND_LOGS_INTERVAL
)
)
# Create a task for the periodic log push
push_task = asyncio.create_task(
periodic_log_push(
batch_buffer=batch_buffer,
cw_client=cw_client,
log_group_name=log_group_name,
log_stream_name=log_stream_name,
max_batch_size=MAX_BATCH_SIZE,
interval=SEND_LOGS_INTERVAL
)
)
# Wait for the log stream task to finish
await log_stream_task
except asyncio.exceptions.CancelledError:
logger.info('The log push task was cancelled')
finally:
# Cancel the log stream task
if log_stream_task and not log_stream_task.done():
log_stream_task.cancel()
await log_stream_task
# Process the remaining log messages
if log_process_task and not log_process_task.done():
log_process_task.cancel()
await log_process_task
# Push the remaining logs if the task exists
if push_task and not push_task.done():
push_task.cancel()
await push_task
async def main():
container = None
docker_client = None
try:
args = parse_arguments()
setup_logging_level(args.log_level)
docker_client = init_docker_client()
cw_session = init_aws_session()
logger.info('Initializing AWS CloudWatch client')
aws_config = AioConfig(
retries={
'max_attempts': MAX_CONNECTION_RETRIES,
'mode': 'standard'
}
)
async with cw_session.create_client(
service_name='logs',
aws_access_key_id=args.aws_access_key_id,
aws_secret_access_key=args.aws_secret_access_key,
region_name=args.aws_region,
config=aws_config
) as cw_client:
await ensure_cw_log_group_and_stream(
cw_client,
args.aws_cloudwatch_group,
args.aws_cloudwatch_stream
)
container = await run_docker_container(
docker_client,
args.docker_image,
# wrap command using bash -c
['/bin/bash', '-c', args.bash_command]
)
# Stream and push logs to CloudWatch
await stream_and_push_logs(
container,
cw_client,
args.aws_cloudwatch_group,
args.aws_cloudwatch_stream
)
except Exception as e:
logger.error(
f'An error occurred: {e}',
exc_info=True
)
except KeyboardInterrupt:
logger.info(
'The process was interrupted by the user'
)
except asyncio.exceptions.CancelledError:
logger.info(
'The process was cancelled'
)
finally:
logger.info('Cleaning up')
if container is not None:
await container.stop(
# Wait for 10 seconds for the container to stop
t=10
)
await container.delete(
force=True
)
# Close the Docker client
if docker_client is not None:
await docker_client.close()
if __name__ == "__main__":
asyncio.run(main())