vagrant-mongodb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,16 +15,17 @@ module VagrantPlugins
15
15
  def call(env)
16
16
  @app.call(env)
17
17
 
18
- # check if the current machine is a member of a replica set
18
+ # check if the current machine is a member of a replica set and the
19
+ # replica set has not already been initiated
19
20
  @logger.info "Checking if '#{@machine.name}' is part of a replica set..."
20
21
  rs = get_replset(@machine.name) if @config
21
- return if !rs
22
+ return if !rs or already_initiated?(rs)
22
23
 
23
24
  # ensure all members are available before initiating replica set
24
25
  if all_members_available?(rs)
25
26
  env[:ui].info @translator.t('initiate', { :name => rs.name })
26
27
  command = "mongo --eval 'printjson(rs.initiate(#{generate_json(rs)}))'"
27
- env[:machine].communicate.execute(command) do |type, data|
28
+ @machine.communicate.execute(command) do |type, data|
28
29
  raise Errors::ReplSetInitiateError if data =~ /"ok" : 0/
29
30
  end
30
31
  end
@@ -92,6 +93,19 @@ module VagrantPlugins
92
93
 
93
94
  ip || machine.ssh_info[:host]
94
95
  end
96
+
97
+ # check if the replica set has already been initiated
98
+ def already_initiated?(rs)
99
+ @logger.info "Checking if '#{rs.name}' has already been initiated..."
100
+ command = "mongo --eval 'printjson(rs.status())'"
101
+ begin
102
+ @machine.communicate.execute(command) do |type, data|
103
+ return true if data =~ /"ok" : 1/
104
+ end
105
+ rescue Vagrant::Errors::VagrantError
106
+ end
107
+ false
108
+ end
95
109
  end
96
110
  end
97
111
  end
@@ -20,9 +20,12 @@ module VagrantPlugins
20
20
  end
21
21
 
22
22
  def replset(name, &block)
23
- rs = ReplSet.new(name)
23
+ rs = @replsets.find { |r| r.name == name.to_sym }
24
+ if !rs
25
+ rs = ReplSet.new(name)
26
+ @replsets << rs
27
+ end
24
28
  block.call(rs)
25
- @replsets << rs
26
29
  end
27
30
 
28
31
  class ReplSet
@@ -30,12 +33,17 @@ module VagrantPlugins
30
33
  attr_reader :members
31
34
 
32
35
  def initialize(name)
33
- @name = name
36
+ @name = name.to_sym
34
37
  @members = []
35
38
  end
36
39
 
37
40
  def member(name, options = {})
38
- @members << options.merge({ :_id => @members.size, :host => name})
41
+ member = @members.find { |m| m[:host] == name.to_sym }
42
+ if member
43
+ member.merge(options)
44
+ else
45
+ @members << options.merge({ :_id => @members.size, :host => name.to_sym })
46
+ end
39
47
  end
40
48
  end
41
49
  end
@@ -9,17 +9,20 @@ module VagrantPlugins
9
9
  This plugin manages a MongoDb replica set.
10
10
  DESC
11
11
 
12
+ def self.replset_initiate(hook)
13
+ setup_logging
14
+ setup_i18n
15
+ hook.before(Vagrant::Action::Builtin::Provision, Actions::ReplSetInitiate)
16
+ end
17
+
12
18
  config(:mongodb) do
13
19
  require_relative 'config'
14
20
  Config
15
21
  end
16
22
 
17
23
  # initiate replica set after machine provisioning
18
- action_hook(:replset_initiate, :machine_action_up) do |hook|
19
- setup_logging
20
- setup_i18n
21
- hook.before(Vagrant::Action::Builtin::Provision, Actions::ReplSetInitiate)
22
- end
24
+ action_hook(:replset_initiate, :machine_action_provision, &method(:replset_initiate))
25
+ action_hook(:replset_initiate, :machine_action_up, &method(:replset_initiate))
23
26
 
24
27
  def self.setup_i18n
25
28
  I18n.load_path << File.expand_path(
@@ -1,5 +1,5 @@
1
1
  module VagrantPlugins
2
2
  module MongoDb
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  end
data/test/Vagrantfile CHANGED
@@ -8,7 +8,7 @@ Vagrant.configure('2') do |config|
8
8
  config.mongodb.replset :rs0 do |rs|
9
9
  rs.member :server1, :priority => 1
10
10
  rs.member :server2, :priority => 2
11
- rs.member :server3, :priority => 1
11
+ rs.member :server3
12
12
  end
13
13
 
14
14
  script = <<-SCRIPT
@@ -20,14 +20,14 @@ Vagrant.configure('2') do |config|
20
20
  config.vm.provision :shell, :inline => script
21
21
 
22
22
  config.vm.define :server1 do |server|
23
- server.vm.network :private_network, :ip => '10.0.5.2'
23
+ server.vm.network :private_network, :ip => '10.10.5.2'
24
24
  end
25
25
 
26
26
  config.vm.define :server2 do |server|
27
- server.vm.network :private_network, :ip => '10.0.5.3'
27
+ server.vm.network :private_network, :ip => '10.10.5.3'
28
28
  end
29
29
 
30
30
  config.vm.define :server3 do |server|
31
- server.vm.network :private_network, :ip => '10.0.5.4'
31
+ server.vm.network :private_network, :ip => '10.10.5.4'
32
32
  end
33
33
  end
data/test/test.sh CHANGED
@@ -6,6 +6,7 @@ cd test
6
6
  vagrant up
7
7
  sleep 30
8
8
  vagrant ssh server1 -c 'mongo --eval "printjson(rs.status())"'
9
+ vagrant provision
9
10
  vagrant destroy -f
10
11
 
11
12
  cd ..
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant-mongodb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-29 00:00:00.000000000 Z
12
+ date: 2013-04-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A Vagrant plugin that manages a MongoDb replicaset
15
15
  email: