Skip to content

Commit

Permalink
Merge pull request #31 from thin-edge/feat-container-log-type
Browse files Browse the repository at this point in the history
feat: add container log file handler
  • Loading branch information
reubenmiller authored Sep 29, 2024
2 parents 883b50d + eeab8f2 commit ae2454d
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ COPY files/tedge/software_update.toml /etc/tedge/operations/
COPY files/tedge/self_update.toml /etc/tedge/operations/
COPY files/tedge/self_update.sh /usr/bin/
COPY files/tedge/container_run.tpl /usr/share/tedge/
# Container log_upload customer handler
COPY files/tedge/container-logs.sh /usr/bin/
COPY files/tedge/log_upload.toml /etc/tedge/operations/
COPY files/tedge/log_upload_container.toml /etc/tedge/operations/


ENV S6_BEHAVIOUR_IF_STAGE2_FAILS=2
Expand Down
97 changes: 97 additions & 0 deletions files/tedge/container-logs.sh
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
44 changes: 44 additions & 0 deletions files/tedge/log_upload.toml
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"
19 changes: 19 additions & 0 deletions files/tedge/log_upload_container.toml
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"
3 changes: 3 additions & 0 deletions files/tedge/plugins/tedge-log-plugin.toml
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" },
]
4 changes: 4 additions & 0 deletions tests/main/operations.robot
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ Install application using docker compose
... {"name": "nodered-instance1", "version": "1.0.0", "softwareType": "container-group"}


Get Container Logs
${operation}= Cumulocity.Get Log File container search_text=tedge maximum_lines=100
Cumulocity.Operation Should Be SUCCESSFUL ${operation}

*** Keywords ***
Get Configuration File
[Arguments] ${typename}
Expand Down

1 comment on commit ae2454d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
12 0 1 12 100 1m18.373308s

Passed Tests

Name ⏱️ Duration Suite
Grace period to allow container to startup 5.002 s Operations
Get Logfile Request 3.458 s Operations
Get Configuration File 4.644 s Operations
Execute Shell Command 2.311 s Operations
Install application using docker compose 19.377 s Operations
Get Container Logs 2.314 s Operations
Trigger self update via local command 11.368 s Self-Update
Self update should only update if there is a new image 3.418 s Self-Update
Self update using software update operation 23.738 s Self-Update
Cloud Connection is Online 0.111 s Telemetry
Service status 0.214 s Telemetry
Sends measurements 2.416 s Telemetry

Please sign in to comment.