Python SDK: Creating/Updating/Removing environment variables (from device itself)

Hi guys,

Maybe you can help with something:

My devices use the Python SDK to set the environment variables they need themselves. After downloading their individual configuration from our backend they use the “resin.models.environment_variables.device.create” function to create the needed variables.

Now, that works fine. But somehow, updating and removing env vars results in errors:

When updating a variable via “resin.models.environment_variables.device.update()” I get this:

15.03.18 21:57:55 (+0100) Now updating config_var 'RESIN_HOST_CONFIG_disable_splash' on device to value '0'
15.03.18 21:57:56 (+0100) Exception in thread Thread-3:
15.03.18 21:57:56 (+0100) Traceback (most recent call last):
15.03.18 21:57:56 (+0100)   File "/usr/local/lib/python2.7/threading.py", line 801, in __bootstrap_inner
15.03.18 21:57:56 (+0100)     self.run()
15.03.18 21:57:56 (+0100)   File "/usr/local/lib/python2.7/threading.py", line 754, in run
15.03.18 21:57:56 (+0100)     self.__target(*self.__args, **self.__kwargs)
15.03.18 21:57:56 (+0100)   File "ResinManager.py", line 142, in set_device_environment_variables_thread
15.03.18 21:57:56 (+0100)     config_var_dict[entry])
15.03.18 21:57:56 (+0100)   File "/usr/local/lib/python2.7/site-packages/resin/models/environment_variables.py", line 128, in update
15.03.18 21:57:56 (+0100)     endpoint=self.settings.get('pine_endpoint')
15.03.18 21:57:56 (+0100)   File "/usr/local/lib/python2.7/site-packages/resin/base_request.py", line 175, in request
15.03.18 21:57:56 (+0100)     raise exceptions.RequestError(response._content)
15.03.18 21:57:56 (+0100) RequestError

And when removing a variable via “resin.models.environment_variables.device.remove()” I get this:

15.03.18 22:04:06 (+0100) Now removing config_var 'RESIN_HOST_CONFIG_gpu_mem' on device with value '16' and id '131778'
15.03.18 22:04:07 (+0100) Exception in thread Thread-3:
15.03.18 22:04:07 (+0100) Traceback (most recent call last):
15.03.18 22:04:07 (+0100)   File "/usr/local/lib/python2.7/threading.py", line 801, in __bootstrap_inner
15.03.18 22:04:07 (+0100)     self.run()
15.03.18 22:04:07 (+0100)   File "/usr/local/lib/python2.7/threading.py", line 754, in run
15.03.18 22:04:07 (+0100)     self.__target(*self.__args, **self.__kwargs)
15.03.18 22:04:07 (+0100)   File "ResinManager.py", line 154, in set_device_environment_variables_thread
15.03.18 22:04:07 (+0100)     resin.models.environment_variables.device.remove(current_vars_on_device_dict[entry][0])
15.03.18 22:04:07 (+0100)   File "/usr/local/lib/python2.7/site-packages/resin/models/environment_variables.py", line 150, in remove
15.03.18 22:04:07 (+0100)     endpoint=self.settings.get('pine_endpoint')
15.03.18 22:04:07 (+0100)   File "/usr/local/lib/python2.7/site-packages/resin/base_request.py", line 175, in request
15.03.18 22:04:07 (+0100)     raise exceptions.RequestError(response._content)
15.03.18 22:04:07 (+0100) RequestError

This can’t be expected behaviour right?

Thanks!

Frans

Hi, We’ve recently released v3 of the python SDK which appears to fix the issues you’re seeing. Could you try the new version and see if that fixes your issue?

Thanks guys, I will try!

Hmm, I’m afraid with ‘remove’ I still get this:

