@@ -11,6 +11,7 @@ import (
11
11
"os"
12
12
"os/exec"
13
13
"path/filepath"
14
+ "strings"
14
15
"time"
15
16
16
17
"github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor"
@@ -19,6 +20,34 @@ import (
19
20
"github.com/spf13/cobra"
20
21
)
21
22
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
+
22
51
var buildCmd = & cobra.Command {
23
52
Use : "rebuild" ,
24
53
Short : "Re-builds the workspace image (useful to debug a workspace custom image)" ,
@@ -140,13 +169,6 @@ var buildCmd = &cobra.Command{
140
169
dockerCmd .Stdout = os .Stdout
141
170
dockerCmd .Stderr = os .Stderr
142
171
143
- go func () {
144
- <- ctx .Done ()
145
- if proc := dockerCmd .Process ; proc != nil {
146
- _ = proc .Kill ()
147
- }
148
- }()
149
-
150
172
err = dockerCmd .Run ()
151
173
if _ , ok := err .(* exec.ExitError ); ok {
152
174
fmt .Println ("Image Build Failed" )
@@ -160,19 +182,29 @@ var buildCmd = &cobra.Command{
160
182
return
161
183
}
162
184
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 \n You 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
+
165
198
dockerRunCmd := exec .Command (
166
199
dockerPath ,
167
200
"run" ,
168
201
"--rm" ,
169
- // "--user", "root",
170
- // "--privileged",
171
- // "--label", "gp-rebuild=true",
172
- // "--name", tag,
202
+ "--label" , "gp-rebuild=true" ,
173
203
"-it" ,
174
204
tag ,
175
205
"bash" ,
206
+ "-c" ,
207
+ fmt .Sprintf ("echo '%s'; bash" , welcomeMessage ),
176
208
)
177
209
178
210
dockerRunCmd .Stdout = os .Stdout
0 commit comments