Skip to content

Commit 55e4633

Browse files
author
Shlomi Noach
committed
Merge pull request #38 from github/credentials-config-cli
This closes #25
2 parents fbfe0c7 + df0a751 commit 55e4633

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/bin/bash
22
#
33
#
4-
RELEASE_VERSION="0.7.2"
4+
RELEASE_VERSION="0.7.3"
55

66
buildpath=/tmp/gh-ost
77
target=gh-ost

go/base/context.go

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ type MigrationContext struct {
4343
AllowedRunningOnMaster bool
4444
SwitchToRowBinlogFormat bool
4545

46-
ConfigFile string
46+
config ContextConfig
47+
configMutex *sync.Mutex
48+
ConfigFile string
49+
CliUser string
50+
CliPassword string
4751

4852
ChunkSize int64
4953
MaxLagMillisecondsThrottleThreshold int64
@@ -96,6 +100,19 @@ type MigrationContext struct {
96100
CanStopStreaming func() bool
97101
}
98102

103+
type ContextConfig struct {
104+
Client struct {
105+
User string
106+
Password string
107+
}
108+
Osc struct {
109+
Chunk_Size int64
110+
Max_Lag_Millis int64
111+
Replication_Lag_Query string
112+
Max_Load string
113+
}
114+
}
115+
99116
var context *MigrationContext
100117

101118
func init() {
@@ -112,6 +129,7 @@ func newMigrationContext() *MigrationContext {
112129
MaxLoad: make(map[string]int64),
113130
throttleMutex: &sync.Mutex{},
114131
ThrottleControlReplicaKeys: mysql.NewInstanceKeyMap(),
132+
configMutex: &sync.Mutex{},
115133
}
116134
}
117135

@@ -210,6 +228,8 @@ func (this *MigrationContext) IsThrottled() (bool, string) {
210228
return this.isThrottled, this.throttleReason
211229
}
212230

231+
// ReadMaxLoad parses the `--max-load` flag, which is in multiple key-value format,
232+
// such as: 'Threads_running=100,Threads_connected=500'
213233
func (this *MigrationContext) ReadMaxLoad(maxLoadList string) error {
214234
if maxLoadList == "" {
215235
return nil
@@ -232,31 +252,37 @@ func (this *MigrationContext) ReadMaxLoad(maxLoadList string) error {
232252
return nil
233253
}
234254

255+
// ApplyCredentials sorts out the credentials between the config file and the CLI flags
256+
func (this *MigrationContext) ApplyCredentials() {
257+
this.configMutex.Lock()
258+
defer this.configMutex.Unlock()
259+
260+
if this.config.Client.User != "" {
261+
this.InspectorConnectionConfig.User = this.config.Client.User
262+
}
263+
if this.CliUser != "" {
264+
// Override
265+
this.InspectorConnectionConfig.User = this.CliUser
266+
}
267+
if this.config.Client.Password != "" {
268+
this.InspectorConnectionConfig.Password = this.config.Client.Password
269+
}
270+
if this.CliPassword != "" {
271+
// Override
272+
this.InspectorConnectionConfig.Password = this.CliPassword
273+
}
274+
}
275+
276+
// ReadConfigFile attempts to read the config file, if it exists
235277
func (this *MigrationContext) ReadConfigFile() error {
278+
this.configMutex.Lock()
279+
defer this.configMutex.Unlock()
280+
236281
if this.ConfigFile == "" {
237282
return nil
238283
}
239-
conf := struct {
240-
Client struct {
241-
User string
242-
Password string
243-
}
244-
Osc struct {
245-
Chunk_Size int64
246-
Max_Lag_Millis int64
247-
Replication_Lag_Query string
248-
Max_Load string
249-
}
250-
}{}
251-
if err := gcfg.ReadFileInto(&conf, this.ConfigFile); err != nil {
284+
if err := gcfg.ReadFileInto(&this.config, this.ConfigFile); err != nil {
252285
return err
253286
}
254-
if this.InspectorConnectionConfig.User == "" {
255-
this.InspectorConnectionConfig.User = conf.Client.User
256-
}
257-
if this.InspectorConnectionConfig.Password == "" {
258-
this.InspectorConnectionConfig.Password = conf.Client.Password
259-
}
260-
261287
return nil
262288
}

go/cmd/gh-ost/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func main() {
2323

2424
flag.StringVar(&migrationContext.InspectorConnectionConfig.Key.Hostname, "host", "127.0.0.1", "MySQL hostname (preferably a replica, not the master)")
2525
flag.IntVar(&migrationContext.InspectorConnectionConfig.Key.Port, "port", 3306, "MySQL port (preferably a replica, not the master)")
26-
flag.StringVar(&migrationContext.InspectorConnectionConfig.User, "user", "root", "MySQL user")
27-
flag.StringVar(&migrationContext.InspectorConnectionConfig.Password, "password", "", "MySQL password")
26+
flag.StringVar(&migrationContext.CliUser, "user", "", "MySQL user")
27+
flag.StringVar(&migrationContext.CliPassword, "password", "", "MySQL password")
2828
flag.StringVar(&migrationContext.ConfigFile, "conf", "", "Config file")
2929

3030
flag.StringVar(&migrationContext.DatabaseName, "database", "", "database name (mandatory)")
@@ -118,6 +118,7 @@ func main() {
118118
if err := migrationContext.ReadMaxLoad(*maxLoad); err != nil {
119119
log.Fatale(err)
120120
}
121+
migrationContext.ApplyCredentials()
121122

122123
log.Infof("starting gh-ost %+v", AppVersion)
123124

0 commit comments

Comments
 (0)