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

Commit

Permalink
[OBS-407]Linux agent: Handle reconfiguration (#244)
Browse files Browse the repository at this point in the history
### 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 <[email protected]>
  • Loading branch information
gmann42 and mgritter authored Oct 25, 2023
1 parent 9609391 commit 3db5466
Showing 1 changed file with 64 additions and 9 deletions.
73 changes: 64 additions & 9 deletions cmd/internal/ec2/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"text/template"

"github.com/AlecAivazis/survey/v2"
"github.com/akitasoftware/akita-cli/printer"
"github.com/akitasoftware/akita-cli/telemetry"
"github.com/pkg/errors"
Expand All @@ -22,6 +23,12 @@ const (
serviceFileName = "postman-lc-agent.service"
serviceFileBasePath = "/usr/lib/systemd/system/"
serviceFilePath = serviceFileBasePath + serviceFileName

// Output of command: systemctl is-enabled postman-lc-agent
// Refer: https://www.freedesktop.org/software/systemd/man/latest/systemctl.html#Exit%20status
enabled = "enabled" // exit code: 0
disabled = "disabled" // exit code: 1
nonExisting = "Failed to get unit file state for postman-lc-agent.service: No such file or directory" // exit code: 1
)

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

func askToReconfigure() error {
var isReconfigure bool

printer.Infof("postman-lc-agent is already present as a systemd service\n")
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)

err := survey.AskOne(
&survey.Confirm{
Message: "Overwrite old API key and Collection ID values in systemd configuration file with current values?",
Default: true,
Help: "Any edits made to systemd configuration files will be over-written.",
},
&isReconfigure,
)
if !isReconfigure {
printer.Infof("Exiting setup \n")
os.Exit(0)
return nil
}
if err != nil {
return errors.Wrap(err, "failed to run reconfiguration prompt")
}
return nil
}

// Check is systemd service already exists
func checkReconfiguration() error {

cmd := exec.Command("systemctl", []string{"is-enabled", "postman-lc-agent"}...)
out, err := cmd.CombinedOutput()

if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
exitCode := exitError.ExitCode()
if exitCode != 1 {
return errors.Wrapf(err, "Received non 1 exitcode for systemctl is-enabled. \n Command output:%s \n Please send this log message to [email protected] for assistance\n", out)
}
if strings.Contains(string(out), disabled) {
return askToReconfigure()
} else if strings.Contains(string(out), nonExisting) {
return nil
}
}
return errors.Wrapf(err, "failed to run systemctl is-enabled posman-lc-agent")
}
if strings.Contains(string(out), enabled) {
return askToReconfigure()
}
return errors.Errorf("The systemctl is-enabled command produced output this tool doesn't recognize: %q. \n Please send this log message to [email protected] for assistance\n", string(out))

}

func checkUserPermissions() error {
// TODO: Make this work without root

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

err := checkReconfiguration()
if err != nil {
return err
}

// Write collectionId and postman-api-key to go template file

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

return nil
}

// Run post-checks
func postChecks() error {
reportStep("EC2:Running post checks")

// TODO: How to Verify if traffic is being captured ?
return nil
}

0 comments on commit 3db5466

Please sign in to comment.