NervesHubLink (nerves_hub_link v2.14.0)

Copy Markdown View Source

The Device-side client for NervesHub.

The :nerves_hub_link Erlang application will start by default if installed as a dependency and use provided configuration to connect to a NervesHub server.

This module primarily provides utility functions for checking the status of the connection and performing some operations such as reconnecting, sending a file to a connected console and more.

Summary

Types

How a device receives firmware.

Functions

Ask NervesHub whether there is firmware waiting for this device.

Checks if the device is connected to the NervesHub device channel.

Return whether there's currently an active console session

Checks if the device is connected to the NervesHub console channel.

Checks if the device is connected to the NervesHub extensions channel.

Whether NervesHub allows this device to manage its own updates.

Restart the socket and device channel

Report an error your application caught to NervesHub.

Send a file to the connected console

Send an update status to web

Ask NervesHub to change how this device receives firmware.

Checks if the device has a socket connection with NervesHub

Ask NervesHub for firmware, and start applying it.

Current status of the update manager

How this device receives firmware, as NervesHub last reported it.

Types

update_mode()

@type update_mode() :: :off | :automatic | :device_managed

How a device receives firmware.

:automatic is the default — the device's deployment group sends firmware on its own schedule. :device_managed means the device asks for firmware itself, on whatever schedule suits it; its deployment group still decides which firmware. :off means it takes none but a manual push from an operator.

update_status()

@type update_status() ::
  :received
  | {:started, downloader_network_interface :: String.t() | nil}
  | {:downloading, non_neg_integer()}
  | {:updating, non_neg_integer()}
  | :completed
  | {:ignored, reason :: String.t()}
  | {:reschedule, delay_for :: pos_integer()}
  | {:reschedule, delay_for :: pos_integer(), reason :: String.t()}
  | {:failed, reason :: String.t()}

Functions

check_for_update(server \\ Socket)

@spec check_for_update(GenServer.server()) ::
  {:ok, %{available?: boolean(), firmware_meta: map() | nil}} | {:error, term()}

Ask NervesHub whether there is firmware waiting for this device.

Allowed in every update mode, including :off — a frozen device can still tell its user that an update exists and an administrator needs to act.

The answer carries firmware metadata and no URL. Firmware URLs are signed and time limited, so one is fetched when it is about to be used, by start_update/1.

Example

NervesHubLink.check_for_update()
#=> {:ok, %{available?: true, firmware_meta: %{"version" => "1.2.0"}}}

connected?(server \\ Socket)

@spec connected?(GenServer.server()) :: boolean()

Checks if the device is connected to the NervesHub device channel.

console_active?(server \\ Socket)

@spec console_active?(GenServer.server()) :: boolean()

Return whether there's currently an active console session

console_connected?(server \\ Socket)

@spec console_connected?(GenServer.server()) :: boolean()

Checks if the device is connected to the NervesHub console channel.

extensions_connected?(server \\ Socket)

@spec extensions_connected?(GenServer.server()) :: boolean()

Checks if the device is connected to the NervesHub extensions channel.

managed_updates_allowed?(server \\ Socket)

@spec managed_updates_allowed?(GenServer.server()) :: boolean()

Whether NervesHub allows this device to manage its own updates.

An operator grants this per device, and it is off until they do. A settings screen should ask before offering the choice, rather than offering a switch NervesHub will refuse.

reconnect(server \\ Socket)

@spec reconnect(GenServer.server()) :: :ok

Restart the socket and device channel

report_error(error, stacktrace \\ [], opts \\ [])

@spec report_error(term(), Exception.stacktrace(), keyword()) :: :ok

Report an error your application caught to NervesHub.

Crashes are reported on their own once the Error Reports extension is turned on. This is for the ones that never reach the runtime: an error you rescued, handled, and still want to see grouped across the fleet.

try do
  charge(order)
rescue
  error -> NervesHubLink.report_error(error, __STACKTRACE__, group: "payments")
end

Options

  • :group - group these reports under a key of your own rather than by their stacktrace. Every failure in a payment integration arriving through one HTTP client function is one issue to the person on call, and six issues to a stacktrace.
  • :context - a map of extra detail to attach to this report alone. What you want on every report belongs in NervesHubLink.Extensions.ErrorReports.Config.context/0 instead.
  • :kind - :error (the default), :exit or :throw, matching the clauses of try/1.

Returns :ok whether or not the extension is running, so a call to this is never the thing that takes an application down. The report is buffered and goes out with the next batch; nothing is sent at all until NervesHub attaches the extension.

send_file(server \\ Socket, file_path)

@spec send_file(GenServer.server(), Path.t()) ::
  :ok | {:error, :too_large | File.posix()}

Send a file to the connected console

send_update_status(server \\ Socket, status)

@spec send_update_status(GenServer.server(), update_status()) :: :ok

Send an update status to web

set_update_mode(server \\ Socket, mode)

@spec set_update_mode(GenServer.server(), :automatic | :device_managed) ::
  :ok | {:error, term()}

Ask NervesHub to change how this device receives firmware.

A device can move itself between :automatic and :device_managed, and only once an operator has allowed it — see managed_updates_allowed?/1. It can never set :off, which would let a device stop its own updates and leave nobody able to fix it remotely.

The mode is held by NervesHub rather than here, so it survives a reboot without this library persisting anything, and update_mode/1 always reports what NervesHub actually has.

Returns {:error, :disconnected} when there is no connection to ask over. The change is not queued: an application that must converge should re-assert it from NervesHubLink.Client.connected/0, which is safe to repeat.

Example

NervesHubLink.set_update_mode(:device_managed)
#=> :ok

# or, when an operator has not allowed this device to manage its own updates
NervesHubLink.set_update_mode(:device_managed)
#=> {:error, :not_permitted}

socket_connected?(server \\ Socket)

@spec socket_connected?(GenServer.server()) :: boolean()

Checks if the device has a socket connection with NervesHub

start_update(server \\ Socket)

@spec start_update(GenServer.server()) :: :ok | {:error, term()}

Ask NervesHub for firmware, and start applying it.

Returns :ok once NervesHub has sent the update — from there it proceeds exactly as an update the deployment group pushed, reporting through NervesHubLink.Client.

Deployment groups pace their rollouts, and a device asking for firmware takes a slot in that pacing like any other update. A device that finds none free gets {:error, {:busy, minutes}} and should try again no sooner than that.

Example

NervesHubLink.start_update()
#=> :ok

# or, when the deployment group has no room right now
NervesHubLink.start_update()
#=> {:error, {:busy, 5}}

status(server \\ UpdateManager)

Current status of the update manager

update_mode(server \\ Socket)

@spec update_mode(GenServer.server()) :: update_mode() | nil

How this device receives firmware, as NervesHub last reported it.

nil until NervesHub says. A server too old to know about update modes never will, and the device goes on taking updates exactly as it did before — so treat nil as "not device managed" rather than as an error.

Example

NervesHubLink.update_mode()
#=> :automatic