forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql.go
103 lines (93 loc) · 3.21 KB
/
mysql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package db
import (
"fmt"
"os/exec"
"strings"
"github.com/openshift/origin/test/extended/util"
)
// MySQL is a MySQL helper for executing commands.
type MySQL struct {
podName string
masterPodName string
}
// NewMysql creates a new util.Database instance.
func NewMysql(podName, masterPodName string) util.Database {
if masterPodName == "" {
masterPodName = podName
}
return &MySQL{
podName: podName,
masterPodName: masterPodName,
}
}
// PodName implements Database.
func (m MySQL) PodName() string {
return m.podName
}
// IsReady pings the MySQL server.
func (m MySQL) IsReady(oc *util.CLI) (bool, error) {
conf, err := getPodConfig(oc.KubeClient().CoreV1().Pods(oc.Namespace()), m.podName)
if err != nil {
return false, err
}
masterConf, err := getPodConfig(oc.KubeClient().CoreV1().Pods(oc.Namespace()), m.masterPodName)
if err != nil {
return false, err
}
out, err := oc.Run("exec").Args(m.podName, "-c", conf.Container, "--", "bash", "-c",
fmt.Sprintf("mysqladmin -h localhost -u%s -p%s ping", masterConf.Env["MYSQL_USER"], masterConf.Env["MYSQL_PASSWORD"])).Output()
if err != nil {
switch err.(type) {
case *util.ExitError, *exec.ExitError:
return false, nil
default:
return false, err
}
}
return strings.Contains(out, "mysqld is alive"), nil
}
// Query executes an SQL query as an ordinary user and returns the result.
func (m MySQL) Query(oc *util.CLI, query string) (string, error) {
container, err := firstContainerName(oc.KubeClient().CoreV1().Pods(oc.Namespace()), m.podName)
if err != nil {
return "", err
}
masterConf, err := getPodConfig(oc.KubeClient().CoreV1().Pods(oc.Namespace()), m.masterPodName)
if err != nil {
return "", err
}
return oc.Run("exec").Args(m.podName, "-c", container, "--", "bash", "-c",
fmt.Sprintf("mysql -h 127.0.0.1 -u%s -p%s -e \"%s\" %s",
masterConf.Env["MYSQL_USER"], masterConf.Env["MYSQL_PASSWORD"], query,
masterConf.Env["MYSQL_DATABASE"])).Output()
}
// QueryPrivileged executes an SQL query as a root user and returns the result.
func (m MySQL) QueryPrivileged(oc *util.CLI, query string) (string, error) {
container, err := firstContainerName(oc.KubeClient().CoreV1().Pods(oc.Namespace()), m.podName)
if err != nil {
return "", err
}
masterConf, err := getPodConfig(oc.KubeClient().CoreV1().Pods(oc.Namespace()), m.masterPodName)
if err != nil {
return "", err
}
return oc.Run("exec").Args(m.podName, "-c", container, "--", "bash", "-c",
fmt.Sprintf("mysql -h 127.0.0.1 -uroot -e \"%s\" %s",
query, masterConf.Env["MYSQL_DATABASE"])).Output()
}
// TestRemoteLogin will test whether we can login through to a remote database.
func (m MySQL) TestRemoteLogin(oc *util.CLI, hostAddress string) error {
container, err := firstContainerName(oc.KubeClient().CoreV1().Pods(oc.Namespace()), m.podName)
if err != nil {
return err
}
masterConf, err := getPodConfig(oc.KubeClient().CoreV1().Pods(oc.Namespace()), m.masterPodName)
if err != nil {
return err
}
err = oc.Run("exec").Args(m.podName, "-c", container, "--", "bash", "-c",
fmt.Sprintf("mysql -h %s -u%s -p%s -e \"SELECT 1;\" %s",
hostAddress, masterConf.Env["MYSQL_USER"], masterConf.Env["MYSQL_PASSWORD"],
masterConf.Env["MYSQL_DATABASE"])).Execute()
return err
}