Skip to content

Commit

Permalink
Merge branch 'master' into centosrepo
Browse files Browse the repository at this point in the history
  • Loading branch information
dcrosta committed Aug 11, 2012
2 parents 787734b + 94667ea commit 787d0aa
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 189 deletions.
24 changes: 15 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@

DIRS = mongodb
COOKBOOK=mongodb
BRANCH=master

BUILD_DIR=build
BUILD_DIR=../build
DIST_PREFIX=$(BUILD_DIR)/$(COOKBOOK)

all: metadata.json

clean:
-rm metadata.json

dist:
mkdir -p $(BUILD_DIR)
for i in $(DIRS); do make -C $$i $@; done

metadata.json:
for i in $(DIRS); do make -C $$i $@; done
-rm $@
knife cookbook metadata -o .. $(COOKBOOK)

clean:
-rm -r $(BUILD_DIR)
dist: clean metadata.json
mkdir -p $(BUILD_DIR)
version=`python -c "import json;c = json.load(open('metadata.json')); print c.get('version', 'UNKNOWN')"`; \
tar --exclude-vcs --exclude=Makefile -cvzf $(DIST_PREFIX)-$$version.tar.gz ../$(COOKBOOK)
159 changes: 152 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,156 @@
# Chef Cookbooks
# DESCRIPTION:

A collection of cookbooks for the chef configuration management system, developed by
edelight GmbH.
Installs and configures MongoDB, supporting:

## MongoDB
* Single MongoDB
* Replication
* Sharding
* Replication and Sharding
* 10gen repository package installation

This cookbooks helps to configure various kind of MongoDB setups, including single node,
Replication, Sharding and Sharding + Replication.
# REQUIREMENTS:

For more detailed information see the documentation of the cookbook itself.
## Platform:

The cookbook aims to be platform independant, but is best tested on debian squeeze systems.

The `10gen_repo` recipe currently supports only the Debian and Ubuntu 10gen repository.
Patches for other platforms are welcome.

# DEFINITIONS:

This cookbook contains a definition `mongodb_instance` which can be used to configure
a certain type of mongodb instance, like the default mongodb or various components
of a sharded setup.

For examples see the USAGE section below.

# ATTRIBUTES:

* `mongodb[:dbpath]` - Location for mongodb data directory, defaults to "/var/lib/mongodb"
* `mongodb[:logpath]` - Path for the logfiles, default is "/var/log/mongodb"
* `mongodb[:port]` - Port the mongod listens on, default is 27017
* `mongodb[:client_role]` - Role identifing all external clients which should have access to a mongod instance
* `mongodb[:cluster_name]` - Name of the cluster, all members of the cluster must
reference to the same name, as this name is used internally to identify all
members of a cluster.
* `mongodb[:shard_name]` - Name of a shard, default is "default"
* `mongodb[:sharded_collections]` - Define which collections are sharded
* `mongodb[:replicaset_name]` - Define name of replicatset

# USAGE:

## 10gen repository