21.03.18 17:13:39 (+0100)  main  Exception in thread Thread-3:
21.03.18 17:13:39 (+0100)  main  Traceback (most recent call last):
21.03.18 17:13:39 (+0100)  main    File "/usr/local/lib/python2.7/threading.py", line 801, in __bootstrap_inner
21.03.18 17:13:39 (+0100)  main      self.run()
21.03.18 17:13:39 (+0100)  main    File "/usr/local/lib/python2.7/threading.py", line 754, in run
21.03.18 17:13:39 (+0100)  main      self.__target(*self.__args, **self.__kwargs)
21.03.18 17:13:39 (+0100)  main    File "ResinManager.py", line 154, in set_device_environment_variables_thread
21.03.18 17:13:39 (+0100)  main      resin.models.environment_variables.device.remove(current_vars_on_device_dict[entry][0])
21.03.18 17:13:39 (+0100)  main    File "/usr/local/lib/python2.7/site-packages/resin/models/environment_variables.py", line 150, in remove
21.03.18 17:13:39 (+0100)  main      endpoint=self.settings.get('pine_endpoint')
21.03.18 17:13:39 (+0100)  main    File "/usr/local/lib/python2.7/site-packages/resin/base_request.py", line 175, in request
21.03.18 17:13:39 (+0100)  main      raise exceptions.RequestError(response._content)
21.03.18 17:13:39 (+0100)  main  RequestError

And with update I get this:

Exception in thread Thread-3:
21.03.18 17:16:39 (+0100)  main  Traceback (most recent call last):
21.03.18 17:16:39 (+0100)  main    File "/usr/local/lib/python2.7/threading.py", line 801, in __bootstrap_inner
21.03.18 17:16:39 (+0100)  main      self.run()
21.03.18 17:16:39 (+0100)  main    File "/usr/local/lib/python2.7/threading.py", line 754, in run
21.03.18 17:16:39 (+0100)  main      self.__target(*self.__args, **self.__kwargs)
21.03.18 17:16:39 (+0100)  main    File "ResinManager.py", line 142, in set_device_environment_variables_thread
21.03.18 17:16:39 (+0100)  main      config_var_dict[entry])
21.03.18 17:16:39 (+0100)  main    File "/usr/local/lib/python2.7/site-packages/resin/models/environment_variables.py", line 128, in update
21.03.18 17:16:39 (+0100)  main      endpoint=self.settings.get('pine_endpoint')
21.03.18 17:16:39 (+0100)  main    File "/usr/local/lib/python2.7/site-packages/resin/base_request.py", line 175, in request
21.03.18 17:16:39 (+0100)  main      raise exceptions.RequestError(response._content)
21.03.18 17:16:39 (+0100)  main  RequestError

So what would be needed to debug this? Shall I paste here the code that sets the env vars? Or would you care to login to one of the devices with this behaviour?

Is this a single container or multicontainer device? We’ve not supported multicontainer yet so it might now work properly. We will release Python SDK v4 which supports multicontainer really soon.

Single container!

About the same issue here with docker build arguments variables not passing through a common python3 script. It wouldn’t be able to parse the passed arguments from Bash she’ll script. I’m afraid!

Hi @ArkTobe
Can you also clarify what version of the python sdk are you using?

I think that I might be missing something.
Can provide a bit more info about what you are trying to achieve?
If you are using build time variables with ARG, these are not going to be available in the container running on the device.
If you on the other hand use the docker-compose.yml environment: to define variables, then those will be available in the containers on run-time.

1 Like

Settings tru dashboard is ok in bash shell. Going through Python scripts with environment variables set isn’t straight forward is it. You can only inject scripts run through RUN ["bash", "-c ","python3 $ENVIRONMENT_PARAMETER" ] , by doing that so, is getting well, finally,. This is not very useful since the output isn’t easily redirected to the standard error.
I have set my template ARG as ENV entries to make them available to further commands, that’s all it can do at build time at the moment. Eventually, all runtime values need to be set through docker-compose.yml env_file: afile.env or dashboard
To be clear, AFAIK security issues may arise if we could use environment variables in Python scripted files, it isn’t safe to produce scripts with access to the environment settings on-the-go.