Skip to content

Commit

Permalink
Merge pull request #15 from rgallor/forwarder
Browse files Browse the repository at this point in the history
feat(forwarder): introduce device runtime forwarder configs
  • Loading branch information
harlem88 authored Apr 10, 2024
2 parents d3fdb18 + fa0c6a7 commit 073ad3c
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 56 deletions.
31 changes: 25 additions & 6 deletions package/edgehog-device-runtime/Config.in
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
#
# Copyright 2022 SECO Mind Srl
# Copyright 2022-2024 SECO Mind Srl
#
# SPDX-License-Identifier: Apache-2.0
#

config BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME
bool "edgehog-device-runtime"
depends on BR2_PACKAGE_HOST_RUSTC_TARGET_ARCH_SUPPORTS
depends on BR2_PACKAGE_HOST_PROTOBUF_ARCH_SUPPORTS
depends on BR2_PACKAGE_HAS_UDEV
depends on BR2_PACKAGE_DBUS
depends on BR2_PACKAGE_SQLITE
depends on BR2_FORTIFY_SOURCE_NONE
select BR2_PACKAGE_EDGEHOG_ASTARTE_INTERFACE
select BR2_PACKAGE_UPOWER
select BR2_PACKAGE_CA_CERTIFICATES
select BR2_PACKAGE_HOST_RUSTC
select BR2_PACKAGE_HOST_PROTOBUF
select BR2_PACKAGE_SQLITE
default y
help
This is a portable middleware written in Rust, that enables remote device management
This is a portable middleware written in Rust, that enables remote device management
on Linux-based systems.

https://github.com/edgehog-device-manager/edgehog-device-runtime

if BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME
choice
choice
prompt "Select an astarte library"
default BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY_ASTARTE_DEVICE_SDK
help
Expand Down Expand Up @@ -80,6 +78,14 @@ if BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME
help
Astarte identifies each device with a 128 bit Device ID which has to be unique within its Realm

config BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_IGNORE_SSL
bool "Ignore SSL errors from Astarte"
help
Ignore SSL errors from the Astarte API or MQTT broker.

This allows using a local Astarte instance or connectig to the API
through HTTP or using a self-signed certificate.

config BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_MESSAGE_HUB_ENDPOINT
string "Astarte Message Hub enpoint"
default "http://[::1]:50051"
Expand All @@ -104,4 +110,17 @@ if BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME
default ""
help
Image version that will be appended to the /etc/os-release file.

config BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_FORWARDER
bool "Enable devide forwarder"
select BR2_PACKAGE_TTYD
help
Enable forwarder functionality on a device.

config BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_FORWARDER_COMMAND
string "TTYD command"
default "login"
depends on BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_FORWARDER
help
TTYD command to execute
endif
25 changes: 0 additions & 25 deletions package/edgehog-device-runtime/edgehog-config.toml

This file was deleted.

59 changes: 34 additions & 25 deletions package/edgehog-device-runtime/edgehog-device-runtime.mk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2022 SECO Mind Srl
# Copyright 2022-2024 SECO Mind Srl
#
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -10,60 +10,69 @@
#
################################################################################

EDGEHOG_DEVICE_RUNTIME_VERSION = v0.7.1
EDGEHOG_DEVICE_RUNTIME_VERSION = v0.8.0
EDGEHOG_DEVICE_RUNTIME_SITE = https://github.com/edgehog-device-manager/edgehog-device-runtime
EDGEHOG_DEVICE_RUNTIME_SITE_METHOD = git
EDGEHOG_DEVICE_RUNTIME_LICENSE = Apache License 2.0
EDGEHOG_DEVICE_RUNTIME_LICENSE_FILES = COPYING
EDGEHOG_DEVICE_RUNTIME_DEPENDENCIES = host-protobuf host-sqlite
EDGEHOG_DEVICE_RUNTIME_DEPENDENCIES = host-sqlite

EDGEHOG_DEVICE_RUNTIME_CARGO_ENV = \
HOST_CC="$(HOSTCC)"

EDGEHOG_DEVICE_RUNTIME_FEATURES = systemd

ifeq ($(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_FORWARDER), y)
EDGEHOG_DEVICE_RUNTIME_FEATURES += forwarder
endif

EDGEHOG_DEVICE_RUNTIME_CARGO_BUILD_OPTS= \
--features systemd
--features "$(EDGEHOG_DEVICE_RUNTIME_FEATURES)"

EDGEHOG_DEVICE_RUNTIME_CARGO_INSTALL_OPTS= \
--features "$(EDGEHOG_DEVICE_RUNTIME_FEATURES)"

EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE= $(TARGET_DIR)/etc/edgehog/edgehog-device-runtime-config.toml
EDGEHOG_DEVICE_RUNTIME_SERVICE= $(TARGET_DIR)/usr/lib/systemd/system/edgehog-device-runtime.service
EDGEHOG_DEVICE_RUNTIME_TTYD_SERVICE= $(TARGET_DIR)/usr/lib/systemd/system/ttyd.service

define EDGEHOG_DEVICE_RUNTIME_INSTALL_INIT_SYSTEMD
$(INSTALL) -D -m 644 $(BR2_EXTERNAL_EDGEHOG_PATH)/package/edgehog-device-runtime/edgehog-device-runtime.service \
$(EDGEHOG_DEVICE_RUNTIME_SERVICE)
@if [ '$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_FORWARDER)' = 'y' ]; then \
$(INSTALL) -D -m 644 $(BR2_EXTERNAL_EDGEHOG_PATH)/package/edgehog-device-runtime/ttyd.service \
$(EDGEHOG_DEVICE_RUNTIME_TTYD_SERVICE); \
$(SED) 's/TTYD_COMMAND/$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_FORWARDER_COMMAND)/' \
$(EDGEHOG_DEVICE_RUNTIME_TTYD_SERVICE); \
fi
endef

ifeq ($(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY_ASTARTE_MESSAGE_HUB), y)
ifeq ($(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY_ASTARTE_MESSAGE_HUB), y)
EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY = "astarte-message-hub"
else
EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY = "astarte-device-sdk"
endif

define EDGEHOG_DEVICE_RUNTIME_INSTALL_CONFIG_DIR
$(INSTALL) -d -m 0755 $(TARGET_DIR)/etc/edgehog/
$(INSTALL) -D -m 644 $(BR2_EXTERNAL_EDGEHOG_PATH)/package/edgehog-device-runtime/edgehog-config.toml \
$(EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE)
$(SED) 's/ASTARTE_DEVICE_ID/$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_DEVICE_ID)/' \
$(EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE)
$(SED) 's/ASTARTE_PAIRING_JWT/$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_PAIRING_JWT)/' \
$(EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE)
$(SED) 's|ASTARTE_PAIRING_BASE_URL|$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_PAIRING_BASE_URL)|' \
$(EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE)
$(SED) 's/ASTARTE_REALM/$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_REALM)/' \
$(EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE)
$(SED) 's/ASTARTE_LIBRARY/$(EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY)/' \
$(EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE)
$(SED) 's|ASTARTE_MESSAGE_HUB_ENDPOINT|$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_MESSAGE_HUB_ENDPOINT)|' \
$(EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE)
@if [ -n '$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_CREDENTIAL_SECRET)' ]; then \
$(SED) 's/ASTARTE_CREDENTIAL_SECRET/$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_CREDENTIAL_SECRET)/' \
$(EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE); \
else \
$(SED) '/ASTARTE_CREDENTIAL_SECRET/d' $(EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE); \
fi

$(BR2_EXTERNAL_EDGEHOG_PATH)/package/edgehog-device-runtime/gen_config.sh \
$(EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE) \
$(EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY) \
$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_DEVICE_ID) \
$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_PAIRING_BASE_URL) \
$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_REALM) \
$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_IGNORE_SSL) \
$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_CREDENTIAL_SECRET) \
$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_PAIRING_JWT) \
$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_MESSAGE_HUB_ENDPOINT)

