|
16 | 16 | package cmd |
17 | 17 |
|
18 | 18 | import ( |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "path" |
| 22 | + "strings" |
| 23 | + |
19 | 24 | "github.com/ProxySQL/dbdeployer/common" |
| 25 | + "github.com/ProxySQL/dbdeployer/defaults" |
20 | 26 | "github.com/ProxySQL/dbdeployer/globals" |
21 | 27 | "github.com/ProxySQL/dbdeployer/providers" |
| 28 | + "github.com/ProxySQL/dbdeployer/providers/postgresql" |
22 | 29 | "github.com/ProxySQL/dbdeployer/sandbox" |
23 | 30 | "github.com/spf13/cobra" |
24 | 31 | ) |
25 | 32 |
|
| 33 | +func deployMultipleNonMySQL(cmd *cobra.Command, args []string, providerName string) { |
| 34 | + flags := cmd.Flags() |
| 35 | + version := args[0] |
| 36 | + nodes, _ := flags.GetInt(globals.NodesLabel) |
| 37 | + |
| 38 | + p, err := providers.DefaultRegistry.Get(providerName) |
| 39 | + if err != nil { |
| 40 | + common.Exitf(1, "provider error: %s", err) |
| 41 | + } |
| 42 | + |
| 43 | + flavor, _ := flags.GetString(globals.FlavorLabel) |
| 44 | + if flavor != "" { |
| 45 | + common.Exitf(1, "--flavor is only valid with --provider=mysql") |
| 46 | + } |
| 47 | + |
| 48 | + if !providers.ContainsString(p.SupportedTopologies(), "multiple") { |
| 49 | + common.Exitf(1, "provider %q does not support topology \"multiple\"\nSupported topologies: %s", |
| 50 | + providerName, strings.Join(p.SupportedTopologies(), ", ")) |
| 51 | + } |
| 52 | + |
| 53 | + if err := p.ValidateVersion(version); err != nil { |
| 54 | + common.Exitf(1, "version validation failed: %s", err) |
| 55 | + } |
| 56 | + |
| 57 | + if _, err := p.FindBinary(version); err != nil { |
| 58 | + common.Exitf(1, "binaries not found: %s", err) |
| 59 | + } |
| 60 | + |
| 61 | + basePort := p.DefaultPorts().BasePort |
| 62 | + if providerName == "postgresql" { |
| 63 | + basePort, _ = postgresql.VersionToPort(version) |
| 64 | + } |
| 65 | + |
| 66 | + sandboxHome := defaults.Defaults().SandboxHome |
| 67 | + topologyDir := path.Join(sandboxHome, fmt.Sprintf("%s_multi_%d", providerName, basePort)) |
| 68 | + if common.DirExists(topologyDir) { |
| 69 | + common.Exitf(1, "sandbox directory %s already exists", topologyDir) |
| 70 | + } |
| 71 | + os.MkdirAll(topologyDir, 0755) |
| 72 | + |
| 73 | + skipStart, _ := flags.GetBool(globals.SkipStartLabel) |
| 74 | + |
| 75 | + for i := 1; i <= nodes; i++ { |
| 76 | + port := basePort + i |
| 77 | + freePort, err := common.FindFreePort(port, []int{}, 1) |
| 78 | + if err == nil { |
| 79 | + port = freePort |
| 80 | + } |
| 81 | + |
| 82 | + nodeDir := path.Join(topologyDir, fmt.Sprintf("node%d", i)) |
| 83 | + config := providers.SandboxConfig{ |
| 84 | + Version: version, |
| 85 | + Dir: nodeDir, |
| 86 | + Port: port, |
| 87 | + Host: "127.0.0.1", |
| 88 | + DbUser: "postgres", |
| 89 | + Options: map[string]string{}, |
| 90 | + } |
| 91 | + |
| 92 | + if _, err := p.CreateSandbox(config); err != nil { |
| 93 | + common.Exitf(1, "error creating node %d: %s", i, err) |
| 94 | + } |
| 95 | + |
| 96 | + if !skipStart { |
| 97 | + if err := p.StartSandbox(nodeDir); err != nil { |
| 98 | + common.Exitf(1, "error starting node %d: %s", i, err) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + fmt.Printf(" Node %d deployed in %s (port: %d)\n", i, nodeDir, port) |
| 103 | + } |
| 104 | + |
| 105 | + fmt.Printf("%s multiple sandbox (%d nodes) deployed in %s\n", providerName, nodes, topologyDir) |
| 106 | +} |
| 107 | + |
26 | 108 | func multipleSandbox(cmd *cobra.Command, args []string) { |
| 109 | + flags := cmd.Flags() |
| 110 | + providerName, _ := flags.GetString(globals.ProviderLabel) |
| 111 | + |
| 112 | + if providerName != "mysql" { |
| 113 | + deployMultipleNonMySQL(cmd, args, providerName) |
| 114 | + return |
| 115 | + } |
| 116 | + |
27 | 117 | var sd sandbox.SandboxDef |
28 | 118 | common.CheckOrigin(args) |
29 | | - flags := cmd.Flags() |
30 | 119 | sd, err := fillSandboxDefinition(cmd, args, false) |
31 | 120 | common.ErrCheckExitf(err, 1, "error filling sandbox definition") |
32 | 121 | // Validate version with provider |
@@ -69,4 +158,5 @@ Use the "unpack" command to get the tarball into the right directory. |
69 | 158 | func init() { |
70 | 159 | deployCmd.AddCommand(multipleCmd) |
71 | 160 | multipleCmd.PersistentFlags().IntP(globals.NodesLabel, "n", globals.NodesValue, "How many nodes will be installed") |
| 161 | + multipleCmd.PersistentFlags().String(globals.ProviderLabel, globals.ProviderValue, "Database provider (mysql, postgresql)") |
72 | 162 | } |
0 commit comments