forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostgresql.go
99 lines (90 loc) · 3.2 KB
/
postgresql.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
package db
import (
"fmt"
"os/exec"
"strings"
"github.com/openshift/origin/test/extended/util"
)
// PostgreSQL is a PostgreSQL helper for executing commands.
type PostgreSQL struct {
podName string
masterPodName string
}
// NewPostgreSQL creates a new util.Database instance.
func NewPostgreSQL(podName, masterPodName string) util.Database {
if masterPodName == "" {
masterPodName = podName
}
return &PostgreSQL{
podName: podName,
masterPodName: masterPodName,
}
}
// PodName implements Database.
func (m PostgreSQL) PodName() string {
return m.podName
}
// IsReady pings the PostgreSQL server.
func (m PostgreSQL) IsReady(oc *util.CLI) (bool, error) {
conf, err := getPodConfig(oc.KubeClient().CoreV1().Pods(oc.Namespace()), m.podName)
if err != nil {
return false, err
}
out, err := oc.Run("exec").Args(m.podName, "-c", conf.Container, "--", "bash", "-c",
"psql postgresql://postgres@127.0.0.1 -x -c \"SELECT 1;\"").Output()
if err != nil {
switch err.(type) {
case *util.ExitError, *exec.ExitError:
return false, nil
default:
return false, err
}
}
return strings.Contains(out, "-[ RECORD 1 ]\n?column? | 1"), nil
}
// Query executes an SQL query as an ordinary user and returns the result.
func (m PostgreSQL) 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("psql postgres://%s:%s@127.0.0.1/%s -x -c \"%s\"",
masterConf.Env["POSTGRESQL_USER"], masterConf.Env["POSTGRESQL_PASSWORD"],
masterConf.Env["POSTGRESQL_DATABASE"], query)).Output()
}
// QueryPrivileged executes an SQL query as a root user and returns the result.
func (m PostgreSQL) 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("psql postgres://postgres:%s@127.0.0.1/%s -x -c \"%s\"",
masterConf.Env["POSTGRESQL_ADMIN_PASSWORD"],
masterConf.Env["POSTGRESQL_DATABASE"], query)).Output()
}
// TestRemoteLogin will test whether we can login through to a remote database.
func (m PostgreSQL) 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("psql postgres://%s:%s@%s/%s -x -c \"SELECT 1;\"",
masterConf.Env["POSTGRESQL_USER"], masterConf.Env["POSTGRESQL_PASSWORD"],
hostAddress, masterConf.Env["POSTGRESQL_DATABASE"])).Execute()
return err
}