github.com/caos/orbos@v1.5.14-0.20221103111702-e6cd0cea7ad4/cmd/orbctl/configure.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/caos/orbos/mntr"
     8  	"github.com/caos/orbos/pkg/cfg"
     9  	"github.com/caos/orbos/pkg/git"
    10  	"github.com/caos/orbos/pkg/kubernetes/cli"
    11  	"github.com/caos/orbos/pkg/orb"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  func ConfigCommand(getRv GetRootValues) *cobra.Command {
    16  
    17  	var (
    18  		newMasterKey string
    19  		newRepoURL   string
    20  		newRepoKey   string
    21  		cmd          = &cobra.Command{
    22  			Use:     "configure",
    23  			Short:   "Configures and reconfigures an orb",
    24  			Long:    "Generates missing ssh keys and other secrets where it makes sense",
    25  			Aliases: []string{"reconfigure", "config", "reconfig"},
    26  		}
    27  	)
    28  
    29  	flags := cmd.Flags()
    30  	flags.StringVar(&newMasterKey, "masterkey", "", "Reencrypts all secrets")
    31  	flags.StringVar(&newRepoURL, "repourl", "", "Configures the repository URL")
    32  	flags.StringVar(&newRepoKey, "repokey", "", "Configures the used key to communicate with the repository")
    33  
    34  	cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
    35  
    36  		rv := getRv("configure", "", map[string]interface{}{"masterkey": newMasterKey != "", "newRepoURL": newRepoURL})
    37  		defer rv.ErrFunc(err)
    38  
    39  		if !rv.Gitops {
    40  			return mntr.ToUserError(errors.New("configure command is only supported with the --gitops flag"))
    41  		}
    42  
    43  		if err := orb.Reconfigure(
    44  			rv.Ctx,
    45  			rv.Monitor,
    46  			rv.OrbConfig,
    47  			newRepoURL,
    48  			newMasterKey,
    49  			newRepoKey,
    50  			rv.GitClient,
    51  			githubClientID,
    52  			githubClientSecret,
    53  		); err != nil {
    54  			return err
    55  		}
    56  
    57  		var uninitialized bool
    58  		k8sClient, err := cli.Init(rv.Monitor, rv.OrbConfig, rv.GitClient, rv.Kubeconfig, rv.Gitops, false, false)
    59  		if err != nil {
    60  			if !errors.Is(err, cli.ErrNotInitialized) {
    61  				return err
    62  			}
    63  			uninitialized = true
    64  			err = nil
    65  		}
    66  
    67  		unmanagedOperators := []git.DesiredFile{git.DatabaseFile, git.ZitadelFile}
    68  		for i := range unmanagedOperators {
    69  			operatorFile := unmanagedOperators[i]
    70  			if rv.GitClient.Exists(operatorFile) {
    71  				return mntr.ToUserError(fmt.Errorf("found %s in git repository. Please use zitadelctl's configure command", operatorFile))
    72  			}
    73  		}
    74  
    75  		if !uninitialized {
    76  			if err := cfg.ApplyOrbconfigSecret(
    77  				rv.OrbConfig,
    78  				k8sClient,
    79  				rv.Monitor,
    80  			); err != nil {
    81  				return err
    82  			}
    83  		}
    84  
    85  		return cfg.ConfigureOperators(
    86  			rv.GitClient,
    87  			rv.OrbConfig.Masterkey,
    88  			cfg.ORBOSConfigurers(
    89  				rv.Ctx,
    90  				rv.Monitor,
    91  				rv.OrbConfig,
    92  				rv.GitClient,
    93  			))
    94  	}
    95  	return cmd
    96  }