Skip to content

Commit 36cd55b

Browse files
[POA-2849] Add systemd restart for already running service in ec2 (#89)
This pull request includes changes to the `cmd/internal/ec2/add.go` file to enhance the handling of the `postman-insights-agent` service. Enhancements to service handling: * [`cmd/internal/ec2/add.go`](diffhunk://#diff-de4c0d4aa9b0bc2881e1a3420db0ab595602fc4d945fbad5383e1586cd62a8d5R36-R38): Added a new constant `active` to represent the active state of the `postman-insights-agent` service. * [`cmd/internal/ec2/add.go`](diffhunk://#diff-de4c0d4aa9b0bc2881e1a3420db0ab595602fc4d945fbad5383e1586cd62a8d5R275-R294): Modified the `enablePostmanInsightsAgent` function to check if the `postman-insights-agent` service is active, and if so, restart it with the new configurations. This includes handling errors and providing informative messages to the user.
1 parent 81b8978 commit 36cd55b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

cmd/internal/ec2/add.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const (
3333
enabled = "enabled" // exit code: 0
3434
disabled = "disabled" // exit code: 1
3535
nonExisting = "Failed to get unit file state for postman-insights-agent.service: No such file or directory" // exit code: 1
36+
37+
// Output of command: systemctl is-active postman-insights-agent
38+
active = "active" // exit code: 0
3639
)
3740

3841
var (
@@ -269,6 +272,26 @@ func enablePostmanInsightsAgent() error {
269272
if err != nil {
270273
return errors.Wrapf(err, "failed to run systemctl daemon-reload")
271274
}
275+
276+
// systemctl check if postman-insights-agent.service is active
277+
cmd = exec.Command("systemctl", []string{"is-active", serviceFileName}...)
278+
output, err := cmd.CombinedOutput()
279+
if err != nil {
280+
printer.Infof("Postman Insights Agent is not running as a systemd service, status: %v\n", err)
281+
} else if strings.Contains(string(output), active) {
282+
printer.Infof("Postman Insights Agent is already running as a systemd service\n")
283+
printer.Infof("Restarting the service with new configurations\n")
284+
285+
// systemctl restart postman-insights-agent.service
286+
cmd = exec.Command("systemctl", []string{"restart", serviceFileName}...)
287+
_, err = cmd.CombinedOutput()
288+
if err != nil {
289+
printer.Errorf("Failed to restart the service. Err: %v", err)
290+
printer.Errorf("Please run the below command to restart the service\n")
291+
printer.Errorf("systemctl restart postman-insights-agent\n")
292+
}
293+
}
294+
272295
// systemctl start postman-insights-agent.service
273296
cmd = exec.Command("systemctl", []string{"enable", "--now", serviceFileName}...)
274297
_, err = cmd.CombinedOutput()

0 commit comments

Comments
 (0)