mysql-slaver 0.1.10 → 0.1.11
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mysql_slaver/cli.rb +2 -2
- data/lib/mysql_slaver/executor.rb +2 -1
- data/lib/mysql_slaver/slaver.rb +4 -0
- data/lib/mysql_slaver/status_fetcher.rb +7 -4
- data/lib/mysql_slaver.rb +1 -0
- data/spec/mysql_slaver/slaver_spec.rb +20 -0
- data/spec/mysql_slaver/status_fetcher_spec.rb +28 -13
- metadata +4 -4
data/lib/mysql_slaver/cli.rb
CHANGED
@@ -7,8 +7,8 @@ module MysqlSlaver
|
|
7
7
|
option :replication_user, :required => true, :desc => "DB user (on the master host), with replication permissions"
|
8
8
|
option :replication_password, :required => true, :desc => "DB password for the replication user"
|
9
9
|
option :root_password, :desc => "Password for the mysql root user (on both master and slave)"
|
10
|
-
option :port, :desc => "Mysql port
|
11
|
-
option :ssh_port, :desc => "SSH port
|
10
|
+
option :port, :default => 3306, :desc => "Mysql port"
|
11
|
+
option :ssh_port, :default => 22, :desc => "SSH port"
|
12
12
|
option :sock, :desc => "Mysql socket file (if any)"
|
13
13
|
option :no_copy, :type => :boolean, :desc => "Do not copy data - just change master status"
|
14
14
|
|
data/lib/mysql_slaver/slaver.rb
CHANGED
@@ -6,6 +6,8 @@
|
|
6
6
|
# connection)
|
7
7
|
module MysqlSlaver
|
8
8
|
class Slaver
|
9
|
+
include Logger
|
10
|
+
|
9
11
|
attr_reader :status_fetcher, :data_copier, :master_changer, :no_copy
|
10
12
|
|
11
13
|
def initialize(params)
|
@@ -51,6 +53,8 @@ module MysqlSlaver
|
|
51
53
|
master_status = status_fetcher.status
|
52
54
|
data_copier.copy! unless no_copy
|
53
55
|
master_changer.change!(master_status)
|
56
|
+
rescue Exception => e
|
57
|
+
log e.message
|
54
58
|
end
|
55
59
|
end
|
56
60
|
end
|
@@ -15,10 +15,13 @@ module MysqlSlaver
|
|
15
15
|
def status
|
16
16
|
params = {:root_password => mysql_root_password, :socket_file => socket_file}
|
17
17
|
cmd = mysql_command("show master status\\G", params)
|
18
|
-
data = executor.execute
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
if data = executor.execute(executor.ssh_command(cmd, master_host))
|
19
|
+
rtn = parse data
|
20
|
+
log "MASTER STATUS - file: #{rtn[:file]}, position: #{rtn[:position]}"
|
21
|
+
rtn
|
22
|
+
else
|
23
|
+
raise Exception.new("Failed to get master status")
|
24
|
+
end
|
22
25
|
end
|
23
26
|
|
24
27
|
private
|
data/lib/mysql_slaver.rb
CHANGED
@@ -17,6 +17,26 @@ module MysqlSlaver
|
|
17
17
|
}
|
18
18
|
}
|
19
19
|
|
20
|
+
before do
|
21
|
+
slaver.stub(:log => nil)
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when status_fetcher fails" do
|
25
|
+
before do
|
26
|
+
status_fetcher.stub(:status) { raise Exception.new("fail!") }
|
27
|
+
end
|
28
|
+
|
29
|
+
it "does not try to copy data" do
|
30
|
+
slaver.enslave!
|
31
|
+
expect(data_copier).to_not have_received(:copy!)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "does not try to change master status" do
|
35
|
+
slaver.enslave!
|
36
|
+
expect(master_changer).to_not have_received(:change!)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
20
40
|
it "fetches master status" do
|
21
41
|
slaver.enslave!
|
22
42
|
expect(status_fetcher).to have_received(:status)
|
@@ -18,27 +18,42 @@ module MysqlSlaver
|
|
18
18
|
end
|
19
19
|
|
20
20
|
describe "#status" do
|
21
|
-
|
21
|
+
context "when ssh connection fails" do
|
22
|
+
before do
|
23
|
+
executor.stub(:execute => nil)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "raises an error" do
|
27
|
+
expect {
|
28
|
+
fetcher.status
|
29
|
+
}.to raise_error(MysqlSlaver::Exception)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when ssh connection is valid" do
|
34
|
+
|
35
|
+
let(:output) { <<EOF
|
22
36
|
*************************** 1. row ***************************
|
23
37
|
File: mysql-bin.003219
|
24
38
|
Position: 37065270
|
25
39
|
Binlog_Do_DB:
|
26
40
|
Binlog_Ignore_DB:
|
27
41
|
EOF
|
28
|
-
|
42
|
+
}
|
29
43
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
44
|
+
before do
|
45
|
+
executor.stub(:execute => output)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "executes show master command over ssh" do
|
49
|
+
show_master = %[mysql -u root -p supersekrit -e "show master status\\G"]
|
50
|
+
fetcher.status
|
51
|
+
expect(executor).to have_received(:ssh_command).with(show_master, 'my.db.host')
|
52
|
+
end
|
39
53
|
|
40
|
-
|
41
|
-
|
54
|
+
it "parses show master output" do
|
55
|
+
expect(fetcher.status).to eq({:file => 'mysql-bin.003219', :position => '37065270'})
|
56
|
+
end
|
42
57
|
end
|
43
58
|
end
|
44
59
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mysql-slaver
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 11
|
10
|
+
version: 0.1.11
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- David Salgado
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2014-04-
|
18
|
+
date: 2014-04-26 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|