couch-replicate 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -1,10 +1,15 @@
1
1
  require 'restclient'
2
2
 
3
3
  module CouchReplicate
4
+ def self.version
5
+ File.read(File.expand_path(File.dirname(__FILE__) + '/../VERSION')).chomp
6
+ end
4
7
 
5
8
  def self.replicate(source_host, target_host, db)
6
- RestClient.post("#{source_host}/_replicate",
7
- %Q|{"source":"#{db}", "target":"#{target_host}/#{db}", "continuous":true}|)
9
+ source = hostify(source_host)
10
+ target = hostify(target_host)
11
+ RestClient.post("#{source}/_replicate",
12
+ %Q|{"source":"#{db}", "target":"#{target}/#{db}", "continuous":true}|)
8
13
  end
9
14
 
10
15
  def self.link(db, hosts)
@@ -19,8 +24,22 @@ module CouchReplicate
19
24
  end
20
25
 
21
26
  def self.nth(n, db, hosts)
27
+ return if n == db.length + 1
22
28
  (Array(hosts) + Array(hosts)[0..n]).each_cons(n+1) do |src, *n_hosts|
23
29
  self.replicate(src, n_hosts.last, db)
24
30
  end
25
31
  end
32
+
33
+ protected
34
+ def self.hostify(raw_host)
35
+ host = raw_host.dup
36
+ host.sub!(%r{/$}, '')
37
+ unless host =~ /\:\d+$/
38
+ host += ":5984"
39
+ end
40
+ unless host =~ %r{^http://}
41
+ host = "http://" + host
42
+ end
43
+ host
44
+ end
26
45
  end
@@ -47,12 +47,30 @@ module CouchReplicate
47
47
  opts.on("-n", "-number [LINK_EVERY=3]", "link every n hosts") do |n|
48
48
  @options[:n] = n.to_i > 0 ? n.to_i : 3
49
49
  end
50
+
51
+ opts.separator ""
52
+
53
+ opts.on_tail("-v", "--version", "Show version") do
54
+ puts File.basename($0) + " " + CouchReplicate.version
55
+ exit
56
+ end
57
+
58
+ # No argument, shows at tail. This will print an options summary.
59
+ # Try it and see!
60
+ opts.on_tail("-h", "--help", "Show this message") do
61
+ puts opts
62
+ exit
63
+ end
50
64
  end
51
65
 
52
66
  begin
53
67
  options_parser.parse!(args)
54
68
  @database = args.shift
55
69
  @hosts = args
70
+
71
+ if @options[:n] && (@options[:n] == @hosts.length + 1)
72
+ raise OptionParser::InvalidOption.new("The number of hosts cannot equal the replication shift.")
73
+ end
56
74
  rescue OptionParser::InvalidOption => e
57
75
  raise e
58
76
  end
@@ -16,6 +16,33 @@ describe "CouchReplicate" do
16
16
  CouchReplicate.replicate(@src_host, @target_host, @db)
17
17
  end
18
18
 
19
+ it "should default replication to port 5984" do
20
+ RestClient.
21
+ should_receive(:post).
22
+ with("#{@src_host}/_replicate",
23
+ %Q|{"source":"#{@db}", "target":"#{@target_host}/#{@db}", "continuous":true}|)
24
+
25
+ CouchReplicate.replicate(@src_host.sub(/:5984/, ''), @target_host, @db)
26
+ end
27
+
28
+ it "should default replication to HTTP" do
29
+ RestClient.
30
+ should_receive(:post).
31
+ with("#{@src_host}/_replicate",
32
+ %Q|{"source":"#{@db}", "target":"#{@target_host}/#{@db}", "continuous":true}|)
33
+
34
+ CouchReplicate.replicate(@src_host.sub(/http:\/\//, ''), @target_host, @db)
35
+ end
36
+
37
+ it "should not care if URL ends with a slash" do
38
+ RestClient.
39
+ should_receive(:post).
40
+ with("#{@src_host}/_replicate",
41
+ %Q|{"source":"#{@db}", "target":"#{@target_host}/#{@db}", "continuous":true}|)
42
+
43
+ CouchReplicate.replicate(@src_host + '/', @target_host, @db)
44
+ end
45
+
19
46
  context "replicating to multiple hosts" do
20
47
  before(:each) do
21
48
  @host01 = 'http://couch01.example.org:5984'
@@ -66,5 +93,12 @@ describe "CouchReplicate" do
66
93
 
67
94
  CouchReplicate.nth(3, @db, [@host01, @host02, @host03, @host04, @host05])
68
95
  end
96
+
97
+ it "should do nothing when node replicate to themselves" do
98
+ CouchReplicate.
99
+ should_not_receive(:replicate)
100
+
101
+ CouchReplicate.nth(5, @db, [@host01, @host02, @host03, @host04, @host05])
102
+ end
69
103
  end
70
104
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couch-replicate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Strom
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-03-21 00:00:00 -04:00
12
+ date: 2010-03-23 00:00:00 -04:00
13
13
  default_executable: couch-replicate
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency