Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.

feat(snap): add support for environment varariable injection #200

Merged
merged 1 commit into from
Apr 26, 2022

Conversation

farshidtz
Copy link
Member

@farshidtz farshidtz commented Apr 25, 2022

For details on the new scheme for setting environment variables, please refer to edgexfoundry/edgex-go#3986.

Signed-off-by: Farshid Tavakolizadeh [email protected]

If your build fails due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/app-rfid-llrp-inventory/blob/main/.github/Contributing.md

PR Checklist

Please check if your PR fulfills the following requirements:

  • I am not introducing a breaking change (if you are, flag in conventional commit message with BREAKING CHANGE: describing the break)
  • I am not introducing a new dependency (add notes below if you are)
  • I have added unit tests for the new feature or bug fix (if not, why?)
  • I have fully tested (add details below) this the new feature or bug fix (if not, why?)
  • I have opened a PR for the related docs change (if not, why?)

Testing Instructions

  1. Set up:
snap install edgexfoundry --edge
snap install edgex-device-camera --channel=edge/pr-200
  1. Enable config and set a global config value:
$ snap set edgex-device-camera config-enabled=true
$ snap set edgex-device-camera config.service-startupmsg="testing injection"

$ snap start edgex-device-camera
Started.

$ snap logs -n=all edgex-device-camera | grep "testing"
2022-04-26T10:47:09+02:00 edgex-device-camera.device-camera[44710]: level=INFO ts=2022-04-26T08:47:09.92085086Z app=device-camera source=variables.go:352 msg="Variables override of 'Service.StartupMsg' by environment variable: SERVICE_STARTUPMSG=testing injection"
2022-04-26T10:47:10+02:00 edgex-device-camera.device-camera[44710]: level=INFO ts=2022-04-26T08:47:10.260535937Z app=device-camera source=message.go:55 msg="testing injection"
  1. Set a app-specific value:
$ snap set edgex-device-camera apps.device-camera.config.service-port=11111

$ snap restart edgex-device-camera
Restarted.

$ snap logs -n=all edgex-device-camera | grep "11111"
2022-04-26T10:47:39+02:00 edgex-device-camera.device-camera[44902]: level=INFO ts=2022-04-26T08:47:39.401816616Z app=device-camera source=variables.go:352 msg="Variables override of 'Service.Port' by environment variable: SERVICE_PORT=11111"
2022-04-26T10:47:39+02:00 edgex-device-camera.device-camera[44902]: level=INFO ts=2022-04-26T08:47:39.414801789Z app=device-camera source=variables.go:352 msg="Variables override of 'Service.Port' by environment variable: SERVICE_PORT=11111"
2022-04-26T10:47:39+02:00 edgex-device-camera.device-camera[44902]: level=INFO ts=2022-04-26T08:47:39.440629755Z app=device-camera source=httpserver.go:123 msg="Web server starting (localhost:11111)"

New Dependency Instructions (If applicable)

@farshidtz farshidtz marked this pull request as ready for review April 26, 2022 08:48
@farshidtz
Copy link
Member Author

@MonicaisHer please review. Thanks.

Copy link
Contributor

@MonicaisHer MonicaisHer left a comment

Choose a reason for hiding this comment

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

@farshidtz Thank you. It works as expected.

Validate error handling:

$ snap install edgex-device-camera --channel=edge/pr-200
$ snap set edgex-device-camera config.service-startupmsg="testing injection"
error: cannot perform the following tasks:
- Run configure hook of "edgex-device-camera" snap (run hook "configure": 
-----
edgex-device-camera.configure: could not process options: 'config.' and 'apps.' options are allowed only when config-enabled is true.

WARNING: Setting config-enabled=true will unset existing 'env.' options and ignore future sets!!
-----)

$ snap set edgex-device-camera config-enabled=true
$ snap set edgex-device-camera config.service.startupmsg="testing injection"
error: cannot perform the following tasks:
- Run configure hook of "edgex-device-camera" snap (run hook "configure": edgex-device-camera.configure: could not process options: error converting config key to environment variable key: config key must not contain dots: service.startupmsg)

Test:

$ snap set edgex-device-camera config.service-startupmsg="testing injection"
$ snap start edgex-device-camera
Started.

$ snap logs -n=all edgex-device-camera | grep "testing"
2022-04-26T11:52:45+02:00 edgex-device-camera.device-camera[1764467]: level=INFO ts=2022-04-26T09:52:45.477255393Z app=device-camera source=variables.go:352 msg="Variables override of 'Service.StartupMsg' by environment variable: SERVICE_STARTUPMSG=testing injection"
2022-04-26T11:52:45+02:00 edgex-device-camera.device-camera[1764467]: level=INFO ts=2022-04-26T09:52:45.496986552Z app=device-camera source=variables.go:352 msg="Variables override of 'Service.StartupMsg' by environment variable: SERVICE_STARTUPMSG=testing injection"


$ snap set edgex-device-camera apps.device-camera.config.service-port=11111
$ snap restart edgex-device-camera
Restarted.

$ snap logs -n=all edgex-device-camera | grep "11111"
2022-04-26T11:53:28+02:00 edgex-device-camera.device-camera[1764673]: level=INFO ts=2022-04-26T09:53:28.528545011Z app=device-camera source=variables.go:352 msg="Variables override of 'Service.Port' by environment variable: SERVICE_PORT=11111"
2022-04-26T11:53:28+02:00 edgex-device-camera.device-camera[1764673]: level=INFO ts=2022-04-26T09:53:28.540771503Z app=device-camera source=variables.go:352 msg="Variables override of 'Service.Port' by environment variable: SERVICE_PORT=11111"
2022-04-26T11:53:28+02:00 edgex-device-camera.device-camera[1764673]: level=INFO ts=2022-04-26T09:53:28.565197185Z app=device-camera source=httpserver.go:123 msg="Web server starting (localhost:11111)"

@lenny-goodell lenny-goodell merged commit 02782fb into edgexfoundry:main Apr 26, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants