Commit 82605a8c authored by John E. Vincent's avatar John E. Vincent

missed some files

parent f46e9723
require File.join(File.dirname(__FILE__), 'configurations')
module Noah
class Application < Model
collection :configurations, Configuration
def to_hash
arr = []
configurations.sort.each {|c| arr << c.to_hash}
super.merge(:name => name, :created_at => created_at, :updated_at => updated_at, :configurations => arr)
end
class << self
def find_or_create(opts = {})
begin
find(opts).first.nil? ? (app = create(opts)) : (app = find(opts).first)
if app.valid?
app.save
end
app
rescue Exception => e
e.message
end
end
end
end
class Applications
def self.all(options = {})
options.empty? ? Application.all.sort : Application.find(options).sort
end
end
end
module Noah
class Configuration < Model
attribute :format
attribute :body
attribute :new_record
reference :application, Application
index :format
index :body
def validate
super
assert_present :format
assert_present :body
assert_present :application_id
assert_unique [:name, :application_id]
end
def to_hash
Application[application_id].nil? ? app_name=nil : app_name=Application[application_id].name
super.merge(:name => name, :format => format, :body => body, :created_at => created_at, :updated_at => updated_at, :application => app_name)
end
class << self
def find_or_create(opts={})
begin
if find(opts).first.nil?
conf = create(opts)
else
conf = find(opts).first
end
rescue Exception => e
e.message
end
end
end
end
class Configurations
def self.all(options = {})
options.empty? ? Configuration.all.sort : Configuration.find(options).sort
end
end
end
require File.join(File.dirname(__FILE__), 'services')
module Noah
class Host < Model
# Host model
# @return {Host} a {Host} object
attribute :status
collection :services, Service
index :status
def validate
super
assert_present :status
assert_unique :name
assert_member :status, ["up","down","pending"]
end
# @return [Hash] A hash representation of a {Host}
def to_hash
arr = []
services.sort.each {|s| arr << s.to_hash}
h = {:name => name, :status => status, :created_at => created_at, :updated_at => updated_at, :services => arr}
super.merge(h)
end
class << self
def find_or_create(opts = {})
begin
# exclude requested status from lookup
h = find(opts.reject{|key,value| key == :status}).first
host = h.nil? ? create(opts) : h
host.status = opts[:status]
if host.valid?
host.save
end
host
rescue Exception => e
e.message
end
end
end
end
class Hosts
# @param [Hash] optional filters for results
# @return [Array] Array of {Host} objects
def self.all(options = {})
options.empty? ? Noah::Host.all.sort : Noah::Host.find(options).sort
end
end
end
......@@ -35,11 +35,21 @@ module Noah
@deleted_name = self.name
end
def dbnum
require 'uri'
o = Ohm.options.first
if o[:db].nil? && o[:url].nil?
# We're on db 0
"0"
else
o[:db].nil? ? "#{o[:url].split('/').last}" : "#{o[:db]}"
end
end
["save", "create", "update", "delete"].each do |meth|
class_eval do
define_method("notify_via_redis_#{meth}".to_sym) do
self.name.nil? ? name=@deleted_name : name=self.name
pub_category = "noah.#{self.class.to_s}[name].#{meth}"
pub_category = "#{dbnum}:noah.#{self.class.to_s}[name].#{meth}"
Ohm.redis.publish(pub_category, self.to_json)
end
end
......@@ -47,8 +57,8 @@ module Noah
end
end
require File.join(File.dirname(__FILE__), 'hosts')
require File.join(File.dirname(__FILE__), 'services')
require File.join(File.dirname(__FILE__), 'applications')
require File.join(File.dirname(__FILE__), 'configurations')
require File.join(File.dirname(__FILE__), 'watchers')
require File.join(File.dirname(__FILE__), 'models','hosts')
require File.join(File.dirname(__FILE__), 'models','services')
require File.join(File.dirname(__FILE__), 'models','applications')
require File.join(File.dirname(__FILE__), 'models','configurations')
require File.join(File.dirname(__FILE__), 'models','watchers')
module Noah
class Service < Model
attribute :status
reference :host, Host
index :status
def validate
super
assert_present :status
assert_present :host_id
assert_unique [:name, :host_id]
assert_member :status, ["up", "down", "pending"]
end
def to_hash
Host[host_id].nil? ? host_name=nil : host_name=Host[host_id].name
super.merge(:name => name, :status => status, :updated_at => updated_at, :host => host_name)
end
class << self
def find_or_create(opts = {})
begin
# convert passed host object to host_id if passed
if opts.has_key?(:host)
opts.merge!({:host_id => opts[:host].id})
opts.reject!{|key, value| key == :host}
end
# exclude requested status from lookup
s = find(opts.reject{|key,value| key == :status}).first
service = s.nil? ? create(opts) : s
service.status = opts[:status]
if service.valid?
service.save
end
service
rescue Exception => e
e.message
end
end
end
end
class Services
def self.all(options = {})
options.empty? ? Service.all.sort : Service.find(options).sort
end
end
end
module Noah
class Watcher < Model #NYI
attribute :client
attribute :endpoint
attribute :event
attribute :action
index :client
index :event
def validate
assert_present :client, :endpoint, :event, :action
assert_unique [:client, :endpoint, :event, :action]
end
end
class Watchers
def self.all(options = {})
options.empty? ? Watcher.all.sort : Watcher.find(options).sort
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment