Skip to content

Commit

Permalink
refactor remote access launch script to use tedge-container
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenmiller committed Nov 28, 2024
1 parent c975229 commit e041979
Showing 1 changed file with 13 additions and 60 deletions.
73 changes: 13 additions & 60 deletions files/tedge/launch-remote-access.sh
Original file line number Diff line number Diff line change
@@ -1,70 +1,23 @@
#!/bin/sh
set -e

# Disable spawning from a container
REMOTE_ACCESS_DISABLE_CONTAINER_SPAWN=${REMOTE_ACCESS_DISABLE_CONTAINER_SPAWN:-0}
REMOTE_ACCESS_DISABLE_CONTAINER_SPAWN="${REMOTE_ACCESS_DISABLE_CONTAINER_SPAWN:-0}"

if ! command -V docker >/dev/null 2>&1 || [ ! -e /var/run/docker.sock ] || [ "$REMOTE_ACCESS_DISABLE_CONTAINER_SPAWN" = 1 ]; then
SUDO=
if command -V sudo >/dev/null 2>&1; then
SUDO="sudo -E"
fi

has_container_api_access() {
$SUDO tedge-container self list >/dev/null 2>&1
}

if [ "$REMOTE_ACCESS_DISABLE_CONTAINER_SPAWN" = 1 ] || ! has_container_api_access; then
echo "Launching session as a child process"
c8y-remote-access-plugin "$@"
exit 0
fi

echo "Launching session in an independent container"
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
CONTAINER_NAME=$(hostname)
fi
CONTAINER_ID=$($DOCKER_CMD inspect "$CONTAINER_NAME" --format "{{.Id}}")

# use the same image as the current container
IMAGE=$($DOCKER_CMD inspect "$CONTAINER_ID" --format "{{.Config.Image}}")

# Inherit docker flags
# Note: The single quotes around 'EOT' prevents variable expansion
TEMPLATE=$(
cat <<'EOT'
{{- with .HostConfig}}
{{- range $e := .ExtraHosts}}
--add-host {{printf "%q" $e}} \
{{- end}}
{{- end}}
{{- with .NetworkSettings -}}
{{- range $n, $conf := .Networks}}
{{- with $conf }}
--network {{printf "%q" $n}} \
{{- end}}
{{- end}}
{{- end}}
EOT
)
OPTIONS=$($DOCKER_CMD inspect "$CONTAINER_ID" --format "$TEMPLATE" | tr -d "\n\\\"")

TEDGE_C8Y_URL=$(tedge config get c8y.url)

# Add a host alias so the spawned container can reference the MQTT broker in the current container
MQTT_CLIENT_HOST=$($DOCKER_CMD inspect "$CONTAINER_ID" --format "{{.NetworkSettings.IPAddress}}" ||:)
if [ -z "$MQTT_CLIENT_HOST" ]; then
echo "Getting container ip address from hostname"
MQTT_CLIENT_HOST=$(getent hosts "$CONTAINER_NAME" | cut -d' ' -f1)
fi

echo "Running command"
set -x
# Launch an independent container to handle the remote access session
# so that it can do things like restarting this container
# shellcheck disable=SC2086
$DOCKER_CMD run --rm -d \
$OPTIONS \
--add-host tedge:"$MQTT_CLIENT_HOST" \
-e TEDGE_MQTT_CLIENT_HOST=tedge \
-e TEDGE_C8Y_URL="$TEDGE_C8Y_URL" \
"$IMAGE" \
c8y-remote-access-plugin --child "$@"
$SUDO tedge-container tools run-in-context --rm -- c8y-remote-access-plugin --child "$@"
exit 0

6 comments on commit e041979

@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
16 0 1 16 100 2m11.265902s

Passed Tests

Name ⏱️ Duration Suite
Grace period to allow container to startup 5.045 s Operations
Service is up 0.152 s Operations
Get Logfile Request 2.353 s Operations
Get Configuration File 4.682 s Operations
Execute Shell Command 2.362 s Operations
Install application using docker compose 15.350 s Operations
Get Container Logs 2.370 s Operations
Get Container Logs without explicit container name 2.358 s Operations
Get Container Logs For Non-existent container 2.380 s Operations
Trigger self update via local command 20.051 s Self-Update
Self update should only update if there is a new image 15.770 s Self-Update
Self update using software update operation 30.643 s Self-Update
Rollback when trying to install a non-tedge based image 24.541 s Self-Update
Cloud Connection is Online 0.182 s Telemetry
Service status 0.269 s Telemetry
Sends measurements 2.718 s Telemetry

