Skip to content

Commit

Permalink
feat: support deleting files in disk injects via 'deletions'
Browse files Browse the repository at this point in the history
  • Loading branch information
glattercj committed Jan 20, 2025
1 parent bfe9380 commit 5c7b96c
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/go/tmpl/templates/minimega_script.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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) "" }}
Expand Down
8 changes: 8 additions & 0 deletions src/go/types/interfaces/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@ type NodeSpec interface {
Hardware() NodeHardware
Network() NodeNetwork
Injections() []NodeInjection
Deletions() []NodeDeletion
Delay() NodeDelay
Advanced() map[string]string
Overrides() map[string]string
Commands() []string
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)
Expand Down Expand Up @@ -206,6 +209,11 @@ type NodeInjection interface {
Permissions() string
}

type NodeDeletion interface {
Path() string
Description() string
}

type NodeDelay interface {
Timer() time.Duration
User() bool
Expand Down
41 changes: 41 additions & 0 deletions src/go/types/version/v0/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{} {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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) {}
Expand Down Expand Up @@ -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 {
Expand Down
68 changes: 68 additions & 0 deletions src/go/types/version/v1/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5c7b96c

Please sign in to comment.