Skip to content

Commit f111836

Browse files
author
Andrea Falzetti
committed
docker run
1 parent 7fc071d commit f111836

File tree

1 file changed

+45
-13
lines changed

1 file changed

+45
-13
lines changed

components/gitpod-cli/cmd/rebuild.go

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"os"
1212
"os/exec"
1313
"path/filepath"
14+
"strings"
1415
"time"
1516

1617
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
@@ -19,6 +20,34 @@ import (
1920
"github.com/spf13/cobra"
2021
)
2122

23+
func TerminateExistingContainer() error {
24+
cmd := exec.Command("docker", "ps", "-q", "-f", "label=gp-rebuild")
25+
containerIds, err := cmd.Output()
26+
if err != nil {
27+
return err
28+
}
29+
30+
for _, id := range strings.Split(string(containerIds), "\n") {
31+
if len(id) == 0 {
32+
continue
33+
}
34+
35+
cmd = exec.Command("docker", "stop", id)
36+
err := cmd.Run()
37+
if err != nil {
38+
return err
39+
}
40+
41+
cmd = exec.Command("docker", "rm", "-f", id)
42+
err = cmd.Run()
43+
if err != nil {
44+
return err
45+
}
46+
}
47+
48+
return nil
49+
}
50+
2251
var buildCmd = &cobra.Command{
2352
Use: "rebuild",
2453
Short: "Re-builds the workspace image (useful to debug a workspace custom image)",
@@ -140,13 +169,6 @@ var buildCmd = &cobra.Command{
140169
dockerCmd.Stdout = os.Stdout
141170
dockerCmd.Stderr = os.Stderr
142171

143-
go func() {
144-
<-ctx.Done()
145-
if proc := dockerCmd.Process; proc != nil {
146-
_ = proc.Kill()
147-
}
148-
}()
149-
150172
err = dockerCmd.Run()
151173
if _, ok := err.(*exec.ExitError); ok {
152174
fmt.Println("Image Build Failed")
@@ -160,19 +182,29 @@ var buildCmd = &cobra.Command{
160182
return
161183
}
162184

163-
// TODO: add message to suggest how to exit
164-
// TODO: add docker run
185+
err = TerminateExistingContainer()
186+
if err != nil {
187+
utils.LogError(ctx, err, "Failed to stop previous gp rebuild container", client)
188+
}
189+
190+
messages := []string{
191+
"\n\nYou are now connected to the container",
192+
"You can inspect the container and make sure the necessary tools & libraries are installed.",
193+
"When you are done, just type exit to return to your Gitpod workspace\n",
194+
}
195+
196+
welcomeMessage := strings.Join(messages, "\n")
197+
165198
dockerRunCmd := exec.Command(
166199
dockerPath,
167200
"run",
168201
"--rm",
169-
// "--user", "root",
170-
// "--privileged",
171-
// "--label", "gp-rebuild=true",
172-
// "--name", tag,
202+
"--label", "gp-rebuild=true",
173203
"-it",
174204
tag,
175205
"bash",
206+
"-c",
207+
fmt.Sprintf("echo '%s'; bash", welcomeMessage),
176208
)
177209

178210
dockerRunCmd.Stdout = os.Stdout

0 commit comments

Comments
 (0)