Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration to be passed in using TEDGE_ environment variables #1790

Merged
merged 7 commits into from
Mar 7, 2023

Conversation

jarhodes314
Copy link
Contributor

@jarhodes314 jarhodes314 commented Mar 3, 2023

Proposed changes

Add support for reading TEDGE_ prefixed environment variables, as detailed in #1783.

e.g.

c8y.url -> TEDGE_C8Y__URL
mqtt.external_port -> TEDGE_MQTT__EXTERNAL_PORT (this is using the path from how tedge.toml is structured, not from the tedge config command)

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue

#1783

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s)
  • I ran cargo fmt as mentioned in CODING_GUIDELINES
  • I used cargo clippy as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

The main change, using figment instead of deserialising directly from the TOML file is quite simple, but generating error messages which contain the relevant environment variable names requires quite a bit of effort. When using things like this though, I think it's really helpful to understand where an incorrect configuration came from, so I think this is ultimately worth the effort. I've also refactored the warnings to use serde_ignored (again, in an annoyingly complex way), but this does mean we no longer need tedge-derive or the display_unknown_for macro which had to be called manually for every field.

@jarhodes314 jarhodes314 temporarily deployed to Test Pull Request March 3, 2023 15:22 — with GitHub Actions Inactive
@github-actions
Copy link
Contributor

github-actions bot commented Mar 3, 2023

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass %
140 0 5 140 100

Passed Tests

