From 63230558a02b4d3d2a00b11bf54d28197330631e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=A9re=C5=A1?= Date: Mon, 23 Oct 2023 00:20:34 -0700 Subject: [PATCH 1/3] swctl: post install steps for docker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Béreš --- cmd/swctl/app/cmd_dependency.go | 47 +++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/cmd/swctl/app/cmd_dependency.go b/cmd/swctl/app/cmd_dependency.go index 42ecbad..7079cdb 100644 --- a/cmd/swctl/app/cmd_dependency.go +++ b/cmd/swctl/app/cmd_dependency.go @@ -491,6 +491,14 @@ func InstallDocker(cli Cli, dockerVersion string) error { color.Fprintln(cli.Out(), out) } + errorGroup := postInstall(cli) + if len(errorGroup) != 0 { + for _, err := range errorGroup { + color.Fprintln(cli.Out(), err) + } + } else { + color.Fprintln(cli.Out(), "Please log out and then log back in for the group membership changes to take effect or enter command \"newgrp docker\"") + } return nil } @@ -747,3 +755,42 @@ func isUserRoot() (bool, error) { return true, nil } +func postInstall(cli Cli) []error { + var errorGroup []error + sudoName, err := logname(cli) + fmt.Fprintln(cli.Out(), sudoName) + // handling case when linux has no login name (e.g. container) + if err == nil { + commands := []string{ + "usermod -aG docker " + sudoName, // add user do docker group + } + + for _, command := range commands { + + out, stderr, err := cli.Exec(GetSudoPrefix(cli)+"bash -c", []string{command}, false) + + if stderr != "" { + errorGroup = append(errorGroup, errors.New(command+": "+stderr)) + } + if err != nil { + errorGroup = append(errorGroup, errors.New(err.Error()+"("+command+")")) + } + color.Fprintln(cli.Out(), out) + } + } else { + color.Fprintln(cli.Out(), err) + } + + return errorGroup +} + +func logname(cli Cli) (string, error) { + stdout, stderr, err := cli.Exec("logname", nil, false) + if stderr != "" { + return "", errors.New(stderr) + } + if err != nil { + return "", err + } + return stdout, err +} From 0189695e618f7be7d9f8f37ca83ab23ad4648b3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=A9re=C5=A1?= Date: Tue, 24 Oct 2023 06:09:11 -0700 Subject: [PATCH 2/3] swctl : docker post install - fixed comments - fixed agentctl permission access MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Béreš --- cmd/swctl/app/cmd_dependency.go | 67 ++++++++++++++++----------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/cmd/swctl/app/cmd_dependency.go b/cmd/swctl/app/cmd_dependency.go index 7079cdb..695c1c0 100644 --- a/cmd/swctl/app/cmd_dependency.go +++ b/cmd/swctl/app/cmd_dependency.go @@ -14,6 +14,7 @@ import ( "github.com/gookit/color" "github.com/olekukonko/tablewriter" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -144,7 +145,7 @@ func installExternalToolsCmd(cli Cli) *cobra.Command { } color.Fprintln(cli.Out(), "Installation of the agentctl tool was successful") } - + color.Fprintln(cli.Out(), color.Red.Sprintf("Please log out and then log back in for the group membership changes to take effect or enter command \"newgrp docker\"")) return nil }, } @@ -491,13 +492,9 @@ func InstallDocker(cli Cli, dockerVersion string) error { color.Fprintln(cli.Out(), out) } - errorGroup := postInstall(cli) - if len(errorGroup) != 0 { - for _, err := range errorGroup { - color.Fprintln(cli.Out(), err) - } - } else { - color.Fprintln(cli.Out(), "Please log out and then log back in for the group membership changes to take effect or enter command \"newgrp docker\"") + err := dockerPostInstall(cli) + if err != nil { + return errors.New("dockerPostInstall:" + err.Error()) } return nil @@ -611,7 +608,7 @@ func InstallAgentCtl(cli Cli, agentctlCommitVersion string) error { // run agentctl build in docker color.Fprintln(cli.Out(), "building agentctl in docker container...") - _, _, err = cli.Exec("docker build", []string{ + _, _, err = cli.Exec(GetSudoPrefix(cli)+"docker build", []string{ "-f", dockerFile, "--build-arg", fmt.Sprintf("COMMIT=%s", agentctlCommitVersion), "-t", builderImage, @@ -629,19 +626,19 @@ func InstallAgentCtl(cli Cli, agentctlCommitVersion string) error { }() // extract agentctl into external tools binary folder - stdout, _, err := cli.Exec("docker create", []string{builderImage}, false) + stdout, _, err := cli.Exec(GetSudoPrefix(cli)+"docker create", []string{builderImage}, false) if err != nil { return fmt.Errorf("can't extract agentctl from builder docker image due "+ "to container creation failure: %w", err) } containerId := fmt.Sprint(stdout) defer func() { // cleanup of docker container - _, _, err = cli.Exec("docker rm", []string{containerId}, false) + _, _, err = cli.Exec(GetSudoPrefix(cli)+"docker rm", []string{containerId}, false) if err != nil { color.Fprintf(cli.Out(), "clean up of agentctl builder container failed (%v), continuing... ", err) } }() - _, _, err = cli.Exec("docker cp", []string{ + _, _, err = cli.Exec(GetSudoPrefix(cli)+"docker cp", []string{ fmt.Sprintf("%s:/go/bin/agentctl", containerId), cmdAgentCtl.installPath(), }, false) if err != nil { @@ -649,9 +646,12 @@ func InstallAgentCtl(cli Cli, agentctlCommitVersion string) error { "to docker cp failure: %w", err) } - err = os.Chmod(cmdAgentCtl.installPath(), 0755) + _, stderr, err := cli.Exec(GetSudoPrefix(cli)+"chmod", []string{"755", cmdAgentCtl.installPath()}, false) + if stderr != "" { + return fmt.Errorf("chmod error: %s", stderr) + } if err != nil { - return fmt.Errorf("can't set for agentctl proper file permissions: %w", err) + return err } // store the release version info @@ -755,33 +755,30 @@ func isUserRoot() (bool, error) { return true, nil } -func postInstall(cli Cli) []error { - var errorGroup []error +func dockerPostInstall(cli Cli) error { + var err error sudoName, err := logname(cli) - fmt.Fprintln(cli.Out(), sudoName) - // handling case when linux has no login name (e.g. container) - if err == nil { - commands := []string{ - "usermod -aG docker " + sudoName, // add user do docker group + logrus.Tracef(sudoName) + if err != nil { + // handling case when linux has no login name (e.g. container) + if strings.Contains(err.Error(), "no login") { + return nil } + return err + } - for _, command := range commands { + command := fmt.Sprintf("usermod -aG docker %s", sudoName) // add user do docker group - out, stderr, err := cli.Exec(GetSudoPrefix(cli)+"bash -c", []string{command}, false) + out, stderr, err := cli.Exec(GetSudoPrefix(cli)+"bash -c", []string{command}, false) + logrus.Traceln(out) - if stderr != "" { - errorGroup = append(errorGroup, errors.New(command+": "+stderr)) - } - if err != nil { - errorGroup = append(errorGroup, errors.New(err.Error()+"("+command+")")) - } - color.Fprintln(cli.Out(), out) - } - } else { - color.Fprintln(cli.Out(), err) + if stderr != "" { + return errors.New(command + ": " + stderr) } - - return errorGroup + if err != nil { + return errors.New(err.Error() + "(" + command + ")") + } + return err } func logname(cli Cli) (string, error) { From 2a63a451c2e5b47e69fed3549242ea4a2e4da036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20B=C3=A9re=C5=A1?= Date: Tue, 24 Oct 2023 07:48:55 -0700 Subject: [PATCH 3/3] fixed comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Daniel Béreš --- cmd/swctl/app/cmd_dependency.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/cmd/swctl/app/cmd_dependency.go b/cmd/swctl/app/cmd_dependency.go index 695c1c0..264a900 100644 --- a/cmd/swctl/app/cmd_dependency.go +++ b/cmd/swctl/app/cmd_dependency.go @@ -145,7 +145,9 @@ func installExternalToolsCmd(cli Cli) *cobra.Command { } color.Fprintln(cli.Out(), "Installation of the agentctl tool was successful") } - color.Fprintln(cli.Out(), color.Red.Sprintf("Please log out and then log back in for the group membership changes to take effect or enter command \"newgrp docker\"")) + if !docker { + color.Fprintln(cli.Out(), color.Red.Sprintf("Please log out and then log back in for the group membership changes to take effect or enter command \"newgrp docker\"")) + } return nil }, } @@ -648,10 +650,10 @@ func InstallAgentCtl(cli Cli, agentctlCommitVersion string) error { _, stderr, err := cli.Exec(GetSudoPrefix(cli)+"chmod", []string{"755", cmdAgentCtl.installPath()}, false) if stderr != "" { - return fmt.Errorf("chmod error: %s", stderr) + return fmt.Errorf("InstallAgentCtl: chmod error: %s", stderr) } if err != nil { - return err + return fmt.Errorf("InstallAgentCtl: %s", err) } // store the release version info @@ -756,9 +758,8 @@ func isUserRoot() (bool, error) { } func dockerPostInstall(cli Cli) error { - var err error sudoName, err := logname(cli) - logrus.Tracef(sudoName) + logrus.Tracef("detected login name: %s\n", sudoName) if err != nil { // handling case when linux has no login name (e.g. container) if strings.Contains(err.Error(), "no login") { @@ -770,7 +771,7 @@ func dockerPostInstall(cli Cli) error { command := fmt.Sprintf("usermod -aG docker %s", sudoName) // add user do docker group out, stderr, err := cli.Exec(GetSudoPrefix(cli)+"bash -c", []string{command}, false) - logrus.Traceln(out) + logrus.Tracef("dockerPostInstall %s: %s\n", command, out) if stderr != "" { return errors.New(command + ": " + stderr)