@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
16 0 1 16 100 2m14.161443999s

Passed Tests

Name ⏱️ Duration Suite
Grace period to allow container to startup 5.040 s Operations
Service is up 0.153 s Operations
Get Logfile Request 2.370 s Operations
Get Configuration File 4.721 s Operations
Execute Shell Command 2.373 s Operations
Install application using docker compose 17.528 s Operations
Get Container Logs 2.377 s Operations
Get Container Logs without explicit container name 2.858 s Operations
Get Container Logs For Non-existent container 2.378 s Operations
Trigger self update via local command 20.769 s Self-Update
Self update should only update if there is a new image 15.835 s Self-Update
Self update using software update operation 30.485 s Self-Update
Rollback when trying to install a non-tedge based image 24.328 s Self-Update
Cloud Connection is Online 0.156 s Telemetry
Service status 0.260 s Telemetry
Sends measurements 2.478 s Telemetry

@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
16 0 1 16 100 2m12.731425s

Passed Tests

Name ⏱️ Duration Suite
Grace period to allow container to startup 5.047 s Operations
Service is up 0.145 s Operations
Get Logfile Request 2.360 s Operations
Get Configuration File 4.694 s Operations
Execute Shell Command 2.379 s Operations
Install application using docker compose 17.473 s Operations
Get Container Logs 2.366 s Operations
Get Container Logs without explicit container name 2.367 s Operations
Get Container Logs For Non-existent container 2.364 s Operations
Trigger self update via local command 19.554 s Self-Update
Self update should only update if there is a new image 17.933 s Self-Update
Self update using software update operation 31.168 s Self-Update
Rollback when trying to install a non-tedge based image 21.952 s Self-Update
Cloud Connection is Online 0.155 s Telemetry
Service status 0.254 s Telemetry
Sends measurements 2.479 s Telemetry

@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
16 0 1 16 100 2m16.95119s

Passed Tests

Name ⏱️ Duration Suite
Grace period to allow container to startup 5.049 s Operations
Service is up 0.149 s Operations
Get Logfile Request 2.364 s Operations
Get Configuration File 4.693 s Operations
Execute Shell Command 2.385 s Operations
Install application using docker compose 17.693 s Operations
Get Container Logs 2.370 s Operations
Get Container Logs without explicit container name 2.382 s Operations
Get Container Logs For Non-existent container 2.371 s Operations
Trigger self update via local command 22.826 s Self-Update
Self update should only update if there is a new image 19.148 s Self-Update
Self update using software update operation 31.950 s Self-Update
Rollback when trying to install a non-tedge based image 20.629 s Self-Update
Cloud Connection is Online 0.163 s Telemetry
Service status 0.259 s Telemetry
Sends measurements 2.475 s Telemetry

@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
16 0 1 16 100 2m29.650737s

Passed Tests

Name ⏱️ Duration Suite
Grace period to allow container to startup 5.045 s Operations
Service is up 0.223 s Operations
Get Logfile Request 2.556 s Operations
Get Configuration File 5.116 s Operations
Execute Shell Command 2.575 s Operations
Install application using docker compose 18.642 s Operations
Get Container Logs 4.737 s Operations
Get Container Logs without explicit container name 2.577 s Operations
Get Container Logs For Non-existent container 2.575 s Operations
Trigger self update via local command 21.594 s Self-Update
Self update should only update if there is a new image 19.355 s Self-Update
Self update using software update operation 34.637 s Self-Update
Rollback when trying to install a non-tedge based image 26.633 s Self-Update
Cloud Connection is Online 0.228 s Telemetry
Service status 0.387 s Telemetry
Sends measurements 2.727 s Telemetry

@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
16 0 1 16 100 2m30.14419s

Passed Tests

Name ⏱️ Duration Suite
Grace period to allow container to startup 5.054 s Operations
Service is up 0.170 s Operations
Get Logfile Request 2.393 s Operations
Get Configuration File 4.808 s Operations
Execute Shell Command 2.434 s Operations
Install application using docker compose 19.810 s Operations
Get Container Logs 2.418 s Operations
Get Container Logs without explicit container name 2.410 s Operations
Get Container Logs For Non-existent container 2.407 s Operations
Trigger self update via local command 23.557 s Self-Update
Self update should only update if there is a new image 21.608 s Self-Update
Self update using software update operation 30.962 s Self-Update
Rollback when trying to install a non-tedge based image 29.014 s Self-Update
Cloud Connection is Online 0.183 s Telemetry
Service status 0.306 s Telemetry
Sends measurements 2.570 s Telemetry

Please sign in to comment.