Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 15 additions & 34 deletions src/cli/broker.toit
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import .config
import .device
import .pod
import .pod-specification
import .scope show Scope
import ..shared.scope show Scope

import .utils
import .utils.patch-build show build-diff-patch build-trivial-patch
Expand Down Expand Up @@ -67,7 +67,6 @@ Manages devices that have an Artemis service running on them.
*/
class Broker:
fleet-id/Uuid
organization-id/Uuid
server-config/ServerConfig
cli_/Cli
network_/net.Client? := null
Expand All @@ -83,7 +82,6 @@ class Broker:

constructor
--.fleet-id/Uuid
--.organization-id/Uuid
--.server-config
--cli/Cli
--tmp-directory/string
Expand All @@ -104,16 +102,6 @@ class Broker:
cli_.ui.abort "$error-message (broker)."
return broker-connection__

/**
The $Scope to use when talking to the broker.

For now derived directly from $organization-id. When the fleet file gains
a per-service scope field this will return the broker's own configured
scope instead.
*/
scope -> Scope:
return Scope.from-organization-id organization-id

short-string-for_ --device-id/Uuid -> string:
if not device-short-strings_: throw "Access to device in non-device fleet."
return device-short-strings_[device-id]
Expand Down Expand Up @@ -142,7 +130,8 @@ class Broker:
return error.contains "duplicate key value" or error.contains "already exists"

/**
Uploads the given $pod to the broker for the given $fleet-id in $organization-id.
Uploads the given $pod to the broker for the given $fleet-id under the
broker's configured scope.

Also uploads the trivial patches.
*/
Expand All @@ -154,25 +143,23 @@ class Broker:
// Only upload if we don't have it in our cache.
key := cache-key-pod-parts
--broker-config=server-config
--organization-id=organization-id
--part-id=id
cli_.cache.get-file-path key: | store/FileStore |
broker-connection_.pod-registry-upload-pod-part contents --part-id=id
--scope=scope
--scope=server-config.scope
store.save contents
key := cache-key-pod-manifest
--broker-config=server-config
--organization-id=organization-id
--pod-id=pod.id
cli_.cache.get-file-path key: | store/FileStore |
encoded := ubjson.encode manifest
broker-connection_.pod-registry-upload-pod-manifest encoded --pod-id=pod.id
--scope=scope
--scope=server-config.scope
store.save encoded

description-ids := broker-connection_.pod-registry-descriptions
--fleet-id=fleet-id
--scope=scope
--scope=server-config.scope
--names=[pod.name]
--create-if-absent

Expand Down Expand Up @@ -215,7 +202,7 @@ class Broker:
upload-patch_ it

/**
Uploads the given $patch to the server under the given $organization-id.
Uploads the given $patch to the broker.
*/
upload-patch_ patch/FirmwarePatch:
diff-and-upload_ patch
Expand All @@ -228,12 +215,11 @@ class Broker:
trivial-id := id_ --to=patch.to_
cache-key := cache-key-patch
--broker-config=server-config
--organization-id=organization-id
--patch-id=trivial-id
cli_.cache.get cache-key: | store/FileStore |
trivial := build-trivial-patch patch.bits_
broker-connection_.upload-firmware trivial
--scope=scope
--scope=server-config.scope
--firmware-id=trivial-id
store.save-via-writer: | writer/io.Writer |
trivial.do: writer.write it
Expand All @@ -245,12 +231,11 @@ class Broker:
old-id := id_ --to=patch.from_
cache-key = cache-key-patch
--broker-config=server-config
--organization-id=organization-id
--patch-id=old-id
trivial-old := cli_.cache.get cache-key: | store/FileStore |
downloaded := null
catch: downloaded = broker-connection_.download-firmware
--scope=scope
--scope=server-config.scope
--id=old-id
if not downloaded:
cli_.ui.emit --warning "Failed to download old firmware for patch $old-id -> $trivial-id."
Expand All @@ -274,7 +259,6 @@ class Broker:
diff-id := id_ --from=patch.from_ --to=patch.to_
cache-key = cache-key-patch
--broker-config=server-config
--organization-id=organization-id
--patch-id=diff-id
cli_.cache.get cache-key: | store/FileStore |
// Build the diff and verify that we can apply it and get the
Expand All @@ -289,7 +273,7 @@ class Broker:
to64 := base64.encode patch.to_ --url-mode
cli_.ui.emit --info "Uploading patch $from64 -> $to64 ($diff-size)."
broker-connection_.upload-firmware diff
--scope=scope
--scope=server-config.scope
--firmware-id=diff-id
store.save-via-writer: | writer/io.Writer |
diff.do: writer.write it
Expand All @@ -312,19 +296,17 @@ class Broker:
is-cached --pod-id/Uuid -> bool:
manifest-key := cache-key-pod-manifest
--broker-config=server-config
--organization-id=organization-id
--pod-id=pod-id
return cli_.cache.contains manifest-key

