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

[WIP] Add "wizard createconfig" command to easily populate configuration files #743

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
25 changes: 25 additions & 0 deletions cmd/ltctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
83 changes: 83 additions & 0 deletions cmd/ltctl/wizard.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions deployment/infrastructure.go
Original file line number Diff line number Diff line change
@@ -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",
},
},
}
Loading