Name ⏱️ Duration Suite
Define Child device 1 ID 0.019 s C8Y Child Alarms Rpi
Normal case when the child device does not exist on c8y cloud 4.53 s C8Y Child Alarms Rpi
Normal case when the child device already exists 0.784 s C8Y Child Alarms Rpi
Reconciliation when the new alarm message arrives, restart the mapper 0.982 s C8Y Child Alarms Rpi
Reconciliation when the alarm that is cleared 5.167 s C8Y Child Alarms Rpi
Prerequisite Parent 16.954 s Child Conf Mgmt Plugin
Prerequisite Child 0.254 s Child Conf Mgmt Plugin
Child device bootstrapping 14.066 s Child Conf Mgmt Plugin
Snapshot from device 19.798000000000002 s Child Conf Mgmt Plugin
Child device config update 17.393 s Child Conf Mgmt Plugin
Configuration types should be detected on file change (without restarting service) 46.378 s Inotify Crate
Child devices support sending simple measurements 45.128 s Child Device Telemetry
Child devices support sending custom measurements 43.968 s Child Device Telemetry
Child devices support sending custom events 40.833 s Child Device Telemetry
Child devices support sending custom events overriding the type 31.258 s Child Device Telemetry
Child devices support sending custom alarms #1699 34.814 s Child Device Telemetry
Child devices support sending inventory data via c8y topic 23.328 s Child Device Telemetry
Main device support sending inventory data via c8y topic 24.412 s Child Device Telemetry
Successful firmware operation 61.436 s Firmware Operation
Install with empty firmware name 48.964 s Firmware Operation
Supports restarting the device 69.32 s Restart Device
Update tedge version from previous using Cumulocity 92.486 s Tedge Self Update
Successful shell command with output 4.13 s Shell Operation
Check Successful shell command with literal double quotes output 3.682 s Shell Operation
Execute multiline shell command 3.415 s Shell Operation
Failed shell command 3.295 s Shell Operation
Software list should be populated during startup 48.064 s Software
Install software via Cumulocity 62.154 s Software
Software list should only show currently installed software and not candidates 38.597 s Software
Stop tedge-agent service 0.145 s Log Path Config
Customize the log path 0.07 s Log Path Config
Initialize tedge-agent 0.109 s Log Path Config
Check created folders 0.073 s Log Path Config
Remove created custom folders 0.085 s Log Path Config
Install latest via script (from current branch) 27.935 s Install Tedge
Install specific version via script (from current branch) 18.691 s Install Tedge
Install latest tedge via script (from main branch) 21.865 s Install Tedge
Support starting and stopping services 35.9 s Service-Control
Supports a reconnect 44.194 s Test-Commands
Supports disconnect then connect 45.877 s Test-Commands
Update unknown setting 29.722 s Test-Commands
Update known setting 24.036 s Test-Commands
Stop c8y-configuration-plugin 0.245 s Health C8Y-Configuration-Plugin
Update the service file 0.225 s Health C8Y-Configuration-Plugin
Reload systemd files 0.646 s Health C8Y-Configuration-Plugin
Start c8y-configuration-plugin 0.217 s Health C8Y-Configuration-Plugin
Start watchdog service 10.231 s Health C8Y-Configuration-Plugin
Check PID of c8y-configuration-plugin 0.063 s Health C8Y-Configuration-Plugin
Kill the PID 0.192 s Health C8Y-Configuration-Plugin
Recheck PID of c8y-configuration-plugin 2.203 s Health C8Y-Configuration-Plugin
Compare PID change 0.001 s Health C8Y-Configuration-Plugin
Stop watchdog service 0.181 s Health C8Y-Configuration-Plugin
Remove entry from service file 0.1 s Health C8Y-Configuration-Plugin
Stop c8y-log-plugin 0.203 s Health C8Y-Log-Plugin
Update the service file 0.231 s Health C8Y-Log-Plugin
Reload systemd files 0.76 s Health C8Y-Log-Plugin
Start c8y-log-plugin 0.298 s Health C8Y-Log-Plugin
Start watchdog service 10.222 s Health C8Y-Log-Plugin
Check PID of c8y-log-plugin 0.075 s Health C8Y-Log-Plugin
Kill the PID 0.074 s Health C8Y-Log-Plugin
Recheck PID of c8y-log-plugin 2.134 s Health C8Y-Log-Plugin
Compare PID change 0.001 s Health C8Y-Log-Plugin
Stop watchdog service 0.076 s Health C8Y-Log-Plugin
Remove entry from service file 0.061 s Health C8Y-Log-Plugin
Stop tedge-mapper 0.242 s Health Tedge Mapper C8Y
Update the service file 0.227 s Health Tedge Mapper C8Y
Reload systemd files 0.905 s Health Tedge Mapper C8Y
Start tedge-mapper 0.172 s Health Tedge Mapper C8Y
Start watchdog service 10.336 s Health Tedge Mapper C8Y
Check PID of tedge-mapper 0.058 s Health Tedge Mapper C8Y
Kill the PID 0.077 s Health Tedge Mapper C8Y
Recheck PID of tedge-mapper 2.128 s Health Tedge Mapper C8Y
Compare PID change 0.001 s Health Tedge Mapper C8Y
Stop watchdog service 0.076 s Health Tedge Mapper C8Y
Remove entry from service file 0.068 s Health Tedge Mapper C8Y
Stop tedge-agent 0.309 s Health Tedge-Agent
Update the service file 0.253 s Health Tedge-Agent
Reload systemd files 0.548 s Health Tedge-Agent
Start tedge-agent 0.128 s Health Tedge-Agent
Start watchdog service 10.28 s Health Tedge-Agent
Check PID of tedge-mapper 0.052 s Health Tedge-Agent
Kill the PID 0.053 s Health Tedge-Agent
Recheck PID of tedge-agent 2.146 s Health Tedge-Agent
Compare PID change 0.001 s Health Tedge-Agent
Stop watchdog service 0.138 s Health Tedge-Agent
Remove entry from service file 0.153 s Health Tedge-Agent
Stop tedge-mapper-az 0.138 s Health Tedge-Mapper-Az
Update the service file 0.234 s Health Tedge-Mapper-Az
Reload systemd files 0.951 s Health Tedge-Mapper-Az
Start tedge-mapper-az 0.147 s Health Tedge-Mapper-Az
Start watchdog service 10.279 s Health Tedge-Mapper-Az
Check PID of tedge-mapper-az 0.105 s Health Tedge-Mapper-Az
Kill the PID 0.087 s Health Tedge-Mapper-Az
Recheck PID of tedge-agent 2.161 s Health Tedge-Mapper-Az
Compare PID change 0.001 s Health Tedge-Mapper-Az
Stop watchdog service 0.097 s Health Tedge-Mapper-Az
Remove entry from service file 0.127 s Health Tedge-Mapper-Az
Stop tedge-mapper-collectd 0.233 s Health Tedge-Mapper-Collectd
Update the service file 0.179 s Health Tedge-Mapper-Collectd
Reload systemd files 0.615 s Health Tedge-Mapper-Collectd
Start tedge-mapper-collectd 0.303 s Health Tedge-Mapper-Collectd
Start watchdog service 10.462 s Health Tedge-Mapper-Collectd
Check PID of tedge-mapper-collectd 0.065 s Health Tedge-Mapper-Collectd
Kill the PID 0.095 s Health Tedge-Mapper-Collectd
Recheck PID of tedge-mapper-collectd 2.175 s Health Tedge-Mapper-Collectd
Compare PID change 0.001 s Health Tedge-Mapper-Collectd
Stop watchdog service 0.153 s Health Tedge-Mapper-Collectd
Remove entry from service file 0.097 s Health Tedge-Mapper-Collectd
c8y-log-plugin health status 5.589 s MQTT health endpoints
c8y-configuration-plugin health status 5.531 s MQTT health endpoints
Wrong package name 0.158 s Improve Tedge Apt Plugin Error Messages
Wrong version 0.138 s Improve Tedge Apt Plugin Error Messages
Wrong type 0.431 s Improve Tedge Apt Plugin Error Messages
tedge_connect_test_positive 0.551 s Tedge Connect Test
tedge_connect_test_negative 0.907 s Tedge Connect Test
tedge_connect_test_sm_services 7.319 s Tedge Connect Test
tedge_disconnect_test_sm_services 0.423 s Tedge Connect Test
Install thin-edge.io 13.183 s Call Tedge
call tedge -V 0.085 s Call Tedge
call tedge -h 0.078 s Call Tedge
call tedge -h -V 0.081 s Call Tedge
call tedge help 0.108 s Call Tedge
tedge config list 0.063 s Call Tedge Config List
tedge config list --all 0.103 s Call Tedge Config List
set/unset device.type 0.376 s Call Tedge Config List
set/unset device.key.path 0.407 s Call Tedge Config List
set/unset device.cert.path 0.248 s Call Tedge Config List
set/unset c8y.root.cert.path 0.402 s Call Tedge Config List
set/unset c8y.smartrest.templates 0.351 s Call Tedge Config List
set/unset az.root.cert.path 0.372 s Call Tedge Config List
set/unset az.mapper.timestamp 0.372 s Call Tedge Config List
set/unset mqtt.bind_address 0.384 s Call Tedge Config List
set/unset mqtt.port 0.293 s Call Tedge Config List
set/unset tmp.path 0.295 s Call Tedge Config List
set/unset logs.path 0.257 s Call Tedge Config List
set/unset run.path 0.327 s Call Tedge Config List
Get Put Delete 1.923 s Http File Transfer Api
Set keys should return value on stdout 0.205 s Tedge Config Get
Unset keys should not return anything on stdout and warnings on stderr 0.388 s Tedge Config Get
Invalid keys should not return anything on stdout and warnings on stderr 0.353 s Tedge Config Get

