Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/preview/prettylog/main.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
/// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package main
6
7
import (
8
"errors"
9
"fmt"
10
"io"
11
"log"
12
"os"
13
"strings"
14
15
"github.com/hpcloud/tail"
16
"github.com/pterm/pterm"
17
"gopkg.in/segmentio/analytics-go.v3"
18
)
19
20
const (
21
telemetryEvent = "localpreview_status"
22
)
23
24
var (
25
segmentIOToken string
26
msgs = []struct {
27
Fail string
28
Success string
29
Status string
30
31
Msg string
32
}{
33
{Msg: "checking prerequisites", Fail: "requires a system with at least", Success: "Gitpod Domain:", Status: "checking prerequisites"},
34
{Msg: "preparing system", Success: "extracting images to download ahead"},
35
{Msg: "downloading images", Success: "images pulled"},
36
{Msg: "preparing Gitpod preview installation", Success: "manifests generated"},
37
{Msg: "starting Gitpod", Success: "Gitpod pods are ready", Status: "starting gitpod"},
38
{Msg: fmt.Sprintf("Gitpod is running. Visit https://%s to access the dashboard", os.Getenv("DOMAIN")), Status: "gitpod ready"},
39
}
40
)
41
42
func main() {
43
// Warn and wait for user approval
44
pterm.FgLightCyan.Println(`
45
Welcome to the local preview of Gitpod. Please note the following limitations:
46
- Performance is limited by the capabilities of your machine - a minimum of 4 cores and 6GB of RAM are required
47
- ARM CPUs including Macs with Apple Silicon (e.g. M1) are currently not supported
48
For more information about these limitation, please visit the local preview documentation: https://www.gitpod.io/docs/self-hosted/latest/local-preview`)
49
50
result, _ := pterm.DefaultInteractiveConfirm.WithDefaultText("Continue?").WithDefaultValue(true).Show()
51
if !result {
52
// send telemetry for user exit
53
send_telemetry("user exit")
54
return
55
}
56
57
file, err := tail.TailFile("logs.txt", tail.Config{Follow: true})
58
if err != nil {
59
log.Fatal(err)
60
}
61
62
var msgIdx int
63
lastSpinner, _ := pterm.DefaultSpinner.Start(msgs[msgIdx].Msg)
64
// send Telemetry update for the first phase
65
send_telemetry(msgs[msgIdx].Status)
66
for tailLine := range file.Lines {
67
line := tailLine.Text
68
msg := msgs[msgIdx]
69
var next bool
70
switch {
71
case msg.Fail != "" && strings.Contains(line, msg.Fail):
72
lastSpinner.Fail()
73
pterm.FgLightRed.Println("Failed with error: " + line)
74
return
75
case msg.Success != "" && strings.Contains(line, msg.Success):
76
lastSpinner.Success()
77
next = true
78
}
79
80
if !next {
81
continue
82
}
83
84
msgIdx++
85
if msgIdx >= len(msgs) {
86
return
87
}
88
lastSpinner, _ = pterm.DefaultSpinner.Start(msgs[msgIdx].Msg)
89
// send Telemetry for phase update
90
send_telemetry(msgs[msgIdx].Status)
91
92
}
93
err = file.Err()
94
if errors.Is(err, io.EOF) {
95
err = nil
96
}
97
if err != nil {
98
panic(err)
99
}
100
}
101
102
func send_telemetry(status string) {
103
if os.Getenv("DO_NOT_TRACK") != "1" && status != "" {
104
if segmentIOToken == "" {
105
panic("No segmentIOToken set during build")
106
}
107
108
client, _ := analytics.NewWithConfig(segmentIOToken, analytics.Config{})
109
defer func() {
110
client.Close()
111
112
}()
113
114
telemetry := analytics.Track{
115
UserId: os.Getenv("USER_ID"),
116
Event: telemetryEvent,
117
Properties: analytics.NewProperties().
118
Set("status", status),
119
}
120
client.Enqueue(telemetry)
121
}
122
}
123
124