Skip to content

Commit

Permalink
Merge pull request #11 from customink/centosrepo
Browse files Browse the repository at this point in the history
add support for the CentOS and Fedora repo
  • Loading branch information
dcrosta committed Aug 17, 2012
2 parents 41ec1ed + d8f593d commit 28f5000
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 12 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ Installs and configures MongoDB, supporting:

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.
The `10gen_repo` recipe configures the package manager to use 10gen's
official package reposotories on Debian, Ubuntu, Redhat, CentOS, Fedora, and
Amazon linux distributions.

# DEFINITIONS:

Expand Down
20 changes: 19 additions & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,31 @@

default[:mongodb][:enable_rest] = false

default[:mongodb][:user] = "mongodb"
default[:mongodb][:group] = "mongodb"
default[:mongodb][:root_group] = "root"

default[:mongodb][:init_dir] = "/etc/init.d"

default[:mongodb][:init_script_template] = "mongodb.init.erb"

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"
default[:mongodb][:package_name] = "mongodb"

when "centos","redhat","fedora","amazon"
default[:mongodb][:defaults_dir] = "/etc/sysconfig"
default[:mongodb][:package_name] = "mongo-10gen-server"
default[:mongodb][:user] = "mongod"
default[:mongodb][:group] = "mongod"
default[:mongodb][:init_script_template] = "redhat-mongodb.init.erb"

else
default[:mongodb][:defaults_dir] = "/etc/default"
default[:mongodb][:init_dir] = "/etc/init.d"
default[:mongodb][:root_group] = "root"
default[:mongodb][:package_name] = "mongodb-10gen"

end
10 changes: 5 additions & 5 deletions definitions/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@

# log dir [make sure it exists]
directory logpath do
owner "mongodb"
group "mongodb"
owner node[:mongodb][:user]
group node[:mongodb][:group]
mode "0755"
action :create
recursive true
Expand All @@ -117,8 +117,8 @@
if type != "mongos"
# dbpath dir [make sure it exists]
directory dbpath do
owner "mongodb"
group "mongodb"
owner node[:mongodb][:user]
group node[:mongodb][:group]
mode "0755"
action :create
recursive true
Expand All @@ -128,7 +128,7 @@
# init script
template "#{node['mongodb']['init_dir']}/#{name}" do
action :create
source "mongodb.init.erb"
source node[:mongodb][:init_script_template]
group node['mongodb']['root_group']
owner "root"
mode "0755"
Expand Down
3 changes: 2 additions & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
recipe "mongodb::replicaset", "Installs and configures a mongodb replicaset"

depends "apt"
depends "yum"

%w{ ubuntu debian freebsd }.each do |os|
%w{ ubuntu debian freebsd centos redhat fedora amazon }.each do |os|
supports os
end

Expand Down
155 changes: 155 additions & 0 deletions mongodb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# DESCRIPTION:

Installs and configures MongoDB, supporting:

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

# REQUIREMENTS:

## 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 and Fedora and CentOS 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

# 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/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.
5 changes: 5 additions & 0 deletions mongodb/templates/default/10gen.repo.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file auto-generated by Chef for node[:fqdn]
[10gen]
name=10gen Repository
baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/<%=@processor%>
gpgcheck=0
8 changes: 6 additions & 2 deletions recipes/10gen_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@
notifies :run, "execute[apt-get update]", :immediately
end

package "mongodb" do
package_name "mongodb-10gen"
when "centos","redhat","fedora","amazon"
yum_repository "10gen" do
description "10gen RPM Repository"
url "http://downloads-distro.mongodb.org/repo/redhat/os/$arch"
action :add
end

else
Chef::Log.warn("Adding the #{node['platform']} 10gen repository is not yet not supported by this cookbook")
end
2 changes: 1 addition & 1 deletion recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# limitations under the License.
#

package "mongodb" do
package node[:mongodb][:package_name] do
action :install
end

Expand Down
5 changes: 5 additions & 0 deletions templates/default/mongodb.default.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ DAEMON_OPTS="$DAEMON_OPTS --replSet <%= @replicaset_name %>"
<% if @enable_rest %>
DAEMON_OPTS="$DAEMON_OPTS --rest"
<% end %>

<% if node.platform?("centos","redhat","fedora","amazon") -%>
DAEMON_OPTS="$DAEMON_OPTS --fork"
DBPATH="<%= @dbpath %>"
<% end -%>
68 changes: 68 additions & 0 deletions templates/default/redhat-mongodb.init.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash

# <%= @provides %> - Startup script for mongod

# chkconfig: 35 85 15
# description: Mongo is a scalable, document-oriented database.
# processname: <%= @provides %>
# config: /etc/mongod.conf
# pidfile: /var/run/mongo/mongo.pid

. /etc/rc.d/init.d/functions

SYSCONFIG="/etc/sysconfig/<%= @provides %>"
DAEMON=/usr/bin/mongod
MONGO_USER=mongod

. "$SYSCONFIG" || true

start()
{
echo -n $"Starting mongod: "
daemon --user "$MONGO_USER" $DAEMON $DAEMON_OPTS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/mongod
}

stop()
{
echo -n $"Stopping mongod: "
killproc -p "$DBPATH"/mongod.lock -d 300 /usr/bin/mongod
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/mongod
}

restart () {
stop
start
}

ulimit -n 12000
RETVAL=0

case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload|force-reload)
restart
;;
condrestart)
[ -f /var/lock/subsys/mongod ] && restart || :
;;
status)
status $DAEMON
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
RETVAL=1
esac

exit $RETVAL

0 comments on commit 28f5000

Please sign in to comment.