download --pod-id/Uuid -> Pod:
manifest-key := cache-key-pod-manifest
--broker-config=server-config
--organization-id=organization-id
--pod-id=pod-id
encoded-manifest := cli_.cache.get manifest-key: | store/FileStore |
bytes := broker-connection_.pod-registry-download-pod-manifest
--pod-id=pod-id
--scope=scope
--scope=server-config.scope
store.save bytes
manifest := ubjson.decode encoded-manifest
return Pod.from-manifest
Expand All @@ -333,12 +315,11 @@ class Broker:
--download=: | part-id/string |
key := cache-key-pod-parts
--broker-config=server-config
--organization-id=organization-id
--part-id=part-id
cli_.cache.get key: | store/FileStore |
bytes := broker-connection_.pod-registry-download-pod-part
part-id
--scope=scope
--scope=server-config.scope
store.save bytes

list-pods --names/List -> Map:
Expand All @@ -348,7 +329,7 @@ class Broker:
else:
descriptions = broker-connection_.pod-registry-descriptions
--fleet-id=fleet-id
--scope=scope
--scope=server-config.scope
--names=names
--no-create-if-absent
result := {:}
Expand All @@ -360,7 +341,7 @@ class Broker:
delete --description-names/List:
descriptions := broker-connection_.pod-registry-descriptions
--fleet-id=fleet-id
--scope=scope
--scope=server-config.scope
--names=description-names
--no-create-if-absent
unknown-pod-descriptions := []
Expand Down Expand Up @@ -429,7 +410,7 @@ class Broker:

descriptions := broker-connection_.pod-registry-descriptions
--fleet-id=fleet-id
--scope=scope
--scope=server-config.scope
--names=names.to-list
--no-create-if-absent

Expand Down
2 changes: 1 addition & 1 deletion src/cli/brokers/broker.toit
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import ..config
import ..event
import ..device
import ..pod-registry
import ..scope show Scope
import ...shared.scope show Scope
import ...shared.server-config
import .supabase
import .http.base
Expand Down
2 changes: 1 addition & 1 deletion src/cli/brokers/http/base.toit
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ..broker
import ...device
import ...event
import ...pod-registry
import ...scope show Scope
import ....shared.scope show Scope
import ....shared.server-config
import ....shared.utils as utils
import ....shared.constants show *
Expand Down
9 changes: 3 additions & 6 deletions src/cli/cache.toit
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,18 @@ cache-key-application-image id/Uuid --broker-config/ServerConfig -> string:

cache-key-pod-parts -> string
--broker-config/ServerConfig
--organization-id/Uuid
--part-id/string:
return "$broker-config.cache-key/$organization-id/pod/parts/$part-id"
return "$broker-config.cache-key/$broker-config.scope.as-uuid/pod/parts/$part-id"

cache-key-pod-manifest -> string
--broker-config/ServerConfig
--organization-id/Uuid
--pod-id/Uuid:
return "$broker-config.cache-key/$organization-id/pod/manifest/$pod-id"
return "$broker-config.cache-key/$broker-config.scope.as-uuid/pod/manifest/$pod-id"

cache-key-patch -> string
--broker-config/ServerConfig
--organization-id/Uuid
--patch-id/string:
return "$broker-config.cache-key/$organization-id/patches/$patch-id"
return "$broker-config.cache-key/$broker-config.scope.as-uuid/patches/$patch-id"

CACHE-ARTIFACT-KIND-ENVELOPE ::= "envelope"
CACHE-ARTIFACT-KIND-PARTITION-TABLE ::= "partitions"
Expand Down
Loading
Loading