From a5e6f322787c9f79fb5a457a3ba4147090db42a8 Mon Sep 17 00:00:00 2001 From: Felipe Martin Date: Fri, 10 May 2024 17:29:20 +0200 Subject: [PATCH] wizard createconfig command --- cmd/ltctl/main.go | 25 +++++++++++ cmd/ltctl/wizard.go | 83 ++++++++++++++++++++++++++++++++++++ deployment/infrastructure.go | 26 +++++++++++ 3 files changed, 134 insertions(+) create mode 100644 cmd/ltctl/wizard.go create mode 100644 deployment/infrastructure.go diff --git a/cmd/ltctl/main.go b/cmd/ltctl/main.go index c17343eeb..9ac114dc3 100644 --- a/cmd/ltctl/main.go +++ b/cmd/ltctl/main.go @@ -420,6 +420,31 @@ func main() { comparisonCmd.AddCommand(runComparisonCmd, destroyComparisonCmd, collectComparisonCmd) rootCmd.AddCommand(comparisonCmd) + wizardCmd := &cobra.Command{ + Use: "wizard", + Short: "Wizard utilities to setup the", + PersistentPreRun: func(cmd *cobra.Command, _ []string) { setServiceEnv(cmd) }, + PersistentPostRun: func(_ *cobra.Command, _ []string) { os.Unsetenv("MM_SERVICEENVIRONMENT") }, + } + + wizardCreateConfigCmd := &cobra.Command{ + Use: "createconfig", + Short: "Create configuration files", + RunE: RunWizardCreateConfigF, + } + wizardCreateConfigCmd.Flags().Bool("create-config", true, "Create a config.json file") + wizardCreateConfigCmd.Flags().Bool("create-deployer", true, "Create a deployer.json file") + wizardCreateConfigCmd.Flags().Int("active-users", 1000, "Number of expected concurrent active users") + wizardCreateConfigCmd.Flags().Bool("with-elasticsearch", false, "Enable Elasticsearch") + wizardCreateConfigCmd.Flags().Bool("with-keycloak", false, "Enable Keycloak") + // cmd.Flags().String("license-file-path", "", "Path to the license file") + // cmd.MarkFlagRequired("license-file-path") + + wizardCmdCommands := []*cobra.Command{wizardCreateConfigCmd} + + wizardCmd.AddCommand(wizardCmdCommands...) + rootCmd.AddCommand(wizardCmd) + if err := rootCmd.Execute(); err != nil { os.Exit(1) } diff --git a/cmd/ltctl/wizard.go b/cmd/ltctl/wizard.go new file mode 100644 index 000000000..60089a334 --- /dev/null +++ b/cmd/ltctl/wizard.go @@ -0,0 +1,83 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/mattermost/mattermost-load-test-ng/defaults" + "github.com/mattermost/mattermost-load-test-ng/deployment" + "github.com/mattermost/mattermost-load-test-ng/loadtest" + "github.com/spf13/cobra" +) + +func RunWizardCreateConfigF(cmd *cobra.Command, args []string) error { + activeUsers, err := cmd.Flags().GetInt("active-users") + if err != nil { + return fmt.Errorf("failed to get active users flag: %w", err) + } + + arch, ok := deployment.Architectures[activeUsers] + if !ok { + var keys []string + for k := range deployment.Architectures { + keys = append(keys, fmt.Sprintf("%d", k)) + } + return fmt.Errorf("reference architecture for %d users not found. Use one of: %s", activeUsers, keys) + } + + createDeployerConfig, err := cmd.Flags().GetBool("create-deployer") + if err != nil { + return fmt.Errorf("failed to get create deployer flag: %w", err) + } + + if createDeployerConfig { + deployerConfig := deployment.Config{} + defaults.Set(&deployerConfig) + + deployerConfig.AppInstanceCount = arch.AppServers.Count + deployerConfig.AppInstanceType = arch.AppServers.InstanceType + + deployerConfig.TerraformDBSettings.InstanceCount = arch.DatabaseServer.Count + deployerConfig.TerraformDBSettings.InstanceType = arch.DatabaseServer.InstanceType + + if err := writeToFile("./config/deployer.json", deployerConfig); err != nil { + return fmt.Errorf("failed to write deployer config: %w", err) + } + } + + createConfig, err := cmd.Flags().GetBool("create-config") + if err != nil { + return fmt.Errorf("failed to get create deployer flag: %w", err) + } + + if createConfig { + config := loadtest.Config{} + defaults.Set(&config) + + if err := writeToFile("./config/config.json", config); err != nil { + return fmt.Errorf("failed to write config: %w", err) + } + } + + return nil +} + +func writeToFile(filename string, cfg any) error { + data, err := json.MarshalIndent(cfg, "", " ") + if err != nil { + return fmt.Errorf("failed to marshal config: %w", err) + } + + f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) + if err != nil { + return fmt.Errorf("failed to open %s: %w", filename, err) + } + defer f.Close() + + if _, err := f.Write(data); err != nil { + return fmt.Errorf("failed to write %s: %w", filename, err) + } + + return nil +} diff --git a/deployment/infrastructure.go b/deployment/infrastructure.go new file mode 100644 index 000000000..9fe3c1e12 --- /dev/null +++ b/deployment/infrastructure.go @@ -0,0 +1,26 @@ +package deployment + +type Server struct { + Count int + InstanceType string +} + +type ReferenceArchitecture struct { + ActiveUsers int + AppServers Server + DatabaseServer Server +} + +var Architectures = map[int]ReferenceArchitecture{ + 100: { + ActiveUsers: 100, + AppServers: Server{ + Count: 1, + InstanceType: "c6i.large", + }, + DatabaseServer: Server{ + Count: 1, + InstanceType: "db.r6g.large", + }, + }, +}