Skip to content
This repository
Browse code

added auto-generation of mongoid.yml file

  • Loading branch information...
commit 9f472611ee314f08ae9f9ef13e4ba51e3072e833 1 parent 6428cd7
authored April 13, 2012
1  cookbooks/mongodb/README.md
Source Rendered
@@ -53,7 +53,6 @@ Things (currently) lacking from this recipe:
53 53
 
54 54
   * Ability to set up scheduled backups of your MongoDB database
55 55
   * Ability to set up a sharded installation
56  
-  * Auto-generation of mongoid.yml files (coming soon)
57 56
   * Arbiter support
58 57
 
59 58
 Known Bugs
18  cookbooks/mongodb/recipes/app.rb
@@ -32,5 +32,23 @@
32 32
       end
33 33
       
34 34
     end
  35
+
  36
+    template "/data/#{mongo_app_name}/shared/config/mongoid.yml" do
  37
+      source "mongoid.yml.erb"
  38
+      owner user[:username]
  39
+      group user[:username]
  40
+      mode 0755
  41
+      
  42
+      hosts = @node[:mongo_utility_instances].select { |instance| instance[:name].match(/#{mongo_app_name}/) }.map { |instance| [ instance[:hostname], @node[:mongo_port].to_i ] }
  43
+      replica_set = @node[:mongo_utility_instances].any? { |instance| instance[:name].match(/^mongodb_repl/) }
  44
+      if replica_set
  45
+        hosts += @node[:mongo_utility_instances].select { |instance| instance[:name].match(/^mongodb_repl/) }.map { |instance| [ instance[:hostname], @node[:mongo_port].to_i ] }
  46
+      end
  47
+      variables(:environment => node[:environment][:framework_env], 
  48
+                :hosts => hosts,
  49
+                :replica_set => replica_set,
  50
+                :mongo_replsetname => node[:environment][:name] )
  51
+    end
  52
+
35 53
   end
36 54
 end
49  cookbooks/mongodb/templates/default/mongoid.yml.erb
... ...
@@ -1,36 +1,13 @@
1  
-defaults: &defaults
2  
-<% if @solo_server %>
3  
-  host: localhost
4  
-<% else %>
5  
-   hosts: 
6  
-<% @mongo_nodes.each do |mongo_node| -%>
7  
-   - - <%=mongo_node[:hostname]%>
8  
-       <%=@mongo_port%>
9  
-<% end -%>
10  
- read_secondary: true
11  
- 
12  
- development:
13  
-  <<: *defaults
14  
-    database: winealign_dev
15  
- 
16  
- test:
17  
-  <<: *defaults
18  
-    database: winealign_staging
19  
- 
20  
-  production:
21  
-  <<: *defaults
22  
-   database: winealign_staging
23  
-  
24  
- username:  deploy
25  
-
26  
-
27  
-<% end %>
28  
-
29  
-# If you use Sinatra (or anything other than rails) you need to do make a mongoid.yml like this:
30  
-#
31  
-# production:
32  
-#   hosts: [[db1.mongoid.org, 27017], [db2.mongoid.org, 27017]]
33  
-#   database: project_production
34  
-# development:
35  
-#   hosts: [[db1.mongoid.org, 27017], [db2.mongoid.org, 27017]]
36  
-#   database: project_development
  1
+<%= @environment %>:
  2
+  database: <%=@mongo_replsetname%>
  3
+  read_secondary: true
  4
+  <% if @replica_set -%>
  5
+  hosts:
  6
+  <% @hosts.each do |host, port| -%>
  7
+    - - <%= host %>
  8
+      - <%= port %>
  9
+  <% end -%>  
  10
+  <% else -%>
  11
+  host: <%= @hosts[0][0] %>
  12
+  port: <%= @hosts[0][1] %>
  13
+  <% end -%>

0 notes on commit 9f47261

Please sign in to comment.
Something went wrong with that request. Please try again.