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] 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 +}