summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-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
7 files changed, 28 insertions, 274 deletions
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