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

feat(snap): use updated environment variable injection #25

Conversation

MonicaisHer
Copy link
Contributor

@MonicaisHer MonicaisHer commented Apr 21, 2022

For details on the new scheme for setting environment variables, please refer to edgexfoundry/edgex-go#3986.
Signed-off-by: Mengyi Wang [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/device-gpio/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?) N/A - they are in the imported module Add tests for snap config options canonical/edgex-snap-testing#53
  • 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-gpio --channel=edge/pr-25
  1. Enable config and set a global config value:
$ snap set edgex-device-gpio config-enabled=true
$ snap set edgex-device-gpio config.service-startupmsg="testing injection"
$ snap start edgex-device-gpio
Started.

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

$ snap logs -n=all edgex-device-gpio | grep "11111"
2022-04-22T11:47:19+02:00 edgex-device-gpio.device-gpio[1257533]: level=INFO ts=2022-04-22T09:47:19.125348038Z app=device-gpio source=variables.go:352 msg="Variables override of 'Service.Port' by environment variable: SERVICE_PORT=11111"
2022-04-22T11:47:19+02:00 edgex-device-gpio.device-gpio[1257533]: level=INFO ts=2022-04-22T09:47:19.126726662Z app=device-gpio source=httpserver.go:123 msg="Web server starting (localhost:11111)"

New Dependency Instructions (If applicable)

@MonicaisHer MonicaisHer force-pushed the EDGEX-425-integrate-env-var-injection-with-device-gpio-snap branch from 0eee028 to 1b28166 Compare April 22, 2022 09:37
@MonicaisHer
Copy link
Contributor Author

MonicaisHer commented Apr 22, 2022

This PR will be ready for review, once the new hook has been tagged and imported by this PR.

@MonicaisHer MonicaisHer marked this pull request as ready for review April 26, 2022 08:56
@MonicaisHer
Copy link
Contributor Author

@farshidtz Please review, thank you.

Copy link
Member

@farshidtz farshidtz left a comment

Choose a reason for hiding this comment

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

Thanks. Works as expected:

$ snap install edgex-device-gpio --channel=edge/pr-25
edgex-device-gpio (edge/pr-25) 2.0.0-dev.2 from Canonical✓ installed

# validate error handling
$ snap set edgex-device-gpio config.service-startupmsg="testing injection"
error: cannot perform the following tasks:
- Run configure hook of "edgex-device-gpio" snap (run hook "configure": 
-----
edgex-device-gpio.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-gpio config-enabled=true
$ snap set edgex-device-gpio config.service.startupmsg="testing injection"
error: cannot perform the following tasks:
- Run configure hook of "edgex-device-gpio" snap (run hook "configure": edgex-device-gpio.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-gpio config-enabled=true
$ snap set edgex-device-gpio config.service-startupmsg="testing injection"
$ snap start edgex-device-gpio
Started.
farshid:~$ snap logs -n=all edgex-device-gpio | grep "testing"
2022-04-26T11:31:20+02:00 edgex-device-gpio.device-gpio[52529]: level=INFO ts=2022-04-26T09:31:20.979233295Z app=device-gpio source=variables.go:352 msg="Variables override of 'Service.StartupMsg' by environment variable: SERVICE_STARTUPMSG=testing injection"
2022-04-26T11:31:21+02:00 edgex-device-gpio.device-gpio[52529]: level=INFO ts=2022-04-26T09:31:21.003927272Z app=device-gpio source=message.go:55 msg="testing injection"

$ snap set edgex-device-gpio apps.device-gpio.config.service-port=11111
$ snap restart edgex-device-gpio
Restarted.
$ snap logs -n=all edgex-device-gpio | grep "11111"
2022-04-26T11:31:39+02:00 edgex-device-gpio.device-gpio[52702]: level=INFO ts=2022-04-26T09:31:39.678107294Z app=device-gpio source=variables.go:352 msg="Variables override of 'Service.Port' by environment variable: SERVICE_PORT=11111"
2022-04-26T11:31:39+02:00 edgex-device-gpio.device-gpio[52702]: level=INFO ts=2022-04-26T09:31:39.680475727Z app=device-gpio source=httpserver.go:123 msg="Web server starting (localhost:11111)"
2022-04-26T11:31:39+02:00 edgex-device-gpio.device-gpio[52702]: level=ERROR ts=2022-04-26T09:31:39.681485921Z app=device-gpio source=httpserver.go:135 msg="Web server failed: listen tcp 127.0.0.1:11111: bind: address already in use"


$ cat /var/snap/edgex-device-gpio/current/config/device-gpio/res/device-gpio.env 
export SERVICE_STARTUPMSG="testing injection"
export SERVICE_PORT="11111"

@lenny-goodell lenny-goodell merged commit 789344b into edgexfoundry:main Apr 26, 2022
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.

3 participants