summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathieu Bornoz <mathieu.bornoz@camptocamp.com>2010-11-12 16:04:49 +0100
committerMathieu Bornoz <mathieu.bornoz@camptocamp.com>2010-11-12 16:06:42 +0100
commitffab59de517086a3dc6372e5ec01fc789539030e (patch)
tree0a73e4962221f11178b95516f100d3187047c166
initial import of couchdb module
-rw-r--r--README.rst9
-rw-r--r--lib/puppet/parser/functions/couchdblookup.rb38
-rw-r--r--manifests/classes/couchdb-backup.pp38
-rw-r--r--manifests/classes/couchdb-base.pp13
-rw-r--r--manifests/classes/couchdb-debian.pp1
-rw-r--r--manifests/classes/couchdb-params.pp18
-rw-r--r--manifests/init.pp12
-rw-r--r--templates/couchdb-backup.py.erb32
8 files changed, 161 insertions, 0 deletions
diff --git a/README.rst b/README.rst
new file mode 100644
index 0000000..2158f4d
--- /dev/null
+++ b/README.rst
@@ -0,0 +1,9 @@
+=======================
+Couchdb Puppet module
+=======================
+
+This module is provided to you by Camptocamp_.
+
+.. _Camptocamp: http://www.camptocamp.com/
+
+For more information about couchdb see http://couchdb.apache.org/
diff --git a/lib/puppet/parser/functions/couchdblookup.rb b/lib/puppet/parser/functions/couchdblookup.rb
new file mode 100644
index 0000000..807c138
--- /dev/null
+++ b/lib/puppet/parser/functions/couchdblookup.rb
@@ -0,0 +1,38 @@
+#
+# A basic function to retrieve data in couchdb
+#
+
+require 'json'
+require 'open-uri'
+
+module Puppet::Parser::Functions
+ newfunction(:couchdblookup, :type => :rvalue) do |args|
+
+ url = args[0]
+ key = args[1]
+
+ raise Puppet::ParseError, ("couchdblookup(): wrong number of arguments (#{args.length}; must be == 2)") if args.length != 2
+
+ begin
+ json = JSON.parse(open(URI.parse(url)).read)
+ rescue OpenURI::HTTPError => error
+ raise Puppet::ParseError, "couchdblookup(): fetching URL #{url} failed with status #{error.message}"
+ end
+
+ result = nil
+ if json.has_key?("rows") and json['total_rows'] > 0 and json['rows'][0].has_key?(key)
+ result = Array.new
+ json['rows'].each do |x|
+ result.push(x[key])
+ end
+ else
+ if json.has_key?(key)
+ result = json[key]
+ end
+ end
+
+ result or raise Puppet::ParseError, "couchdblookup(): key '#{key}' not found in JSON object !"
+
+ end
+end
+
diff --git a/manifests/classes/couchdb-backup.pp b/manifests/classes/couchdb-backup.pp
new file mode 100644
index 0000000..6baea1d
--- /dev/null
+++ b/manifests/classes/couchdb-backup.pp
@@ -0,0 +1,38 @@
+class couchdb::backup {
+
+ include couchdb::params
+
+ # used in ERB templates
+ $bind_address = $couchdb::params::bind_address
+ $port = $couchdb::params::port
+ $backupdir = $couchdb::params::backupdir
+
+ file {$couchdb::params::backupdir:
+ ensure => directory,
+ mode => 755,
+ require => Package["couchdb"],
+ }
+
+ file { "/usr/local/sbin/couchdb-backup.py":
+ ensure => present,
+ owner => root,
+ group => root,
+ mode => 755,
+ content => template("couchdb/couchdb-backup.py.erb"),
+ require => [ File[$couchdb::params::backupdir], Exec["install python-couchdb"] ],
+ }
+
+ cron { "couchdb-backup":
+ command => "/usr/local/sbin/couchdb-backup.py 2> /dev/null",
+ hour => 3,
+ minute => 0,
+ require => File["/usr/local/sbin/couchdb-backup.py"],
+ }
+
+ exec {"install python-couchdb":
+ command => "easy_install couchdb",
+ require => Package["python-setuptools"],
+ unless => "grep -q CouchDB-0.8-py2.5.egg /usr/lib/python2.5/site-packages/easy-install.pth"
+ }
+
+}
diff --git a/manifests/classes/couchdb-base.pp b/manifests/classes/couchdb-base.pp
new file mode 100644
index 0000000..9713f9b
--- /dev/null
+++ b/manifests/classes/couchdb-base.pp
@@ -0,0 +1,13 @@
+class couchdb::base {
+
+ package {["couchdb","libjs-jquery"]:
+ ensure => present,
+ }
+
+ service {"couchdb":
+ ensure => running,
+ hasstatus => true,
+ require => Package["couchdb"],
+ }
+
+}
diff --git a/manifests/classes/couchdb-debian.pp b/manifests/classes/couchdb-debian.pp
new file mode 100644
index 0000000..1b5090d
--- /dev/null
+++ b/manifests/classes/couchdb-debian.pp
@@ -0,0 +1 @@
+class couchdb::debian inherits couchdb::base {}
diff --git a/manifests/classes/couchdb-params.pp b/manifests/classes/couchdb-params.pp
new file mode 100644
index 0000000..df59ad0
--- /dev/null
+++ b/manifests/classes/couchdb-params.pp
@@ -0,0 +1,18 @@
+class couchdb::params {
+
+ $bind_address = $couchdb_bind_address ? {
+ "" => "127.0.0.1",
+ default => $couchdb_bind_address,
+ }
+
+ $port = $couchdb_port ? {
+ "" => "5984",
+ default => $couchdb_port,
+ }
+
+ $backupdir = $couchdb_backupdir ? {
+ "" => "/var/backups/couchdb",
+ default => $couchdb_backupdir,
+ }
+
+}
diff --git a/manifests/init.pp b/manifests/init.pp
new file mode 100644
index 0000000..5100da0
--- /dev/null
+++ b/manifests/init.pp
@@ -0,0 +1,12 @@
+import "classes/*.pp"
+
+class couchdb {
+ case $operatingsystem {
+ Debian: {
+ case $lsbdistcodename {
+ lenny : { include couchdb::debian }
+ default: { fail "couchdb not available for ${operatingsystem}/${lsbdistcodename}"}
+ }
+ }
+ }
+}
diff --git a/templates/couchdb-backup.py.erb b/templates/couchdb-backup.py.erb
new file mode 100644
index 0000000..4fa311f
--- /dev/null
+++ b/templates/couchdb-backup.py.erb
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+# file manage by puppet
+
+import os
+import gzip
+import tarfile
+import datetime
+import urllib2
+import simplejson
+import couchdb.tools.dump
+from os.path import join
+
+DB_URL="http://<%= bind_address %>:<%= port %>"
+DUMP_DIR="<%= backupdir %>"
+TODAY=datetime.datetime.today().strftime("%A").lower()
+
+ftar = os.path.join(DUMP_DIR,"%s.tar" % TODAY)
+tmp_ftar = os.path.join(DUMP_DIR,"_%s.tar" % TODAY)
+tar = tarfile.open(tmp_ftar, "w")
+
+databases = simplejson.load(urllib2.urlopen("%s/_all_dbs" % DB_URL))
+
+for db in databases:
+ db_file = os.path.join(DUMP_DIR,"%s.gz" % db)
+ f = gzip.open(db_file, 'wb')
+ couchdb.tools.dump.dump_db(os.path.join(DB_URL,db), output=f)
+ f.close()
+ tar.add(db_file,"%s.gz" % db)
+ os.remove(db_file)
+
+tar.close()
+os.rename(tmp_ftar,ftar)