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.
@@ -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 (if not 3306)"
11
- option :ssh_port, :desc => "SSH port (if not 22)"
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
 
@@ -19,7 +19,8 @@ module MysqlSlaver
19
19
  def execute(cmd)
20
20
  string = cmd.is_a?(Array) ? cmd.join('; ') : cmd
21
21
  log "CMD: #{string}"
22
- `#{string}`
22
+ result = `#{string}`
23
+ $?.success? ? result : nil
23
24
  end
24
25
  end
25
26
  end
@@ -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 executor.ssh_command(cmd, master_host)
19
- rtn = parse data
20
- log "MASTER STATUS - file: #{rtn[:file]}, position: #{rtn[:position]}"
21
- rtn
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
@@ -3,6 +3,7 @@ require 'thor'
3
3
 
4
4
  libdir = File.join(File.dirname(__FILE__), 'mysql_slaver')
5
5
 
6
+ require "#{libdir}/exception"
6
7
  require "#{libdir}/logger"
7
8
  require "#{libdir}/logger"
8
9
  require "#{libdir}/executor"
@@ -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
- let(:output) { <<EOF
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
- before do
31
- executor.stub(:execute => output)
32
- end
33
-
34
- it "executes show master command over ssh" do
35
- show_master = %[mysql -u root -p supersekrit -e "show master status\\G"]
36
- fetcher.status
37
- expect(executor).to have_received(:ssh_command).with(show_master, 'my.db.host')
38
- end
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
- it "parses show master output" do
41
- expect(fetcher.status).to eq({:file => 'mysql-bin.003219', :position => '37065270'})
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: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 10
10
- version: 0.1.10
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-20 00:00:00 Z
18
+ date: 2014-04-26 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec