@@ -129,10 +129,28 @@ func (sd *Systemd) Process(ctx, conn context.Context, skew int) {
129129}
130130
131131func (sd * Systemd ) MethodEngine (ctx context.Context , conn context.Context , change * object.Change , path string ) error {
132+ var changeType string = "unknown"
133+ var curr * string = nil
132134 var prev * string = nil
133135 if change != nil {
136+ if change .From .Name != "" {
137+ prev = & change .From .Name
138+ }
134139 if change .To .Name != "" {
135- prev = & change .To .Name
140+ curr = & change .To .Name
141+ }
142+ if change .From .Name == "" && change .To .Name != "" {
143+ changeType = "create"
144+ }
145+ if change .From .Name != "" && change .To .Name != "" {
146+ if change .From .Name == change .To .Name {
147+ changeType = "update"
148+ } else {
149+ changeType = "rename"
150+ }
151+ }
152+ if change .From .Name != "" && change .To .Name == "" {
153+ changeType = "delete"
136154 }
137155 }
138156 nonRootHomeDir := os .Getenv ("HOME" )
@@ -148,7 +166,7 @@ func (sd *Systemd) MethodEngine(ctx context.Context, conn context.Context, chang
148166 if change != nil {
149167 sd .initialRun = true
150168 }
151- return sd .systemdPodman (ctx , conn , path , dest , prev )
169+ return sd .systemdPodman (ctx , conn , path , dest , prev , curr , & changeType )
152170}
153171
154172func (sd * Systemd ) Apply (ctx , conn context.Context , currentState , desiredState plumbing.Hash , tags * []string ) error {
@@ -162,7 +180,7 @@ func (sd *Systemd) Apply(ctx, conn context.Context, currentState, desiredState p
162180 return nil
163181}
164182
165- func (sd * Systemd ) systemdPodman (ctx context.Context , conn context.Context , path , dest string , prev * string ) error {
183+ func (sd * Systemd ) systemdPodman (ctx context.Context , conn context.Context , path , dest string , prev * string , curr * string , changeType * string ) error {
166184 logger .Infof ("Deploying systemd file(s) %s" , path )
167185 if sd .autoUpdateAll {
168186 if ! sd .initialRun {
@@ -187,14 +205,26 @@ func (sd *Systemd) systemdPodman(ctx context.Context, conn context.Context, path
187205 logger .Infof ("Systemd target %s successfully processed" , sd .Name )
188206 return nil
189207 }
190- if (sd .Enable && ! sd .Restart ) || sd .initialRun {
191- if sd .Enable {
192- return sd .enableRestartSystemdService (conn , "enable" , dest , filepath .Base (path ))
208+ if * changeType == "create" {
209+ return sd .enableRestartSystemdService (conn , "enable" , dest , filepath .Base (* curr ))
210+ }
211+ if * changeType == "update" {
212+ if sd .Restart {
213+ return sd .enableRestartSystemdService (conn , "restart" , dest , filepath .Base (* curr ))
214+ } else {
215+ return sd .enableRestartSystemdService (conn , "enable" , dest , filepath .Base (* curr ))
193216 }
194217 }
195- if sd .Restart {
196- return sd .enableRestartSystemdService (conn , "restart" , dest , filepath .Base (path ))
218+ if * changeType == "rename" {
219+ if err := sd .enableRestartSystemdService (conn , "stop" , dest , filepath .Base (* prev )); err != nil {
220+ return err
221+ }
222+ return sd .enableRestartSystemdService (conn , "enable" , dest , filepath .Base (* curr ))
223+ }
224+ if * changeType == "delete" {
225+ return sd .enableRestartSystemdService (conn , "stop" , dest , filepath .Base (* prev ))
197226 }
227+ logger .Infof ("Systemd target %s %s not processed" , sd .Name , * changeType )
198228 return nil
199229}
200230
0 commit comments