Skip to content

Commit

Permalink
Merge pull request #805 from signal18/provision
Browse files Browse the repository at this point in the history
Start Stop script for On Premise Proxies
  • Loading branch information
caffeinated92 authored Aug 9, 2024
2 parents e89c635 + 4e698df commit a006444
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 28 deletions.
26 changes: 16 additions & 10 deletions cluster/prov_onpremise_haproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cluster

import (
"bytes"
"io"
"os"
"strings"

"github.com/signal18/replication-manager/config"
)
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
26 changes: 16 additions & 10 deletions cluster/prov_onpremise_proxysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package cluster
import (
"bytes"
"errors"
"io"
"os"
"strings"

"github.com/signal18/replication-manager/config"
)
Expand Down Expand Up @@ -45,26 +47,28 @@ 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())
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()
Expand All @@ -85,26 +89,28 @@ 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())
return errors.New("Cancel dbjob can't open script")

}
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()
Expand Down
1 change: 1 addition & 0 deletions cluster/prx.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type DatabaseProxy interface {
GetConfigDatadir() string
GetConfigConfigdir() string
GetEnv() map[string]string
GetSshEnv() string
GetConfigProxyModule(variable string) string
SendStats() error

Expand Down
20 changes: 20 additions & 0 deletions cluster/prx_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
6 changes: 2 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
6 changes: 2 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
20 changes: 20 additions & 0 deletions share/scripts/proxy_start_stop
Original file line number Diff line number Diff line change
@@ -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 ...

0 comments on commit a006444

Please sign in to comment.