From 5c7b96c5457a48b428b657c942a0c0eb4bae9f75 Mon Sep 17 00:00:00 2001 From: glattercj Date: Mon, 20 Jan 2025 15:07:40 -0700 Subject: [PATCH] feat: support deleting files in disk injects via 'deletions' --- src/go/tmpl/templates/minimega_script.tmpl | 3 + src/go/types/interfaces/topology.go | 8 +++ src/go/types/version/v0/node.go | 41 +++++++++++++ src/go/types/version/v1/node.go | 68 ++++++++++++++++++++++ 4 files changed, 120 insertions(+) diff --git a/src/go/tmpl/templates/minimega_script.tmpl b/src/go/tmpl/templates/minimega_script.tmpl index fcc3b952..099fbf7e 100644 --- a/src/go/tmpl/templates/minimega_script.tmpl +++ b/src/go/tmpl/templates/minimega_script.tmpl @@ -36,6 +36,9 @@ disk snapshot {{ $firstDrive.Image }} {{ $.SnapshotName .General.Hostname }} {{- if gt (len .Injections) 0 }} disk inject {{ $.SnapshotName .General.Hostname }}:{{ $firstDrive.GetInjectPartition }} files {{ .FileInjects $basedir }} {{- end }} + {{- if gt (len .Deletions) 0 }} +disk inject {{ $.SnapshotName .General.Hostname }}:{{ $firstDrive.GetInjectPartition }} delete files {{ .FileDeletions }} + {{- end }} {{- end }} clear vm config {{- if ne (index $.Schedules .General.Hostname) "" }} diff --git a/src/go/types/interfaces/topology.go b/src/go/types/interfaces/topology.go index e801506b..3b98f4ae 100644 --- a/src/go/types/interfaces/topology.go +++ b/src/go/types/interfaces/topology.go @@ -28,6 +28,7 @@ type NodeSpec interface { Hardware() NodeHardware Network() NodeNetwork Injections() []NodeInjection + Deletions() []NodeDeletion Delay() NodeDelay Advanced() map[string]string Overrides() map[string]string @@ -35,12 +36,14 @@ type NodeSpec interface { External() bool SetInjections([]NodeInjection) + SetDeletions([]NodeDeletion) AddLabel(string, string) AddHardware(string, int, int) NodeHardware AddNetworkInterface(string, string, string) NodeNetworkInterface AddNetworkRoute(string, string, int) AddInject(string, string, string, string) + AddDeletion(string, string) SetAdvanced(map[string]string) AddAdvanced(string, string) @@ -206,6 +209,11 @@ type NodeInjection interface { Permissions() string } +type NodeDeletion interface { + Path() string + Description() string +} + type NodeDelay interface { Timer() time.Duration User() bool diff --git a/src/go/types/version/v0/node.go b/src/go/types/version/v0/node.go index 91402b8e..fdcf4935 100644 --- a/src/go/types/version/v0/node.go +++ b/src/go/types/version/v0/node.go @@ -18,6 +18,7 @@ type Node struct { HardwareF *Hardware `json:"hardware" yaml:"hardware" structs:"hardware" mapstructure:"hardware"` NetworkF *Network `json:"network" yaml:"network" structs:"network" mapstructure:"network"` InjectionsF []*Injection `json:"injections" yaml:"injections" structs:"injections" mapstructure:"injections"` + DeletionsF []*Deletion `json:"deletions" yaml:"deletions" structs:"deletions" mapstructure:"deletions"` } func (this Node) Annotations() map[string]interface{} { @@ -54,6 +55,16 @@ func (this Node) Injections() []ifaces.NodeInjection { return injects } +func (this Node) Deletions() []ifaces.NodeDeletion { + deletions := make([]ifaces.NodeDeletion, len(this.DeletionsF)) + + for i, j := range this.DeletionsF { + deletions[i] = j + } + + return deletions +} + func (this Node) Delay() ifaces.NodeDelay { return new(Delay) } @@ -84,6 +95,16 @@ func (this *Node) SetInjections(injections []ifaces.NodeInjection) { this.InjectionsF = injects } +func (this *Node) SetDeletions(deletions []ifaces.NodeDeletion) { + deletionList := make([]*Deletion, len(deletions)) + + for i, j := range deletions { + deletionList[i] = j.(*Deletion) + } + + this.DeletionsF = deletionList +} + func (this *Node) AddLabel(k, v string) { this.LabelsF[k] = v } @@ -139,6 +160,13 @@ func (this *Node) AddInject(src, dst, perms, desc string) { }) } +func (this *Node) AddDeletion(path, desc string) { + this.DeletionsF = append(this.DeletionsF, &Deletion{ + PathF: path, + DescriptionF: desc, + }) +} + func (Node) SetAdvanced(map[string]string) {} func (Node) AddAdvanced(string, string) {} func (Node) AddOverride(string, string) {} @@ -309,6 +337,19 @@ func (this Injection) Permissions() string { return this.PermissionsF } +type Deletion struct { + PathF string `json:"path" yaml:"path" structs:"path" mapstructure:"path"` + DescriptionF string `json:"description" yaml:"description" structs:"description" mapstructure:"description"` +} + +func (this Deletion) Path() string { + return this.PathF +} + +func (this Deletion) Description() string { + return this.DescriptionF +} + type Delay struct{} func (this Delay) Timer() time.Duration { diff --git a/src/go/types/version/v1/node.go b/src/go/types/version/v1/node.go index 6166adf4..a4076a08 100644 --- a/src/go/types/version/v1/node.go +++ b/src/go/types/version/v1/node.go @@ -18,6 +18,7 @@ type Node struct { HardwareF *Hardware `json:"hardware" yaml:"hardware" structs:"hardware" mapstructure:"hardware"` NetworkF *Network `json:"network" yaml:"network" structs:"network" mapstructure:"network"` InjectionsF []*Injection `json:"injections" yaml:"injections" structs:"injections" mapstructure:"injections"` + DeletionsF []*Deletion `json:"deletions" yaml:"deletions" structs:"deletions" mapstructure:"deletions"` AdvancedF map[string]string `json:"advanced" yaml:"advanced" structs:"advanced" mapstructure:"advanced"` OverridesF map[string]string `json:"overrides" yaml:"overrides" structs:"overrides" mapstructure:"overrides"` DelayF *Delay `json:"delay" yaml:"delay" structs:"delay" mapstructure:"delay"` @@ -59,6 +60,16 @@ func (this Node) Injections() []ifaces.NodeInjection { return injects } +func (this Node) Deletions() []ifaces.NodeDeletion { + deletions := make([]ifaces.NodeDeletion, len(this.DeletionsF)) + + for i, j := range this.DeletionsF { + deletions[i] = j + } + + return deletions +} + func (this Node) Delay() ifaces.NodeDelay { if this.DelayF == nil { return new(Delay) @@ -102,6 +113,16 @@ func (this *Node) SetInjections(injections []ifaces.NodeInjection) { this.InjectionsF = injects } +func (this *Node) SetDeletions(deletions []ifaces.NodeDeletion) { + deletionList := make([]*Deletion, len(deletions)) + + for i, j := range deletions { + deletionList[i] = j.(*Deletion) + } + + this.DeletionsF = deletionList +} + func (this *Node) AddLabel(k, v string) { if this.LabelsF == nil { this.LabelsF = make(map[string]string) @@ -180,6 +201,30 @@ func (this *Node) AddInject(src, dst, perms, desc string) { } } +func (this *Node) AddDeletion(path, desc string) { + if _, ok := this.LabelsF["disable-injects"]; ok { + return + } + + var exists bool + + for _, deletion := range this.DeletionsF { + if deletion.PathF == path { + deletion.DescriptionF = desc + + exists = true + break + } + } + + if !exists { + this.DeletionsF = append(this.DeletionsF, &Deletion{ + PathF: path, + DescriptionF: desc, + }) + } +} + func (this *Node) SetAdvanced(adv map[string]string) { this.AdvancedF = adv } @@ -449,6 +494,19 @@ func (this Injection) Permissions() string { return this.PermissionsF } +type Deletion struct { + PathF string `json:"path" yaml:"path" structs:"path" mapstructure:"path"` + DescriptionF string `json:"description" yaml:"description" structs:"description" mapstructure:"description"` +} + +func (this Deletion) Path() string { + return this.PathF +} + +func (this Deletion) Description() string { + return this.DescriptionF +} + func (this Node) validate() error { if this.ExternalF == nil { return nil @@ -575,6 +633,16 @@ func (this Node) FileInjects(baseDir string) string { return strings.Join(injects, " ") } +func (this Node) FileDeletions() string { + deletions := make([]string, len(this.DeletionsF)) + + for i, deletion := range this.DeletionsF { + deletions[i] = fmt.Sprintf(`"%s"`, deletion.PathF) + } + + return strings.Join(deletions, ",") +} + func (this Node) RouterName() string { if !strings.EqualFold(this.TypeF, "router") { return this.GeneralF.HostnameF