Some checks failed
Tests / 🛃 Unit tests (push) Failing after 13s
Tests / 🗄 DB tests (push) Failing after 19s
Tests / 🐍 Lint python scripts (push) Failing after 1s
Tests / ⌨ Golang Lint (push) Failing after 1s
Tests / 📦 Packit config lint (push) Failing after 1s
Tests / 🔍 Check source preparation (push) Failing after 1s
Tests / 🔍 Check for valid snapshot urls (push) Failing after 1s
Tests / 🔍 Check for missing or unused runner repos (push) Failing after 1s
Tests / 🐚 Shellcheck (push) Failing after 1s
Tests / 📦 RPMlint (push) Failing after 1s
Tests / Gitlab CI trigger helper (push) Failing after 1s
Tests / 🎀 kube-linter (push) Failing after 1s
Tests / 🧹 cloud-cleaner-is-enabled (push) Successful in 3s
Tests / 🔍 Check spec file osbuild/images dependencies (push) Failing after 1s
216 lines
5.3 KiB
Go
216 lines
5.3 KiB
Go
package production
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
// ProductionConfigManager handles loading and saving production configuration
|
|
type ProductionConfigManager struct {
|
|
configPath string
|
|
config *ProductionConfig
|
|
}
|
|
|
|
// LoadProductionConfig loads production configuration from file
|
|
func LoadProductionConfig(configPath string) (*ProductionConfig, error) {
|
|
manager := &ProductionConfigManager{
|
|
configPath: configPath,
|
|
}
|
|
|
|
return manager.Load()
|
|
}
|
|
|
|
// Load loads configuration from file
|
|
func (pcm *ProductionConfigManager) Load() (*ProductionConfig, error) {
|
|
// Check if config file exists
|
|
if _, err := os.Stat(pcm.configPath); os.IsNotExist(err) {
|
|
// Create default configuration
|
|
pcm.config = pcm.createDefaultConfig()
|
|
return pcm.config, pcm.Save()
|
|
}
|
|
|
|
// Read existing configuration
|
|
data, err := os.ReadFile(pcm.configPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read config file: %w", err)
|
|
}
|
|
|
|
// Parse configuration
|
|
pcm.config = &ProductionConfig{}
|
|
if err := json.Unmarshal(data, pcm.config); err != nil {
|
|
return nil, fmt.Errorf("failed to parse config file: %w", err)
|
|
}
|
|
|
|
return pcm.config, nil
|
|
}
|
|
|
|
// Save saves configuration to file
|
|
func (pcm *ProductionConfigManager) Save() error {
|
|
if pcm.config == nil {
|
|
return fmt.Errorf("no configuration to save")
|
|
}
|
|
|
|
// Create directory if it doesn't exist
|
|
configDir := filepath.Dir(pcm.configPath)
|
|
if err := os.MkdirAll(configDir, 0755); err != nil {
|
|
return fmt.Errorf("failed to create config directory: %w", err)
|
|
}
|
|
|
|
// Marshal configuration
|
|
data, err := json.MarshalIndent(pcm.config, "", " ")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to marshal config: %w", err)
|
|
}
|
|
|
|
// Write to file
|
|
if err := os.WriteFile(pcm.configPath, data, 0644); err != nil {
|
|
return fmt.Errorf("failed to write config file: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// UpdateConfig updates configuration and saves to file
|
|
func (pcm *ProductionConfigManager) UpdateConfig(updates map[string]interface{}) error {
|
|
if pcm.config == nil {
|
|
return fmt.Errorf("no configuration loaded")
|
|
}
|
|
|
|
// Apply updates
|
|
for key, value := range updates {
|
|
switch key {
|
|
case "enabled":
|
|
if boolVal, ok := value.(bool); ok {
|
|
pcm.config.Enabled = boolVal
|
|
}
|
|
case "environment":
|
|
if strVal, ok := value.(string); ok {
|
|
pcm.config.Environment = strVal
|
|
}
|
|
case "deployment_path":
|
|
if strVal, ok := value.(string); ok {
|
|
pcm.config.DeploymentPath = strVal
|
|
}
|
|
case "performance":
|
|
if boolVal, ok := value.(bool); ok {
|
|
pcm.config.Performance = boolVal
|
|
}
|
|
case "automation":
|
|
if boolVal, ok := value.(bool); ok {
|
|
pcm.config.Automation = boolVal
|
|
}
|
|
case "support":
|
|
if boolVal, ok := value.(bool); ok {
|
|
pcm.config.Support = boolVal
|
|
}
|
|
case "metadata":
|
|
if mapVal, ok := value.(map[string]string); ok {
|
|
pcm.config.Metadata = mapVal
|
|
}
|
|
}
|
|
}
|
|
|
|
// Save updated configuration
|
|
return pcm.Save()
|
|
}
|
|
|
|
// createDefaultConfig creates a default production configuration
|
|
func (pcm *ProductionConfigManager) createDefaultConfig() *ProductionConfig {
|
|
return &ProductionConfig{
|
|
Enabled: true,
|
|
Environment: "staging",
|
|
DeploymentPath: "/var/lib/debian-forge/production",
|
|
Performance: true,
|
|
Automation: true,
|
|
Support: true,
|
|
Metadata: map[string]string{
|
|
"version": "1.0.0",
|
|
"created": time.Now().Format(time.RFC3339),
|
|
"description": "Default production configuration for Debian Forge",
|
|
},
|
|
}
|
|
}
|
|
|
|
// ValidateConfig validates the configuration
|
|
func (pcm *ProductionConfigManager) ValidateConfig() error {
|
|
if pcm.config == nil {
|
|
return fmt.Errorf("no configuration loaded")
|
|
}
|
|
|
|
// Validate deployment path
|
|
if pcm.config.DeploymentPath == "" {
|
|
return fmt.Errorf("deployment path is required")
|
|
}
|
|
|
|
// Validate environment
|
|
if pcm.config.Environment == "" {
|
|
return fmt.Errorf("environment is required")
|
|
}
|
|
|
|
// Validate paths are absolute
|
|
if !isAbsolutePath(pcm.config.DeploymentPath) {
|
|
return fmt.Errorf("deployment path must be absolute")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// isAbsolutePath checks if a path is absolute
|
|
func isAbsolutePath(path string) bool {
|
|
return len(path) > 0 && path[0] == '/'
|
|
}
|
|
|
|
// GetPerformanceConfig returns performance configuration
|
|
func (pcm *ProductionConfigManager) GetPerformanceConfig() *PerformanceConfig {
|
|
if pcm.config == nil {
|
|
return nil
|
|
}
|
|
|
|
return &PerformanceConfig{
|
|
Enabled: pcm.config.Performance,
|
|
LoadTesting: true,
|
|
Scalability: true,
|
|
Benchmarking: true,
|
|
Thresholds: map[string]int{
|
|
"max_response_time": 2000,
|
|
"max_build_time": 1800,
|
|
"max_memory_usage": 80,
|
|
},
|
|
Metadata: pcm.config.Metadata,
|
|
}
|
|
}
|
|
|
|
// GetDeploymentConfig returns deployment configuration
|
|
func (pcm *ProductionConfigManager) GetDeploymentConfig() *DeploymentConfig {
|
|
if pcm.config == nil {
|
|
return nil
|
|
}
|
|
|
|
return &DeploymentConfig{
|
|
Enabled: pcm.config.Automation,
|
|
Scripts: true,
|
|
ConfigMgmt: true,
|
|
Provisioning: true,
|
|
Testing: true,
|
|
Metadata: pcm.config.Metadata,
|
|
}
|
|
}
|
|
|
|
// GetSupportConfig returns support configuration
|
|
func (pcm *ProductionConfigManager) GetSupportConfig() *SupportConfig {
|
|
if pcm.config == nil {
|
|
return nil
|
|
}
|
|
|
|
return &SupportConfig{
|
|
Enabled: pcm.config.Support,
|
|
Documentation: true,
|
|
Maintenance: true,
|
|
Troubleshooting: true,
|
|
Training: true,
|
|
Metadata: pcm.config.Metadata,
|
|
}
|
|
}
|