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 for variables in the configuration parser filename #34

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions server/config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,33 @@ package server
import (
"runtime"

"github.com/gammazero/workerpool"
"fmt"
"strings"

"github.com/gammazero/workerpool"
"github.com/pelican-dev/wings/internal/ufs"
)

// Helper function to replace variables in the file path of the configuration parser
func replaceParserConfigPathVariables(filename string, envvars map[string]interface{}) string {
// Check if filename contains at least one '{' and one '}'
// This is here for performance as 99% of the eggs configuration parsers do not have variables in its path
if !strings.Contains(filename, "{") || !strings.Contains(filename, "}") {
return filename
}

// Replace "{{" with "${" and "}}" with "}"
filename = strings.ReplaceAll(filename, "{{", "${")
filename = strings.ReplaceAll(filename, "}}", "}")

// replaces ${varname} with varval
for varname, varval := range envvars {
filename = strings.ReplaceAll(filename, fmt.Sprintf("${%s}", varname), fmt.Sprint(varval))
}

return filename
}

// UpdateConfigurationFiles updates all the defined configuration files for
// a server automatically to ensure that they always use the specified values.
func (s *Server) UpdateConfigurationFiles() {
Expand All @@ -20,7 +42,8 @@ func (s *Server) UpdateConfigurationFiles() {
f := cf

pool.Submit(func() {
file, err := s.Filesystem().UnixFS().Touch(f.FileName, ufs.O_RDWR|ufs.O_CREATE, 0o644)
filename := replaceParserConfigPathVariables(f.FileName, s.Config().EnvVars)
file, err := s.Filesystem().UnixFS().Touch(filename, ufs.O_RDWR|ufs.O_CREATE, 0o644)
if err != nil {
s.Log().WithField("file_name", f.FileName).WithField("error", err).Error("failed to open file for configuration")
return
Expand Down
Loading