97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func generateEnv(yamlFile string) {
|
|
config := loadConfig(yamlFile)
|
|
|
|
// Check that all local directories exist
|
|
for _, logEntry := range config.Logs {
|
|
if _, err := os.Stat(logEntry.LocalDirectory); os.IsNotExist(err) {
|
|
log.Fatalf("User is required to create %s", logEntry.LocalDirectory)
|
|
}
|
|
}
|
|
|
|
// Generate .env file for each log
|
|
for _, logEntry := range config.Logs {
|
|
envPath := filepath.Join(logEntry.LocalDirectory, ".env")
|
|
|
|
// Create combined roots.pem file
|
|
rootsPemPath := filepath.Join(logEntry.LocalDirectory, "roots.pem")
|
|
err := createCombinedRootsPem(config.Roots, logEntry.ExtraRoots, rootsPemPath)
|
|
if err != nil {
|
|
log.Fatalf("Failed to create %s: %v", rootsPemPath, err)
|
|
}
|
|
fmt.Printf("Generated %s\n", rootsPemPath)
|
|
|
|
// Build TESSERACT_ARGS string
|
|
args := []string{
|
|
fmt.Sprintf("--private_key=%s", logEntry.Secret),
|
|
fmt.Sprintf("--origin=%s.log.ct.ipng.ch", logEntry.ShortName),
|
|
fmt.Sprintf("--storage_dir=%s", logEntry.LocalDirectory),
|
|
fmt.Sprintf("--roots_pem_file=%s", rootsPemPath),
|
|
}
|
|
|
|
// Add http_endpoint if Listen is specified
|
|
if logEntry.Listen != "" {
|
|
args = append(args, fmt.Sprintf("--http_endpoint=%s", logEntry.Listen))
|
|
}
|
|
|
|
tesseractArgs := strings.Join(args, " ")
|
|
envContent := fmt.Sprintf("TESSERACT_ARGS=\"%s\"\n", tesseractArgs)
|
|
|
|
err = os.WriteFile(envPath, []byte(envContent), 0644)
|
|
if err != nil {
|
|
log.Fatalf("Failed to write %s: %v", envPath, err)
|
|
}
|
|
|
|
fmt.Printf("Generated %s\n", envPath)
|
|
}
|
|
}
|
|
|
|
func createCombinedRootsPem(rootsFile, extraRootsFile, outputPath string) error {
|
|
// Create output file
|
|
outputFile, err := os.Create(outputPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create output file: %v", err)
|
|
}
|
|
defer outputFile.Close()
|
|
|
|
// Copy main roots file
|
|
if rootsFile != "" {
|
|
rootsData, err := os.Open(rootsFile)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open roots file %s: %v", rootsFile, err)
|
|
}
|
|
defer rootsData.Close()
|
|
|
|
_, err = io.Copy(outputFile, rootsData)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to copy roots file: %v", err)
|
|
}
|
|
}
|
|
|
|
// Append extra roots file if it exists
|
|
if extraRootsFile != "" {
|
|
extraRootsData, err := os.Open(extraRootsFile)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open extra roots file %s: %v", extraRootsFile, err)
|
|
}
|
|
defer extraRootsData.Close()
|
|
|
|
_, err = io.Copy(outputFile, extraRootsData)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to copy extra roots file: %v", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|