Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #38 from balenablocks/support-local-mode
Browse files Browse the repository at this point in the history
Add support for local mode
  • Loading branch information
bulldozer-balena[bot] authored Oct 20, 2021
2 parents 1476aca + 6b795c0 commit 93f00bc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ services:
restart: always
labels:
io.balena.features.balena-api: '1' # necessary to discover services
io.balena.features.supervisor-api: 1 # necessary to discover services in local mode
privileged: true # necessary to change container hostname
ports:
- "8080" # only necessary if using ExternalHttpListener (see below)
Expand All @@ -47,6 +48,7 @@ services:
restart: always
labels:
io.balena.features.balena-api: '1' # necessary to discover services
io.balena.features.supervisor-api: 1 # necessary to discover services in local mode
privileged: true # necessary to change container hostname
ports:
- "8080" # only necessary if using ExternalHttpListener (see below)
Expand Down
27 changes: 19 additions & 8 deletions autowire.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pluginbase import PluginBase
from balena import Balena
import toml
import requests

class AutoWire():
balena = ""
Expand All @@ -15,15 +16,25 @@ def __init__(self, balena):
### Gets the services running on the device from the release definition ###
def GetServices(self):
if self.services is None:
# Use the device UUID to get the device model
# Use the device UUID to get the device model and check device mode
device_id = os.environ.get('BALENA_DEVICE_UUID')
device = self.balena.models.device.get_with_service_details(device_id, False)
# get the commit the device is on
commit = device["is_on__commit"]
# use the commit to get the release the device is on
release = self.balena.models.release.get(commit)
# use the release to find the services configured
self.services = release["composition"]["services"]
local_mode = balena.models.device.is_in_local_mode(device_id)

if local_mode:
supervisor_address = os.environ.get('BALENA_SUPERVISOR_ADDRESS')
r = requests.get(supervisor_address + '/v2/local/target-state')
services = r.json()["state"]["local"]["apps"]["1"]["services"]
self.services = {}
for service in services:
self.services[service.get("serviceName")] = service.get("config")
else:
device = self.balena.models.device.get_with_service_details(device_id, False)
# get the commit the device is on
commit = device["is_on__commit"]
# use the commit to get the release the device is on
release = self.balena.models.release.get(commit)
# use the release to find the services configured
self.services = release["composition"]["services"]

return self.services

Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ services:
image: balenablocks/connector:raspberrypi3
restart: always
labels:
io.balena.features.balena-api: '1' # necessary to discover services
io.balena.features.balena-api: '1' # necessary to discover services
io.balena.features.supervisor-api: 1 # necessary to discover services in local mode
privileged: true # necessary to change container hostname
ports:
- "8080" # only necessary if using ExternalHttpListener (see below)
4 changes: 2 additions & 2 deletions plugins/internalHttpPullInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def FindPullDataSources(services):
if "expose" in details.keys():
portNumber = details["expose"]
if len(portNumber) == 1:
if portNumber[0] == pullSourcePort:
if portNumber[0].split('/')[0] == pullSourcePort:
outputDict[service] = portNumber

return outputDict
Expand All @@ -31,7 +31,7 @@ def invoke(services):
timeout = "{timeout}s"
data_format = "json"
name_override = "{service}"
""".format(service=service, port=port[0], timeout=timeout)
""".format(service=service, port=port[0].split('/')[0], timeout=timeout)
output = (output + sourceConf)

return output
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
balena-sdk==9.5.0
PluginBase
toml
toml
requests
PyJWT==2.1.0

0 comments on commit 93f00bc

Please sign in to comment.