NervesHubLink.UpdateManager.Updater behaviour (nerves_hub_link v2.9.0)
View SourceA 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 theFwuplibrarycleanup/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
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 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