Skip to content

Instantly share code, notes, and snippets.

@Schwitzd
Last active August 13, 2024 17:12
Show Gist options
  • Select an option

  • Save Schwitzd/517b5ba2add1bcad9528dd5f37e0fdaf to your computer and use it in GitHub Desktop.

Select an option

Save Schwitzd/517b5ba2add1bcad9528dd5f37e0fdaf to your computer and use it in GitHub Desktop.
MikroTik script to update a single container
# Base container info
:local containerName "lego"
:local containerID [/container find comment~("" . $containerName)]
:log info "Starting container $containerName image update process..."
# Read container settings from the existing container
:local containerVeth [/container/get $containerID interface]
:local containerImage [/container/get $containerID tag]
:local containerEnvList [/container/get $containerID envlist]
:local containerMounts [/container/get $containerID mounts]
:local containerRootDir [/container/get $containerID root-dir]
:local containerDNS [/container/get $containerID dns]
:local containerUser [/container/get $containerID user]
# Stop the Container
/container/stop $containerID
# Wait for container(s) to stop with a timeout of 30 seconds
:local stopTimeout 30
:local stopTimeElapsed 0
:while ( [/container print count-only where comment~$containerName status=stopping] > 0 && $stopTimeElapsed < $stopTimeout) do={
:delay 1
:set stopTimeElapsed ($stopTimeElapsed + 1)
}
# Check if the container failed to stop within the timeout period
:if ($stopTimeElapsed >= $stopTimeout) do={
:log error "Container $containerName failed to stop within the timeout period."
:error "Stopping script due to container stop timeout."
}
# Remove old Container(s)
/container/remove $containerID
# Short delay to ensure removal is processed
:delay 3
# Create a new container
/container/add interface=$containerVeth remote-image=$containerImage mounts=$containerMounts envlist=$containerEnvList root-dir=$containerRootDir dns=$containerDNS user=$containerUser start-on-boot=yes logging=yes comment=$containerName
# Short delay to ensure container is created
:delay 1
# Wait for new container to be ready with a timeout of 60 seconds
:local readyTimeout 60
:local readyTimeElapsed 0
:while ( [/container print count-only where comment~$containerName status=extracting] > 0 && $readyTimeElapsed < $readyTimeout) do={
:delay 1
:set readyTimeElapsed ($readyTimeElapsed + 1)
}
# Check if the container failed to be ready within the timeout period
:if ($readyTimeElapsed >= $readyTimeout) do={
:log error "Container $containerName failed to be ready within the timeout period."
:error "Stopping script due to container readiness timeout."
}
# Start the new container
/container/start [find where comment~$containerName]
:log info "Container $containerName updated and started successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment