summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2013-12-22 06:33:21 -0800
committerazul <azul@riseup.net>2013-12-22 06:33:21 -0800
commit41dee6ee7242a731763bb1b75e12cc97403aa4a4 (patch)
treedfacb19930d1c28cf53fe041cc8151fe39fe358e
parent8104b882032a851604ba6a2a904c7e267523f158 (diff)
parente4df501035434cbb1920ccca21e489599b5ad382 (diff)
Merge pull request #5 from azul/feature/use-couchrest-changes
Version 0.2.0: use CouchRest::Changes
-rw-r--r--Gemfile.lock7
-rw-r--r--config/default.yaml38
-rw-r--r--lib/tapicero.rb37
-rw-r--r--lib/tapicero/config.rb99
-rw-r--r--lib/tapicero/couch_changes.rb89
-rw-r--r--lib/tapicero/couch_stream.rb28
-rw-r--r--lib/tapicero/json_stream.rb31
-rw-r--r--lib/tapicero/version.rb2
-rw-r--r--lib/tapicero_daemon.rb16
-rw-r--r--tapicero.gemspec1
10 files changed, 55 insertions, 293 deletions
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 <<self
attr_accessor :logger
+ attr_accessor :config
end
-end
-#
-# Load Config
-# this must come first, because CouchRest needs the connection defined before the models are defined.
-#
-require 'tapicero/config'
-Tapicero::Config.load(BASE_DIR, 'config/default.yaml', LEAP_CA_CONFIG, ARGV.grep(/\.ya?ml$/).first)
+ #
+ # 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.logger = CouchRest::Changes::Config.logger
+
+ #
+ # Load Tapicero Parts
+ #
+ require 'tapicero/user_database'
-#
-# Load Tapicero
-#
-# require 'tapicero/json_stream'
-# require 'tapicero/couch_stream'
-require 'tapicero/couch_changes'
-require 'tapicero/user_database'
+ def self.user_database(id)
+ UserDatabase.new(config.couch_host, config.options[:db_prefix] + id)
+ end
+end
diff --git a/lib/tapicero/config.rb b/lib/tapicero/config.rb
deleted file mode 100644
index 7d7d0e1..0000000
--- a/lib/tapicero/config.rb
+++ /dev/null
@@ -1,99 +0,0 @@
-require 'yaml'
-
-module Tapicero
- module Config
- extend self
-
- attr_accessor :users_db_name
- attr_accessor :db_prefix
- attr_accessor :couch_connection
- attr_accessor :security
- attr_accessor :seq_file
- attr_accessor :log_file
- attr_accessor :log_level
-
- def self.load(base_dir, *configs)
- loaded = configs.collect do |file_path|
- file = find_file(base_dir, file_path)
- load_config(file)
- end
- init_logger
- log_loaded_configs(loaded.compact)
- end
-
- def couch_host(conf = nil)
- conf ||= couch_connection
- userinfo = [conf[:username], conf[:password]].compact.join(':')
- userinfo += '@' unless userinfo.empty?
- "#{conf[:protocol]}://#{userinfo}#{conf[:host]}:#{conf[:port]}"
- end
-
- def couch_host_without_password
- couch_host couch_connection.merge({:password => 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"