Skip to content

Commit ac0493c

Browse files
authored
fix systemd restart logic (#329)
* fix systemd restart logic Signed-off-by: Brandon Peterson <[email protected]> * add systemd rename handling Signed-off-by: Brandon Peterson <[email protected]> --------- Signed-off-by: Brandon Peterson <[email protected]>
1 parent 430c44a commit ac0493c

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ To develop and test changes of FetchIt, the FetchIt image can be built locally a
1111

1212
```
1313
go mod tidy
14+
go mod vendor
1415
podman build . --file Dockerfile --tag quay.io/fetchit/fetchit-amd:latest
1516
podman tag quay.io/fetchit/fetchit-amd:latest quay.io/fetchit/fetchit:latest
1617
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ require (
242242
golang.org/x/sys v0.5.0 // indirect
243243
golang.org/x/term v0.5.0 // indirect
244244
golang.org/x/text v0.7.0 // indirect
245-
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
245+
golang.org/x/time v0.1.0 // indirect
246246
golang.org/x/tools v0.3.0 // indirect
247247
google.golang.org/appengine v1.6.7 // indirect
248248
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3526,7 +3526,6 @@ golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba/go.mod h1:tRJNPiyCQ0inRvYxb
35263526
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
35273527
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
35283528
golang.org/x/time v0.0.0-20220411224347-583f2d630306/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
3529-
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 h1:ftMN5LMiBFjbzleLqtoBZk7KdJwhuybIU+FckUHgoyQ=
35303529
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
35313530
golang.org/x/time v0.0.0-20220922220347-f3bd1da661af/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
35323531
golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA=

pkg/engine/systemd.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,28 @@ func (sd *Systemd) Process(ctx, conn context.Context, skew int) {
129129
}
130130

131131
func (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

154172
func (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

Comments
 (0)