From e4df501035434cbb1920ccca21e489599b5ad382 Mon Sep 17 00:00:00 2001 From: Azul Date: Thu, 19 Dec 2013 13:18:28 +0100 Subject: Version 0.2.0: use CouchRest::Changes This also changes the format of the config file. Please make sure to adjust it. --- Gemfile.lock | 7 ++- config/default.yaml | 38 +++++++++-------- lib/tapicero.rb | 37 +++++++++------- lib/tapicero/config.rb | 99 ------------------------------------------- lib/tapicero/couch_changes.rb | 89 -------------------------------------- lib/tapicero/couch_stream.rb | 28 ------------ lib/tapicero/json_stream.rb | 31 -------------- lib/tapicero/version.rb | 2 +- lib/tapicero_daemon.rb | 16 +++---- tapicero.gemspec | 1 + 10 files changed, 55 insertions(+), 293 deletions(-) delete mode 100644 lib/tapicero/config.rb delete mode 100644 lib/tapicero/couch_changes.rb delete mode 100644 lib/tapicero/couch_stream.rb delete mode 100644 lib/tapicero/json_stream.rb diff --git a/Gemfile.lock b/Gemfile.lock index f9bfda6..c428bd7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,8 +1,9 @@ PATH remote: . specs: - tapicero (0.1.0) + tapicero (0.2.0) couchrest (~> 1.1.3) + couchrest_changes (~> 0.0.1) daemons syslog_logger (~> 2.0.0) yajl-ruby @@ -14,6 +15,10 @@ GEM mime-types (~> 1.15) multi_json (~> 1.0) rest-client (~> 1.6.1) + couchrest_changes (0.0.1) + couchrest (~> 1.1.3) + syslog_logger (~> 2.0.0) + yajl-ruby daemons (1.1.9) highline (1.6.19) metaclass (0.0.1) diff --git a/config/default.yaml b/config/default.yaml index 018f151..25792db 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -2,29 +2,15 @@ # Default configuration options for Tapicero # -# database to observe for changes: -users_db_name: "users" - -# prefix for per user databases: -db_prefix: "user-" - # couch connection configuration -couch_connection: +connection: protocol: "http" host: "localhost" port: 5984 username: ~ password: ~ - -# security settings to be used for the per user databases -security: - admins: - names: [] - roles: [] - members: - names: - - soledad - roles: [] + prefix: "" + suffix: "" # file to store the last processed user record in so we can resume after # a restart: @@ -32,4 +18,20 @@ seq_file: "/var/log/leap/tapicero.seq" # Configure log_file like this if you want to log to a file instead of syslog: # log_file: "/var/leap/log/tapicero.log" -log_level: info +log_level: debug + +# tapicero specific options +options: + # prefix for per user databases: + db_prefix: "user-" + + # security settings to be used for the per user databases + security: + admins: + names: [] + roles: [] + members: + names: + - soledad + roles: [] + diff --git a/lib/tapicero.rb b/lib/tapicero.rb index fd66030..aba9fad 100644 --- a/lib/tapicero.rb +++ b/lib/tapicero.rb @@ -1,28 +1,33 @@ unless defined? BASE_DIR BASE_DIR = File.expand_path('../..', __FILE__) end -unless defined? LEAP_CA_CONFIG - LEAP_CA_CONFIG = '/etc/leap/tapicero.yaml' +unless defined? TAPICERO_CONFIG + TAPICERO_CONFIG = '/etc/leap/tapicero.yaml' end module Tapicero class < nil}) - end - - private - - def init_logger - if log_file - require 'logger' - Tapicero.logger = Logger.new(log_file) - else - require 'syslog/logger' - Tapicero.logger = Syslog::Logger.new('tapicero') - end - Tapicero.logger.level = Logger.const_get(log_level.upcase) - end - - def load_config(file_path) - return unless file_path - load_settings YAML.load(File.read(file_path)) - return file_path - rescue NoMethodError => exc - init_logger - Tapicero.logger.fatal "Error in file #{file_path}" - Tapicero.logger.fatal exc - exit(1) - end - - def load_settings(hash) - return unless hash - hash.each do |key, value| - apply_setting(key, value) - end - end - - def apply_setting(key, value) - if value.is_a? Hash - value = symbolize_keys(value) - end - self.send("#{key}=", value) - rescue NoMethodError => exc - STDERR.puts "'#{key}' is not a valid option" - raise exc - end - - def self.symbolize_keys(hsh) - newhsh = {} - hsh.keys.each do |key| - newhsh[key.to_sym] = hsh[key] - end - newhsh - end - - def self.find_file(base_dir, file_path) - return nil unless file_path - if defined? CWD - return File.expand_path(file_path, CWD) if File.exists?(File.expand_path(file_path, CWD)) - end - return File.expand_path(file_path, base_dir) if File.exists?(File.expand_path(file_path, base_dir)) - return nil - end - - def log_loaded_configs(files) - files.each do |file| - Tapicero.logger.info "Loaded config from #{file} ." - end - end - end -end diff --git a/lib/tapicero/couch_changes.rb b/lib/tapicero/couch_changes.rb deleted file mode 100644 index fddebec..0000000 --- a/lib/tapicero/couch_changes.rb +++ /dev/null @@ -1,89 +0,0 @@ -require 'couchrest' -require 'fileutils' - -module Tapicero - class CouchChanges - - attr_accessor :db - - def initialize(db, seq_filename) - @db = db - @seq_filename = seq_filename - read_seq(seq_filename) - end - - def created(hash = {}, &block) - if block_given? - @created = block - else - @created && @created.call(hash) - end - end - - def deleted(hash = {}, &block) - if block_given? - @deleted = block - else - @deleted && @deleted.call(hash) - end - end - - def listen - Tapicero.logger.info "listening..." - Tapicero.logger.debug "Starting at sequence #{since}" - result = db.changes :feed => :continuous, :since => since, :heartbeat => 1000 do |hash| - callbacks(hash) - store_seq(hash["seq"]) - end - Tapicero.logger.info "couch stream ended unexpectedly." - Tapicero.logger.debug result.inspect - end - - protected - - def since - @since ||= 0 # fetch_last_seq - end - - def callbacks(hash) - #changed callback - # let's not track design document changes - return if hash['id'].start_with? '_design/' - return unless changes = hash["changes"] - return deleted(hash) if hash["deleted"] - created(hash) if changes[0]["rev"].start_with?('1-') - #updated callback - end - - def read_seq(seq_filename) - Tapicero.logger.debug "Looking up sequence here: #{seq_filename}" - FileUtils.touch(seq_filename) - unless File.writable?(seq_filename) - raise StandardError.new("Can't access sequence file") - end - @since = File.read(seq_filename) - if @since == '' - @since = nil - Tapicero.logger.debug "Found no sequence in the file." - else - Tapicero.logger.debug "Found sequence: #{@since}" - end - rescue Errno::ENOENT => e - Tapicero.logger.warn "No sequence file found. Starting from scratch" - end - - def store_seq(seq) - File.write(@seq_filename, seq.to_json) - end - - # - # UNUSED: this is useful for only following new sequences. - # - def fetch_last_seq - hash = db.changes :limit => 1, :descending => true - Tapicero.logger.info "starting at seq: " + hash["last_seq"] - return hash["last_seq"] - end - - end -end diff --git a/lib/tapicero/couch_stream.rb b/lib/tapicero/couch_stream.rb deleted file mode 100644 index b63a9fd..0000000 --- a/lib/tapicero/couch_stream.rb +++ /dev/null @@ -1,28 +0,0 @@ -# -# UNUSED: we currently use CouchRest's streamer instead. Still keeping this -# around because it's a simple alternative that works. -# - -module Tapicero - class CouchStream - def initialize(database_url) - @database_url = database_url - end - - def get(path, options) - url = url_for(path, options) - # puts url - Tapicero::JsonStream.get(url, :symbolize_keys => true) do |hash| - yield(hash) - end - end - - protected - - def url_for(path, options = {}) - url = [@database_url, path].join('/') - url += '?' if options.any? - url += options.map {|k,v| "#{k}=#{v}"}.join('&') - end - end -end diff --git a/lib/tapicero/json_stream.rb b/lib/tapicero/json_stream.rb deleted file mode 100644 index 64b160f..0000000 --- a/lib/tapicero/json_stream.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'net/http' -require 'uri' -require 'yajl' - -# UNUSED: We're currently using couchrest instead as that is what we use all -# over the place. It internally uses curl to fetch the stream. -# -# Since Yajl HTTP Stream will go a way in version 2.0 here's a simple substitude. -# -module Tapicero - class JsonStream - - def self.get(url, options, &block) - uri = URI(url) - parser = Yajl::Parser.new(options) - parser.on_parse_complete = block - Net::HTTP.start(uri.host, uri.port) do |http| - request = Net::HTTP::Get.new uri.request_uri - - - http.request request do |response| - response.read_body do |chunk| - parser << chunk - end - end - end - - end - end -end - diff --git a/lib/tapicero/version.rb b/lib/tapicero/version.rb index ec18e78..3688f62 100644 --- a/lib/tapicero/version.rb +++ b/lib/tapicero/version.rb @@ -1,4 +1,4 @@ module Tapicero - VERSION = "0.1.0" + VERSION = "0.2.0" REQUIRE_PATHS = ['lib'] end diff --git a/lib/tapicero_daemon.rb b/lib/tapicero_daemon.rb index 9223acb..9020fa2 100644 --- a/lib/tapicero_daemon.rb +++ b/lib/tapicero_daemon.rb @@ -8,22 +8,18 @@ require 'tapicero' module Tapicero - Tapicero.logger.info "Observing #{Config.couch_host_without_password}" - Tapicero.logger.info "Tracking #{Config.users_db_name}" - # stream = CouchStream.new(Config.couch_host + '/' + Config.users_db_name) - db = CouchRest.new(Config.couch_host).database(Config.users_db_name) - users = CouchChanges.new(db, Config.seq_file) + users = CouchRest::Changes.new('users') users.created do |hash| - Tapicero.logger.debug "Created user " + hash['id'] - db = UserDatabase.new(Config.couch_host, Config.db_prefix + hash['id']) + logger.debug "Created user " + hash['id'] + db = user_database(hash['id']) db.create - db.secure(Config.security) + db.secure(config.options[:security]) end users.deleted do |hash| - Tapicero.logger.debug "Deleted user " + hash['id'] - db = UserDatabase.new(Config.couch_host, Config.db_prefix + hash['id']) + logger.debug "Deleted user " + hash['id'] + db = user_database(hash['id']) db.destroy end diff --git a/tapicero.gemspec b/tapicero.gemspec index dd3a4ca..73582bc 100644 --- a/tapicero.gemspec +++ b/tapicero.gemspec @@ -19,6 +19,7 @@ Gem::Specification.new do |s| s.executables << 'tapicero' s.add_dependency "couchrest", "~> 1.1.3" + s.add_dependency "couchrest_changes", "~> 0.0.1" s.add_dependency "daemons" s.add_dependency "yajl-ruby" s.add_dependency "syslog_logger", "~> 2.0.0" -- cgit v1.2.3