Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/cmd/root.go
2498 views
1
// Copyright (c) 2020 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 cmd
6
7
import (
8
"fmt"
9
"io"
10
"os"
11
"time"
12
13
"github.com/sirupsen/logrus"
14
"github.com/spf13/cobra"
15
16
"github.com/gitpod-io/gitpod/common-go/log"
17
prefixed "github.com/x-cray/logrus-prefixed-formatter"
18
)
19
20
// rootCmd represents the base command when called without any subcommands.
21
var rootCmd = &cobra.Command{
22
Use: "supervisor",
23
Short: "Workspace container init process",
24
}
25
26
var (
27
// ServiceName is the name we use for tracing/logging.
28
ServiceName = "supervisor"
29
// Version of this service - set during build.
30
Version = ""
31
)
32
33
// Execute adds all child commands to the root command and sets flags appropriately.
34
// This is called by main.main(). It only needs to happen once to the rootCmd.
35
func Execute() {
36
log.Init(ServiceName, Version, true, false)
37
38
if err := rootCmd.Execute(); err != nil {
39
fmt.Println(err)
40
os.Exit(1)
41
}
42
}
43
44
type fatalTerminationLogHook struct{}
45
46
func (fatalTerminationLogHook) Levels() []logrus.Level {
47
return []logrus.Level{logrus.FatalLevel}
48
}
49
50
func (fatalTerminationLogHook) Fire(e *logrus.Entry) error {
51
msg := e.Message
52
if err := e.Data[logrus.ErrorKey]; err != nil {
53
msg += ": " + err.(error).Error()
54
}
55
56
return os.WriteFile("/dev/termination-log", []byte(msg), 0o644)
57
}
58
59
const (
60
gitpodLogDir = "/var/log/gitpod"
61
supervisorLogFilePath = gitpodLogDir + "/supervisor.log"
62
)
63
64
func initLog(json bool) io.Closer {
65
log.Init(ServiceName, Version, json, os.Getenv("SUPERVISOR_DEBUG_ENABLE") == "true")
66
log.Log.Logger.AddHook(fatalTerminationLogHook{})
67
if err := os.MkdirAll(gitpodLogDir, 0755); err != nil {
68
log.WithError(err).Error("cannot create gitpod log directory")
69
return io.NopCloser(nil)
70
}
71
supervisorLogFile, err := os.OpenFile(supervisorLogFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
72
if err != nil {
73
log.WithError(err).Error("cannot open supervisor log file")
74
return io.NopCloser(nil)
75
}
76
log.Log.Logger.AddHook(&FileHook{
77
Writer: supervisorLogFile,
78
Formatter: &prefixed.TextFormatter{
79
TimestampFormat: time.RFC3339Nano,
80
FullTimestamp: true,
81
},
82
})
83
return supervisorLogFile
84
}
85
86
type FileHook struct {
87
Writer io.Writer
88
Formatter logrus.Formatter
89
}
90
91
func (hook *FileHook) Fire(entry *logrus.Entry) error {
92
formatted, err := hook.Formatter.Format(entry)
93
if err != nil {
94
return err
95
}
96
_, err = hook.Writer.Write(formatted)
97
return err
98
}
99
100
func (hook *FileHook) Levels() []logrus.Level {
101
return logrus.AllLevels
102
}
103
104