diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index f2f281c..a728113 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -65,7 +65,7 @@ jobs: gh release upload "$TAG" tools/onboard/dist/* --clobber # One Data Preparation package per Smart Function, which the Data - # Preparation UI imports and cumulocity/setup-tenant.sh deploys. + # Preparation UI imports. smart-functions: name: Smart Function packages needs: release-please @@ -85,6 +85,34 @@ jobs: cumulocity/smart-functions/package.sh dist gh release upload "$TAG" dist/*.tar.gz --clobber + # The tenant setup as a c8y-tenant-sync bundle: cumulocity/tenant.yaml with + # the DTM parameter sets and Smart Functions it deploys. It has no version + # in its name, so releases/latest/download/tiny-edge-tenant.tar.gz always + # gives the latest. + tenant-bundle: + name: Tenant bundle + needs: release-please + if: needs.release-please.outputs.released == 'true' + runs-on: ubuntu-24.04 + permissions: + contents: write + steps: + - uses: actions/checkout@v4 + with: + ref: ${{ needs.release-please.outputs.tag }} + - uses: reubenmiller/setup-go-c8y-cli@main + with: + showVersion: false + showTenant: false + extensions: reubenmiller/c8y-tenant-sync@v0.11.1 + - name: Bundle and attach + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ needs.release-please.outputs.tag }} + run: | + c8y tenant-sync bundle cumulocity/tenant.yaml -o tiny-edge-tenant.tar.gz + gh release upload "$TAG" tiny-edge-tenant.tar.gz --clobber + # One checksums file for all of tedge-onboard's archives, which # "c8y extensions install" uses to verify the download. It runs after # every build has attached its archives. diff --git a/cumulocity/README.md b/cumulocity/README.md index adba4a5..1a9da6c 100644 --- a/cumulocity/README.md +++ b/cumulocity/README.md @@ -17,33 +17,64 @@ parameters, shell) need no mapping. ## Set up a tenant -With a go-c8y-cli session for the tenant (`set-session`), one command does -everything below: +[c8y-tenant-sync](https://github.com/reubenmiller/c8y-tenant-sync), a +go-c8y-cli extension, sets up a tenant from [tenant.yaml](tenant.yaml). It +declares everything the devices need: -```sh -cumulocity/setup-tenant.sh --dry-run # what it would change; changes nothing -cumulocity/setup-tenant.sh -``` +| Section | What it sets up | +|---|---| +| `features` | `mqtt-service.smartrest`, `certificate-authority`, and `data-prep.ui-visibility`, which shows the Data Preparation application | +| `microservices` | `mqtt-service`, `data-prep-ctrl` (Smart Functions), `dtm` and `device-parameter` (Parameters tab) | +| `applications` | Data Preparation and Digital Twin Manager | +| `digitalTwin` | the DTM parameter sets in `dtm/` | +| `smartFunctions` | every Smart Function in `smart-functions/` | -It prints the host it is about to change, then: +Without `mqtt-service.smartrest` the MQTT Service drops a device as soon as +it subscribes to `devicecontrol/notifications`; the device logs -113. -1. Checks that the tenant features `mqtt-service.smartrest` and - `certificate-authority` are active, and stops if one is not, naming the - command that enables it (`c8y features enable --key `). - `--enable-features` enables them instead. Without - `mqtt-service.smartrest` the MQTT Service drops a device as soon as it - subscribes to `devicecontrol/notifications`; the device logs -113. -2. Deploys every Smart Function under `smart-functions/`, and reports each - rule's HTTP status. Any status other than 200 fails the script. -3. Registers the DTM parameter sets with `dtm/register.sh`. A set that is - already there is left alone; `--replace` re-registers it. +Each GitHub release (after 0.10.2) carries the manifest and its files as a +bundle, `tiny-edge-tenant.tar.gz`. Install the extension once, then, with a +go-c8y-cli session for the tenant (`set-session`), apply the latest release: + +```sh +c8y extensions install reubenmiller/c8y-tenant-sync +c8y tenant-sync run https://github.com/thin-edge/tiny-edge/releases/latest/download/tiny-edge-tenant.tar.gz --dry # what it would change; changes nothing +c8y tenant-sync run https://github.com/thin-edge/tiny-edge/releases/latest/download/tiny-edge-tenant.tar.gz +``` -An application registers its own parameter sets with `--dtm-dir`, alone or -with the rest: +From a checkout, `c8y tenant-sync run --dry` in this directory applies the +checkout's `tenant.yaml`. + +The run enables a feature that is not active, subscribes a missing +application, deploys a Smart Function whose code, configuration or tests +differ from the deployed rule, and creates or updates a DTM definition that +differs from its file. Anything already in place is left alone, so the run +can be repeated, and it fails when an item fails. The applications are owned +by the `management` tenant: a tenant which may not subscribe them itself +reports them as skipped, and its parent tenant (or the Cumulocity provider) +subscribes them. `--only digitalTwin,smartFunctions` applies only the DTM +sets and the Smart Functions. + +To keep the tenant on the release its devices run, include that release's +bundle from the tenant's own manifest. An application adds its own +parameter sets there too: + +```yaml +# tenant.yaml +include: + - github: + repo: thin-edge/tiny-edge + release: v # or latest + assets: ["tiny-edge-tenant.tar.gz"] + +digitalTwin: + propertyDefinitions: + - source: + path: ./cumulocity/dtm # the application's sets: every *.json in it +``` ```sh -path/to/tiny-edge/cumulocity/setup-tenant.sh --dtm-dir cumulocity/dtm -path/to/tiny-edge/cumulocity/dtm/register.sh --dir cumulocity/dtm # only the application's sets +c8y tenant-sync run -f tenant.yaml --dry ``` Set up the tenant before devices connect, or reconnect them afterwards @@ -53,19 +84,16 @@ connection. Without the twin Smart Function those messages are dropped, so a device that connected first shows no Parameters tab until it reconnects. Measurements recover on their own because the device keeps sending them. -`--no-smart-functions` and `--no-dtm` skip steps 2 and 3 (`--dtm-dir` sets -are still registered). `--help` lists every option. - ## Smart Functions Each directory is a Data Preparation rule package: `data-prep.yaml` (the -topic pattern), the JavaScript, and `tests/*.yaml`. `setup-tenant.sh` -deploys all of them. Each GitHub release also carries one -`.tar.gz` per Smart Function, which you can import in the Data -Preparation UI or deploy as it is. `smart-functions/package.sh ` -builds the same archives from a checkout. +topic pattern), the JavaScript, and `tests/*.yaml`. The manifest deploys all +of them. Each GitHub release also carries one `.tar.gz` per Smart +Function, which you can import in the Data Preparation UI. +`smart-functions/package.sh ` builds the same archives from a +checkout. -To deploy one package by hand, send it as the raw request body: +To deploy a package without c8y-tenant-sync, send it as the raw request body: ```sh cumulocity/smart-functions/package.sh /tmp/rules @@ -128,14 +156,12 @@ client writes both fragments itself. ## Parameters tab (DTM) -The tenant needs the `dtm` and `device-parameter` microservices, and the -user registering needs `ROLE_DIGITAL_TWIN_DEFINITIONS_CREATE` and `_ADMIN`. +The tenant needs the `dtm` and `device-parameter` microservices (the +manifest subscribes them), and the user registering needs `ROLE_DIGITAL_TWIN_DEFINITIONS_CREATE` and `_ADMIN`. -```sh -cumulocity/dtm/register.sh # registers every set not yet there -cumulocity/dtm/register.sh --replace zephyr_modbus_telemetry # after a declaration changed -cumulocity/dtm/register.sh --dry-run # what it would register; changes nothing -``` +The manifest registers the sets in `dtm/`, and updates a definition when its +file changes: after a declaration changed, regenerate the file and run the +manifest again. | Identifier | Declared by | Parameters | |---|---|---| @@ -204,7 +230,7 @@ them needs these changes once its devices run a newer release: `tedge-zephyr-*` ones. They take the same topics, so with both deployed every message is stored twice. - **Parameters tab:** register `tiny_edge` and `tiny_edge_remote_access` - (`cumulocity/dtm/register.sh`). Keep the `zephyr_tedge` definitions while + (the manifest does, see "Set up a tenant"). Keep the `zephyr_tedge` definitions while devices on older releases remain. - **Old fragments:** an updated device writes `tiny_edge` and `tiny_edge_remote_access`, and leaves the old `zephyr_tedge` fragments on diff --git a/cumulocity/dtm/register.sh b/cumulocity/dtm/register.sh deleted file mode 100755 index 7c09ae4..0000000 --- a/cumulocity/dtm/register.sh +++ /dev/null @@ -1,86 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: Apache-2.0 -# -# Register the client's own parameter sets in a tenant's Digital Twin -# Manager, so the device's Parameters tab can show and edit them. -# -# cumulocity/dtm/register.sh [--replace] [--dry-run] [--dir ] [set...] -# -# Without sets it registers every *.json in (default: this directory), -# so an application can register its own sets with this script: -# path/to/tiny-edge/cumulocity/dtm/register.sh --dir cumulocity/dtm -# -# Needs a go-c8y-cli session, the dtm and device-parameter microservices, and -# ROLE_DIGITAL_TWIN_DEFINITIONS_CREATE/_ADMIN. An identifier that already -# exists is left alone; --replace deletes and re-registers it (DTM cannot -# update one in place). --dry-run only reads the tenant and says what it -# would do. The files were printed by the firmware itself -# (`tedge params schema `, CONFIG_TEDGE_PARAMETERS_SCHEMA), so they match -# what the devices validate against; regenerate them when a declaration changes. -set -euo pipefail -dir=$(dirname "$0") -replace=0 -dry=0 -failed=0 -while [[ $# -gt 0 ]]; do - case $1 in - --replace) replace=1 ;; - --dry-run) dry=1 ;; - --dir) dir=${2:?--dir needs a directory}; shift ;; - -h | --help) sed -n '3,19s/^# \{0,1\}//p' "$0"; exit 0 ;; - -*) echo "unknown option: $1" >&2; exit 2 ;; - *) break ;; - esac - shift -done -cd "$dir" -sets=("$@") -if [[ ${#sets[@]} -eq 0 ]]; then - for f in *.json; do - [[ -f $f ]] && sets+=("${f%.json}") - done -fi -if [[ ${#sets[@]} -eq 0 ]]; then - echo "no *.json definitions in $(pwd)" >&2 - exit 1 -fi -# The listing is paged whatever pageSize asks for (a tenant with ~20 -# definitions returned them over several pages), so read every page: a check -# that saw only the first made --replace skip the delete and the POST fail. -exists() { - local page=1 found - while :; do - found=$(c8y api GET "/service/dtm/definitions/properties?pageSize=100¤tPage=$page" --raw < /dev/null | - python3 -c 'import json,sys; d=json.load(sys.stdin); d=d.get("definitions", []) if isinstance(d, dict) else d; print("yes" if any(i.get("identifier")==sys.argv[1] for i in d) else ("end" if not d else "no"))' "$1") - case $found in - yes) return 0 ;; - end) return 1 ;; - esac - page=$((page + 1)) - done -} -for s in "${sets[@]}"; do - if exists "$s"; then - if [[ $replace -eq 0 ]]; then - echo "exists: $s (use --replace to re-register)" - continue - fi - if [[ $dry -eq 1 ]]; then - echo "would replace: $s" - continue - fi - c8y api DELETE "/service/dtm/definitions/properties/$s?contexts=asset,event,operation" --force < /dev/null >/dev/null - echo "deleted: $s" - elif [[ $dry -eq 1 ]]; then - echo "would register: $s" - continue - fi - if out=$(c8y api POST /service/dtm/definitions/properties --data "$(cat "$s.json")" --force --raw < /dev/null 2>&1); then - echo "registered: $s" - else - # DTM refuses a duplicate identifier *or* title (HTTP 409). - echo "failed: $s: $out" >&2 - failed=1 - fi -done -exit $failed diff --git a/cumulocity/setup-tenant.sh b/cumulocity/setup-tenant.sh deleted file mode 100755 index f68fc31..0000000 --- a/cumulocity/setup-tenant.sh +++ /dev/null @@ -1,145 +0,0 @@ -#!/usr/bin/env bash -# SPDX-License-Identifier: Apache-2.0 -# -# Set up the Cumulocity tenant of the active go-c8y-cli session for tiny-edge -# devices, in one go: -# -# 1. check the tenant features the devices need (mqtt-service.smartrest, -# certificate-authority); -# 2. deploy every Smart Function under cumulocity/smart-functions/; -# 3. register the DTM parameter sets (cumulocity/dtm/register.sh). -# -# cumulocity/setup-tenant.sh [options] -# -# Options: -# --dry-run read the tenant and say what would change; change nothing -# --enable-features enable a missing tenant feature instead of stopping -# --replace re-register DTM sets that already exist -# --dtm-dir also register the *.json sets in (repeatable), -# e.g. an application's own cumulocity/dtm -# --no-smart-functions skip step 2 -# --no-dtm skip tiny-edge's own DTM sets (--dtm-dir still runs) -# -# Start a session first (set-session), which sets C8Y_HOST and C8Y_HEADER. -set -euo pipefail -here=$(cd "$(dirname "$0")" && pwd) - -dry=0 -enable=0 -replace=() -deploy_rules=1 -own_dtm=1 -dtm_dirs=() -while [[ $# -gt 0 ]]; do - case $1 in - --dry-run) dry=1 ;; - --enable-features) enable=1 ;; - --replace) replace=(--replace) ;; - --dtm-dir) dtm_dirs+=("$(cd "${2:?--dtm-dir needs a directory}" && pwd)"); shift ;; - --no-smart-functions) deploy_rules=0 ;; - --no-dtm) own_dtm=0 ;; - -h | --help) sed -n '3,24s/^# \{0,1\}//p' "$0"; exit 0 ;; - *) echo "unknown option: $1 (see --help)" >&2; exit 2 ;; - esac - shift -done -[[ $own_dtm -eq 1 ]] && dtm_dirs=("$here/dtm" "${dtm_dirs[@]+"${dtm_dirs[@]}"}") -[[ $dry -eq 1 ]] && dtm_flags=(--dry-run) || dtm_flags=() - -# --- session -------------------------------------------------------------- -# c8y reads stdin when it is not a terminal, so every call gets < /dev/null -# or a script hangs. -if [[ -z ${C8Y_HOST:-} || -z ${C8Y_HEADER:-} ]]; then - echo "No go-c8y-cli session: run set-session (it sets C8Y_HOST and C8Y_HEADER), then run this again." >&2 - exit 1 -fi -host=${C8Y_HOST%/} -[[ $host == *://* ]] || host="https://$host" -tenant=$(c8y currenttenant get --select name --output csv < /dev/null) || { - echo "The go-c8y-cli session for $host does not work (c8y currenttenant get failed)." >&2 - exit 1 -} -if [[ $dry -eq 1 ]]; then - echo "Dry run against $host (tenant $tenant): nothing will be changed." -else - echo "Setting up $host (tenant $tenant) for tiny-edge." -fi - -# --- 1. tenant features ----------------------------------------------------- -# Without mqtt-service.smartrest the MQTT Service drops a device as soon as -# it subscribes to devicecontrol/notifications (the device logs -113). -feature_active() { - c8y features get --key "$1" --raw < /dev/null 2>/dev/null | - python3 -c 'import json,sys; print("yes" if json.load(sys.stdin).get("active") else "no")' 2>/dev/null || - echo no -} -missing=0 -for f in mqtt-service.smartrest certificate-authority; do - if [[ $(feature_active "$f") == yes ]]; then - echo "feature $f: active" - elif [[ $enable -eq 1 && $dry -eq 0 ]]; then - if c8y features enable --key "$f" --force < /dev/null >/dev/null; then - echo "feature $f: enabled" - else - echo "feature $f: could not enable it; ask whoever manages the tenant to enable it." >&2 - missing=1 - fi - elif [[ $enable -eq 1 ]]; then - echo "feature $f: not active, would enable it" - else - echo "feature $f: not active. Enable it with: c8y features enable --key $f" >&2 - missing=1 - fi -done -if [[ $missing -eq 1 ]]; then - if [[ $dry -eq 1 ]]; then - echo "(a real run stops here; pass --enable-features to have the script enable them)" >&2 - else - echo "Stopping: enable the features above, or run again with --enable-features." >&2 - exit 1 - fi -fi - -failed=0 - -# --- 2. Smart Functions ----------------------------------------------------- -# The package has to be the raw request body: `c8y api --file` always sends -# multipart, which the service answers with 400 "Failed to read request -# body". So curl it, with C8Y_HEADER (the whole "Authorization: ..." line). -if [[ $deploy_rules -eq 1 ]]; then - work=$(mktemp -d) - trap 'rm -rf "$work"' EXIT - pkgs=$("$here/smart-functions/package.sh" "$work") - while read -r pkg; do - name=$(basename "$pkg" .tar.gz) - url="$host/service/dataprep/v1/rules/$name/deployed" - if [[ $dry -eq 1 ]]; then - echo "smart function $name: would PUT $(wc -c < "$pkg" | tr -d ' ') bytes to $url" - continue - fi - status=$(curl -sS -o "$work/$name.out" -w '%{http_code}' -X PUT \ - -H "$C8Y_HEADER" -H "Content-Type: application/gzip" \ - --data-binary "@$pkg" "$url") || status=000 # curl printed why - if [[ $status == 200 ]]; then - echo "smart function $name: deployed (HTTP 200)" - else - echo "smart function $name: HTTP $status: $(head -c 500 "$work/$name.out" 2>/dev/null)" >&2 - failed=1 - fi - done <<< "$pkgs" -fi - -# --- 3. DTM parameter sets ---------------------------------------------------- -for d in "${dtm_dirs[@]+"${dtm_dirs[@]}"}"; do - echo "DTM sets in $d:" - "$here/dtm/register.sh" "${replace[@]+"${replace[@]}"}" "${dtm_flags[@]+"${dtm_flags[@]}"}" --dir "$d" || failed=1 -done - -if [[ $failed -eq 1 ]]; then - echo "Done, with failures (see above)." >&2 - exit 1 -fi -echo "Done." -if [[ $deploy_rules -eq 1 && $dry -eq 0 ]]; then - echo "Devices that were already connected send their twin fragments again only when they reconnect: restart them to get the Parameters tab." -fi diff --git a/cumulocity/tenant.yaml b/cumulocity/tenant.yaml new file mode 100644 index 0000000..fbb384e --- /dev/null +++ b/cumulocity/tenant.yaml @@ -0,0 +1,43 @@ +# SPDX-License-Identifier: Apache-2.0 +# +# Everything a Cumulocity tenant needs for tiny-edge devices, as a +# c8y-tenant-sync manifest (https://github.com/reubenmiller/c8y-tenant-sync). +# Each release carries it, with the files it uses, as the bundle +# tiny-edge-tenant.tar.gz: +# +# c8y tenant-sync run https://github.com/thin-edge/tiny-edge/releases/latest/download/tiny-edge-tenant.tar.gz --dry +# +# The applications are owned by the management tenant. A tenant which may not +# subscribe them itself reports them as skipped; its parent tenant (or the +# Cumulocity provider) subscribes them. + +features: + # Without it the MQTT Service drops a device as soon as it subscribes to + # devicecontrol/notifications; the device logs -113. + - key: mqtt-service.smartrest + # Devices get their certificate from the tenant's CA. + - key: certificate-authority + # Shows the Data Preparation application. + - key: data-prep.ui-visibility + +microservices: + - name: mqtt-service # the devices' connection + - name: data-prep-ctrl # runs the Smart Functions + - name: dtm # Digital Twin Manager definitions + - name: device-parameter # the Parameters tab + +applications: + - name: Data Preparation + - name: digital-twin-manager + +# The client's own parameter sets, for the Parameters tab +digitalTwin: + propertyDefinitions: + - source: + path: ./dtm + +# The Smart Functions which map the te/ topics to measurements, events and +# managed-object fragments +smartFunctions: + - source: + path: ./smart-functions