-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(install): local validation for OHI installations (#1516)
- Loading branch information
1 parent
d9cf52b
commit 0f2c034
Showing
7 changed files
with
134 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package validation | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type integrationType struct { | ||
Name string `yaml:"name"` | ||
Env map[string]string `yaml:"env"` | ||
} | ||
|
||
type configType struct { | ||
Integrations []integrationType `yaml:"integrations"` | ||
} | ||
|
||
// Validate an integration using its <integrationName>-config.yml | ||
// by iterating over the defined integrations and running the | ||
// integration command with its defined environment variables. | ||
// <integrationName>-config.yml is determined to be valid if every | ||
// defined integration exits without error (exits with exit code 0). | ||
// The <integrationName>-config.yml is located in the | ||
// default configuration directory, which may vary by GOOS. | ||
func ValidateIntegration(integrationName string) (string, error) { | ||
configBasename := fmt.Sprintf("%s-config.yml", integrationName) | ||
|
||
configPath := filepath.Join(ConfigurationsDirname, configBasename) | ||
|
||
config, err := readConfig(configPath) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
for _, integration := range config.Integrations { | ||
integrationBasename := integration.Name | ||
|
||
binPath := filepath.Join(IntegrationsDirname, integrationBasename) | ||
|
||
cmd := exec.Command(binPath) | ||
|
||
for k, v := range integration.Env { | ||
env := fmt.Sprintf("%s=%s", k, v) | ||
|
||
cmd.Env = append(cmd.Env, env) | ||
} | ||
|
||
var e strings.Builder | ||
|
||
cmd.Stderr = &e | ||
|
||
if err := cmd.Run(); err != nil { | ||
stderr := e.String() | ||
|
||
if stderr != "" { | ||
s := strings.TrimSpace(stderr) | ||
|
||
return "", fmt.Errorf("%w: %s", err, s) | ||
} | ||
|
||
return "", err | ||
} | ||
} | ||
|
||
return "", nil | ||
} | ||
|
||
// Reads and unmarshals an <integrationName>-config.yml from the | ||
// given configPath, returning a configType{} containing the | ||
// defined integrations and their respective environments. | ||
// Returns an empty configType{} and an error if: | ||
// 1.) The file does not exist | ||
// 2.) The file can not be read | ||
// 3.) The file cannot be unmarshalled to a configType{} | ||
func readConfig(configPath string) (configType, error) { | ||
if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) { | ||
return configType{}, err | ||
} | ||
|
||
b, err := os.ReadFile(configPath) | ||
|
||
if err != nil { | ||
return configType{}, err | ||
} | ||
|
||
config := configType{} | ||
|
||
err = yaml.Unmarshal(b, &config) | ||
|
||
if err != nil { | ||
return configType{}, err | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package validation | ||
|
||
// There are no integrations for macOS, | ||
// therefore there is no directory | ||
// for integrations or configurations. | ||
const IntegrationsDirname = "/dev/null" | ||
const ConfigurationsDirname = "/dev/null" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package validation | ||
|
||
const IntegrationsDirname = "/var/db/newrelic-infra/newrelic-integrations/bin/" | ||
const ConfigurationsDirname = "/etc/newrelic-infra/integrations.d/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package validation | ||
|
||
const IntegrationsDirname = "C:\\Program Files\\New Relic\\newrelic-infra\\newrelic-integrations\\bin\\" | ||
const ConfigurationsDirname = "C:\\Program Files\\New Relic\\newrelic-infra\\integrations.d\\" |