-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add debug prep scripts and configuration for server (#1104)
- Loading branch information
1 parent
e09f8b2
commit a2cb69d
Showing
11 changed files
with
253 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,5 @@ configuration/ | |
tags | ||
nohup.out | ||
test/mocks | ||
__debug_bin | ||
__debug_bin* | ||
.DS_Store |
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
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
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,5 @@ | ||
module github.com/stackrox/infra/scripts/local-dev | ||
|
||
go 1.20 | ||
|
||
require gopkg.in/yaml.v2 v2.4.0 // indirect |
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,3 @@ | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= | ||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= |
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,162 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"text/template" | ||
|
||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
var localConfigurationDir = "configuration/" | ||
|
||
func createLocalConfigurationDir() error { | ||
// remove previous configurations | ||
err := os.RemoveAll(localConfigurationDir) | ||
if err != nil { | ||
return fmt.Errorf("error while deleting the local configuration directory: %v", err) | ||
} | ||
|
||
// create clean configuration directory | ||
err = os.MkdirAll(localConfigurationDir, os.ModePerm) | ||
if err != nil { | ||
return fmt.Errorf("error occurred creating the local configuration directory: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func readFileToMap(path string) (map[string]string, error) { | ||
data := make(map[string]string) | ||
fileContent, err := os.ReadFile(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = yaml.Unmarshal(fileContent, &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func getPathFromKey(key string) string { | ||
// replace __ with / for directories | ||
filepath := strings.Join(strings.Split(key, "__"), "/") | ||
// replace last _ with . for file ending | ||
index := strings.LastIndex(filepath, "_") | ||
if index != -1 { | ||
return filepath[:index] + "." + filepath[index+1:] | ||
} | ||
|
||
return filepath | ||
} | ||
|
||
func determineValues() (map[string]interface{}, error) { | ||
values := map[string]interface{}{ | ||
"Values": map[string]interface{}{ | ||
"testMode": true, | ||
}, | ||
} | ||
|
||
data := make(map[string]interface{}) | ||
fileContent, err := os.ReadFile("chart/infra-server/Chart.yaml") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = yaml.Unmarshal(fileContent, &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
values["Chart"] = map[string]interface{}{ | ||
"Annotations": data["annotations"], | ||
} | ||
|
||
return values, nil | ||
} | ||
|
||
func renderFile(path, content string, decodeString bool) error { | ||
configPath := fmt.Sprintf("%s%s", localConfigurationDir, path) | ||
dir := filepath.Dir(configPath) | ||
err := os.MkdirAll(dir, os.ModePerm) | ||
if err != nil { | ||
return fmt.Errorf("error while creating directories: %v", err) | ||
} | ||
|
||
if decodeString { | ||
decodedContent, err := base64.StdEncoding.DecodeString(content) | ||
if err != nil { | ||
return fmt.Errorf("error while decoding base64 content: %v", err) | ||
} | ||
content = string(decodedContent) | ||
} | ||
|
||
// Use a simple template engine to render the template | ||
tmpl, err := template.New("template").Parse(content) | ||
if err != nil { | ||
return fmt.Errorf("error while parsing the template: %v", err) | ||
} | ||
|
||
outputFile, err := os.Create(configPath) | ||
if err != nil { | ||
return fmt.Errorf("error while creating the output file: %v", err) | ||
} | ||
defer outputFile.Close() | ||
|
||
values, err := determineValues() | ||
if err != nil { | ||
return fmt.Errorf("An error occurred while determining values: %v", err) | ||
} | ||
err = tmpl.Execute(outputFile, values) | ||
if err != nil { | ||
return fmt.Errorf("An error occurred while rendering the template: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func renderFlavors() error { | ||
path := "flavors.yaml" | ||
fileContent, err := os.ReadFile(fmt.Sprintf("chart/infra-server/static/%s", path)) | ||
if err != nil { | ||
return err | ||
} | ||
return renderFile(path, string(fileContent), false) | ||
} | ||
|
||
func main() { | ||
if err := createLocalConfigurationDir(); err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
return | ||
} | ||
|
||
data, err := readFileToMap("chart/infra-server/configuration/development-values-from-files.yaml") | ||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
return | ||
} | ||
|
||
for key, content := range data { | ||
filepath := getPathFromKey(key) | ||
err := renderFile(filepath, content, true) | ||
if err != nil { | ||
fmt.Printf("Error creating file %s: %v\n", filepath, err) | ||
return | ||
} else { | ||
fmt.Println("Created", filepath) | ||
} | ||
} | ||
|
||
if err := renderFlavors(); err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
return | ||
} else { | ||
fmt.Println("Created flavors.yaml") | ||
} | ||
} |
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,30 @@ | ||
#!/bin/bash | ||
|
||
set -euo pipefail | ||
|
||
if ! sed --version | grep -q GNU; then | ||
echo "Only GNU sed is supported" | ||
exit 1 | ||
fi | ||
|
||
echo "INFO: Download secrets" | ||
ENVIRONMENT=development make secrets-download | ||
|
||
echo "INFO: Place configuration with rendered values" | ||
go run scripts/local-dev/main.go | ||
|
||
echo "INFO: Replace /configuration/ -> ../../configuration/ in infra.yaml" | ||
sed -i 's/\/configuration\//..\/..\/configuration\/tls\//g' configuration/infra.yaml | ||
echo "INFO: Replace configuration/ -> ../../configuration/ in flavors.yaml" | ||
sed -i 's/configuration\//..\/..\/configuration\//g' configuration/flavors.yaml | ||
|
||
echo "INFO: Replace /etc/infra/static -> ../../ui/build in infra.yaml" | ||
sed -i 's/\/etc\/infra\/static/..\/..\/ui\/build/g' configuration/infra.yaml | ||
|
||
echo "INFO: Copy workflow templates in place" | ||
cp chart/infra-server/static/{test,workflow}-*.yaml configuration/ | ||
|
||
echo "Prepare UI + CLI (for downloads)" | ||
make -C ui | ||
make cli | ||
cp -R bin ui/build/downloads |
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 was deleted.
Oops, something went wrong.