-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmain.go
1307 lines (1168 loc) · 37.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.
package main
import (
"archive/zip"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"os/signal"
"path/filepath"
"reflect"
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/google/uuid"
"github.com/hashicorp/go-version"
"golang.org/x/xerrors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
yaml "gopkg.in/yaml.v2"
"github.com/gitpod-io/gitpod/common-go/log"
"github.com/gitpod-io/gitpod/common-go/util"
gitpod "github.com/gitpod-io/gitpod/gitpod-protocol"
"github.com/gitpod-io/gitpod/jetbrains/launcher/pkg/constant"
supervisor "github.com/gitpod-io/gitpod/supervisor/api"
)
const (
defaultBackendPort = "63342"
maxDefaultXmx = 8 * 1024
minDefaultXmx = 2 * 1024
)
var (
// ServiceName is the name we use for tracing/logging.
ServiceName = "jetbrains-launcher"
)
type LaunchContext struct {
startTime time.Time
port string
alias string
label string
warmup bool
preferToolbox bool
qualifier string
productDir string
backendDir string
info *ProductInfo
backendVersion *version.Version
wsInfo *supervisor.WorkspaceInfoResponse
vmOptionsFile string
platformPropertiesFile string
projectDir string
configDir string
systemDir string
projectContextDir string
riderSolutionFile string
env []string
// Custom fields
// shouldWaitBackendPlugin is controlled by env GITPOD_WAIT_IDE_BACKEND
shouldWaitBackendPlugin bool
}
func (c *LaunchContext) getCommonJoinLinkResponse(appPid int, joinLink string) *JoinLinkResponse {
return &JoinLinkResponse{
AppPid: appPid,
JoinLink: joinLink,
IDEVersion: fmt.Sprintf("%s-%s", c.info.ProductCode, c.info.BuildNumber),
ProjectPath: c.projectContextDir,
}
}
// JB startup entrypoint
func main() {
if len(os.Args) == 3 && os.Args[1] == "env" && os.Args[2] != "" {
var mark = os.Args[2]
content, err := json.Marshal(os.Environ())
exitStatus := 0
if err != nil {
fmt.Fprintf(os.Stderr, "%s", err)
exitStatus = 1
}
fmt.Printf("%s%s%s", mark, content, mark)
os.Exit(exitStatus)
return
}
// supervisor refer see https://github.com/gitpod-io/gitpod/blob/main/components/supervisor/pkg/supervisor/supervisor.go#L961
shouldWaitBackendPlugin := os.Getenv("GITPOD_WAIT_IDE_BACKEND") == "true"
debugEnabled := os.Getenv("SUPERVISOR_DEBUG_ENABLE") == "true"
preferToolbox := os.Getenv("GITPOD_PREFER_TOOLBOX") == "true"
log.Init(ServiceName, constant.Version, true, debugEnabled)
log.Info(ServiceName + ": " + constant.Version)
startTime := time.Now()
log.WithField("shouldWait", shouldWaitBackendPlugin).Info("should wait backend plugin")
var port string
var warmup bool
if len(os.Args) < 2 {
log.Fatalf("Usage: %s (warmup|<port>)\n", os.Args[0])
}
if os.Args[1] == "warmup" {
if len(os.Args) < 3 {
log.Fatalf("Usage: %s %s <alias>\n", os.Args[0], os.Args[1])
}
warmup = true
} else {
if len(os.Args) < 3 {
log.Fatalf("Usage: %s <port> <kind> [<link label>]\n", os.Args[0])
}
port = os.Args[1]
}
alias := os.Args[2]
label := "Open JetBrains IDE"
if len(os.Args) > 3 {
label = os.Args[3]
}
qualifier := os.Getenv("JETBRAINS_BACKEND_QUALIFIER")
if qualifier == "stable" {
qualifier = ""
} else {
qualifier = "-" + qualifier
}
productDir := "/ide-desktop/" + alias + qualifier
backendDir := productDir + "/backend"
info, err := resolveProductInfo(backendDir)
if err != nil {
log.WithError(err).Error("failed to resolve product info")
return
}
backendVersion, err := version.NewVersion(info.Version)
if err != nil {
log.WithError(err).Error("failed to resolve backend version")
return
}
wsInfo, err := resolveWorkspaceInfo(context.Background())
if err != nil || wsInfo == nil {
log.WithError(err).WithField("wsInfo", wsInfo).Error("resolve workspace info failed")
return
}
launchCtx := &LaunchContext{
startTime: startTime,
warmup: warmup,
port: port,
alias: alias,
label: label,
preferToolbox: preferToolbox,
qualifier: qualifier,
productDir: productDir,
backendDir: backendDir,
info: info,
backendVersion: backendVersion,
wsInfo: wsInfo,
shouldWaitBackendPlugin: shouldWaitBackendPlugin,
}
if launchCtx.warmup {
launch(launchCtx)
return
}
if preferToolbox {
err = configureToolboxCliProperties(backendDir)
if err != nil {
log.WithError(err).Error("failed to write toolbox cli config file")
return
}
}
// we should start serving immediately and postpone launch
// in order to enable a JB Gateway to connect as soon as possible
go launch(launchCtx)
// IMPORTANT: don't put startup logic in serve!!!
serve(launchCtx)
}
func serve(launchCtx *LaunchContext) {
debugAgentPrefix := "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:"
http.HandleFunc("/debug", func(w http.ResponseWriter, r *http.Request) {
options, err := readVMOptions(launchCtx.vmOptionsFile)
if err != nil {
log.WithError(err).Error("failed to configure debug agent")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
debugPort := ""
i := len(options) - 1
for i >= 0 && debugPort == "" {
option := options[i]
if strings.HasPrefix(option, debugAgentPrefix) {
debugPort = option[len(debugAgentPrefix):]
if debugPort == "0" {
debugPort = ""
}
}
i--
}
if debugPort != "" {
fmt.Fprint(w, debugPort)
return
}
netListener, err := net.Listen("tcp", "localhost:0")
if err != nil {
log.WithError(err).Error("failed to configure debug agent")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
debugPort = strconv.Itoa(netListener.(*net.TCPListener).Addr().(*net.TCPAddr).Port)
_ = netListener.Close()
debugOptions := []string{debugAgentPrefix + debugPort}
options = deduplicateVMOption(options, debugOptions, func(l, r string) bool {
return strings.HasPrefix(l, debugAgentPrefix) && strings.HasPrefix(r, debugAgentPrefix)
})
err = writeVMOptions(launchCtx.vmOptionsFile, options)
if err != nil {
log.WithError(err).Error("failed to configure debug agent")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprint(w, debugPort)
restart(r)
})
http.HandleFunc("/restart", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "terminated")
restart(r)
})
http.HandleFunc("/joinLink", func(w http.ResponseWriter, r *http.Request) {
backendPort := r.URL.Query().Get("backendPort")
if backendPort == "" {
backendPort = defaultBackendPort
}
jsonLink, err := resolveJsonLink(backendPort)
if err != nil {
log.WithError(err).Error("cannot resolve join link")
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
fmt.Fprint(w, jsonLink)
})
http.HandleFunc("/joinLink2", func(w http.ResponseWriter, r *http.Request) {
backendPort := r.URL.Query().Get("backendPort")
if backendPort == "" {
backendPort = defaultBackendPort
}
jsonResp, err := resolveJsonLink2(launchCtx, backendPort)
if err != nil {
log.WithError(err).Error("cannot resolve join link")
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
json.NewEncoder(w).Encode(jsonResp)
})
http.HandleFunc("/gatewayLink", func(w http.ResponseWriter, r *http.Request) {
backendPort := r.URL.Query().Get("backendPort")
if backendPort == "" {
backendPort = defaultBackendPort
}
jsonLink, err := resolveGatewayLink(backendPort, launchCtx.wsInfo)
if err != nil {
log.WithError(err).Error("cannot resolve gateway link")
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
fmt.Fprint(w, jsonLink)
})
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
if launchCtx.preferToolbox {
response := make(map[string]string)
toolboxLink, err := resolveToolboxLink(launchCtx.wsInfo)
if err != nil {
log.WithError(err).Error("cannot resolve toolbox link")
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
response["link"] = toolboxLink
response["label"] = launchCtx.label
response["clientID"] = "jetbrains-toolbox"
response["kind"] = launchCtx.alias
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(response)
return
}
backendPort := r.URL.Query().Get("backendPort")
if backendPort == "" {
backendPort = defaultBackendPort
}
if err := isBackendPluginReady(r.Context(), backendPort, launchCtx.shouldWaitBackendPlugin); err != nil {
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
gatewayLink, err := resolveGatewayLink(backendPort, launchCtx.wsInfo)
if err != nil {
log.WithError(err).Error("cannot resolve gateway link")
http.Error(w, err.Error(), http.StatusServiceUnavailable)
return
}
response := make(map[string]string)
response["link"] = gatewayLink
response["label"] = launchCtx.label
response["clientID"] = "jetbrains-gateway"
response["kind"] = launchCtx.alias
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(response)
})
fmt.Printf("Starting status proxy for desktop IDE at port %s\n", launchCtx.port)
if err := http.ListenAndServe(fmt.Sprintf(":%s", launchCtx.port), nil); err != nil {
log.Fatal(err)
}
}
// isBackendPluginReady checks if the backend plugin is ready via backend plugin CLI GitpodCLIService.kt
func isBackendPluginReady(ctx context.Context, backendPort string, shouldWaitBackendPlugin bool) error {
if !shouldWaitBackendPlugin {
log.Debug("will not wait plugin ready")
return nil
}
log.WithField("backendPort", backendPort).Debug("wait backend plugin to be ready")
// Use op=metrics so that we don't need to rebuild old backend-plugin
url, err := url.Parse("http://localhost:" + backendPort + "/api/gitpod/cli?op=metrics")
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("backend plugin is not ready: %d", resp.StatusCode)
}
return nil
}
func restart(r *http.Request) {
backendPort := r.URL.Query().Get("backendPort")
if backendPort == "" {
backendPort = defaultBackendPort
}
err := terminateIDE(backendPort)
if err != nil {
log.WithError(err).Error("failed to terminate IDE gracefully")
os.Exit(1)
}
os.Exit(0)
}
type Projects struct {
JoinLink string `json:"joinLink"`
}
type Response struct {
AppPid int `json:"appPid"`
JoinLink string `json:"joinLink"`
Projects []Projects `json:"projects"`
}
type JoinLinkResponse struct {
AppPid int `json:"appPid"`
JoinLink string `json:"joinLink"`
// IDEVersion is the ideVersionHint that required by Toolbox to `setAutoConnectOnEnvironmentReady`
IDEVersion string `json:"ideVersion"`
// ProjectPath is the projectPathHint that required by Toolbox to `setAutoConnectOnEnvironmentReady`
ProjectPath string `json:"projectPath"`
}
func resolveToolboxLink(wsInfo *supervisor.WorkspaceInfoResponse) (string, error) {
gitpodUrl, err := url.Parse(wsInfo.GitpodHost)
if err != nil {
return "", err
}
debugWorkspace := wsInfo.DebugWorkspaceType != supervisor.DebugWorkspaceType_noDebug
link := url.URL{
Scheme: "jetbrains",
Host: "gateway",
Path: "io.gitpod.toolbox.gateway/open-in-toolbox",
RawQuery: fmt.Sprintf("userId=%shost=%s&workspaceId=%s&debugWorkspace=%t", wsInfo.OwnerId, gitpodUrl.Hostname(), wsInfo.WorkspaceId, debugWorkspace),
}
return link.String(), nil
}
func resolveGatewayLink(backendPort string, wsInfo *supervisor.WorkspaceInfoResponse) (string, error) {
gitpodUrl, err := url.Parse(wsInfo.GitpodHost)
if err != nil {
return "", err
}
debugWorkspace := wsInfo.DebugWorkspaceType != supervisor.DebugWorkspaceType_noDebug
link := url.URL{
Scheme: "jetbrains-gateway",
Host: "connect",
Fragment: fmt.Sprintf("gitpodHost=%s&workspaceId=%s&backendPort=%s&debugWorkspace=%t", gitpodUrl.Hostname(), wsInfo.WorkspaceId, backendPort, debugWorkspace),
}
return link.String(), nil
}
func resolveJsonLink(backendPort string) (string, error) {
var (
hostStatusUrl = "http://localhost:" + backendPort + "/codeWithMe/unattendedHostStatus?token=gitpod"
client = http.Client{Timeout: 1 * time.Second}
)
resp, err := client.Get(hostStatusUrl)
if err != nil {
return "", err
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", xerrors.Errorf("failed to resolve project status: %s (%d)", bodyBytes, resp.StatusCode)
}
jsonResp := &Response{}
err = json.Unmarshal(bodyBytes, &jsonResp)
if err != nil {
return "", err
}
if len(jsonResp.Projects) > 0 {
return jsonResp.Projects[0].JoinLink, nil
}
return jsonResp.JoinLink, nil
}
func resolveJsonLink2(launchCtx *LaunchContext, backendPort string) (*JoinLinkResponse, error) {
var (
hostStatusUrl = "http://localhost:" + backendPort + "/codeWithMe/unattendedHostStatus?token=gitpod"
client = http.Client{Timeout: 1 * time.Second}
)
resp, err := client.Get(hostStatusUrl)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, xerrors.Errorf("failed to resolve project status: %s (%d)", bodyBytes, resp.StatusCode)
}
jsonResp := &Response{}
err = json.Unmarshal(bodyBytes, &jsonResp)
if err != nil {
return nil, err
}
if len(jsonResp.Projects) > 0 {
return launchCtx.getCommonJoinLinkResponse(jsonResp.AppPid, jsonResp.Projects[0].JoinLink), nil
}
if len(jsonResp.JoinLink) > 0 {
return launchCtx.getCommonJoinLinkResponse(jsonResp.AppPid, jsonResp.JoinLink), nil
}
log.Error("failed to resolve JetBrains JoinLink")
return nil, xerrors.Errorf("failed to resolve JoinLink")
}
func terminateIDE(backendPort string) error {
var (
hostStatusUrl = "http://localhost:" + backendPort + "/codeWithMe/unattendedHostStatus?token=gitpod&exit=true"
client = http.Client{Timeout: 10 * time.Second}
)
resp, err := client.Get(hostStatusUrl)
if err != nil {
return err
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return xerrors.Errorf("failed to resolve terminate IDE: %s (%d)", bodyBytes, resp.StatusCode)
}
return nil
}
func resolveWorkspaceInfo(ctx context.Context) (*supervisor.WorkspaceInfoResponse, error) {
resolve := func(ctx context.Context) (wsInfo *supervisor.WorkspaceInfoResponse, err error) {
supervisorConn, err := grpc.Dial(util.GetSupervisorAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
err = errors.New("dial supervisor failed: " + err.Error())
return
}
defer supervisorConn.Close()
if wsInfo, err = supervisor.NewInfoServiceClient(supervisorConn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{}); err != nil {
err = errors.New("get workspace info failed: " + err.Error())
return
}
return
}
// try resolve workspace info 10 times
for attempt := 0; attempt < 10; attempt++ {
if wsInfo, err := resolve(ctx); err != nil {
log.WithError(err).Error("resolve workspace info failed")
time.Sleep(1 * time.Second)
} else {
return wsInfo, err
}
}
return nil, errors.New("failed with attempt 10 times")
}
func launch(launchCtx *LaunchContext) {
projectDir := launchCtx.wsInfo.GetCheckoutLocation()
gitpodConfig, err := parseGitpodConfig(projectDir)
if err != nil {
log.WithError(err).Error("failed to parse .gitpod.yml")
}
// configure vmoptions
idePrefix := launchCtx.alias
if launchCtx.alias == "intellij" {
idePrefix = "idea"
}
// [idea64|goland64|pycharm64|phpstorm64].vmoptions
launchCtx.vmOptionsFile = fmt.Sprintf(launchCtx.backendDir+"/bin/%s64.vmoptions", idePrefix)
err = configureVMOptions(gitpodConfig, launchCtx.alias, launchCtx.vmOptionsFile)
if err != nil {
log.WithError(err).Error("failed to configure vmoptions")
}
var riderSolutionFile string
if launchCtx.alias == "rider" {
riderSolutionFile, err = findRiderSolutionFile(projectDir)
if err != nil {
log.WithError(err).Error("failed to find a rider solution file")
}
}
launchCtx.projectDir = projectDir
launchCtx.configDir = fmt.Sprintf("/workspace/.config/JetBrains%s/RemoteDev-%s", launchCtx.qualifier, launchCtx.info.ProductCode)
launchCtx.systemDir = fmt.Sprintf("/workspace/.cache/JetBrains%s/RemoteDev-%s", launchCtx.qualifier, launchCtx.info.ProductCode)
launchCtx.riderSolutionFile = riderSolutionFile
launchCtx.projectContextDir = resolveProjectContextDir(launchCtx)
launchCtx.platformPropertiesFile = launchCtx.backendDir + "/bin/idea.properties"
_, err = configurePlatformProperties(launchCtx.platformPropertiesFile, launchCtx.configDir, launchCtx.systemDir)
if err != nil {
log.WithError(err).Error("failed to update platform properties file")
}
_, err = syncInitialContent(launchCtx, Options)
if err != nil {
log.WithError(err).Error("failed to sync initial options")
}
launchCtx.env = resolveLaunchContextEnv()
_, err = syncInitialContent(launchCtx, Plugins)
if err != nil {
log.WithError(err).Error("failed to sync initial plugins")
}
// install project plugins
err = installPlugins(gitpodConfig, launchCtx)
installPluginsCost := time.Now().Local().Sub(launchCtx.startTime).Milliseconds()
if err != nil {
log.WithError(err).WithField("cost", installPluginsCost).Error("installing repo plugins: done")
} else {
log.WithField("cost", installPluginsCost).Info("installing repo plugins: done")
}
// install gitpod plugin
err = linkRemotePlugin(launchCtx)
if err != nil {
log.WithError(err).Error("failed to install gitpod-remote plugin")
}
// run backend
run(launchCtx)
}
func run(launchCtx *LaunchContext) {
var args []string
if launchCtx.warmup {
args = append(args, "warmup")
} else if launchCtx.preferToolbox {
args = append(args, "serverMode")
} else {
args = append(args, "run")
}
args = append(args, launchCtx.projectContextDir)
cmd := remoteDevServerCmd(args, launchCtx)
cmd.Env = append(cmd.Env, "JETBRAINS_GITPOD_BACKEND_KIND="+launchCtx.alias)
workspaceUrl, err := url.Parse(launchCtx.wsInfo.WorkspaceUrl)
if err == nil {
cmd.Env = append(cmd.Env, "JETBRAINS_GITPOD_WORKSPACE_HOST="+workspaceUrl.Hostname())
}
// Enable host status endpoint
cmd.Env = append(cmd.Env, "CWM_HOST_STATUS_OVER_HTTP_TOKEN=gitpod")
if err := cmd.Start(); err != nil {
log.WithError(err).Error("failed to start")
}
// Nicely handle SIGTERM sinal
go handleSignal()
if err := cmd.Wait(); err != nil {
log.WithError(err).Error("failed to wait")
}
log.Info("IDE stopped, exiting")
os.Exit(cmd.ProcessState.ExitCode())
}
// resolveUserEnvs emulats the interactive login shell to ensure that all user defined shell scripts are loaded
func resolveUserEnvs() (userEnvs []string, err error) {
shell := os.Getenv("SHELL")
if shell == "" {
shell = "/bin/bash"
}
mark, err := uuid.NewRandom()
if err != nil {
return
}
self, err := os.Executable()
if err != nil {
return
}
envCmd := exec.Command(shell, []string{"-i", "-l", "-c", strings.Join([]string{self, "env", mark.String()}, " ")}...)
envCmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
envCmd.Stderr = os.Stderr
envCmd.WaitDelay = 3 * time.Second
time.AfterFunc(8*time.Second, func() {
_ = syscall.Kill(-envCmd.Process.Pid, syscall.SIGKILL)
})
output, err := envCmd.Output()
if errors.Is(err, exec.ErrWaitDelay) {
// For some reason the command doesn't close it's I/O pipes but it already run successfully
// so just ignore this error
log.Warn("WaitDelay expired before envCmd I/O completed")
} else if err != nil {
return
}
markByte := []byte(mark.String())
start := bytes.Index(output, markByte)
if start == -1 {
err = fmt.Errorf("no %s in output", mark.String())
return
}
start = start + len(markByte)
if start > len(output) {
err = fmt.Errorf("no %s in output", mark.String())
return
}
end := bytes.LastIndex(output, markByte)
if end == -1 {
err = fmt.Errorf("no %s in output", mark.String())
return
}
err = json.Unmarshal(output[start:end], &userEnvs)
return
}
func resolveLaunchContextEnv() []string {
var launchCtxEnv []string
userEnvs, err := resolveUserEnvs()
if err == nil {
launchCtxEnv = append(launchCtxEnv, userEnvs...)
} else {
log.WithError(err).Error("failed to resolve user env vars")
launchCtxEnv = os.Environ()
}
// instead put them into /ide-desktop/${alias}${qualifier}/backend/bin/idea64.vmoptions
// otherwise JB will complain to a user on each startup
// by default remote dev already set -Xmx2048m, see /ide-desktop/${alias}${qualifier}/backend/plugins/remote-dev-server/bin/launcher.sh
launchCtxEnv = append(launchCtxEnv, "INTELLIJ_ORIGINAL_ENV_JAVA_TOOL_OPTIONS="+os.Getenv("JAVA_TOOL_OPTIONS"))
launchCtxEnv = append(launchCtxEnv, "JAVA_TOOL_OPTIONS=")
// Force it to be disabled as we update platform properties file already
// TODO: Some ides have it enabled by default still, check pycharm and remove next release
launchCtxEnv = append(launchCtxEnv, "REMOTE_DEV_LEGACY_PER_PROJECT_CONFIGS=0")
log.WithField("env", strings.Join(launchCtxEnv, "\n")).Info("resolved launch env")
return launchCtxEnv
}
func remoteDevServerCmd(args []string, launchCtx *LaunchContext) *exec.Cmd {
cmd := exec.Command(launchCtx.backendDir+"/bin/remote-dev-server.sh", args...)
cmd.Env = launchCtx.env
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
return cmd
}
func handleSignal() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
<-sigChan
log.WithField("port", defaultBackendPort).Info("receive SIGTERM signal, terminating IDE")
if err := terminateIDE(defaultBackendPort); err != nil {
log.WithError(err).Error("failed to terminate IDE")
}
log.Info("asked IDE to terminate")
}
func configurePlatformProperties(platformOptionsPath string, configDir string, systemDir string) (bool, error) {
buffer, err := os.ReadFile(platformOptionsPath)
if err != nil {
return false, err
}
content := string(buffer)
updated, content := updatePlatformProperties(content, configDir, systemDir)
if updated {
return true, os.WriteFile(platformOptionsPath, []byte(content), 0)
}
return false, nil
}
func updatePlatformProperties(content string, configDir string, systemDir string) (bool, string) {
lines := strings.Split(content, "\n")
configMap := make(map[string]bool)
for _, v := range lines {
v = strings.TrimSpace(v)
if v != "" && !strings.HasPrefix(v, "#") {
key, _, found := strings.Cut(v, "=")
if found {
configMap[key] = true
}
}
}
updated := false
if _, found := configMap["idea.config.path"]; !found {
updated = true
content = strings.Join([]string{
content,
fmt.Sprintf("idea.config.path=%s", configDir),
fmt.Sprintf("idea.plugins.path=%s", configDir+"/plugins"),
fmt.Sprintf("idea.system.path=%s", systemDir),
fmt.Sprintf("idea.log.path=%s", systemDir+"/log"),
}, "\n")
}
return updated, content
}
func configureVMOptions(config *gitpod.GitpodConfig, alias string, vmOptionsPath string) error {
options, err := readVMOptions(vmOptionsPath)
if err != nil {
return err
}
newOptions := updateVMOptions(config, alias, options)
return writeVMOptions(vmOptionsPath, newOptions)
}
func readVMOptions(vmOptionsPath string) ([]string, error) {
content, err := os.ReadFile(vmOptionsPath)
if err != nil {
return nil, err
}
return strings.Fields(string(content)), nil
}
func writeVMOptions(vmOptionsPath string, vmoptions []string) error {
// vmoptions file should end with a newline
content := strings.Join(vmoptions, "\n") + "\n"
return os.WriteFile(vmOptionsPath, []byte(content), 0)
}
// deduplicateVMOption append new VMOptions onto old VMOptions and remove any duplicated leftmost options
func deduplicateVMOption(oldLines []string, newLines []string, predicate func(l, r string) bool) []string {
var result []string
var merged = append(oldLines, newLines...)
for i, left := range merged {
for _, right := range merged[i+1:] {
if predicate(left, right) {
left = ""
break
}
}
if left != "" {
result = append(result, left)
}
}
return result
}
func updateVMOptions(
config *gitpod.GitpodConfig,
alias string,
// original vmoptions (inherited from $JETBRAINS_IDE_HOME/bin/idea64.vmoptions)
ideaVMOptionsLines []string) []string {
// inspired by how intellij platform merge the VMOptions
// https://github.com/JetBrains/intellij-community/blob/master/platform/platform-impl/src/com/intellij/openapi/application/ConfigImportHelper.java#L1115
filterFunc := func(l, r string) bool {
isEqual := l == r
isXmx := strings.HasPrefix(l, "-Xmx") && strings.HasPrefix(r, "-Xmx")
isXms := strings.HasPrefix(l, "-Xms") && strings.HasPrefix(r, "-Xms")
isXss := strings.HasPrefix(l, "-Xss") && strings.HasPrefix(r, "-Xss")
isXXOptions := strings.HasPrefix(l, "-XX:") && strings.HasPrefix(r, "-XX:") &&
strings.Split(l, "=")[0] == strings.Split(r, "=")[0]
return isEqual || isXmx || isXms || isXss || isXXOptions
}
// Gitpod's default customization
var gitpodVMOptions []string
gitpodVMOptions = append(gitpodVMOptions, "-Dgtw.disable.exit.dialog=true")
// temporary disable auto-attach of the async-profiler to prevent JVM crash
// see https://youtrack.jetbrains.com/issue/IDEA-326201/SIGSEGV-on-startup-2023.2-IDE-backend-on-gitpod.io?s=SIGSEGV-on-startup-2023.2-IDE-backend-on-gitpod.io
gitpodVMOptions = append(gitpodVMOptions, "-Dfreeze.reporter.profiling=false")
if alias == "intellij" {
gitpodVMOptions = append(gitpodVMOptions, "-Djdk.configure.existing=true")
}
// container relevant options
gitpodVMOptions = append(gitpodVMOptions, "-XX:+UseContainerSupport")
cpuCount := os.Getenv("GITPOD_CPU_COUNT")
parsedCPUCount, err := strconv.Atoi(cpuCount)
// if CPU count is set and is parseable as a positive number
if err == nil && parsedCPUCount > 0 && parsedCPUCount <= 16 {
gitpodVMOptions = append(gitpodVMOptions, "-XX:ActiveProcessorCount="+cpuCount)
}
memory := os.Getenv("GITPOD_MEMORY")
parsedMemory, err := strconv.Atoi(memory)
if err == nil && parsedMemory > 0 {
xmx := (float64(parsedMemory) * 0.6)
if xmx > maxDefaultXmx { // 8G
xmx = maxDefaultXmx
}
if xmx > minDefaultXmx {
gitpodVMOptions = append(gitpodVMOptions, fmt.Sprintf("-Xmx%dm", int(xmx)))
}
}
vmoptions := deduplicateVMOption(ideaVMOptionsLines, gitpodVMOptions, filterFunc)
// user-defined vmoptions (EnvVar)
userVMOptionsVar := os.Getenv(strings.ToUpper(alias) + "_VMOPTIONS")
userVMOptions := strings.Fields(userVMOptionsVar)
if len(userVMOptions) > 0 {
vmoptions = deduplicateVMOption(vmoptions, userVMOptions, filterFunc)
}
// project-defined vmoptions (.gitpod.yml)
if config != nil {
productConfig := getProductConfig(config, alias)
if productConfig != nil {
projectVMOptions := strings.Fields(productConfig.Vmoptions)
if len(projectVMOptions) > 0 {
vmoptions = deduplicateVMOption(vmoptions, projectVMOptions, filterFunc)
}
}
}
return vmoptions
}
/*
*
{
"buildNumber" : "221.4994.44",
"customProperties" : [ ],
"dataDirectoryName" : "IntelliJIdea2022.1",
"launch" : [ {
"javaExecutablePath" : "jbr/bin/java",
"launcherPath" : "bin/idea.sh",
"os" : "Linux",
"startupWmClass" : "jetbrains-idea",
"vmOptionsFilePath" : "bin/idea64.vmoptions"
} ],
"name" : "IntelliJ IDEA",
"productCode" : "IU",
"svgIconPath" : "bin/idea.svg",
"version" : "2022.1",
"versionSuffix" : "EAP"
}
*/
type ProductInfo struct {
BuildNumber string `json:"buildNumber"`
Version string `json:"version"`
ProductCode string `json:"productCode"`
}
func resolveProductInfo(backendDir string) (*ProductInfo, error) {
f, err := os.Open(backendDir + "/product-info.json")
if err != nil {
return nil, err
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
var info ProductInfo
err = json.Unmarshal(content, &info)
return &info, err
}
type SyncTarget string
const (
Options SyncTarget = "options"
Plugins SyncTarget = "plugins"
)
func syncInitialContent(launchCtx *LaunchContext, target SyncTarget) (bool, error) {
destDir, err, alreadySynced := ensureInitialSyncDest(launchCtx, target)
if alreadySynced {
log.Infof("initial %s is already synced, skipping", target)
return alreadySynced, nil
}
if err != nil {
return alreadySynced, err
}
srcDirs, err := collectSyncSources(launchCtx, target)
if err != nil {
return alreadySynced, err
}
if len(srcDirs) == 0 {
// nothing to sync
return alreadySynced, nil
}
for _, srcDir := range srcDirs {
if target == Plugins {
files, err := ioutil.ReadDir(srcDir)
if err != nil {
return alreadySynced, err
}
for _, file := range files {
err := syncPlugin(file, srcDir, destDir)
if err != nil {
log.WithError(err).WithField("file", file.Name()).WithField("srcDir", srcDir).WithField("destDir", destDir).Error("failed to sync plugin")
}
}
} else {
cp := exec.Command("cp", "-rf", srcDir+"/.", destDir)
err = cp.Run()
if err != nil {
return alreadySynced, err
}
}
}
return alreadySynced, nil
}
func syncPlugin(file fs.FileInfo, srcDir, destDir string) error {
if file.IsDir() {
_, err := os.Stat(filepath.Join(destDir, file.Name()))
if !os.IsNotExist(err) {
log.WithField("plugin", file.Name()).Info("plugin is already synced, skipping")
return nil
}
return exec.Command("cp", "-rf", filepath.Join(srcDir, file.Name()), destDir).Run()