A behaviour to help orchestrate the complete workflow of downloading and installing/applying firmware updates.
This module provides a set of callbacks that must be implemented by any
updater module. The callbacks are used to start the update process, handle
messages from the NervesHubLink.Downloader, decide when to start the installation via
the Fwup library, and perform any necessary cleanup.
Creating a new updater module involves implementing the following callbacks:
start_update/3: Called byNervesHubLink.UpdateManagerto start the update process.start/1: Callback to setup, prepare, and trigger the download.handle_downloader_message/2: Process messages from theNervesHubLink.Downloader.handle_fwup_message/2: Process messages received from theFwuplibraryhandle_message/2: Process any other message sent to the updater process.cleanup/1: Perform any necessary cleanup.
To simplify the implementation of these callbacks, you can use the provided
NervesHubLink.UpdateManager.Updater module as a base. This module provides
default implementations for the callbacks, which you can override as needed.
Example Usage
Here's an example of how to create a new updater module using the provided
NervesHubLink.UpdateManager.Updater module as a base:
defmodule MyApp.Updater do
use NervesHubLink.UpdateManager.Updater
def start_update(update_info, fwup_config, fwup_public_keys) do
# Implement the start_update callback
end
def start(state) do
# eg. Setup a temporary directory for storing downloaded files
end
def handle_downloader_message(message, state) do
# eg. Forward packets to the Fwup module
end
# Use the default implementation provided by the base module
#
# def handle_fwup_message(message, state) do
# end
def cleanup(state) do
# eg. Remove any temporary files or resources created during the update process
end
def log_prefix do
"[MyApp.Updater]"
end
end
Summary
Callbacks
Run any cleanup that might need to take place
Process messages from the Downloader
Process messages received from the Fwup library
Process any other message sent to the updater process.
Report the update's progress to NervesHub.
A little hook to allow for customization of the logging prefix
Setup and prepare for the firmware update.
Start an updater GenServer
Types
Callbacks
@callback cleanup(state :: term()) :: :ok
Run any cleanup that might need to take place
@callback handle_downloader_message(message :: term(), state :: term()) :: {:ok, new_state :: term()} | {:error, reason :: term(), new_state :: term()} | {:stop, reason :: term(), new_state :: term()}
Process messages from the Downloader
@callback handle_fwup_message(message :: term(), state :: term()) :: {:ok, new_state :: term()} | {:stop, reason :: term(), new_state :: term()}
Process messages received from the Fwup library
@callback handle_message(message :: term(), state :: term()) :: {:ok, new_state :: term()} | {:stop, reason :: term(), new_state :: term()}
Process any other message sent to the updater process.
This is how an updater picks up messages it sends itself, which is useful for
work that shouldn't happen inside a handle_downloader_message/2 callback -
the NervesHubLink.Downloader blocks while that callback runs, and gives up
on the updater if it takes too long.
Exits that aren't from the download in state.download arrive here as
{:EXIT, pid, reason}, which is how an updater running more than one
download at a time finds out that one of them stopped.
Optional. Updaters built on this module get an implementation that logs and carries on.
@callback handle_progress( stage :: :downloading | :updating, percent :: number(), state :: term() ) :: new_state :: term()
Report the update's progress to NervesHub.
stage is :downloading while the firmware arrives and :updating while fwup
writes it. Updaters built on this module call it through state.updater, the
module that started the update, so an updater that hands its work to another one
can still change what is reported, for example to put work of its own before the
download in the same progress bar. It is a callback rather than a message to
handle_message/2 so that an updater whose handle_message/2 ignores what it
doesn't know still reports progress.
Optional. Updaters built on this module get an implementation that sends the progress at most every 500 ms, and only when it has grown.
@callback log_prefix() :: String.t()
A little hook to allow for customization of the logging prefix
Setup and prepare for the firmware update.
@callback start_update( NervesHubLink.Message.UpdateInfo.t(), NervesHubLink.FwupConfig.t(), fwup_public_keys :: [] ) :: GenServer.on_start()
Start an updater GenServer