@@ -149,6 +149,8 @@ func (s IDEKind) String() string {
149
149
return "unknown"
150
150
}
151
151
152
+ var childProcEnvvars []string
153
+
152
154
// Run serves as main entrypoint to the supervisor.
153
155
func Run (options ... RunOption ) {
154
156
exitCode := 0
@@ -182,15 +184,15 @@ func Run(options ...RunOption) {
182
184
183
185
// BEWARE: we can only call buildChildProcEnv once, because it might download env vars from a one-time-secret
184
186
// URL, which would fail if we tried another time.
185
- childProcEnvvars : = buildChildProcEnv (cfg , nil , opts .RunGP )
187
+ childProcEnvvars = buildChildProcEnv (cfg , nil , opts .RunGP )
186
188
187
189
err = AddGitpodUserIfNotExists ()
188
190
if err != nil {
189
191
log .WithError (err ).Fatal ("cannot ensure Gitpod user exists" )
190
192
}
191
193
symlinkBinaries (cfg )
192
194
193
- configureGit (cfg , childProcEnvvars )
195
+ configureGit (cfg )
194
196
195
197
telemetry := analytics .NewFromEnvironment ()
196
198
defer telemetry .Close ()
@@ -365,15 +367,15 @@ func Run(options ...RunOption) {
365
367
if ! cfg .isPrebuild () {
366
368
// We need to checkout dotfiles first, because they may be changing the path which affects the IDE.
367
369
// TODO(cw): provide better feedback if the IDE start fails because of the dotfiles (provide any feedback at all).
368
- installDotfiles (ctx , cfg , tokenService , childProcEnvvars )
370
+ installDotfiles (ctx , cfg , tokenService )
369
371
}
370
372
371
373
var ideWG sync.WaitGroup
372
374
ideWG .Add (1 )
373
- go startAndWatchIDE (ctx , cfg , & cfg .IDE , childProcEnvvars , & ideWG , cstate , ideReady , WebIDE , supervisorMetrics )
375
+ go startAndWatchIDE (ctx , cfg , & cfg .IDE , & ideWG , cstate , ideReady , WebIDE , supervisorMetrics )
374
376
if cfg .DesktopIDE != nil {
375
377
ideWG .Add (1 )
376
- go startAndWatchIDE (ctx , cfg , cfg .DesktopIDE , childProcEnvvars , & ideWG , cstate , desktopIdeReady , DesktopIDE , supervisorMetrics )
378
+ go startAndWatchIDE (ctx , cfg , cfg .DesktopIDE , & ideWG , cstate , desktopIdeReady , DesktopIDE , supervisorMetrics )
377
379
}
378
380
379
381
var (
@@ -394,7 +396,7 @@ func Run(options ...RunOption) {
394
396
go startAPIEndpoint (ctx , cfg , & wg , apiServices , tunneledPortsService , metricsReporter , apiEndpointOpts ... )
395
397
396
398
wg .Add (1 )
397
- go startSSHServer (ctx , cfg , & wg , childProcEnvvars )
399
+ go startSSHServer (ctx , cfg , & wg )
398
400
399
401
wg .Add (1 )
400
402
tasksSuccessChan := make (chan taskSuccess , 1 )
@@ -435,12 +437,11 @@ func Run(options ...RunOption) {
435
437
log .Debugf ("unshallow of local repository took %v" , time .Since (start ))
436
438
}()
437
439
438
- if ! isShallowRepository (repoRoot , childProcEnvvars ) {
440
+ if ! isShallowRepository (repoRoot ) {
439
441
return
440
442
}
441
443
442
444
cmd := runAsGitpodUser (exec .Command ("git" , "fetch" , "--unshallow" , "--tags" ))
443
- cmd .Env = childProcEnvvars
444
445
cmd .Dir = repoRoot
445
446
cmd .Stdout = os .Stdout
446
447
cmd .Stderr = os .Stderr
@@ -475,9 +476,8 @@ func Run(options ...RunOption) {
475
476
wg .Wait ()
476
477
}
477
478
478
- func isShallowRepository (rootDir string , env [] string ) bool {
479
+ func isShallowRepository (rootDir string ) bool {
479
480
cmd := runAsGitpodUser (exec .Command ("git" , "rev-parse" , "--is-shallow-repository" ))
480
- cmd .Env = env
481
481
cmd .Dir = rootDir
482
482
out , err := cmd .CombinedOutput ()
483
483
if err != nil {
@@ -494,7 +494,7 @@ func isShallowRepository(rootDir string, env []string) bool {
494
494
return isShallow
495
495
}
496
496
497
- func installDotfiles (ctx context.Context , cfg * Config , tokenService * InMemoryTokenService , childProcEnvvars [] string ) {
497
+ func installDotfiles (ctx context.Context , cfg * Config , tokenService * InMemoryTokenService ) {
498
498
repo := cfg .DotfileRepo
499
499
if repo == "" {
500
500
return
@@ -509,15 +509,7 @@ func installDotfiles(ctx context.Context, cfg *Config, tokenService *InMemoryTok
509
509
prep := func (cfg * Config , out io.Writer , name string , args ... string ) * exec.Cmd {
510
510
cmd := exec .Command (name , args ... )
511
511
cmd .Dir = "/home/gitpod"
512
- cmd .Env = childProcEnvvars
513
- cmd .SysProcAttr = & syscall.SysProcAttr {
514
- // All supervisor children run as gitpod user. The environment variables we produce are also
515
- // gitpod user specific.
516
- Credential : & syscall.Credential {
517
- Uid : gitpodUID ,
518
- Gid : gitpodGID ,
519
- },
520
- }
512
+ runAsGitpodUser (cmd )
521
513
cmd .Stdout = out
522
514
cmd .Stderr = out
523
515
return cmd
@@ -706,7 +698,7 @@ func symlinkBinaries(cfg *Config) {
706
698
}
707
699
}
708
700
709
- func configureGit (cfg * Config , childProcEnvvars [] string ) {
701
+ func configureGit (cfg * Config ) {
710
702
settings := [][]string {
711
703
{"push.default" , "simple" },
712
704
{"alias.lg" , "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" },
@@ -723,7 +715,6 @@ func configureGit(cfg *Config, childProcEnvvars []string) {
723
715
for _ , s := range settings {
724
716
cmd := exec .Command ("git" , append ([]string {"config" , "--global" }, s ... )... )
725
717
cmd = runAsGitpodUser (cmd )
726
- cmd .Env = childProcEnvvars
727
718
cmd .Stdout = os .Stdout
728
719
cmd .Stderr = os .Stderr
729
720
err := cmd .Run ()
@@ -768,7 +759,7 @@ var (
768
759
errSignalTerminated = errors .New ("signal: terminated" )
769
760
)
770
761
771
- func startAndWatchIDE (ctx context.Context , cfg * Config , ideConfig * IDEConfig , childProcEnvvars [] string , wg * sync.WaitGroup , cstate * InMemoryContentState , ideReady * ideReadyState , ide IDEKind , metrics * metrics.SupervisorMetrics ) {
762
+ func startAndWatchIDE (ctx context.Context , cfg * Config , ideConfig * IDEConfig , wg * sync.WaitGroup , cstate * InMemoryContentState , ideReady * ideReadyState , ide IDEKind , metrics * metrics.SupervisorMetrics ) {
772
763
defer wg .Done ()
773
764
defer log .WithField ("ide" , ide .String ()).Debug ("startAndWatchIDE shutdown" )
774
765
@@ -795,7 +786,7 @@ supervisorLoop:
795
786
796
787
ideStopped = make (chan struct {}, 1 )
797
788
startTime := time .Now ()
798
- cmd = prepareIDELaunch (cfg , ideConfig , childProcEnvvars )
789
+ cmd = prepareIDELaunch (cfg , ideConfig )
799
790
launchIDE (cfg , ideConfig , cmd , ideStopped , ideReady , & ideStatus , ide )
800
791
801
792
if firstStart {
@@ -896,7 +887,7 @@ func launchIDE(cfg *Config, ideConfig *IDEConfig, cmd *exec.Cmd, ideStopped chan
896
887
}()
897
888
}
898
889
899
- func prepareIDELaunch (cfg * Config , ideConfig * IDEConfig , childProcEnvvars [] string ) * exec.Cmd {
890
+ func prepareIDELaunch (cfg * Config , ideConfig * IDEConfig ) * exec.Cmd {
900
891
args := ideConfig .EntrypointArgs
901
892
for i := range args {
902
893
args [i ] = strings .ReplaceAll (args [i ], "{IDEPORT}" , strconv .Itoa (cfg .IDEPort ))
@@ -905,20 +896,15 @@ func prepareIDELaunch(cfg *Config, ideConfig *IDEConfig, childProcEnvvars []stri
905
896
log .WithField ("args" , args ).WithField ("entrypoint" , ideConfig .Entrypoint ).Info ("preparing IDE launch" )
906
897
907
898
cmd := exec .Command (ideConfig .Entrypoint , args ... )
908
- cmd .SysProcAttr = & syscall.SysProcAttr {
909
- // We need the child process to run in its own process group, s.t. we can suspend and resume
910
- // IDE and its children.
911
- Setpgid : true ,
912
- Pdeathsig : syscall .SIGKILL ,
913
-
914
- // All supervisor children run as gitpod user. The environment variables we produce are also
915
- // gitpod user specific.
916
- Credential : & syscall.Credential {
917
- Uid : gitpodUID ,
918
- Gid : gitpodGID ,
919
- },
920
- }
921
- cmd .Env = childProcEnvvars
899
+
900
+ // All supervisor children run as gitpod user. The environment variables we produce are also
901
+ // gitpod user specific.
902
+ runAsGitpodUser (cmd )
903
+
904
+ // We need the child process to run in its own process group, s.t. we can suspend and resume
905
+ // IDE and its children.
906
+ cmd .SysProcAttr .Setpgid = true
907
+ cmd .SysProcAttr .Pdeathsig = syscall .SIGKILL
922
908
923
909
// Here we must resist the temptation to "neaten up" the IDE output for headless builds.
924
910
// This would break the JSON parsing of the headless builds.
@@ -1384,7 +1370,7 @@ func stopWhenTasksAreDone(ctx context.Context, wg *sync.WaitGroup, shutdown chan
1384
1370
shutdown <- ShutdownReasonSuccess
1385
1371
}
1386
1372
1387
- func startSSHServer (ctx context.Context , cfg * Config , wg * sync.WaitGroup , childProcEnvvars [] string ) {
1373
+ func startSSHServer (ctx context.Context , cfg * Config , wg * sync.WaitGroup ) {
1388
1374
defer wg .Done ()
1389
1375
1390
1376
if cfg .isHeadless () {
@@ -1783,6 +1769,7 @@ func runAsGitpodUser(cmd *exec.Cmd) *exec.Cmd {
1783
1769
if cmd .SysProcAttr .Credential == nil {
1784
1770
cmd .SysProcAttr .Credential = & syscall.Credential {}
1785
1771
}
1772
+ cmd .Env = append (cmd .Env , childProcEnvvars ... )
1786
1773
cmd .SysProcAttr .Credential .Uid = gitpodUID
1787
1774
cmd .SysProcAttr .Credential .Gid = gitpodGID
1788
1775
return cmd
0 commit comments