Adds the stable [10gen repo](http://www.mongodb.org/downloads#packages) for the
corresponding platform. Currently only implemented for the Debian and Ubuntu repository.

Usage: just add `recipe[mongodb::10gen_repo]` to the node run_list *before* any other
MongoDB recipe, and the mongodb-10gen **stable** packages will be installed instead of the distribution default.

## Single mongodb instance

Simply add

```ruby
include_recipe "mongodb::default"
```

to your recipe. This will run the mongodb instance as configured by your distribution.
You can change the dbpath, logpath and port settings (see ATTRIBUTES) for this node by
using the `mongodb_instance` definition:

```ruby
mongodb_instance "mongodb" do
port node['application']['port']
end
```

This definition also allows you to run another mongod instance with a different
name on the same node

```ruby
mongodb_instance "my_instance" do
port node['mongodb']['port'] + 100
dbpath "/data/"
end
```

The result is a new system service with

```shell
/etc/init.d/my_instance <start|stop|restart|status>
```

## Replicasets

Add `mongodb::replicaset` to the node's run_list. Also choose a name for your
replicaset cluster and set the value of `node[:mongodb][:cluster_name]` for each
member to this name.

## Sharding

You need a few more components, but the idea is the same: identification of the
members with their different internal roles (mongos, configserver, etc.) is done via
the `node[:mongodb][:cluster_name]` and `node[:mongodb][:shard_name]` attributes.

Let's have a look at a simple sharding setup, consisting of two shard servers, one
config server and one mongos.

First we would like to configure the two shards. For doing so, just use
`mongodb::shard` in the node's run_list and define a unique `mongodb[:shard_name]`
for each of these two nodes, say "shard1" and "shard2".

Then configure a node to act as a config server - by using the `mongodb::configserver`
recipe.

And finally you need to configure the mongos. This can be done by using the
`mongodb::mongos` recipe. The mongos needs some special configuration, as these
mongos are actually doing the configuration of the whole sharded cluster.
Most importantly you need to define what collections should be sharded by setting the
attribute `mongodb[:sharded_collections]`:

```javascript
{
"mongodb": {
"sharded_collections": {
"test.addressbook": "name",
"mydatabase.calendar": "date"
}
}
}
```

Now mongos will automatically enable sharding for the "test" and the "mydatabase"
database. Also the "addressbook" and the "calendar" collection will be sharded,
with sharding key "name" resp. "date".
In the context of a sharding cluster always keep in mind to use a single role
which is added to all members of the cluster to identify all member nodes.
Also shard names are important to distinguish the different shards.
This is esp. important when you want to replicate shards.

## Sharding + Replication

The setup is not much different to the one described above. All you have to do is adding the
`mongodb::replicaset` recipe to all shard nodes, and make sure that all shard
nodes which should be in the same replicaset have the same shard name.

For more details, you can find a [tutorial for Sharding + Replication](https://github.com/edelight/chef-cookbooks/wiki/MongoDB%3A-Replication%2BSharding) in the wiki.

# LICENSE and AUTHOR:

Author:: Markus Korn <[email protected]>

Copyright:: 2011, edelight GmbH

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
16 changes: 15 additions & 1 deletion mongodb/attributes/default.rb → attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@
default[:mongodb][:logpath] = "/var/log/mongodb"
default[:mongodb][:port] = 27017

# roles
# cluster identifier
default[:mongodb][:client_roles] = []
default[:mongodb][:cluster_name] = nil
default[:mongodb][:replicaset_name] = nil
default[:mongodb][:shard_name] = "default"

default[:mongodb][:enable_rest] = false

case node['platform']
when "freebsd"
default[:mongodb][:defaults_dir] = "/etc/rc.conf.d"
default[:mongodb][:init_dir] = "/usr/local/etc/rc.d"
default[:mongodb][:root_group] = "wheel"
else
default[:mongodb][:defaults_dir] = "/etc/default"
default[:mongodb][:init_dir] = "/etc/init.d"
default[:mongodb][:root_group] = "root"
end
44 changes: 33 additions & 11 deletions mongodb/definitions/mongodb.rb → definitions/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

define :mongodb_instance, :mongodb_type => "mongod" , :action => [:enable, :start], :port => 27017 , \
:logpath => "/var/log/mongodb", :dbpath => "/data", :configfile => "/etc/mongodb.conf", \
:configserver => [], :replicaset => nil, :notifies => [] do
:configserver => [], :replicaset => nil, :enable_rest => false, \
:notifies => [] do

include_recipe "mongodb::default"

Expand All @@ -41,10 +42,28 @@
configserver_nodes = params[:configserver]

replicaset = params[:replicaset]
begin
replicaset_name = "rs_#{replicaset['mongodb']['shard_name']}" # Looks weird, but we need just some name
rescue
replicaset_name = nil
if type == "shard"
if replicaset.nil?
replicaset_name = nil
else
# for replicated shards we autogenerate the replicaset name for each shard
replicaset_name = "rs_#{replicaset['mongodb']['shard_name']}"
end
else
# if there is a predefined replicaset name we use it,
# otherwise we try to generate one using 'rs_$SHARD_NAME'
begin
replicaset_name = replicaset['mongodb']['replicaset_name']
rescue
replicaset_name = nil
end
if replicaset_name.nil?
begin
replicaset_name = "rs_#{replicaset['mongodb']['shard_name']}"
rescue
replicaset_name = nil
end
end
end

if !["mongod", "shard", "configserver", "mongos"].include?(type)
Expand All @@ -64,10 +83,10 @@
end

# default file
template "/etc/default/#{name}" do
template "#{node['mongodb']['defaults_dir']}/#{name}" do
action :create
source "mongodb.default.erb"
group "root"
group node['mongodb']['root_group']
owner "root"
mode "0644"
variables(
Expand All @@ -80,7 +99,8 @@
"dbpath" => dbpath,
"replicaset_name" => replicaset_name,
"configsrv" => false, #type == "configserver", this might change the port
"shardsrv" => false #type == "shard", dito.
"shardsrv" => false, #type == "shard", dito.
"enable_rest" => params[:enable_rest]
)
notifies :restart, "service[#{name}]"
end
Expand All @@ -91,6 +111,7 @@
group "mongodb"
mode "0755"
action :create
recursive true
end

if type != "mongos"
Expand All @@ -100,14 +121,15 @@
group "mongodb"
mode "0755"
action :create
recursive true
end
end

# init script
template "/etc/init.d/#{name}" do
template "#{node['mongodb']['init_dir']}/#{name}" do
action :create
source "mongodb.init.erb"
group "root"
group node['mongodb']['root_group']
owner "root"
mode "0755"
variables :provides => name
Expand All @@ -123,7 +145,7 @@
notifies :create, "ruby_block[config_replicaset]"
end
if type == "mongos"
notifies :create, "ruby_block[config_sharding]"
notifies :create, "ruby_block[config_sharding]", :immediately
end
if name == "mongodb"
# we don't care about a running mongodb service in these cases, all we need is stopping it
Expand Down
7 changes: 6 additions & 1 deletion mongodb/libraries/mongodb.rb → libraries/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ def self.configure_replicaset(node, name, members)
require 'mongo'

if members.length == 0
abort("cannot configure replicaset '#{name}', no member nodes found")
if Chef::Config[:solo]
abort("Cannot configure replicaset '#{name}', no member nodes found")
else
Chef::Log.warn("Cannot configure replicaset '#{name}', no member nodes found")
return
end
end

begin
Expand Down
11 changes: 10 additions & 1 deletion mongodb/metadata.rb → metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

depends "apt"

%w{ ubuntu debian }.each do |os|
%w{ ubuntu debian freebsd }.each do |os|
supports os
end

Expand Down Expand Up @@ -51,3 +51,12 @@
:display_name => "Sharded Collections",
:description => "collections to shard",
:default => {}

attribute "mongodb/replicaset_name",
:display_name => "Replicaset_name",
:description => "Name of a mongodb replicaset",
:default => nil

attribute "mongodb/enable_rest",
:display_name => "Enable Rest",
:description => "Enable the ReST interface of the webserver"
20 changes: 0 additions & 20 deletions mongodb/Makefile

This file was deleted.

Loading

0 comments on commit 787d0aa

Please sign in to comment.