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