From 54f3815e85a37958b6d49e99e5b8d7f04ed3c5b8 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 16 Sep 2013 16:00:21 +0200 Subject: couch-doc-update now also works for _security It also clears doc fields that are not set in the new data when updating a doc. --- files/couch-doc-update | 58 ++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/files/couch-doc-update b/files/couch-doc-update index f448046..7cc5799 100644 --- a/files/couch-doc-update +++ b/files/couch-doc-update @@ -32,42 +32,49 @@ require 'couchrest' require 'json' def main + db, id, data = process_options + result = set_document(db, id, data) + exit 0 if result['ok'] + raise StandardError.new(result.inspect) +rescue StandardError => exc + puts "ERROR: " + exc.to_s + exit 1 +end + +def process_options # # parse options # - @host = nil - @db_name = nil - @doc_id = nil - @new_data = nil + host = nil + db_name = nil + doc_id = nil + new_data = nil loop do case ARGV[0] - when '--host' then ARGV.shift; @host = ARGV.shift - when '--db' then ARGV.shift; @db_name = ARGV.shift - when '--id' then ARGV.shift; @doc_id = ARGV.shift - when '--data' then ARGV.shift; @new_data = ARGV.shift + when '--host' then ARGV.shift; host = ARGV.shift + when '--db' then ARGV.shift; db_name = ARGV.shift + when '--id' then ARGV.shift; doc_id = ARGV.shift + when '--data' then ARGV.shift; new_data = ARGV.shift when /^-/ then usage("Unknown option: #{ARGV[0].inspect}") else break end end - usage("Missing required option") unless @db_name && @doc_id && @new_data - @new_data = JSON.parse(@new_data) + usage("Missing required option") unless db_name && doc_id && new_data + new_data = JSON.parse(new_data) + db = CouchRest.database(connection_string(db_name, host)) + return db, doc_id, new_data +end # # update document # - begin - @db = CouchRest.database(connection_string(@db_name, @host)) - @doc = get_document(@db, @doc_id) - result = if @doc - update_document(@db, @doc, @new_data) - else - create_document(@db, @doc_id, @new_data) - end - exit 0 if result['ok'] - raise StandardError.new(result.inspect) - rescue StandardError => exc - puts "ERROR: " + exc.to_s - exit 1 +def set_document(db, id, data) + doc = get_document(db, id) + if doc + doc.id ||= id + update_document(db, doc, data) + else + create_document(db, id, data) end end @@ -80,9 +87,8 @@ def get_document(db, doc_id) end def update_document(db, doc, data) - data.each do |key, value| - doc[key] = value - end + doc.reject! {|k,v| !k.start_with? '_'} + doc.merge! data db.save_doc(doc) end -- cgit v1.2.3 From a6d9c2dea3d67f7368fef373e96bfb0fda612851 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 16 Sep 2013 16:00:40 +0200 Subject: Revert "don't use couchdb::document for creating _security, cause this special doc doesn't have an _id (#3706)" This reverts commit dacaeb4d98be9468336923f5501822d389bda768. couch-doc-update and thus couchdb::document now also work for _security documents. We simply add the given _id when none is present in the existing doc. Using the normal saving of CouchRest::Document then works. --- manifests/create_db.pp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/manifests/create_db.pp b/manifests/create_db.pp index 32bc974..690892c 100644 --- a/manifests/create_db.pp +++ b/manifests/create_db.pp @@ -4,6 +4,8 @@ define couchdb::create_db ( $readers="{\"names\": [], \"roles\": [] }" ) { + Couchdb::Query["create_db_${name}"] -> Couchdb::Document["${name}_security"] + couchdb::query { "create_db_${name}": cmd => 'PUT', host => $host, @@ -11,11 +13,10 @@ define couchdb::create_db ( unless => "/usr/bin/curl -s --netrc-file /etc/couchdb/couchdb.netrc ${host}/${name} | grep -q -v '{\"error\":\"not_found\"'" } - couchdb::query { "${name}_security": - cmd => 'PUT', - host => $host, - path => "${name}/_security", - data => "{ \"admins\": ${admins}, \"readers\": ${readers} }", - require => Couchdb::Query["create_db_${name}"] + couchdb::document { "${name}_security": + db => $name, + id => '_security', + host => $host, + data => "{ \"admins\": ${admins}, \"readers\": ${readers} }" } } -- cgit v1.2.3 From 1eff45c923e2ff196e3b52d1033185453358e047 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 20 Sep 2013 09:27:24 +0200 Subject: use require => rather than the -> syntax --- manifests/create_db.pp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/manifests/create_db.pp b/manifests/create_db.pp index 690892c..9335e4b 100644 --- a/manifests/create_db.pp +++ b/manifests/create_db.pp @@ -4,8 +4,6 @@ define couchdb::create_db ( $readers="{\"names\": [], \"roles\": [] }" ) { - Couchdb::Query["create_db_${name}"] -> Couchdb::Document["${name}_security"] - couchdb::query { "create_db_${name}": cmd => 'PUT', host => $host, @@ -17,6 +15,7 @@ define couchdb::create_db ( db => $name, id => '_security', host => $host, - data => "{ \"admins\": ${admins}, \"readers\": ${readers} }" + data => "{ \"admins\": ${admins}, \"readers\": ${readers} }", + require => Couchdb::Query["create_db_${name}"] } } -- cgit v1.2.3