Skip to content

Commit

Permalink
Add remote configuration support as requested by Samantha.
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan committed Jul 21, 2022
1 parent 0c8e55d commit dd5b3be
Showing 1 changed file with 69 additions and 5 deletions.
74 changes: 69 additions & 5 deletions tools/minify/common/buildconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ package common

import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"regexp"
"sort"
"time"

"github.com/influxdata/telegraf/filter"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/toml"
"github.com/influxdata/toml/ast"
)

// fetchURLRe is a regex to determine whether the requested file should
// be fetched from a remote or read from the filesystem.
var fetchURLRe = regexp.MustCompile(`^\w+://`)
var httpLoadConfigRetryInterval = 10 * time.Second

type BuildConfig struct {
Aggregators map[string]bool
Inputs map[string]bool
Expand Down Expand Up @@ -40,16 +50,17 @@ func LoadConfiguration(filename string) (*BuildConfig, error) {
return config, nil
}

func ImportConfigurations(filenames []string) (*BuildConfig, error) {
func ImportConfigurations(configurations []string) (*BuildConfig, error) {
config := NewConfiguration(false)
for _, filename := range filenames {
buf, err := os.ReadFile(filename)

for _, cfg := range configurations {
buf, err := loadConfig(cfg)
if err != nil {
return nil, fmt.Errorf("reading %q failed: %v", filename, err)
return nil, fmt.Errorf("reading %q failed: %v", cfg, err)
}

if err := config.extractPluginsFromConfig(buf); err != nil {
return nil, fmt.Errorf("extracting plugins from %q failed: %v", filename, err)
return nil, fmt.Errorf("extracting plugins from %q failed: %v", cfg, err)
}
}

Expand Down Expand Up @@ -372,3 +383,56 @@ func globSelection(selection map[string]bool, available []packageInfo) map[strin

return globbed
}

func loadConfig(config string) ([]byte, error) {
if fetchURLRe.MatchString(config) {
u, err := url.Parse(config)
if err != nil {
return nil, err
}

switch u.Scheme {
case "https", "http":
return fetchConfig(u)
default:
return nil, fmt.Errorf("scheme %q not supported", u.Scheme)
}
}

// If it isn't a https scheme, try it as a file
return os.ReadFile(config)
}

func fetchConfig(u *url.URL) ([]byte, error) {
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}

if v, exists := os.LookupEnv("INFLUX_TOKEN"); exists {
req.Header.Add("Authorization", "Token "+v)
}
req.Header.Add("Accept", "application/toml")
req.Header.Set("User-Agent", internal.ProductToken())

retries := 3
for i := 0; i <= retries; i++ {
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("retry %d of %d failed connecting to HTTP config server %s", i, retries, err)
}

if resp.StatusCode != http.StatusOK {
if i < retries {
log.Printf("Error getting HTTP config. Retry %d of %d in %s. Status=%d", i, retries, httpLoadConfigRetryInterval, resp.StatusCode)
time.Sleep(httpLoadConfigRetryInterval)
continue
}
return nil, fmt.Errorf("retry %d of %d failed to retrieve remote config: %s", i, retries, resp.Status)
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}

return nil, nil
}

0 comments on commit dd5b3be

Please sign in to comment.