package modules import ( "fmt" "os" "path/filepath" "strings" "sync" "time" "github.com/sirupsen/logrus" ) // DebianModulesManager handles Debian-adapted blue-build modules type DebianModulesManager struct { logger *logrus.Logger config *ModulesConfig modules map[string]DebianModule adaptations map[string]ModuleAdaptation mu sync.RWMutex } // ModulesConfig holds modules configuration type ModulesConfig struct { Enabled bool `json:"enabled"` ModulesPath string `json:"modules_path"` Adaptations bool `json:"adaptations"` Validation bool `json:"validation"` Metadata map[string]string `json:"metadata"` } // DebianModule represents a Debian-adapted module type DebianModule struct { ID string `json:"id"` Name string `json:"name"` Description string `json:"description"` Type string `json:"type"` Source string `json:"source"` Adapted bool `json:"adapted"` Enabled bool `json:"enabled"` Metadata map[string]interface{} `json:"metadata"` } // ModuleAdaptation represents module adaptation from blue-build type ModuleAdaptation struct { ID string `json:"id"` OriginalID string `json:"original_id"` Name string `json:"name"` Description string `json:"description"` Type string `json:"type"` Changes []string `json:"changes"` Status string `json:"status"` Enabled bool `json:"enabled"` Metadata map[string]interface{} `json:"metadata"` } // NewDebianModulesManager creates a new Debian modules manager func NewDebianModulesManager(config *ModulesConfig, logger *logrus.Logger) *DebianModulesManager { manager := &DebianModulesManager{ logger: logger, config: config, modules: make(map[string]DebianModule), adaptations: make(map[string]ModuleAdaptation), } // Initialize Debian modules manager.initializeDebianModules() manager.initializeModuleAdaptations() return manager } // initializeDebianModules initializes Debian-specific modules func (dmm *DebianModulesManager) initializeDebianModules() { // APT package management module (Debian equivalent of dnf) dmm.modules["apt"] = DebianModule{ ID: "apt", Name: "APT Package Management", Description: "Debian package and repository management using apt", Type: "apt", Source: "debian-native", Adapted: false, Enabled: true, Metadata: map[string]interface{}{ "package_manager": "apt", "repository_type": "deb", }, } // dpkg module (Debian equivalent of rpm-ostree) dmm.modules["dpkg"] = DebianModule{ ID: "dpkg", Name: "DPKG Package Management", Description: "Debian package management using dpkg", Type: "dpkg", Source: "debian-native", Adapted: false, Enabled: true, Metadata: map[string]interface{}{ "package_manager": "dpkg", "package_format": "deb", }, } // Debian-specific modules dmm.modules["debian-release"] = DebianModule{ ID: "debian-release", Name: "Debian Release Management", Description: "Manage Debian release information and configuration", Type: "debian-release", Source: "debian-native", Adapted: false, Enabled: true, Metadata: map[string]interface{}{ "release_type": "debian", "config_path": "/etc/debian_version", }, } // Debian kernel modules dmm.modules["debian-kernel"] = DebianModule{ ID: "debian-kernel", Name: "Debian Kernel Management", Description: "Manage Debian kernel configurations and modules", Type: "debian-kernel", Source: "debian-native", Adapted: false, Enabled: true, Metadata: map[string]interface{}{ "kernel_type": "debian", "module_path": "/lib/modules", }, } // Debian initramfs dmm.modules["debian-initramfs"] = DebianModule{ ID: "debian-initramfs", Name: "Debian Initramfs", Description: "Manage Debian initramfs configuration", Type: "debian-initramfs", Source: "debian-native", Adapted: false, Enabled: true, Metadata: map[string]interface{}{ "initramfs_type": "debian", "config_path": "/etc/initramfs-tools", }, } } // initializeModuleAdaptations initializes adaptations from blue-build modules func (dmm *DebianModulesManager) initializeModuleAdaptations() { // DNF to APT adaptation dmm.adaptations["dnf-to-apt"] = ModuleAdaptation{ ID: "dnf-to-apt", OriginalID: "dnf", Name: "DNF to APT Adaptation", Description: "Adapt DNF module functionality for Debian APT", Type: "adaptation", Changes: []string{ "Replace dnf with apt", "Replace RPM repositories with DEB repositories", "Adapt package installation syntax", "Replace COPR with Debian backports", }, Status: "completed", Enabled: true, Metadata: map[string]interface{}{ "original_module": "dnf", "target_module": "apt", "compatibility": "high", }, } // RPM-OSTree to DPKG adaptation dmm.adaptations["rpm-ostree-to-dpkg"] = ModuleAdaptation{ ID: "rpm-ostree-to-dpkg", OriginalID: "rpm-ostree", Name: "RPM-OSTree to DPKG Adaptation", Description: "Adapt RPM-OSTree module functionality for Debian DPKG", Type: "adaptation", Changes: []string{ "Replace rpm-ostree with dpkg", "Replace RPM packages with DEB packages", "Adapt repository management", "Replace COPR with Debian backports", }, Status: "completed", Enabled: true, Metadata: map[string]interface{}{ "original_module": "rpm-ostree", "target_module": "dpkg", "compatibility": "high", }, } // OS Release adaptation dmm.adaptations["os-release-to-debian"] = ModuleAdaptation{ ID: "os-release-to-debian", OriginalID: "os-release", Name: "OS Release to Debian Adaptation", Description: "Adapt OS release module for Debian", Type: "adaptation", Changes: []string{ "Replace Fedora-specific paths with Debian paths", "Adapt release file format", "Update version detection logic", }, Status: "completed", Enabled: true, Metadata: map[string]interface{}{ "original_module": "os-release", "target_module": "debian-release", "compatibility": "high", }, } // SystemD adaptation (mostly compatible) dmm.adaptations["systemd-debian"] = ModuleAdaptation{ ID: "systemd-debian", OriginalID: "systemd", Name: "SystemD Debian Adaptation", Description: "Adapt SystemD module for Debian", Type: "adaptation", Changes: []string{ "Verify Debian systemd compatibility", "Adapt unit file paths if needed", "Ensure Debian-specific configurations", }, Status: "completed", Enabled: true, Metadata: map[string]interface{}{ "original_module": "systemd", "target_module": "systemd", "compatibility": "high", }, } } // GetModule returns a module by ID func (dmm *DebianModulesManager) GetModule(moduleID string) (*DebianModule, error) { dmm.mu.RLock() defer dmm.mu.RUnlock() module, exists := dmm.modules[moduleID] if !exists { return nil, fmt.Errorf("module not found: %s", moduleID) } return &module, nil } // GetAdaptation returns an adaptation by ID func (dmm *DebianModulesManager) GetAdaptation(adaptationID string) (*ModuleAdaptation, error) { dmm.mu.RLock() defer dmm.mu.RUnlock() adaptation, exists := dmm.adaptations[adaptationID] if !exists { return nil, fmt.Errorf("adaptation not found: %s", adaptationID) } return &adaptation, nil } // ListModules returns all available modules func (dmm *DebianModulesManager) ListModules() []DebianModule { dmm.mu.RLock() defer dmm.mu.RUnlock() modules := make([]DebianModule, 0, len(dmm.modules)) for _, module := range dmm.modules { if module.Enabled { modules = append(modules, module) } } return modules } // ListAdaptations returns all available adaptations func (dmm *DebianModulesManager) ListAdaptations() []ModuleAdaptation { dmm.mu.RLock() defer dmm.mu.RUnlock() adaptations := make([]ModuleAdaptation, 0, len(dmm.adaptations)) for _, adaptation := range dmm.adaptations { if adaptation.Enabled { adaptations = append(adaptations, adaptation) } } return adaptations } // ValidateModule validates a module configuration func (dmm *DebianModulesManager) ValidateModule(moduleID string, config map[string]interface{}) error { module, err := dmm.GetModule(moduleID) if err != nil { return err } // Module-specific validation switch module.Type { case "apt": return dmm.validateAPTModule(config) case "dpkg": return dmm.validateDPKGModule(config) case "debian-release": return dmm.validateDebianReleaseModule(config) case "debian-kernel": return dmm.validateDebianKernelModule(config) case "debian-initramfs": return dmm.validateDebianInitramfsModule(config) default: return fmt.Errorf("unknown module type: %s", module.Type) } } // validateAPTModule validates APT module configuration func (dmm *DebianModulesManager) validateAPTModule(config map[string]interface{}) error { // Validate required fields if _, ok := config["repos"]; !ok { return fmt.Errorf("APT module requires 'repos' configuration") } if _, ok := config["install"]; !ok { return fmt.Errorf("APT module requires 'install' configuration") } return nil } // validateDPKGModule validates DPKG module configuration func (dmm *DebianModulesManager) validateDPKGModule(config map[string]interface{}) error { // Validate required fields if _, ok := config["packages"]; !ok { return fmt.Errorf("DPKG module requires 'packages' configuration") } return nil } // validateDebianReleaseModule validates Debian release module configuration func (dmm *DebianModulesManager) validateDebianReleaseModule(config map[string]interface{}) error { // Validate required fields if _, ok := config["release"]; !ok { return fmt.Errorf("Debian release module requires 'release' configuration") } return nil } // validateDebianKernelModule validates Debian kernel module configuration func (dmm *DebianModulesManager) validateDebianKernelModule(config map[string]interface{}) error { // Validate required fields if _, ok := config["kernel"]; !ok { return fmt.Errorf("Debian kernel module requires 'kernel' configuration") } return nil } // validateDebianInitramfsModule validates Debian initramfs module configuration func (dmm *DebianModulesManager) validateDebianInitramfsModule(config map[string]interface{}) error { // Validate required fields if _, ok := config["initramfs"]; !ok { return fmt.Errorf("Debian initramfs module requires 'initramfs' configuration") } return nil } // CreateModuleTemplate creates a template for a module func (dmm *DebianModulesManager) CreateModuleTemplate(moduleID string) (map[string]interface{}, error) { module, err := dmm.GetModule(moduleID) if err != nil { return nil, err } // Create module-specific templates switch module.Type { case "apt": return dmm.createAPTTemplate() case "dpkg": return dmm.createDPKGTemplate() case "debian-release": return dmm.createDebianReleaseTemplate() case "debian-kernel": return dmm.createDebianKernelTemplate() case "debian-initramfs": return dmm.createDebianInitramfsTemplate() default: return nil, fmt.Errorf("unknown module type: %s", module.Type) } } // createAPTTemplate creates an APT module template func (dmm *DebianModulesManager) createAPTTemplate() map[string]interface{} { return map[string]interface{}{ "type": "apt", "repos": map[string]interface{}{ "cleanup": true, "files": []string{ "https://example.com/debian-repo.list", }, "keys": []string{ "https://example.com/debian-repo.gpg", }, }, "install": map[string]interface{}{ "packages": []string{ "package1", "package2", }, }, "remove": map[string]interface{}{ "packages": []string{ "unwanted-package", }, }, } } // createDPKGTemplate creates a DPKG module template func (dmm *DebianModulesManager) createDPKGTemplate() map[string]interface{} { return map[string]interface{}{ "type": "dpkg", "packages": []string{ "https://example.com/package.deb", "local-package.deb", }, "remove": []string{ "unwanted-package", }, } } // createDebianReleaseTemplate creates a Debian release module template func (dmm *DebianModulesManager) createDebianReleaseTemplate() map[string]interface{} { return map[string]interface{}{ "type": "debian-release", "release": "bookworm", "codename": "Debian Bookworm", "version": "12", } } // createDebianKernelTemplate creates a Debian kernel module template func (dmm *DebianModulesManager) createDebianKernelTemplate() map[string]interface{} { return map[string]interface{}{ "type": "debian-kernel", "kernel": "linux-image-amd64", "modules": []string{ "overlay", "bridge", }, "parameters": map[string]string{ "console": "ttyS0", "root": "/dev/sda1", }, } } // createDebianInitramfsTemplate creates a Debian initramfs module template func (dmm *DebianModulesManager) createDebianInitramfsTemplate() map[string]interface{} { return map[string]interface{}{ "type": "debian-initramfs", "initramfs": "initramfs-tools", "config": map[string]interface{}{ "modules": []string{ "overlay", "bridge", }, "hooks": []string{ "resume", "rootfs", }, }, } }