Packages
nerves_hub_link
2.10.1
2.12.0
2.11.1
2.11.0
retired
2.10.2
2.10.1
2.10.0
2.9.0
2.9.0-rc.3
2.9.0-rc.2
2.9.0-rc.1
2.8.1
2.8.0
2.7.3
2.7.2
2.7.0
retired
2.6.0
2.5.2
2.5.1
2.5.0
2.4.0
2.3.0
2.2.1
2.2.0
2.1.1
2.1.0
2.0.0
1.4.1
1.4.0
1.3.0
1.2.0
1.1.0
1.0.1
1.0.0
0.13.1
0.13.0
0.12.1
0.12.0
0.11.0
0.10.2
0.10.1
retired
0.10.0
retired
0.10.0-rc.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.2
0.8.1
0.8.0
0.7.6
Manage your Nerves fleet by connecting it to NervesHub
Current section
Files
Jump to
Current section
Files
lib/nerves_hub_link/update_manager/streaming_updater.ex
# SPDX-FileCopyrightText: 2025 Josh Kalderimis
#
# SPDX-License-Identifier: Apache-2.0
#
defmodule NervesHubLink.UpdateManager.StreamingUpdater do
@moduledoc """
This module is responsible for updating firmware using a streaming approach.
It uses the `NervesHubLink.Downloader` module to download the firmware and then sends the chunks to the
`Fwup` module to update the firmware.
This is the default `NervesHubLink.UpdateManager.Updater` used by the `NervesHubLink.UpdateManager` module.
"""
use NervesHubLink.UpdateManager.Updater
alias NervesHubLink.Downloader
alias NervesHubLink.FwupConfig
alias NervesHubLink.UpdateInfo
require Logger
@impl NervesHubLink.UpdateManager.Updater
def start(state) do
{:ok, download} =
Downloader.start_download(state.update_info.firmware_url, state.reporting_download_fun)
{:ok, fwup} =
Fwup.stream(self(), fwup_args(state.fwup_config, state.fwup_public_keys),
fwup_env: state.fwup_config.fwup_env
)
url_without_query =
state.update_info.firmware_url
|> URI.to_string()
|> String.replace(~r/\?.*/, "?...")
Logger.info("[#{log_prefix()}] Downloading firmware: #{url_without_query}")
{:ok,
Map.merge(state, %{
status: {:updating, 0},
download: download,
fwup: fwup
})}
end
@impl NervesHubLink.UpdateManager.Updater
def handle_downloader_message(:complete, state) do
Logger.info("[#{log_prefix()}] Firmware download complete")
{:ok, state}
end
def handle_downloader_message({:error, reason}, state) do
Logger.error("[#{log_prefix()}] Nonfatal HTTP download error: #{inspect(reason)}")
{:ok, state}
end
# Data from the downloader is sent to fwup
def handle_downloader_message({:data, data, _percent}, state) do
Fwup.Stream.send_chunk(state.fwup, data)
{:ok, state}
rescue
error ->
Logger.error("[#{log_prefix()}] Error sending chunk to fwup: #{inspect(error)}")
{:error, error, state}
end
@spec fwup_args(FwupConfig.t(), list(String.t())) :: [String.t()]
defp fwup_args(%FwupConfig{} = config, fwup_public_keys) do
args =
[
"--apply",
"--no-unmount",
"-d",
config.fwup_devpath,
"--task",
config.fwup_task
] ++ config.fwup_extra_options
Enum.reduce(fwup_public_keys, args, fn public_key, args ->
args ++ ["--public-key", public_key]
end)
end
@impl NervesHubLink.UpdateManager.Updater
def log_prefix(), do: "NervesHubLink:StreamingUpdater"
end