Skip to content

Commit

Permalink
feat(snap): Add support for environment variable injection (#3986)
Browse files Browse the repository at this point in the history
* feat(snap): use updated environment variable injection

Signed-off-by: Siggi Skulason <[email protected]>

* feat(snap): add proxy option handling

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

Co-authored-by: Siggi Skulason <[email protected]>
  • Loading branch information
farshidtz and Siggi Skulason authored Apr 26, 2022
1 parent 8a434ef commit 780750f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 4 deletions.
4 changes: 3 additions & 1 deletion snap/local/hooks/cmd/configure/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ var cli *hooks.CtlCli = hooks.NewSnapCtl()
func main() {
// no subcommand, as called by snapd
if len(os.Args) == 1 {
// configure everything
// process the EdgeX >=2.2 snap options
processAppOptions()
// configure everything else, incl. the legacy snap options
configure()
return
}
Expand Down
52 changes: 52 additions & 0 deletions snap/local/hooks/cmd/configure/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"

hooks "github.com/canonical/edgex-snap-hooks/v2"
app_options "github.com/canonical/edgex-snap-hooks/v2/options"
)

func applyConfigOptions(service string) error {
Expand Down Expand Up @@ -60,8 +61,59 @@ func options() {

hooks.Info("edgexfoundry:configure-options: handling config options for a single service: " + *service)

// process the EdgeX >=2.2 snap options
err = app_options.ProcessAppCustomOptions(*service)
if err != nil {
hooks.Error(fmt.Sprintf("edgexfoundry:configure-options: could not process custom options: %v", err))
os.Exit(1)
}

// process the legacy snap options
if err := applyConfigOptions(*service); err != nil {
hooks.Error(fmt.Sprintf("edgexfoundry:configure-options: error handling config options for %s: %v", *service, err))
os.Exit(1)
}
}

func processAppOptions() {
err := app_options.ProcessAppConfig(
"core-data",
"core-metadata",
"core-command",
"support-notifications",
"support-scheduler",
"app-service-configurable",
"device-virtual",
"security-secret-store",
"security-secretstore-setup",
"security-proxy-setup",
"security-bootstrapper",
"sys-mgmgt-agent",
)
if err != nil {
hooks.Error(fmt.Sprintf("edgexfoundry:configure could not process config options: %v", err))
os.Exit(1)
}

// After installation, the configure hook initiates the deferred startup of services,
// processes snap options and exits. The actual services startup happens only
// after the configure hook exits.
//
// The following options should not be processed within the configure hook during
// the initial installation (install-mode=defer-startup). They should be processed
// only on follow-up calls to the configure hook (i.e. when snap set/unset is called)
installMode, err := hooks.NewSnapCtl().Config("install-mode") // this set in the install hook
if err != nil {
hooks.Error(fmt.Sprintf("edgexfoundry:configure failed to read 'install-mode': %s", err))
os.Exit(1)
}
if installMode != "defer-startup" {
err = app_options.ProcessAppCustomOptions(
"secrets-config", // also processed in security-proxy-post-setup.sh
)
if err != nil {
hooks.Error(fmt.Sprintf("edgexfoundry:configure: could not process custom options: %v", err))
os.Exit(1)
}
}
}
5 changes: 4 additions & 1 deletion snap/local/hooks/go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/canonical/edgex-go/hooks

require github.com/canonical/edgex-snap-hooks/v2 v2.1.3
require github.com/canonical/edgex-snap-hooks/v2 v2.2.0-beta.5

// replace github.com/canonical/edgex-snap-hooks/v2 => ./edgex-snap-hooks
// replace github.com/canonical/edgex-snap-hooks/v2 => github.com/farshidtz/edgex-snap-hooks/v2 d43ccc771100d663099c8ca8e3974d78076b2058

go 1.17
4 changes: 2 additions & 2 deletions snap/local/hooks/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/canonical/edgex-snap-hooks/v2 v2.1.3 h1:mcV/atn6k6sN6Uik+lSQGEZi4Q6r96epgBW+u6AGZ3Y=
github.com/canonical/edgex-snap-hooks/v2 v2.1.3/go.mod h1:rOxrwdYL7hJDhxFH3uV+nVgLPjWOhJWgM5PRD5YG1jI=
github.com/canonical/edgex-snap-hooks/v2 v2.2.0-beta.5 h1:EDFjmHy8CG4T8uFPqD+Per8Hgk250PvRsMgEDCXjYtE=
github.com/canonical/edgex-snap-hooks/v2 v2.2.0-beta.5/go.mod h1:rOxrwdYL7hJDhxFH3uV+nVgLPjWOhJWgM5PRD5YG1jI=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
3 changes: 3 additions & 0 deletions snap/local/runtime-helpers/bin/security-proxy-post-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ export PATH="$SNAP/bin:$PATH"
# Several config options depend on resources that only exist after proxy
# setup. This re-applies the config options logic after deferred startup:
$SNAP/snap/hooks/configure options --service=security-proxy

# Process the EdgeX >=2.2 snap options
$SNAP/snap/hooks/configure options --service=secrets-config

0 comments on commit 780750f

Please sign in to comment.