From 560a0d9f4f655ea39f36a403ce8a8947c56b0d59 Mon Sep 17 00:00:00 2001 From: caffeinated92 Date: Fri, 9 Aug 2024 12:56:39 +0700 Subject: [PATCH 1/2] merge proxy start stop script --- cluster/prov_onpremise_haproxy.go | 8 ++++---- cluster/prov_onpremise_proxysql.go | 8 ++++---- config/config.go | 6 ++---- server/server.go | 6 ++---- 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/cluster/prov_onpremise_haproxy.go b/cluster/prov_onpremise_haproxy.go index 3a51a7dfe..d693f463f 100644 --- a/cluster/prov_onpremise_haproxy.go +++ b/cluster/prov_onpremise_haproxy.go @@ -44,7 +44,7 @@ func (cluster *Cluster) OnPremiseStopHaproxyService(server DatabaseProxy) error return err } defer client.Close() - if cluster.Conf.OnPremiseSSHStopHaproxyScript == "" { + if cluster.Conf.OnPremiseSSHStopProxyScript == "" { out, err := client.Cmd("systemctl stop haproxy").SmartOutput() if err != nil { return err @@ -53,7 +53,7 @@ func (cluster *Cluster) OnPremiseStopHaproxyService(server DatabaseProxy) error } else { var r, stdout, stderr bytes.Buffer - srcpath := cluster.Conf.OnPremiseSSHStopHaproxyScript + srcpath := cluster.Conf.OnPremiseSSHStopProxyScript filerc, err2 := os.Open(srcpath) if err2 != nil { cluster.LogModulePrintf(cluster.Conf.Verbose, config.ConstLogModProxy, config.LvlErr, "Failed to load start script %s for SSH, err : %s", srcpath, err2.Error()) @@ -80,7 +80,7 @@ func (cluster *Cluster) OnPremiseStartHaProxyService(server DatabaseProxy) error return err } defer client.Close() - if cluster.Conf.OnPremiseSSHStartHaproxyScript == "" { + if cluster.Conf.OnPremiseSSHStartProxyScript == "" { out, err := client.Cmd("systemctl start haproxy").SmartOutput() if err != nil { return err @@ -89,7 +89,7 @@ func (cluster *Cluster) OnPremiseStartHaProxyService(server DatabaseProxy) error } else { var r, stdout, stderr bytes.Buffer - srcpath := cluster.Conf.OnPremiseSSHStartHaproxyScript + srcpath := cluster.Conf.OnPremiseSSHStartProxyScript filerc, err2 := os.Open(srcpath) if err2 != nil { cluster.LogModulePrintf(cluster.Conf.Verbose, config.ConstLogModProxy, config.LvlErr, "Failed to load start script %s for SSH, err : %s", srcpath, err2.Error()) diff --git a/cluster/prov_onpremise_proxysql.go b/cluster/prov_onpremise_proxysql.go index 12c6a61fb..dd73bd67d 100644 --- a/cluster/prov_onpremise_proxysql.go +++ b/cluster/prov_onpremise_proxysql.go @@ -45,7 +45,7 @@ func (cluster *Cluster) OnPremiseStopProxySQLService(server DatabaseProxy) error return err } defer client.Close() - if cluster.Conf.OnPremiseSSHStopProxysqlScript == "" { + if cluster.Conf.OnPremiseSSHStopProxyScript == "" { out, err := client.Cmd("systemctl stop proxysql").SmartOutput() if err != nil { return err @@ -54,7 +54,7 @@ func (cluster *Cluster) OnPremiseStopProxySQLService(server DatabaseProxy) error } else { var r, stdout, stderr bytes.Buffer - srcpath := cluster.Conf.OnPremiseSSHStopProxysqlScript + srcpath := cluster.Conf.OnPremiseSSHStopProxyScript filerc, err2 := os.Open(srcpath) if err2 != nil { cluster.LogModulePrintf(cluster.Conf.Verbose, config.ConstLogModProxy, config.LvlErr, "Failed to load start script %s for SSH, err : %s", srcpath, err2.Error()) @@ -85,7 +85,7 @@ func (cluster *Cluster) OnPremiseStartProxySQLService(server DatabaseProxy) erro } defer client.Close() - if cluster.Conf.OnPremiseSSHStartProxysqlScript == "" { + if cluster.Conf.OnPremiseSSHStartProxyScript == "" { out, err := client.Cmd("systemctl start proxysql").SmartOutput() if err != nil { return err @@ -94,7 +94,7 @@ func (cluster *Cluster) OnPremiseStartProxySQLService(server DatabaseProxy) erro } else { var r, stdout, stderr bytes.Buffer - srcpath := cluster.Conf.OnPremiseSSHStartProxysqlScript + srcpath := cluster.Conf.OnPremiseSSHStartProxyScript filerc, err2 := os.Open(srcpath) if err2 != nil { cluster.LogModulePrintf(cluster.Conf.Verbose, config.ConstLogModProxy, config.LvlErr, "Failed to load start script %s for SSH, err : %s", srcpath, err2.Error()) diff --git a/config/config.go b/config/config.go index d0da82f63..0097b8463 100644 --- a/config/config.go +++ b/config/config.go @@ -445,10 +445,8 @@ type Config struct { OnPremiseSSHCredential string `mapstructure:"onpremise-ssh-credential" toml:"onpremise-ssh-credential" json:"onpremiseSshCredential"` OnPremiseSSHPrivateKey string `mapstructure:"onpremise-ssh-private-key" toml:"onpremise-ssh-private-key" json:"onpremiseSshPrivateKey"` OnPremiseSSHStartDbScript string `mapstructure:"onpremise-ssh-start-db-script" toml:"onpremise-ssh-start-db-script" json:"onpremiseSshStartDbScript"` - OnPremiseSSHStartProxysqlScript string `mapstructure:"onpremise-ssh-start-proxysql-script" toml:"onpremise-ssh-start-proxysql-script" json:"onpremiseSshStartProxysqlScript"` - OnPremiseSSHStopProxysqlScript string `mapstructure:"onpremise-ssh-stop-proxysql-script" toml:"onpremise-ssh-stop-proxysql-script" json:"onpremiseSshStopProxysqlScript"` - OnPremiseSSHStartHaproxyScript string `mapstructure:"onpremise-ssh-start-haproxy-script" toml:"onpremise-ssh-start-haproxy-script" json:"onpremiseSshStartHaproxyScript"` - OnPremiseSSHStopHaproxyScript string `mapstructure:"onpremise-ssh-stop-haproxy-script" toml:"onpremise-ssh-stop-haproxy-script" json:"onpremiseSshStopHaproxyScript"` + OnPremiseSSHStartProxyScript string `mapstructure:"onpremise-ssh-start-proxy-script" toml:"onpremise-ssh-start-proxy-script" json:"onpremiseSshStartProxyScript"` + OnPremiseSSHStopProxyScript string `mapstructure:"onpremise-ssh-stop-proxy-script" toml:"onpremise-ssh-stop-proxy-script" json:"onpremiseSshStopProxyScript"` OnPremiseSSHDbJobScript string `mapstructure:"onpremise-ssh-db-job-script" toml:"onpremise-ssh-db-job-script" json:"onpremiseSshDbJobScript"` ProvOpensvcP12Certificate string `mapstructure:"opensvc-p12-certificate" toml:"opensvc-p12-certificate" json:"opensvcP12Certificate"` ProvOpensvcP12Secret string `mapstructure:"opensvc-p12-secret" toml:"opensvc-p12-secret" json:"opensvcP12Secret"` diff --git a/server/server.go b/server/server.go index 22abaff00..d6eb7083f 100644 --- a/server/server.go +++ b/server/server.go @@ -755,10 +755,8 @@ func (repman *ReplicationManager) AddFlags(flags *pflag.FlagSet, conf *config.Co flags.IntVar(&conf.OnPremiseSSHPort, "onpremise-ssh-port", 22, "Connect to host via SSH using ssh port") flags.StringVar(&conf.OnPremiseSSHCredential, "onpremise-ssh-credential", "root:", "User:password for ssh if no password using current user private key") flags.StringVar(&conf.OnPremiseSSHStartDbScript, "onpremise-ssh-start-db-script", "", "Run via ssh a custom script to start database") - flags.StringVar(&conf.OnPremiseSSHStartHaproxyScript, "onpremise-ssh-start-haproxy-script", "", "Run via ssh a custom script to start HAProxy") - flags.StringVar(&conf.OnPremiseSSHStopHaproxyScript, "onpremise-ssh-stop-haproxy-script", "", "Run via ssh a custom script to stop HAProxy") - flags.StringVar(&conf.OnPremiseSSHStartProxysqlScript, "onpremise-ssh-start-proxysql-script", "", "Run via ssh a custom script to start ProxySQL") - flags.StringVar(&conf.OnPremiseSSHStopProxysqlScript, "onpremise-ssh-stop-proxysql-script", "", "Run via ssh a custom script to stop ProxySQL") + flags.StringVar(&conf.OnPremiseSSHStartProxyScript, "onpremise-ssh-start-proxy-script", "", "Run via ssh a custom script to start Proxy") + flags.StringVar(&conf.OnPremiseSSHStopProxyScript, "onpremise-ssh-stop-proxy-script", "", "Run via ssh a custom script to stop Proxy") flags.StringVar(&conf.OnPremiseSSHDbJobScript, "onpremise-ssh-db-job-script", "", "Run via ssh a custom script to execute database jobs") if WithProvisioning == "ON" { From 4e698df5bbd3c63802b25894fb156705d32bd819 Mon Sep 17 00:00:00 2001 From: caffeinated92 Date: Fri, 9 Aug 2024 13:28:14 +0700 Subject: [PATCH 2/2] use single script for proxies --- cluster/prov_onpremise_haproxy.go | 18 ++++++++++++------ cluster/prov_onpremise_proxysql.go | 18 ++++++++++++------ cluster/prx.go | 1 + cluster/prx_get.go | 20 ++++++++++++++++++++ share/scripts/proxy_start_stop | 20 ++++++++++++++++++++ 5 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 share/scripts/proxy_start_stop diff --git a/cluster/prov_onpremise_haproxy.go b/cluster/prov_onpremise_haproxy.go index d693f463f..408b895a7 100644 --- a/cluster/prov_onpremise_haproxy.go +++ b/cluster/prov_onpremise_haproxy.go @@ -2,7 +2,9 @@ package cluster import ( "bytes" + "io" "os" + "strings" "github.com/signal18/replication-manager/config" ) @@ -51,7 +53,7 @@ func (cluster *Cluster) OnPremiseStopHaproxyService(server DatabaseProxy) error } strOut = string(out) } else { - var r, stdout, stderr bytes.Buffer + var stdout, stderr bytes.Buffer srcpath := cluster.Conf.OnPremiseSSHStopProxyScript filerc, err2 := os.Open(srcpath) @@ -60,9 +62,11 @@ func (cluster *Cluster) OnPremiseStopHaproxyService(server DatabaseProxy) error return err2 } defer filerc.Close() - r.ReadFrom(filerc) - if err = client.Shell().SetStdio(&r, &stdout, &stderr).Start(); err != nil { + envBuf := strings.NewReader(server.GetSshEnv()) + r := io.MultiReader(envBuf, filerc) + + if err = client.Shell().SetStdio(r, &stdout, &stderr).Start(); err != nil { return err } strOut = stdout.String() @@ -87,7 +91,7 @@ func (cluster *Cluster) OnPremiseStartHaProxyService(server DatabaseProxy) error } strOut = string(out) } else { - var r, stdout, stderr bytes.Buffer + var stdout, stderr bytes.Buffer srcpath := cluster.Conf.OnPremiseSSHStartProxyScript filerc, err2 := os.Open(srcpath) @@ -96,9 +100,11 @@ func (cluster *Cluster) OnPremiseStartHaProxyService(server DatabaseProxy) error return err2 } defer filerc.Close() - r.ReadFrom(filerc) - if err = client.Shell().SetStdio(&r, &stdout, &stderr).Start(); err != nil { + envBuf := strings.NewReader(server.GetSshEnv()) + r := io.MultiReader(envBuf, filerc) + + if err = client.Shell().SetStdio(r, &stdout, &stderr).Start(); err != nil { return err } strOut = stdout.String() diff --git a/cluster/prov_onpremise_proxysql.go b/cluster/prov_onpremise_proxysql.go index dd73bd67d..0dd50c310 100644 --- a/cluster/prov_onpremise_proxysql.go +++ b/cluster/prov_onpremise_proxysql.go @@ -3,7 +3,9 @@ package cluster import ( "bytes" "errors" + "io" "os" + "strings" "github.com/signal18/replication-manager/config" ) @@ -52,7 +54,7 @@ func (cluster *Cluster) OnPremiseStopProxySQLService(server DatabaseProxy) error } strOut = string(out) } else { - var r, stdout, stderr bytes.Buffer + var stdout, stderr bytes.Buffer srcpath := cluster.Conf.OnPremiseSSHStopProxyScript filerc, err2 := os.Open(srcpath) @@ -62,9 +64,11 @@ func (cluster *Cluster) OnPremiseStopProxySQLService(server DatabaseProxy) error } defer filerc.Close() - r.ReadFrom(filerc) - if err = client.Shell().SetStdio(&r, &stdout, &stderr).Start(); err != nil { + envBuf := strings.NewReader(server.GetSshEnv()) + r := io.MultiReader(envBuf, filerc) + + if err = client.Shell().SetStdio(r, &stdout, &stderr).Start(); err != nil { return err } strOut = stdout.String() @@ -92,7 +96,7 @@ func (cluster *Cluster) OnPremiseStartProxySQLService(server DatabaseProxy) erro } strOut = string(out) } else { - var r, stdout, stderr bytes.Buffer + var stdout, stderr bytes.Buffer srcpath := cluster.Conf.OnPremiseSSHStartProxyScript filerc, err2 := os.Open(srcpath) @@ -102,9 +106,11 @@ func (cluster *Cluster) OnPremiseStartProxySQLService(server DatabaseProxy) erro } defer filerc.Close() - r.ReadFrom(filerc) - if err = client.Shell().SetStdio(&r, &stdout, &stderr).Start(); err != nil { + envBuf := strings.NewReader(server.GetSshEnv()) + r := io.MultiReader(envBuf, filerc) + + if err = client.Shell().SetStdio(r, &stdout, &stderr).Start(); err != nil { return err } strOut = stdout.String() diff --git a/cluster/prx.go b/cluster/prx.go index c2a36c375..4a10727a4 100644 --- a/cluster/prx.go +++ b/cluster/prx.go @@ -123,6 +123,7 @@ type DatabaseProxy interface { GetConfigDatadir() string GetConfigConfigdir() string GetEnv() map[string]string + GetSshEnv() string GetConfigProxyModule(variable string) string SendStats() error diff --git a/cluster/prx_get.go b/cluster/prx_get.go index fff4b2ec6..38a876cd4 100644 --- a/cluster/prx_get.go +++ b/cluster/prx_get.go @@ -398,3 +398,23 @@ func (p *Proxy) GetCluster() *Cluster { func (p *Proxy) GetURL() string { return p.GetHost() + ":" + p.GetPort() } + +func (p *Proxy) GetSshEnv() string { + /* + REPLICATION_MANAGER_USER + REPLICATION_MANAGER_PASSWORD + REPLICATION_MANAGER_URL + REPLICATION_MANAGER_CLUSTER_NAME + REPLICATION_MANAGER_HOST_NAME + REPLICATION_MANAGER_HOST_USER + REPLICATION_MANAGER_HOST_PASSWORD + REPLICATION_MANAGER_HOST_PORT + REPLICATION_MANAGER_HOST_TYPE + */ + adminuser := "admin" + adminpassword := "repman" + if user, ok := p.ClusterGroup.APIUsers[adminuser]; ok { + adminpassword = user.Password + } + return "export REPLICATION_MANAGER_HOST_USER=\"" + p.GetUser() + "\";export REPLICATION_MANAGER_HOST_PASSWORD=\"" + p.GetPass() + "\";export REPLICATION_MANAGER_URL=\"https://" + p.ClusterGroup.Conf.MonitorAddress + ":" + p.ClusterGroup.Conf.APIPort + "\";export REPLICATION_MANAGER_USER=\"" + adminuser + "\";export REPLICATION_MANAGER_PASSWORD=\"" + adminpassword + "\";export REPLICATION_MANAGER_HOST_NAME=\"" + p.GetHost() + "\";export REPLICATION_MANAGER_HOST_PORT=\"" + p.GetPort() + "\";export REPLICATION_MANAGER_HOST_TYPE=\"" + p.Type + "\";export REPLICATION_MANAGER_CLUSTER_NAME=\"" + p.ClusterGroup.Name + "\"\n" +} diff --git a/share/scripts/proxy_start_stop b/share/scripts/proxy_start_stop new file mode 100644 index 000000000..877ffbfb4 --- /dev/null +++ b/share/scripts/proxy_start_stop @@ -0,0 +1,20 @@ +#!/bin/bash +echo "REPLICATION_MANAGER_USER: $REPLICATION_MANAGER_USER" +echo "REPLICATION_MANAGER_PASSWORD: $REPLICATION_MANAGER_PASSWORD" +echo "REPLICATION_MANAGER_URL: $REPLICATION_MANAGER_URL" +echo "REPLICATION_MANAGER_CLUSTER_NAME: $REPLICATION_MANAGER_CLUSTER_NAME" +echo "REPLICATION_MANAGER_HOST_NAME: $REPLICATION_MANAGER_HOST_NAME" +echo "REPLICATION_MANAGER_HOST_USER: $REPLICATION_MANAGER_HOST_USER" +echo "REPLICATION_MANAGER_HOST_PASSWORD: $REPLICATION_MANAGER_HOST_PASSWORD" +echo "REPLICATION_MANAGER_HOST_PORT: $REPLICATION_MANAGER_HOST_PORT" +echo "REPLICATION_MANAGER_HOST_TYPE: $REPLICATION_MANAGER_HOST_TYPE" + +# Begin script here +# Please use separate file for start and stop script. This example only echo environment + +# For start script +# systemctl start ... +# +# +# For stop script +# systemctl stop ... \ No newline at end of file