$(SED) 's/EDGEHOG_SERIAL_NUMBER/$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_SERIAL_NUMBER)/' \
$(EDGEHOG_DEVICE_RUNTIME_SERVICE)
$(SED) 's/EDGEHOG_PART_NUMBER/$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_PART_NUMBER)/' \
$(EDGEHOG_DEVICE_RUNTIME_SERVICE)
@if [ -n '$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_IMAGE_VERSION)' ]; then \
$(SED) '/IMAGE_ID/d' $(TARGET_DIR)/etc/os-release; \
echo 'IMAGE_ID=$(BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_IMAGE_VERSION)' >> $(TARGET_DIR)/etc/os-release ; \
fi
endef
Expand Down
95 changes: 95 additions & 0 deletions package/edgehog-device-runtime/gen_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env bash

set -exEuo pipefail

EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE="$1"
EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY="$2"
BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_DEVICE_ID="$3"
BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_PAIRING_BASE_URL="$4"
BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_REALM="$5"
BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_IGNORE_SSL="$6"
BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_CREDENTIAL_SECRET="${7:-}"
BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_PAIRING_JWT="${8:-}"
BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_MESSAGE_HUB_ENDPOINT="${9:-}"

config_device_sdk() {
local device_id="$1"
local pairing_url="$2"
local realm="$3"
local ignore_ssl="$4"

if [ "$ignore_ssl" = 'y' ]; then
ignore_ssl='true'
else
ignore_ssl='false'
fi

cat <<-EOF
[astarte_device_sdk]
device_id = "$device_id"
pairing_url = "$pairing_url"
realm = "$realm"
ignore_ssl = $ignore_ssl
EOF

# Optional
local credentials_secret="$5"
local pairing_token="$6"

if [[ -n $credentials_secret ]]; then
echo "credentials_secret = \"$credentials_secret\""
elif [[ -n $pairing_token ]]; then
echo "pairing_token = \"$pairing_token\""
else
>&2 echo "No credential secret nor pairing token"
exit 1
fi
}

config_message_hub() {
local endpoint="$1"

cat <<-EOF
[astarte_message_hub]
endpoint = "$endpoint"
EOF
}

if [ "$EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY" = "astarte-device-sdk" ]; then
config=$(config_device_sdk \
"$BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_DEVICE_ID" \
"$BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_PAIRING_BASE_URL" \
"$BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_REALM" \
"$BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_IGNORE_SSL" \
"$BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_CREDENTIAL_SECRET" \
"$BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_PAIRING_JWT")
elif [ "$EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY" = "astarte-message-hub" ]; then
config=$(config_message_hub \
"$BR2_PACKAGE_EDGEHOG_DEVICE_RUNTIME_ASTARTE_MESSAGE_HUB_ENDPOINT")
else
>&2 echo "unrecognized library $EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY"
exit 1
fi

cat >"$EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE" <<-EOF
#
# Copyright 2022-2024 SECO Mind Srl
#
# SPDX-License-Identifier: Apache-2.0
#
astarte_library = "$EDGEHOG_DEVICE_RUNTIME_ASTARTE_LIBRARY"
interfaces_directory = "/usr/share/edgehog-astarte-interfaces"
store_directory = "/var/lib/edgehog-device-runtime/"
download_directory = "/var/tmp/"
$config
[[telemetry_config]]
interface_name = "io.edgehog.devicemanager.SystemStatus"
enabled = true
period = 60
EOF

chmod 644 "$EDGEHOG_DEVICE_RUNTIME_CONFIG_TOML_FILE"
20 changes: 20 additions & 0 deletions package/edgehog-device-runtime/ttyd.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# Copyright 2024 SECO Mind Srl
#
# SPDX-License-Identifier: Apache-2.0
#

[Unit]
Description=TTYD
# For development, keep restarting to wait for the platform to come online
StartLimitIntervalSec=0

[Service]
Type=simple
ExecStart=/usr/bin/ttyd --writable TTYD_COMMAND
Restart=on-failure
# Avoid restarting too quickly
RestartSec=2

[Install]
WantedBy=multi-user.target

0 comments on commit 073ad3c

Please sign in to comment.