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

[OBS-407]Linux agent: Handle reconfiguration #244

Merged
merged 7 commits into from
Oct 25, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Please send this log message to [email protected] for assistance\n")
}
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 doesnt' 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
}