Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[installer]: create config command and deprecate init command #12679

Merged
merged 4 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .werft/jobs/build/installer/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions install/installer/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ installer
dist
versions.yaml
!cmd/testdata/render/versions.yaml
gitpod.config.yaml
2 changes: 1 addition & 1 deletion install/installer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions install/installer/cmd/config.go
Original file line number Diff line number Diff line change
@@ -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")
}
50 changes: 50 additions & 0 deletions install/installer/cmd/config_init.go
Original file line number Diff line number Diff line change
@@ -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")
}
5 changes: 3 additions & 2 deletions install/installer/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion install/installer/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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() {
Expand Down Expand Up @@ -71,3 +84,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
}
1 change: 1 addition & 0 deletions install/installer/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ certificate:
name: https-certificates
containerRegistry:
inCluster: true
privateBaseImageAllowList: []
database:
inCluster: true
disableDefinitelyGp: true
Expand Down