-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from thin-edge/feat-container-log-type
feat: add container log file handler
- Loading branch information
Showing
6 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#!/bin/sh | ||
set -e | ||
|
||
CONTAINER_NAME= | ||
DATE_FROM=${DATE_FROM:-"24h"} | ||
DATE_TO=${DATE_TO:-"0m"} | ||
MAX_LINES=${MAX_LINES:-1000} | ||
UPLOAD_URL= | ||
TYPE=${TYPE:-container} | ||
|
||
while [ $# -gt 0 ]; do | ||
case "$1" in | ||
--container) | ||
CONTAINER_NAME="$2" | ||
shift | ||
;; | ||
--type) | ||
TYPE="$2" | ||
shift | ||
;; | ||
--since) | ||
DATE_FROM="$2" | ||
shift | ||
;; | ||
--until) | ||
DATE_TO="$2" | ||
shift | ||
;; | ||
--max-lines|-n) | ||
MAX_LINES="$2" | ||
shift | ||
;; | ||
--url) | ||
UPLOAD_URL="$2" | ||
shift | ||
;; | ||
--help|-h) | ||
;; | ||
*) | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
DOCKER_CMD=docker | ||
if ! docker ps >/dev/null 2>&1; then | ||
if command -V sudo >/dev/null 2>&1; then | ||
DOCKER_CMD="sudo docker" | ||
fi | ||
fi | ||
|
||
CONTAINER_NAME=${CONTAINER_NAME:-} | ||
if [ -z "$CONTAINER_NAME" ]; then | ||
# Use the name of the container rather than the hostname as it human friendly | ||
# and strip any leading slash (/) | ||
CONTAINER_NAME=$($DOCKER_CMD inspect "$(hostname)" --format "{{.Name}}" | sed 's|^/||g') | ||
fi | ||
|
||
TMP_LOG_DIR=$(mktemp -d) | ||
# Ensure directory is always deleted afterwards | ||
trap 'rm -rf -- "$TMP_LOG_DIR"' EXIT | ||
TMP_FILE="${TMP_LOG_DIR}/${TYPE}_${CONTAINER_NAME}_$(date -Iseconds).log" | ||
|
||
# Add log header to give information about the contents | ||
{ | ||
echo "---------------- log parameters ----------------------" | ||
echo "container: $CONTAINER_NAME" | ||
echo "dateFrom: $DATE_FROM" | ||
echo "dateTo: $DATE_TO" | ||
echo "maxLines: $MAX_LINES" | ||
echo "command: $DOCKER_CMD logs -n \"$MAX_LINES\" --since \"$DATE_FROM\" --until \"$DATE_TO\" \"$CONTAINER_NAME\"" | ||
echo "------------------------------------------------------" | ||
echo | ||
} > "$TMP_FILE" | ||
|
||
# Write logs to file (stripping any ansci colour codes) | ||
$DOCKER_CMD logs -n "$MAX_LINES" --since "$DATE_FROM" --until "$DATE_TO" "$CONTAINER_NAME" \ | ||
| sed -e 's/\x1b\[[0-9;]*m//g' \ | ||
| tee -a "$TMP_FILE" | ||
|
||
echo "Uploading log file to $UPLOAD_URL" >&2 | ||
|
||
# Use mtls if configured | ||
if [ -f "$(tedge config get http.client.auth.key_file)" ] && [ -f "$(tedge config get http.client.auth.cert_file)" ]; then | ||
# Upload using mtl | ||
echo "Uploading log file using mtls" | ||
curl -4 -sf \ | ||
-XPUT \ | ||
--data-binary "@$TMP_FILE" \ | ||
--capath "$(tedge config get http.ca_path)" \ | ||
--key "$(tedge config get http.client.auth.key_file)" \ | ||
--cert "$(tedge config get http.client.auth.cert_file)" \ | ||
"$UPLOAD_URL" | ||
else | ||
# Upload using default | ||
curl -4 -sf -XPUT --data-binary "@$TMP_FILE" "$UPLOAD_URL" | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
operation = "log_upload" | ||
|
||
[init] | ||
action = "proceed" | ||
on_success = "executing" | ||
|
||
[executing] | ||
action = "proceed" | ||
on_success = "check" | ||
|
||
[check] | ||
script = "sh -c '[ ${.payload.type} = container ] && exit 0 || exit 1'" | ||
on_exit.0 = "custom_log_handler" | ||
on_exit.1 = "process" | ||
on_exit._ = "failed" | ||
|
||
[custom_log_handler] | ||
# Optional step where a self-update is performed | ||
operation = "log_upload_${.payload.type}" | ||
input.type = "${.payload.type}" | ||
input.lines = "${.payload.lines}" | ||
input.dateFrom = "${.payload.dateFrom}" | ||
input.dateTo = "${.payload.dateTo}" | ||
input.searchText = "${.payload.searchText}" | ||
input.tedgeUrl = "${.payload.tedgeUrl}" | ||
on_exec = "await-log-handler" | ||
|
||
[await-log-handler] | ||
action = "await-operation-completion" | ||
on_success = "successful" | ||
|
||
[process] | ||
operation = "builtin:log_upload" | ||
on_exec = "wait-for-builtin-log-handler" | ||
|
||
[wait-for-builtin-log-handler] | ||
action = "await-operation-completion" | ||
on_success = "successful" | ||
|
||
[successful] | ||
action = "cleanup" | ||
|
||
[failed] | ||
action = "cleanup" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
operation = "log_upload_container" | ||
|
||
[init] | ||
action = "proceed" | ||
on_success = "executing" | ||
|
||
[executing] | ||
action = "proceed" | ||
on_success = "process" | ||
|
||
[process] | ||
script = "container-logs.sh --type ${.payload.type} -n ${.payload.lines} --since ${.payload.dateFrom} --until ${.payload.dateTo} --container ${.payload.searchText} --url ${.payload.tedgeUrl}" | ||
on_success = "successful" | ||
|
||
[successful] | ||
action = "cleanup" | ||
|
||
[failed] | ||
action = "cleanup" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
files = [ | ||
{ type = "software-management", path = "/data/tedge/logs/agent/workflow-software*" }, | ||
{ type = "shell", path = "/data/tedge/logs/agent/c8y_Command-*" }, | ||
|
||
# Note: Custom log handler, log_upload_container and the path is ignored | ||
{ type = "container", path = "/tmp/container" }, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ae2454d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Robot Results
Passed Tests
Operations
Operations
Operations
Operations
Operations
Operations
Self-Update
Self-Update
Self-Update
Telemetry
Telemetry
Telemetry