Add example environment configuration and Docker Compose setup
CI / Go Tests (push) Failing after 9s
CI / Build (push) Successful in 9s
Format / gofmt (push) Successful in 7s
Release Artifacts / Validate release tag (push) Successful in 1s
Release Artifacts / Build and release executables (push) Failing after 8s
Release Artifacts / Build and release Docker image (push) Has been skipped

- Introduced a new `.env.example` file to provide a template for environment variables used by the Scratchbox server.
- Added a `docker-compose.example.yml` file to facilitate easy deployment of the Scratchbox service with Docker.
- Updated `docker-entrypoint.sh` to enforce environment variable settings for server configuration.
- Removed the generation of a config key file from the workflow, simplifying the configuration process.
- Adjusted the README.md to reflect changes in configuration management and usage instructions.
This commit is contained in:
2026-06-01 04:36:53 -05:00
parent f74e028227
commit bc9077660b
10 changed files with 672 additions and 112 deletions
+88 -18
View File
@@ -32,18 +32,6 @@ func repoRoot(t *testing.T) string {
return filepath.Clean(filepath.Join(filepath.Dir(file), "..", ".."))
}
func writeConfigKeyFile(t *testing.T, configPath string) {
t.Helper()
keyPath := filepath.Join(filepath.Dir(configPath), "config.key")
key, err := config.GenerateEncryptionKey()
if err != nil {
t.Fatalf("GenerateEncryptionKey() error = %v", err)
}
if err := os.WriteFile(keyPath, []byte(key+"\n"), 0o600); err != nil {
t.Fatalf("WriteFile(config.key) error = %v", err)
}
}
func TestMainProcessSuccess(t *testing.T) {
t.Parallel()
@@ -77,7 +65,6 @@ security:
if err := os.WriteFile(cfgPath, []byte(cfg), 0o644); err != nil {
t.Fatalf("WriteFile() error = %v", err)
}
writeConfigKeyFile(t, cfgPath)
cmd := exec.Command(os.Args[0], "-test.run=TestMainHelperProcess")
cmd.Dir = repoRoot(t)
@@ -105,6 +92,75 @@ func TestMainProcessBadConfigFails(t *testing.T) {
}
}
func TestMainProcessMissingConfigAutoGenerates(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
cfgPath := filepath.Join(tmp, "nested", "config.yaml")
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, os.Args[0], "-test.run=TestMainHelperProcess")
cmd.Dir = repoRoot(t)
cmd.Env = append(os.Environ(),
"GO_WANT_MAIN_HELPER=1",
"MAIN_HELPER_MODE=missing-config-autogen",
"MAIN_HELPER_CONFIG="+cfgPath,
)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("helper process failed: %v, output=%s", err, string(out))
}
if !strings.Contains(string(out), "Review and adjust it, then re-run the same command.") {
t.Fatalf("helper output missing expected guidance, output=%q", string(out))
}
raw, err := os.ReadFile(cfgPath)
if err != nil {
t.Fatalf("ReadFile(generated config) error = %v", err)
}
if !strings.Contains(string(raw), "server:") {
t.Fatalf("generated config missing expected section")
}
}
func TestMainProcessEnvSuccess(t *testing.T) {
t.Parallel()
tmp := t.TempDir()
cmd := exec.Command(os.Args[0], "-test.run=TestMainHelperProcess")
cmd.Dir = repoRoot(t)
cmd.Env = append(os.Environ(),
"GO_WANT_MAIN_HELPER=1",
"MAIN_HELPER_MODE=env-success",
"SCRATCHBOX_SERVER_LISTEN_ADDR=127.0.0.1:0",
"SCRATCHBOX_STORAGE_DATA_DIR="+filepath.Join(tmp, "data"),
"SCRATCHBOX_SECURITY_ALLOWED_IPS=",
)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("helper process failed: %v, output=%s", err, string(out))
}
}
func TestServerConfigAndEnvMutuallyExclusive(t *testing.T) {
t.Parallel()
cmd := newRootCmd()
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
cmd.SetArgs([]string{"server", "--config", "config.yaml", "--env"})
err := cmd.Execute()
if err == nil {
t.Fatalf("expected error when both --config and --env are set")
}
if !strings.Contains(err.Error(), "none of the others can be") {
t.Fatalf("error = %q, want mutually exclusive flags error", err.Error())
}
}
func TestMainProcessStorageInitFails(t *testing.T) {
t.Parallel()
@@ -142,7 +198,6 @@ security:
if err := os.WriteFile(cfgPath, []byte(cfg), 0o644); err != nil {
t.Fatalf("WriteFile() error = %v", err)
}
writeConfigKeyFile(t, cfgPath)
cmd := exec.Command(os.Args[0], "-test.run=TestMainHelperProcess")
cmd.Dir = repoRoot(t)
@@ -181,6 +236,25 @@ func TestMainHelperProcess(t *testing.T) {
os.Args = []string{"scratchbox", "server", "--config", "/no/such/config.yaml"}
main()
os.Exit(0)
case "missing-config-autogen":
cfgPath := os.Getenv("MAIN_HELPER_CONFIG")
if cfgPath == "" {
os.Exit(2)
}
os.Args = []string{"scratchbox", "server", "--config", cfgPath}
main()
os.Exit(0)
case "env-success":
go func() {
time.Sleep(120 * time.Millisecond)
proc, err := os.FindProcess(os.Getpid())
if err == nil {
_ = proc.Signal(os.Interrupt)
}
}()
os.Args = []string{"scratchbox", "server", "--env"}
main()
os.Exit(0)
case "storage-fail":
cfgPath := os.Getenv("MAIN_HELPER_CONFIG")
os.Args = []string{"scratchbox", "server", "--config", cfgPath}
@@ -229,9 +303,6 @@ func TestGenerateConfigCommandWritesDefaultYAML(t *testing.T) {
if cfg.Storage.DataDir != "./data" {
t.Fatalf("storage.data_dir = %q, want %q", cfg.Storage.DataDir, "./data")
}
if cfg.Storage.EncryptionKeyFile != "config.key" {
t.Fatalf("storage.encryption_key_file = %q, want %q", cfg.Storage.EncryptionKeyFile, "config.key")
}
if cfg.Security.RateLimitUI.RequestsPerMinute != 360 {
t.Fatalf("security.rate_limit_ui.requests_per_minute = %d, want %d", cfg.Security.RateLimitUI.RequestsPerMinute, 360)
}
@@ -335,7 +406,6 @@ security:
if err := os.WriteFile(cfgPath, []byte(cfgRaw), 0o644); err != nil {
t.Fatalf("WriteFile() error = %v", err)
}
writeConfigKeyFile(t, cfgPath)
cfg, err := config.Load(cfgPath)
if err != nil {
+56 -3
View File
@@ -2,11 +2,14 @@ package main
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
@@ -20,21 +23,40 @@ import (
func newServerCmd() *cobra.Command {
var configPath string
var useEnv bool
cmd := &cobra.Command{
Use: "server",
Short: "Run the scratchbox service",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
return run(configPath)
return run(configPath, useEnv)
},
}
cmd.Flags().StringVarP(&configPath, "config", "c", "", "path to config file")
cmd.Flags().BoolVar(&useEnv, "env", false, "load config from SCRATCHBOX_* environment variables")
cmd.MarkFlagsMutuallyExclusive("config", "env")
return cmd
}
func run(configPath string) error {
cfg, err := config.Load(configPath)
func run(configPath string, useEnv bool) error {
var (
cfg config.Config
err error
)
if useEnv {
cfg, err = config.LoadFromEnv()
} else {
generated, err := ensureConfigFile(configPath)
if err != nil {
return fmt.Errorf("config init failed: %w", err)
}
if generated {
fmt.Fprintf(os.Stdout, "Generated default config at %s\nReview and adjust it, then re-run the same command.\n", strings.TrimSpace(configPath))
return nil
}
cfg, err = config.Load(configPath)
}
if err != nil {
return fmt.Errorf("config load failed: %w", err)
}
@@ -100,3 +122,34 @@ func run(configPath string) error {
}
return nil
}
func ensureConfigFile(configPath string) (bool, error) {
path := strings.TrimSpace(configPath)
if path == "" {
return false, nil
}
info, err := os.Stat(path)
if err == nil {
if info.IsDir() {
return false, fmt.Errorf("config path %q is a directory", path)
}
return false, nil
}
if !errors.Is(err, os.ErrNotExist) {
return false, fmt.Errorf("stat config path %q: %w", path, err)
}
cfg := config.Default()
content, err := marshalYAMLWithComments(cfg)
if err != nil {
return false, fmt.Errorf("marshal default config: %w", err)
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return false, fmt.Errorf("create config dir: %w", err)
}
if err := os.WriteFile(path, content, 0o644); err != nil {
return false, fmt.Errorf("write default config: %w", err)
}
return true, nil
}