Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/cmd/root.go
2498 views
1
// Copyright (c) 2021 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
cryptoRand "crypto/rand"
9
"math/rand"
10
"os"
11
"os/user"
12
"path/filepath"
13
"strings"
14
15
"github.com/gitpod-io/gitpod/common-go/log"
16
"github.com/gitpod-io/gitpod/installer/pkg/common"
17
"github.com/sirupsen/logrus"
18
"github.com/spf13/cobra"
19
)
20
21
// rootCmd represents the base command when called without any subcommands
22
var rootCmd = &cobra.Command{
23
Use: "gitpod-installer",
24
Short: "Installs Gitpod",
25
}
26
27
func Execute() {
28
cobra.CheckErr(rootCmd.Execute())
29
}
30
31
var rootOpts struct {
32
VersionMF string
33
StrictConfigParse bool
34
SeedValue int64
35
LogLevel string
36
}
37
38
func init() {
39
cobra.OnInitialize(setSeed, setLogLevel)
40
rootCmd.PersistentFlags().StringVar(&rootOpts.VersionMF, "debug-version-file", "", "path to a version manifest - not intended for production use")
41
rootCmd.PersistentFlags().Int64Var(&rootOpts.SeedValue, "seed", 0, "specify the seed value for randomization - if 0 it is kept as the default")
42
rootCmd.PersistentFlags().BoolVar(&rootOpts.StrictConfigParse, "strict-parse", true, "toggle strict configuration parsing")
43
rootCmd.PersistentFlags().StringVar(&rootOpts.LogLevel, "log-level", "info", "set the log level")
44
}
45
46
func setLogLevel() {
47
newLevel, err := logrus.ParseLevel(rootOpts.LogLevel)
48
if err != nil {
49
log.WithError(err).Errorf("cannot change log level to '%v'", rootOpts.LogLevel)
50
return
51
}
52
log.Log.Logger.SetLevel(newLevel)
53
}
54
55
func setSeed() {
56
if rootOpts.SeedValue != 0 {
57
rand.Seed(rootOpts.SeedValue)
58
59
// crypto/rand is used by the bcrypt package to generate its random values
60
str, err := common.RandomString(128)
61
if err != nil {
62
panic(err)
63
}
64
cryptoRand.Reader = strings.NewReader(str)
65
}
66
}
67
68
type kubeConfig struct {
69
Config string
70
}
71
72
// checkKubeConfig performs validation on the Kubernetes struct and fills in default values if necessary
73
func checkKubeConfig(kube *kubeConfig) error {
74
if kube.Config == "" {
75
kube.Config = os.Getenv("KUBECONFIG")
76
}
77
if kube.Config == "" {
78
u, err := user.Current()
79
if err != nil {
80
return err
81
}
82
kube.Config = filepath.Join(u.HomeDir, ".kube", "config")
83
}
84
85
return nil
86
}
87
88
// getEnvvar gets an envvar and allows a default value
89
func getEnvvar(key, fallback string) string {
90
if value, ok := os.LookupEnv(key); ok {
91
return value
92
}
93
return fallback
94
}
95
96