diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/tapicero.rb | 26 | ||||
-rw-r--r-- | lib/tapicero/user_database.rb | 53 | ||||
-rw-r--r-- | lib/tapicero/user_event_handler.rb | 44 | ||||
-rw-r--r-- | lib/tapicero/version.rb | 3 | ||||
-rw-r--r-- | lib/tapicero_daemon.rb | 26 |
5 files changed, 91 insertions, 61 deletions
diff --git a/lib/tapicero.rb b/lib/tapicero.rb index e2a8f38..a098287 100644 --- a/lib/tapicero.rb +++ b/lib/tapicero.rb @@ -1,9 +1,6 @@ unless defined? BASE_DIR BASE_DIR = Pathname.new(__FILE__) + '../..' end -unless defined? TAPICERO_CONFIG - TAPICERO_CONFIG = '/etc/leap/tapicero.yaml' -end module Tapicero class <<self @@ -11,27 +8,24 @@ module Tapicero attr_accessor :config end - + # reraise exceptions instead of retrying + # used in tests + unless defined? RERAISE + RERAISE = false + end # # Load Config # this must come first, because CouchRest needs the connection # defined before the models are defined. # require 'couchrest/changes' - configs = ['config/default.yaml', TAPICERO_CONFIG, ARGV.grep(/\.ya?ml$/).first] - self.config = CouchRest::Changes::Config.load(BASE_DIR, *configs) + self.config = CouchRest::Changes::Config.load(BASE_DIR, *CONFIGS) self.logger = CouchRest::Changes::Config.logger # hand flags over to CouchRest::Changes - config.flags = FLAGS - puts "flags: #{FLAGS}" if FLAGS.any? - - # - # Load Tapicero Parts - # - require 'tapicero/user_database' - - def self.user_database(id) - UserDatabase.new(config.couch_host, config.options[:db_prefix] + id) + if defined? FLAGS + config.flags = FLAGS + puts "flags: #{FLAGS}" if FLAGS.any? end + end diff --git a/lib/tapicero/user_database.rb b/lib/tapicero/user_database.rb index 3cfdd70..26d013d 100644 --- a/lib/tapicero/user_database.rb +++ b/lib/tapicero/user_database.rb @@ -4,16 +4,8 @@ require 'json' module Tapicero class UserDatabase - def initialize(host, name) - @host = host - @name = name - end - - def prepare(config) - create - secure(config.options[:security]) - add_design_docs - Tapicero.logger.info "Prepared storage " + name + def initialize(user_id) + @db = couch.database(config.options[:db_prefix] + user_id) end def create @@ -22,7 +14,8 @@ module Tapicero end end - def secure(security) + def secure(security = nil) + security ||= config.options[:security] # let's not overwrite if we have a security doc already return if secured? && !Tapicero::FLAGS.include?('--overwrite-security') retry_request_once "Writing security to" do @@ -42,9 +35,9 @@ module Tapicero end def upload_design_doc(file) - url = design_url(file.basename('.json')) - CouchRest.put url, JSON.parse(file.read) - rescue RestClient::Conflict + old = CouchRest.get design_url(file.basename('.json')) + rescue RestClient::ResourceNotFound + CouchRest.put design_url(file.basename('.json')), JSON.parse(file.read) end @@ -54,28 +47,33 @@ module Tapicero end end + def name + db.name + end + protected def create_db - CouchRest.new(host).create_db(name) - rescue RestClient::PreconditionFailed # database already existed + db.info # test if db exists + rescue RestClient::ResourceNotFound + couch.create_db(db.name) end def delete_db - db = CouchRest.new(host).database(name) db.delete! if db rescue RestClient::ResourceNotFound # no database found end def retry_request_once(action) second_try ||= false - Tapicero.logger.debug "#{action} #{name}" + Tapicero.logger.debug "#{action} #{db.name}" yield rescue RestClient::Exception => exc + raise exc if Tapicero::RERAISE if second_try - log_error "#{action} #{name} failed twice due to:", exc + log_error "#{action} #{db.name} failed twice due to:", exc else - log_error "#{action} #{name} failed due to:", exc + log_error "#{action} #{db.name} failed due to:", exc sleep 5 second_try = true retry @@ -95,13 +93,22 @@ module Tapicero end def security_url - "#{host}/#{name}/_security" + db.root + "/_security" end def design_url(doc_name) - "#{host}/#{name}/_design/#{doc_name}" + db.root + "/_design/#{doc_name}" + end + + attr_reader :db + + def couch + @couch ||= CouchRest.new(config.couch_host) + end + + def config + Tapicero.config end - attr_reader :host, :name end end diff --git a/lib/tapicero/user_event_handler.rb b/lib/tapicero/user_event_handler.rb new file mode 100644 index 0000000..38cf8f8 --- /dev/null +++ b/lib/tapicero/user_event_handler.rb @@ -0,0 +1,44 @@ +require 'tapicero/user_database' +module Tapicero + class UserEventHandler + def initialize(users) + users.created do |hash| + prepare_db(hash['id']) + end + + # Sometimes changes log starts with rev 2. So the + # detection of is currently not working properly + # Working around this until a new version of + # couchrest changes takes this into account. + users.updated do |hash| + prepare_db(hash['id']) + end + + users.deleted do |hash| + destroy_db(hash['id']) + end + end + + protected + + def prepare_db(id) + db = user_database(id) + db.create + db.secure + db.add_design_docs + logger.info "Prepared storage " + db.name + end + + def destroy_db(id) + user_database(id).destroy + end + + def logger + Tapicero.logger + end + + def user_database(id) + UserDatabase.new(id) + end + end +end diff --git a/lib/tapicero/version.rb b/lib/tapicero/version.rb index 1d372e5..8b7a6b0 100644 --- a/lib/tapicero/version.rb +++ b/lib/tapicero/version.rb @@ -1,5 +1,6 @@ module Tapicero - VERSION = "0.3.2" + VERSION = "0.4.0" REQUIRE_PATHS = ['lib'] FLAGS = [] + CONFIGS = ['config/default.yaml', '/etc/leap/tapicero.yaml'] end diff --git a/lib/tapicero_daemon.rb b/lib/tapicero_daemon.rb index 23248e3..89566de 100644 --- a/lib/tapicero_daemon.rb +++ b/lib/tapicero_daemon.rb @@ -4,30 +4,14 @@ # # Daemons.run('tapicero_daemon.rb') # - require 'tapicero' module Tapicero - users = CouchRest::Changes.new('users') - - users.created do |hash| - logger.debug "Created user " + hash['id'] - user_database(hash['id']).prepare(config) - end + module Daemon + require 'tapicero/user_event_handler' + users = CouchRest::Changes.new('users') + UserEventHandler.new(users) + users.listen - # Sometimes changes log starts with rev 2. So the - # detection of is currently not working properly - # Working around this until a new version of - # couchrest changes takes this into account. - users.updated do |hash| - logger.debug "Updated user " + hash['id'] - user_database(hash['id']).prepare(config) end - - users.deleted do |hash| - logger.debug "Deleted user " + hash['id'] - user_database(hash['id']).destroy - end - - users.listen end |