diff --git a/build.sh b/build.sh index 1699a21..ed8c086 100644 --- a/build.sh +++ b/build.sh @@ -1,7 +1,7 @@ #!/bin/bash # # -RELEASE_VERSION="0.7.2" +RELEASE_VERSION="0.7.3" buildpath=/tmp/gh-ost target=gh-ost diff --git a/go/base/context.go b/go/base/context.go index c3bbe36..ee280a3 100644 --- a/go/base/context.go +++ b/go/base/context.go @@ -43,7 +43,11 @@ type MigrationContext struct { AllowedRunningOnMaster bool SwitchToRowBinlogFormat bool - ConfigFile string + config ContextConfig + configMutex *sync.Mutex + ConfigFile string + CliUser string + CliPassword string ChunkSize int64 MaxLagMillisecondsThrottleThreshold int64 @@ -96,6 +100,19 @@ type MigrationContext struct { CanStopStreaming func() bool } +type ContextConfig struct { + Client struct { + User string + Password string + } + Osc struct { + Chunk_Size int64 + Max_Lag_Millis int64 + Replication_Lag_Query string + Max_Load string + } +} + var context *MigrationContext func init() { @@ -112,6 +129,7 @@ func newMigrationContext() *MigrationContext { MaxLoad: make(map[string]int64), throttleMutex: &sync.Mutex{}, ThrottleControlReplicaKeys: mysql.NewInstanceKeyMap(), + configMutex: &sync.Mutex{}, } } @@ -210,6 +228,8 @@ func (this *MigrationContext) IsThrottled() (bool, string) { return this.isThrottled, this.throttleReason } +// ReadMaxLoad parses the `--max-load` flag, which is in multiple key-value format, +// such as: 'Threads_running=100,Threads_connected=500' func (this *MigrationContext) ReadMaxLoad(maxLoadList string) error { if maxLoadList == "" { return nil @@ -232,31 +252,37 @@ func (this *MigrationContext) ReadMaxLoad(maxLoadList string) error { return nil } +// ApplyCredentials sorts out the credentials between the config file and the CLI flags +func (this *MigrationContext) ApplyCredentials() { + this.configMutex.Lock() + defer this.configMutex.Unlock() + + if this.config.Client.User != "" { + this.InspectorConnectionConfig.User = this.config.Client.User + } + if this.CliUser != "" { + // Override + this.InspectorConnectionConfig.User = this.CliUser + } + if this.config.Client.Password != "" { + this.InspectorConnectionConfig.Password = this.config.Client.Password + } + if this.CliPassword != "" { + // Override + this.InspectorConnectionConfig.Password = this.CliPassword + } +} + +// ReadConfigFile attempts to read the config file, if it exists func (this *MigrationContext) ReadConfigFile() error { + this.configMutex.Lock() + defer this.configMutex.Unlock() + if this.ConfigFile == "" { return nil } - conf := struct { - Client struct { - User string - Password string - } - Osc struct { - Chunk_Size int64 - Max_Lag_Millis int64 - Replication_Lag_Query string - Max_Load string - } - }{} - if err := gcfg.ReadFileInto(&conf, this.ConfigFile); err != nil { + if err := gcfg.ReadFileInto(&this.config, this.ConfigFile); err != nil { return err } - if this.InspectorConnectionConfig.User == "" { - this.InspectorConnectionConfig.User = conf.Client.User - } - if this.InspectorConnectionConfig.Password == "" { - this.InspectorConnectionConfig.Password = conf.Client.Password - } - return nil } diff --git a/go/cmd/gh-ost/main.go b/go/cmd/gh-ost/main.go index 877f1d6..e6cdaca 100644 --- a/go/cmd/gh-ost/main.go +++ b/go/cmd/gh-ost/main.go @@ -23,8 +23,8 @@ func main() { flag.StringVar(&migrationContext.InspectorConnectionConfig.Key.Hostname, "host", "127.0.0.1", "MySQL hostname (preferably a replica, not the master)") flag.IntVar(&migrationContext.InspectorConnectionConfig.Key.Port, "port", 3306, "MySQL port (preferably a replica, not the master)") - flag.StringVar(&migrationContext.InspectorConnectionConfig.User, "user", "root", "MySQL user") - flag.StringVar(&migrationContext.InspectorConnectionConfig.Password, "password", "", "MySQL password") + flag.StringVar(&migrationContext.CliUser, "user", "", "MySQL user") + flag.StringVar(&migrationContext.CliPassword, "password", "", "MySQL password") flag.StringVar(&migrationContext.ConfigFile, "conf", "", "Config file") flag.StringVar(&migrationContext.DatabaseName, "database", "", "database name (mandatory)") @@ -118,6 +118,7 @@ func main() { if err := migrationContext.ReadMaxLoad(*maxLoad); err != nil { log.Fatale(err) } + migrationContext.ApplyCredentials() log.Infof("starting gh-ost %+v", AppVersion)