From 1c513161907089a7f7f8b19828a43262d3e09a2e Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Tue, 23 Aug 2022 21:13:34 +0000 Subject: [PATCH 1/4] [installer]: add getEnvvar to root cmd --- install/installer/cmd/root.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/install/installer/cmd/root.go b/install/installer/cmd/root.go index 185794fa6dd640..72dd34dacda994 100644 --- a/install/installer/cmd/root.go +++ b/install/installer/cmd/root.go @@ -71,3 +71,11 @@ func checkKubeConfig(kube *kubeConfig) error { return nil } + +// getEnvvar gets an envvar and allows a default value +func getEnvvar(key, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + return fallback +} From acef6acb8cfe9f250d739b966838295b721d6012 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Wed, 24 Aug 2022 14:20:20 +0000 Subject: [PATCH 2/4] [installer]: add logging framework to the Installer --- install/installer/cmd/root.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/install/installer/cmd/root.go b/install/installer/cmd/root.go index 72dd34dacda994..0e483bd867bcbc 100644 --- a/install/installer/cmd/root.go +++ b/install/installer/cmd/root.go @@ -12,7 +12,9 @@ import ( "path/filepath" "strings" + "github.com/gitpod-io/gitpod/common-go/log" "github.com/gitpod-io/gitpod/installer/pkg/common" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -30,13 +32,24 @@ var rootOpts struct { VersionMF string StrictConfigParse bool SeedValue int64 + LogLevel string } func init() { - cobra.OnInitialize(setSeed) + cobra.OnInitialize(setSeed, setLogLevel) rootCmd.PersistentFlags().StringVar(&rootOpts.VersionMF, "debug-version-file", "", "path to a version manifest - not intended for production use") rootCmd.PersistentFlags().Int64Var(&rootOpts.SeedValue, "seed", 0, "specify the seed value for randomization - if 0 it is kept as the default") rootCmd.PersistentFlags().BoolVar(&rootOpts.StrictConfigParse, "strict-parse", true, "toggle strict configuration parsing") + rootCmd.PersistentFlags().StringVar(&rootOpts.LogLevel, "log-level", "info", "set the log level") +} + +func setLogLevel() { + newLevel, err := logrus.ParseLevel(rootOpts.LogLevel) + if err != nil { + log.WithError(err).Errorf("cannot change log level to '%v'", rootOpts.LogLevel) + return + } + log.Log.Logger.SetLevel(newLevel) } func setSeed() { From 72a7b0d510c34cd5c05087912e79e6dc72dbe749 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Tue, 23 Aug 2022 19:46:40 +0000 Subject: [PATCH 3/4] [installer]: create config command and deprecate init command --- install/installer/.gitignore | 1 + install/installer/README.md | 2 +- install/installer/cmd/config.go | 64 +++++++++++++++++++++++++++ install/installer/cmd/config_init.go | 50 +++++++++++++++++++++ install/installer/cmd/init.go | 5 ++- install/installer/example-config.yaml | 1 + 6 files changed, 120 insertions(+), 3 deletions(-) create mode 100644 install/installer/cmd/config.go create mode 100644 install/installer/cmd/config_init.go diff --git a/install/installer/.gitignore b/install/installer/.gitignore index 31702bfa3c15c8..eda36f96e4b817 100644 --- a/install/installer/.gitignore +++ b/install/installer/.gitignore @@ -2,3 +2,4 @@ installer dist versions.yaml !cmd/testdata/render/versions.yaml +gitpod.config.yaml diff --git a/install/installer/README.md b/install/installer/README.md index 986b6fa8f45de5..99f4056c054163 100644 --- a/install/installer/README.md +++ b/install/installer/README.md @@ -77,7 +77,7 @@ gitpod-installer version ## Generate the base config ```shell -gitpod-installer init > gitpod.config.yaml +gitpod-installer config init -c gitpod.config.yaml ``` ## Customise your config diff --git a/install/installer/cmd/config.go b/install/installer/cmd/config.go new file mode 100644 index 00000000000000..55aaf9e22b5506 --- /dev/null +++ b/install/installer/cmd/config.go @@ -0,0 +1,64 @@ +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. +// Licensed under the MIT License. See License-MIT.txt in the project root for license information. + +package cmd + +import ( + "errors" + "os" + "path/filepath" + + "github.com/gitpod-io/gitpod/common-go/log" + "github.com/gitpod-io/gitpod/installer/pkg/config" + "github.com/spf13/cobra" + "k8s.io/utils/pointer" +) + +var configOpts struct { + ConfigFile string +} + +// configCmd represents the validate command +var configCmd = &cobra.Command{ + Use: "config", + Short: "Perform configuration tasks", +} + +// configFileExists checks if the configuration file is present on disk +func configFileExists() (*bool, error) { + if _, err := os.Stat(configOpts.ConfigFile); err == nil { + return pointer.Bool(true), nil + } else if errors.Is(err, os.ErrNotExist) { + return pointer.Bool(false), nil + } else { + return nil, err + } +} + +// saveConfigFile converts the config to YAML and saves to disk +func saveConfigFile(cfg interface{}) error { + fc, err := config.Marshal(config.CurrentVersion, cfg) + if err != nil { + return err + } + + err = os.WriteFile(configOpts.ConfigFile, fc, 0644) + if err != nil { + return err + } + + log.Infof("File written to %s", configOpts.ConfigFile) + + return nil +} + +func init() { + rootCmd.AddCommand(configCmd) + + dir, err := os.Getwd() + if err != nil { + log.WithError(err).Fatal("Failed to get working directory") + } + + configCmd.PersistentFlags().StringVarP(&configOpts.ConfigFile, "config", "c", getEnvvar("CONFIG_FILE", filepath.Join(dir, "gitpod.config.yaml")), "path to the configuration file") +} diff --git a/install/installer/cmd/config_init.go b/install/installer/cmd/config_init.go new file mode 100644 index 00000000000000..1d358e89e41ab0 --- /dev/null +++ b/install/installer/cmd/config_init.go @@ -0,0 +1,50 @@ +// Copyright (c) 2022 Gitpod GmbH. All rights reserved. +// Licensed under the MIT License. See License-MIT.txt in the project root for license information. + +package cmd + +import ( + "fmt" + + "github.com/gitpod-io/gitpod/installer/pkg/config" + "github.com/spf13/cobra" +) + +var configInitOpts struct { + OverwriteConfig bool +} + +// configInitCmd represents the validate command +var configInitCmd = &cobra.Command{ + Use: "init", + Short: "Create a base config file", + Long: `Create a base config file + +This file contains all the credentials to install a Gitpod instance and +be saved to a repository.`, + Example: ` # Save config to config.yaml. +gitpod-installer config init -c ./gitpod.config.yaml`, + RunE: func(cmd *cobra.Command, args []string) error { + // Check file isn't present + exists, err := configFileExists() + if err != nil { + return err + } + if *exists && !configInitOpts.OverwriteConfig { + return fmt.Errorf("file %s exists - to overwrite add --overwrite flag", configOpts.ConfigFile) + } + + cfg, err := config.NewDefaultConfig() + if err != nil { + return err + } + + return saveConfigFile(cfg) + }, +} + +func init() { + configCmd.AddCommand(configInitCmd) + + configInitCmd.Flags().BoolVar(&configInitOpts.OverwriteConfig, "overwrite", false, "overwrite config file if it exists") +} diff --git a/install/installer/cmd/init.go b/install/installer/cmd/init.go index 1d4750cec05072..5ae6795a8f274c 100644 --- a/install/installer/cmd/init.go +++ b/install/installer/cmd/init.go @@ -13,8 +13,9 @@ import ( // initCmd represents the init command var initCmd = &cobra.Command{ - Use: "init", - Short: "Create a base config file", + Use: "init", + Deprecated: `use "config init" command instead`, + Short: "Create a base config file", Long: `Create a base config file This file contains all the credentials to install a Gitpod instance and diff --git a/install/installer/example-config.yaml b/install/installer/example-config.yaml index 6845d858504ea1..ebbf05f4fd70c1 100644 --- a/install/installer/example-config.yaml +++ b/install/installer/example-config.yaml @@ -8,6 +8,7 @@ certificate: name: https-certificates containerRegistry: inCluster: true + privateBaseImageAllowList: [] database: inCluster: true disableDefinitelyGp: true From d884eab0ac2e0cb4ce7114bb90f483b6d0281362 Mon Sep 17 00:00:00 2001 From: Simon Emms Date: Sat, 10 Sep 2022 15:27:35 +0000 Subject: [PATCH 4/4] [installer]: use new config init command in werft job --- .werft/jobs/build/installer/installer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.werft/jobs/build/installer/installer.ts b/.werft/jobs/build/installer/installer.ts index 5734549e560f55..bf611b1ff55eb4 100644 --- a/.werft/jobs/build/installer/installer.ts +++ b/.werft/jobs/build/installer/installer.ts @@ -52,7 +52,7 @@ export class Installer { { slice: slice }, ); exec(`chmod +x /tmp/installer`, { slice: slice }); - exec(`/tmp/installer init > ${this.options.installerConfigPath}`, { slice: slice }); + exec(`/tmp/installer config init --overwrite --config ${this.options.installerConfigPath}`, { slice: slice }); this.options.werft.done(slice); }