Skip to content

[Gitpod CLI] Auto-install Docker when it's not available in the system #15839

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

Closed
wants to merge 1 commit into from
Closed
Changes from all 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
65 changes: 62 additions & 3 deletions components/gitpod-cli/cmd/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ package cmd
import (
"context"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"os/signal"
"os/user"
"path/filepath"
"strings"
"syscall"
Expand All @@ -18,6 +21,7 @@ import (
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/utils"
"github.com/gitpod-io/gitpod/supervisor/api"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -134,9 +138,64 @@ func runRebuild(ctx context.Context, supervisorClient *supervisor.SupervisorClie

dockerPath, err := exec.LookPath("docker")
if err != nil {
fmt.Println("Docker is not installed in your workspace")
event.Set("ErrorCode", utils.RebuildErrorCode_DockerNotFound)
return utils.Outcome_SystemErr, err
currentUser, userErr := user.Current()
if userErr != nil {
event.Set("ErrorCode", utils.RebuildErrorCode_DockerNotFound)
return utils.Outcome_SystemErr, err
}

if currentUser.Username != "root" {
_, sudoErr := exec.LookPath("sudo")
if sudoErr != nil {
event.Set("ErrorCode", utils.RebuildErrorCode_DockerNotFound)
return utils.Outcome_SystemErr, err
}
}

fmt.Println("Docker is not installed in your workspace.")
fmt.Println("It can be automatically installed using Docker Install Script (https://github.com/docker/docker-install)")

prompt := promptui.Select{
Label: "Do you want to proceed with the automatic install?",
Items: []string{"Yes", "No"},
}

_, result, promptErr := prompt.Run()
if promptErr != nil {
return utils.Outcome_SystemErr, err
}

if result == "No" {
event.Set("ErrorCode", utils.RebuildErrorCode_DockerNotFound)
return utils.Outcome_SystemErr, err
}

resp, err := http.Get("https://get.docker.com")
if err != nil {
return utils.Outcome_SystemErr, err
}
defer resp.Body.Close()

dockerInstallScript := filepath.Join(tmpDir, "get-docker.sh")

file, err := os.Create(dockerInstallScript)
if err != nil {
return utils.Outcome_SystemErr, err
}
defer file.Close()

_, err = io.Copy(file, resp.Body)
if err != nil {
return utils.Outcome_SystemErr, err
}

dockerInstallCmd := exec.CommandContext(ctx, "sh", dockerInstallScript)
dockerInstallCmd.Stdout = os.Stdout
dockerInstallCmd.Stderr = os.Stderr
err = dockerInstallCmd.Run()
if err != nil {
return utils.Outcome_SystemErr, err
}
}

tag := "gp-rebuild-temp-build"
Expand Down