Signed-off-by: James Rhodes <jarhodes314@gmail.com>
Signed-off-by: James Rhodes <jarhodes314@gmail.com>
@jarhodes314 jarhodes314 temporarily deployed to Test Pull Request March 3, 2023 16:32 — with GitHub Actions Inactive
Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Nice cleanup.

crates/common/tedge_config/src/tedge_config_cli/figment.rs Outdated Show resolved Hide resolved
crates/common/download/Cargo.toml Show resolved Hide resolved
@jarhodes314 jarhodes314 temporarily deployed to Test Pull Request March 6, 2023 13:34 — with GitHub Actions Inactive
Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really surprised by the fact a write access to the config is required by the tedge connect command. Only tedge config set and tedge config reset should have this access.

crates/common/tedge_config/src/tedge_config_cli/figment.rs Outdated Show resolved Hide resolved
config_repository: context.config_repository,
config_repository: context.config_repository.skip_environment_variables(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the tedge connect require write access to the config?

This seems to an issue:

  1. I don't see what can be the point here.
  2. Using tegde connect in combination with $ENV setting is one of the main point of this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tedge connect updates tedge.toml with updated MQTT settings. It obviously also updates the mosquitto config with the relevant settings, but making that work in the context of thin-edge running in a container requires some sort of persistence for /etc/tedge anyway, so I'm not entirely sure what we gain by avoiding using /etc/tedge/tedge.toml in that case? @reubenmiller can you provide some insight as to how you're expecting this feature to be used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether instead of opt-out of environment variables entirely when storing values, we should replace the store method with an update_toml method that takes an argument impl FnOnce(Config) -> Config. The repository calls that with only the TOML config, and saves whatever is returned back to the TOML file. This allows us to better scope the updates to the TOML config in cases like this where we may want to refer to environment variables but also update the persistent configuration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tedge connect updates tedge.toml with updated MQTT settings.

This comes as a surprise to me. I really don't understand what's the point.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tedge connect updates tedge.toml with updated MQTT settings. It obviously also updates the mosquitto config with the relevant settings, but making that work in the context of thin-edge running in a container requires some sort of persistence for /etc/tedge anyway, so I'm not entirely sure what we gain by avoiding using /etc/tedge/tedge.toml in that case? @reubenmiller can you provide some insight as to how you're expecting this feature to be used?

Idea is to create multiple containers where each container runs one component of thin-edge, e.g. one container for tedge-agent, one for tedge-mapper-c8y, one for mosquitto. Each container will be configured to use an external mqtt broker by setting the mqtt client settings to point to the mosquitto broker (via environment variables). The actual values inside the tedge.toml don't need to store the values set in the environment variables.

The tedge connect command will only be used to setup the mosquitto broker bridge settings (or in some cases it will not be used at all, as technically the user can create the bridge settings themselves).

Signed-off-by: James Rhodes <jarhodes314@gmail.com>
Signed-off-by: James Rhodes <jarhodes314@gmail.com>
Signed-off-by: James Rhodes <jarhodes314@gmail.com>
Signed-off-by: James Rhodes <jarhodes314@gmail.com>
@jarhodes314 jarhodes314 temporarily deployed to Test Pull Request March 6, 2023 18:18 — with GitHub Actions Inactive
Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved.

The new way to update the file is simpler as well as the types used to distinguish the FileOnly behavior from FileAndEnvironment.

fn store(&self, config: &TEdgeConfig) -> Result<(), TEdgeConfigError> {
let toml = toml::to_string_pretty(&config.data)?;

// Create `$HOME/.tedge` or `/etc/tedge` directory in case it does not exist yet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea to use $HOME/.tedge has been fully deprecated. Now, the default tedge config directory is /etc/tedge and can be provided on the command line using the --tedge-config option.

Signed-off-by: James Rhodes <jarhodes314@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants