diff --git a/cluster/prov_onpremise_haproxy.go b/cluster/prov_onpremise_haproxy.go index 3a51a7dfe..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" ) @@ -44,25 +46,27 @@ 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 } strOut = string(out) } else { - var r, stdout, stderr bytes.Buffer + var 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()) 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() @@ -80,25 +84,27 @@ 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 } strOut = string(out) } else { - var r, stdout, stderr bytes.Buffer + var 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()) 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 12c6a61fb..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" ) @@ -45,16 +47,16 @@ 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 } strOut = string(out) } else { - var r, stdout, stderr bytes.Buffer + var 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()) @@ -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() @@ -85,16 +89,16 @@ 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 } strOut = string(out) } else { - var r, stdout, stderr bytes.Buffer + var 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()) @@ -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/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" { 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