Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 3db5466

Browse files
gmann42mgritter
andauthored
[OBS-407]Linux agent: Handle reconfiguration (#244)
### What - Handle the case when linux setup command is run again on a system on which it was previously used. - There can be three possibilities - systemd service exists and is enabled: Ask user if old values should be over-written with current input - systemd service exists and is disabled: Ask user if old values should be over-written with current input and enable the service - systemd service doesn't exist: Proceed with normal flow Ticket: https://postmanlabs.atlassian.net/browse/OBS-407 --------- Co-authored-by: Mark Gritter <mgritter@akitasoftware.com>
1 parent 9609391 commit 3db5466

File tree

1 file changed

+64
-9
lines changed

1 file changed

+64
-9
lines changed

cmd/internal/ec2/add.go

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"text/template"
1010

11+
"github.com/AlecAivazis/survey/v2"
1112
"github.com/akitasoftware/akita-cli/printer"
1213
"github.com/akitasoftware/akita-cli/telemetry"
1314
"github.com/pkg/errors"
@@ -22,6 +23,12 @@ const (
2223
serviceFileName = "postman-lc-agent.service"
2324
serviceFileBasePath = "/usr/lib/systemd/system/"
2425
serviceFilePath = serviceFileBasePath + serviceFileName
26+
27+
// Output of command: systemctl is-enabled postman-lc-agent
28+
// Refer: https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#Exit%20status
29+
enabled = "enabled" // exit code: 0
30+
disabled = "disabled" // exit code: 1
31+
nonExisting = "Failed to get unit file state for postman-lc-agent.service: No such file or directory" // exit code: 1
2532
)
2633

2734
// Embed files inside the binary. Requires Go >=1.16
@@ -63,8 +70,59 @@ func setupAgentForServer(collectionId string) error {
6370
return nil
6471
}
6572

73+
func askToReconfigure() error {
74+
var isReconfigure bool
75+
76+
printer.Infof("postman-lc-agent is already present as a systemd service\n")
77+
printer.Infof("Helpful commands \n Check status: systemctl status postman-lc-agent \n Disable agent: systemctl disable --now postman-lc-agent \n Check Logs: journalctl -fu postman-lc-agent\n Check env file: cat %s \n Check systemd service file: cat %s \n", envFilePath, serviceFilePath)
78+
79+
err := survey.AskOne(
80+
&survey.Confirm{
81+
Message: "Overwrite old API key and Collection ID values in systemd configuration file with current values?",
82+
Default: true,
83+
Help: "Any edits made to systemd configuration files will be over-written.",
84+
},
85+
&isReconfigure,
86+
)
87+
if !isReconfigure {
88+
printer.Infof("Exiting setup \n")
89+
os.Exit(0)
90+
return nil
91+
}
92+
if err != nil {
93+
return errors.Wrap(err, "failed to run reconfiguration prompt")
94+
}
95+
return nil
96+
}
97+
98+
// Check is systemd service already exists
99+
func checkReconfiguration() error {
100+
101+
cmd := exec.Command("systemctl", []string{"is-enabled", "postman-lc-agent"}...)
102+
out, err := cmd.CombinedOutput()
103+
104+
if err != nil {
105+
if exitError, ok := err.(*exec.ExitError); ok {
106+
exitCode := exitError.ExitCode()
107+
if exitCode != 1 {
108+
return errors.Wrapf(err, "Received non 1 exitcode for systemctl is-enabled. \n Command output:%s \n Please send this log message to observability-support@postman.com for assistance\n", out)
109+
}
110+
if strings.Contains(string(out), disabled) {
111+
return askToReconfigure()
112+
} else if strings.Contains(string(out), nonExisting) {
113+
return nil
114+
}
115+
}
116+
return errors.Wrapf(err, "failed to run systemctl is-enabled posman-lc-agent")
117+
}
118+
if strings.Contains(string(out), enabled) {
119+
return askToReconfigure()
120+
}
121+
return errors.Errorf("The systemctl is-enabled command produced output this tool doesn't recognize: %q. \n Please send this log message to observability-support@postman.com for assistance\n", string(out))
122+
123+
}
124+
66125
func checkUserPermissions() error {
67-
// TODO: Make this work without root
68126

69127
// Exact permissions required are
70128
// read/write permissions on /etc/default/postman-lc-agent
@@ -101,6 +159,11 @@ func configureSystemdFiles(collectionId string) error {
101159
printer.Infof(message + "\n")
102160
reportStep(message)
103161

162+
err := checkReconfiguration()
163+
if err != nil {
164+
return err
165+
}
166+
104167
// Write collectionId and postman-api-key to go template file
105168

106169
tmpl, err := template.ParseFS(envFileFS, envFileTemplateName)
@@ -173,11 +236,3 @@ func enablePostmanAgent() error {
173236

174237
return nil
175238
}
176-
177-
// Run post-checks
178-
func postChecks() error {
179-
reportStep("EC2:Running post checks")
180-
181-
// TODO: How to Verify if traffic is being captured ?
182-
return nil
183-
}

0 commit comments

Comments
 (0)