How to use the balena-sdk Python module with openBalena

Hi,
sorry for the newbie question, but I’ve spent some time trying to query the openBalena API via the balena-sdk Python module with no success. Maybe someone here can provide an example?

Something like this:

#!/usr/bin/env python

import balena
import os

openbalena_api_key  = 'SuperSecret'

os.environ['BALENARC_BALENA_URL'] = 'openbalena.myown.com'
os.environ['BALENA_API_KEY'] = 'MyOwnSuperSecretKey'
balenaapi = balena.Balena()
balenadb = balenaapi.models.device.get_all()
for balena in balenadb:
    print( "BalenaOS device {} found...".format(balena['device_name']))

Hey @daghemo,

You need to change some endpoints which are api_endpoint, pine_endpoint and image_maker_endpoint in settings file so the Python will send the requests to your OpenBalena. The settings file can be found in your user 's home directory.

Thanks @nghiant2710!

I found this post some hours before your reply:

Do you plan to update the Python SDK to support openBalena in (near) the future?
I’m going to try with api_endpoint, pine_endpoint and image_maker_endpoint as you suggested.

I’ve only been testing openBalena in the last couple of days and it looks impressive! You have done a great work with it, with balenaCloud and the whole Balena ecosystem! Also, I’m an happy user of one of your first balenaFin V1.1 and I’m planning to buy some other.

But I also have to say that it lacks a lot of features from balenaCloud. Not only multiple users support, web-based dashboard and updates with binary container deltas as shown in the FAQ on Open-source software to manage connected IoT devices. That is, SSH support, out-of-the-box support for the SDK, support for environment variables… It would be great to have a kind-of public roadmap for that…

I asked the team, and the SSH support is in ongoing development.

Environment variables are supported, can you explain a bit more what you find lacking there? Is it as discussed in this other thread Error trying to set environment variable with balena-cli - #6 by dfunckt ?

Also, if you run into any issues, feel free to also open an issue at GitHub - balena-io/open-balena: Open source software to manage connected IoT devices at scale As mentioned in that page as well,

OpenBalena is currently in beta. While fully functional, it lacks features we consider important before we can comfortably call it production-ready. During this phase, don’t be alarmed if things don’t work as expected just yet (and please let us know about any bugs or errors you encounter!)

Thus at this stage some use cases might be more difficult than expected, and the information might be more scattered than it should. We are looking at pulling information together better. And thanks a lot for your feedback, it’s very helpful!

For SSH there’s also this how-to: HowTo: SSH into host device

Thanks @imrehg!
I wrote:

But I also have to say that it lacks a lot of features from balenaCloud. Not only multiple users support, web-based dashboard and updates with binary container deltas as shown in the FAQ on Open-source software to manage connected IoT devices. That is, SSH support, out-of-the-box support for the SDK, support for environment variables… It would be great to have a kind-of public roadmap for that…

That is, I know that openBalena is beta and it has not to be considered for production use. I’m just wondering if you have a kind of a roadmap, in particular for out-of-the-box support of SSH, Python SDK and configuration variables.

When I say “out-of-the-box” I mean using “balena ssh” not only for locallly conneceted devices or via “balena tunnel”, or using balena-sdk without modifying the balena.cfg config file (e.g. this makes hard to use balenaCloud and openBalena at the same time), or using both “balena env” and “balena envs” with no need to interact directly with the API via curl or others. Feel free to replace “out-of-the-box” with “just like I do in balenaCloud”.

IMHO, it would help to have a kind-of roadmap, a detailed comparison page between balenaCloud and openBalena or even an addendum to the FAQ on the homepage (e.g. openBalena would never have a UI, but would have support for SSH, etc). I’m mentioning these features since I believe these are the most important ones, IMHO.

Please, don’t take this as a “you have to hurry up”, since you’ve already done a great job and I know this is an open-source project. I strongly believe that the suggested approach may even save you some time from replying to post in the forum. :wink:
Also, please excuse me, since I know this is OT!

To @nghiant2710, I’ve patched the URL within both pine_endpoint and api_endpoint in my ~/.balena/balena.cfg file and I can talk with the openBalena API! But what about image_maker_endpoint? Does it have any counterpart in openBalena?

Also, patching the balena.cfg makes hard to talk with both balenaCloud and openBalena from the same python scripts. Do you see any alternative here?

Just an update on the topic.
I’ve ended up using balena.settings.set() as follows:

#!/usr/bin/env python

import balena
import os

os.environ['BALENA_API_KEY'] = 'SuperSecret'
balenaapi = balena.Balena()
balenaapi.settings.set(key='pine_endpoint',value='https://api.balena-cloud.com/v5/')
balenaapi.settings.set(key='api_endpoint',value='https://api.balena-cloud.com/')
balenadevices = balenaapi.models.device.get_all()
for balenadevice in balenadevices:
    print( "BalenaOS device {} found...".format(balenadevice['device_name']))

os.environ['BALENA_API_KEY'] = 'ThisToo'
balenaapi = balena.Balena()
balenaapi.settings.set(key='pine_endpoint',value='https://api.fleet.aghemo.com/v5/')
balenaapi.settings.set(key='api_endpoint',value='https://api.fleet.aghemo.com/')
balenadevices = balenaapi.models.device.get_all()
for balenadevice in balenadevices:
    print( "BalenaOS device {} found...".format(balenadevice['device_name']))

Using balena.settings.set() allows me to easily switch between balenaCloud and openBalena in the same Python code via the SDK. Just note that set() calls the private method __write_settings(), so the ~/.balena/balena.cfg file is changed. That is, maybe I have to use set() again to place a default value within balena.cfg before leaving the script.
Another option would be to monkey patch at least get() and get_all() or maybe ugly overriding _Settings__read_settings().

Hope this may helps others sharing the same problem. :slight_smile:

1 Like

@daghemo Thanks for the update, I hope other users will find it useful. Regarding the open-balena roadmap, for now https://github.com/balena-io/open-balena#roadmap this is all we have, and there is also https://trello.com/b/9CSzdCXQ/balena-product-roadmap for balenaCloud’s roadmap. Once we feel like openBalena is ready to graduate from beta we might have a more detailed roadmap, but this is just my personal assumption. Anyhow, I pinged someone working on openBalena so they keep your suggestions in mind.

1 Like