Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swctl: post install steps for docker #150

Merged
merged 3 commits into from
Oct 24, 2023
Merged
Changes from 2 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
58 changes: 51 additions & 7 deletions cmd/swctl/app/cmd_dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/gookit/color"
"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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\""))
Giluerre marked this conversation as resolved.
Show resolved Hide resolved
return nil
},
}
Expand Down Expand Up @@ -491,6 +492,10 @@ func InstallDocker(cli Cli, dockerVersion string) error {
color.Fprintln(cli.Out(), out)

}
err := dockerPostInstall(cli)
if err != nil {
return errors.New("dockerPostInstall:" + err.Error())
}

return nil
}
Expand Down Expand Up @@ -603,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,
Expand All @@ -621,29 +626,32 @@ 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 {
return fmt.Errorf("can't extract agentctl from builder docker image due "+
"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
Giluerre marked this conversation as resolved.
Show resolved Hide resolved
}

// store the release version info
Expand Down Expand Up @@ -747,3 +755,39 @@ func isUserRoot() (bool, error) {
return true, nil

}
func dockerPostInstall(cli Cli) error {
var err error
Giluerre marked this conversation as resolved.
Show resolved Hide resolved
sudoName, err := logname(cli)
logrus.Tracef(sudoName)
Giluerre marked this conversation as resolved.
Show resolved Hide resolved
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
}

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)

if stderr != "" {
return errors.New(command + ": " + stderr)
}
if err != nil {
return errors.New(err.Error() + "(" + command + ")")
}
return err
}

func logname(cli Cli) (string, error) {
stdout, stderr, err := cli.Exec("logname", nil, false)
Giluerre marked this conversation as resolved.
Show resolved Hide resolved
if stderr != "" {
return "", errors.New(stderr)
}
if err != nil {
return "", err
}
return stdout, err
}