Skip to content

How to upgrade agent (systemd and version tag)

LaV edited this page Jul 29, 2025 · 4 revisions

It is possible to upgrade your agent from netbox with the following setup.

Use systemd with a similar configuration :

        [Unit]
        Description=Netbox Docker Agent
        After=docker.service
        Requires=docker.service
        [Service]
        TimeoutStartSec=0
        ExecStart=/usr/bin/docker run -p 1880:1880 -e ENABLE_EDITOR=1 -v /var/run/docker.sock:/var/run/docker.sock:rw -v netbox-docker-agent:/data --name netbox-docker-agent saashup/netbox-docker-agent
        ExecStop=/usr/bin/docker stop netbox-docker-agent
        Restart=always
        RestartSec=5s
        [Install]
        WantedBy=multi-user.target

upgrade-agent.sh script :

#!/bin/bash
 
_quit(){ 
    local retCode="$1" msg="${*:2}"
 
    printf '%s\n' "$msg"
    exit "$retCode"
}
 
: "${DEBUG:-0}"
if (( DEBUG > 0 )); then
    set -x
    _run="echo"
else
    _run=""
fi
 
docker_run() {
    $_run docker run -d --name "$container_name" "${image_name}:${latest_release[0]}"
}
 
for bin in curl jq docker; do
    type "$bin" &>/dev/null || _quit 1 "Required binary '$bin' not found. Please install it."
done
 
image_name="saashup/netbox-docker-agent"
arch_name="amd64"
container_name="netbox-docker-agent"
 
mapfile -t latest_release < <(curl -s "[https://registry.hub.docker.com/v2/repositories/${image_name}/tags/?page_size=100"](https://registry.hub.docker.com/v2/repositories/$%7Bimage_name%7D/tags/?page_size=100%22) | jq --arg arch "$arch_name" -r '.results[] | select((.images[]?.architecture==$arch) and (.name|test("v.*")) ) | .name ' )
 
 
[[ ${#latest_release[@]} -eq 0 ]] && _quit 1 "No releases found for image '$image_name' with architecture '$arch_name'."
 
image_sha="$(docker inspect --format='{{ index .Image }}' "$container_name")"
image_version="$(docker image inspect --format='{{index .RepoTags 0 }}' "${image_sha/sha256:/}")"
 
if [[ -z "$image_sha" ]]; then
    docker_run && _quit 0 "Container '$container_name' started with the latest image version '${latest_release[0]}'."
fi
 
if [[ -z "$image_version" ]]; then
    _quit 1 "Container '$container_name' is not running or does not exist."
fi
 
if [[ "${image_version##*:}" != "${latest_release[0]}" ]]; then
    printf 'Updating container "%s" to version "%s" ... \n' "$container_name" "${latest_release[0]}"
 
    $_run docker pull "${image_name}:${latest_release[0]}" || _quit 1 "Failed to pull the latest image."
 
    $_run docker stop "$container_name" || _quit 1 "Failed to stop the container '$container_name'."
    $_run docker rm "$container_name" || _quit 1 "Failed to remove the container '$container_name'."
 
    docker_run || _quit 1 "Failed to start the new container with the latest image."
    _quit 0 "Container '$container_name' updated successfully to version '${latest_release[0]}'."
else
    _quit 0 "Container '$container_name' is already up-to-date with version '${image_version##*:}'."
fi

If you run the netbox-docker-agent with this configuration you can then connect to your netbox to upgrade.

Go on the host you want to upgrade and check version and status :

Screenshot from 2025-05-28 11-26-31

Then select the netbox-docker-agent container on this host :

Screenshot from 2025-05-28 11-27-03

Click on the stop Button : Screenshot from 2025-05-28 14-05-33

By stopping the container systemd will detect it and launch the pull / delete and create of the netbox-docker-agent container.

Going back on the Host you will see a new netbox-docker-agent container. Select it and check the status :

Screenshot from 2025-05-28 11-28-01

Going back on the host page you will see the new version of the host agent :

Screenshot from 2025-05-28 11-28-42

Voila! Your agent is up to date.

Clone this wiki locally