-
Notifications
You must be signed in to change notification settings - Fork 13
/
mysqlsh.go
249 lines (212 loc) · 8.07 KB
/
mysqlsh.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2018 Oracle and/or its affiliates. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mysqlsh
import (
"bytes"
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"sync"
"github.com/golang/glog"
"github.com/pkg/errors"
utilexec "k8s.io/utils/exec"
"github.com/oracle/mysql-operator/pkg/cluster/innodb"
)
// Interface is an injectable interface for running mysqlsh commands.
type Interface interface {
IsClustered(ctx context.Context) bool
// CreateCluster creates a new InnoDB cluster called
// innodb.DefaultClusterName.
CreateCluster(ctx context.Context, opts Options) (*innodb.ClusterStatus, error)
// GetClusterStatus gets the status of the innodb.DefaultClusterName InnoDB
// cluster.
GetClusterStatus(ctx context.Context) (*innodb.ClusterStatus, error)
// CheckInstanceState verifies the existing data on the instance (specified
// by URI) does not prevent it from joining a cluster.
CheckInstanceState(ctx context.Context, uri string) (*innodb.InstanceState, error)
// AddInstanceToCluster adds the instance (specified by URI) the InnoDB
// cluster.
AddInstanceToCluster(ctx context.Context, uri string, opts Options) error
// RejoinInstanceToCluster rejoins an instance (specified by URI) to the
// InnoDB cluster.
RejoinInstanceToCluster(ctx context.Context, uri string, opts Options) error
// RemoveInstanceFromCluster removes an instance (specified by URI) to the
// InnoDB cluster.
RemoveInstanceFromCluster(ctx context.Context, uri string, opts Options) error
// RebootClusterFromCompleteOutage recovers a cluster when all of its members
// have failed.
RebootClusterFromCompleteOutage(ctx context.Context) error
}
// errorRegex is used to parse Python tracebacks generated by mysql-shell.
var errorRegex = regexp.MustCompile(`Traceback.*\n(?: (.*)\n){1,}(?P<type>[\w\.]+)\: (?P<message>.*)`)
func sanitizeJSON(json []byte) []byte {
return bytes.Replace(json, []byte("\\'"), []byte("'"), -1)
}
// New creates a new MySQL Shell Interface.
func New(exec utilexec.Interface, uri string) Interface {
return &runner{exec: exec, uri: uri}
}
// runner implements Interface in terms of exec("mysqlsh").
type runner struct {
mu sync.Mutex
exec utilexec.Interface
// uri is Uniform Resource Identifier of the MySQL instance to connect to.
// Format: [user[:pass]]@host[:port][/db].
uri string
}
func (r *runner) IsClustered(ctx context.Context) bool {
python := fmt.Sprintf("dba.get_cluster('%s')", innodb.DefaultClusterName)
_, err := r.run(ctx, python)
return err == nil
}
func (r *runner) CreateCluster(ctx context.Context, opts Options) (*innodb.ClusterStatus, error) {
python := fmt.Sprintf("print dba.create_cluster('%s', %s).status()", innodb.DefaultClusterName, opts)
output, err := r.run(ctx, python)
if err != nil {
return nil, err
}
// Skip non-json spat out on stdout.
var jsonData string
for _, line := range strings.Split(string(output), "\n") {
if strings.HasPrefix(line, "{") {
jsonData = line
break
}
}
if jsonData == "" {
return nil, errors.Errorf("no json found in output: %q", output)
}
status := &innodb.ClusterStatus{}
err = json.Unmarshal(sanitizeJSON([]byte(jsonData)), status)
if err != nil {
return nil, errors.Wrapf(err, "decoding cluster status output: %q", output)
}
return status, nil
}
func (r *runner) GetClusterStatus(ctx context.Context) (*innodb.ClusterStatus, error) {
python := fmt.Sprintf("print dba.get_cluster('%s').status()", innodb.DefaultClusterName)
output, err := r.run(ctx, python)
if err != nil {
return nil, err
}
status := &innodb.ClusterStatus{}
err = json.Unmarshal(sanitizeJSON(output), status)
if err != nil {
return nil, errors.Wrapf(err, "decoding cluster status output: %q", output)
}
return status, nil
}
func (r *runner) CheckInstanceState(ctx context.Context, uri string) (*innodb.InstanceState, error) {
python := fmt.Sprintf("print dba.get_cluster('%s').check_instance_state('%s')", innodb.DefaultClusterName, uri)
output, err := r.run(ctx, python)
if err != nil {
return nil, err
}
state := &innodb.InstanceState{}
err = json.Unmarshal(sanitizeJSON(output), state)
if err != nil {
return nil, fmt.Errorf("decoding instance state: %v", err)
}
return state, nil
}
func (r *runner) AddInstanceToCluster(ctx context.Context, uri string, opts Options) error {
python := fmt.Sprintf("dba.get_cluster('%s').add_instance('%s', %s)", innodb.DefaultClusterName, uri, opts)
_, err := r.run(ctx, python)
return err
}
func (r *runner) RejoinInstanceToCluster(ctx context.Context, uri string, opts Options) error {
python := fmt.Sprintf("dba.get_cluster('%s').rejoin_instance('%s', %s)", innodb.DefaultClusterName, uri, opts)
_, err := r.run(ctx, python)
return err
}
func (r *runner) RemoveInstanceFromCluster(ctx context.Context, uri string, opts Options) error {
python := fmt.Sprintf("dba.get_cluster('%s').remove_instance('%s', %s)", innodb.DefaultClusterName, uri, opts)
_, err := r.run(ctx, python)
return err
}
// stripPasswordWarning strips the password warning output by mysqlsh due to the
// fact we pass the password as part of the connection URI.
func (r *runner) stripPasswordWarning(in []byte) []byte {
warning := []byte("mysqlx: [Warning] Using a password on the command line interface can be insecure.\n")
return bytes.Replace(in, warning, []byte(""), 1)
}
func (r *runner) run(ctx context.Context, python string) ([]byte, error) {
r.mu.Lock()
defer r.mu.Unlock()
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
args := []string{"--no-wizard", "--uri", r.uri, "--py", "-e", python}
cmd := r.exec.CommandContext(ctx, "mysqlsh", args...)
cmd.SetStdout(stdout)
cmd.SetStderr(stderr)
glog.V(6).Infof("Running command: mysqlsh %v", args)
err := cmd.Run()
glog.V(6).Infof(" stdout: %s\n stderr: %s\n err: %s", stdout, stderr, err)
if err != nil {
underlying := NewErrorFromStderr(stderr.String())
if underlying != nil {
return nil, errors.WithStack(underlying)
}
}
return r.stripPasswordWarning(stdout.Bytes()), err
}
func (r *runner) RebootClusterFromCompleteOutage(ctx context.Context) error {
// Reset the seed list. This is needed if the cluster is going
// through a full restart and the pods are being created in order,
// because the StatefulSet will not create the service names
// pointing to the other pods until the previous pods are fully up.
// This causes MySQL to fail initializing the group replication
// plugin because it assumes that all the peer names must be valid
// hostnames and, even if they are not reachable from a
// connectivity point of view, they still must resolve to a valid
// IP address, which is not the case considering the StatefulSet
// behavior.
python := fmt.Sprintf("session.query('SET GLOBAL group_replication_group_seeds = \"\"')")
_, err := r.run(ctx, python)
if err != nil {
return err
}
python = fmt.Sprintf("dba.reboot_cluster_from_complete_outage('%s')", innodb.DefaultClusterName)
_, err = r.run(ctx, python)
return err
}
// Error holds errors from mysql-shell commands.
type Error struct {
error
Type string
Message string
}
func (e *Error) Error() string {
return fmt.Sprintf("%s: %s", e.Type, e.Message)
}
// NewErrorFromStderr parses the given output from mysql-shell into an Error if
// one is present.
func NewErrorFromStderr(stderr string) error {
matches := errorRegex.FindAllStringSubmatch(stderr, -1)
if len(matches) == 0 {
return nil
}
result := make(map[string]string)
for i, name := range errorRegex.SubexpNames() {
if i != 0 && name != "" {
result[name] = matches[len(matches)-1][i]
}
}
return &Error{
Type: result["type"],
Message: result["message"],
}
}