Commit c9f80708 authored by John E. Vincent's avatar John E. Vincent

work on standalone version

parent 671e2221
#!/usr/bin/env ruby
require File.join(File.dirname(__FILE__), './../noah')
NoahApp.run!
logfile: logs/noah.log
loglevel: DEBUG
mode: development
production: "redis://localhost:6379/0"
development: "redis://localhost:6379/1"
test: "redis://localhost:6379/2"
module Noah
module Config
@conf ||= "config/config.yml"
def self.config_file
@conf
end
def self.config_file=(config)
@conf = config
end
def self.configured?
@conf.nil? ? false : true
end
def self.configure!
puts "Configuring Noah from: #{@conf}"
end
end
end
module Noah
module Helpers
def host(opts = {})
Noah::Models::Host.find(opts).first
end
def hosts(opts = {})
Noah::Models::Hosts.all(opts)
end
def service(opts = {})
Service.find(options)
end
def services(opts = {})
Services.all(opts)
end
def host_service(hostname, servicename)
h = Host.find(:name => hostname).first
if h.nil?
nil
else
Service.find(:host_id => h.id, :name => servicename).first
end
end
def host_services(hostname)
h = Host.find(:name => hostname).first
if h.nil?
nil
else
Services.all(:host_id => id)
end
end
def application(opts = {})
Application.find(opts).first
end
def applications(opts = {})
Applications.all(opts)
end
def configuration(opts = {})
Configuration.find(opts).first
end
def configurations(opts = {})
Configurations.all(opts)
end
end
helpers NoahHelpers
end
require 'ohm/contrib'
module Noah
module Models
class Host < Ohm::Model
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
include Ohm::ExtraValidations
attribute :name
attribute :status
collection :services, Service
index :name
index :status
def validate
assert_present :name
assert_present :status
assert_unique :name
assert_member :status, ["up","down","pending"]
end
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
def is_new?
self.created_at == self.updated_at
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 Service < Ohm::Model
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
include Ohm::ExtraValidations
attribute :name
attribute :status
reference :host, Host
index :name
index :status
def validate
assert_present :name
assert_present :status
assert_present :host_id
assert_unique [:name, :host_id]
assert_member :status, ["up", "down", "pending"]
end
def to_hash
super.merge(:name => name, :status => status, :updated_at => updated_at, :host => Host[host_id].name)
end
def is_new?
self.created_at == self.updated_at
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 Configuration < Ohm::Model
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
include Ohm::ExtraValidations
attribute :name
attribute :format
attribute :body
attribute :new_record
reference :application, Application
index :name
def validate
assert_present :name
assert_present :format
assert_present :body
assert_unique [:name, :application_id]
end
def to_hash
super.merge(:name => name, :format => format, :body => body, :update_at => updated_at, :application => Application[application_id].name)
end
def is_new?
self.created_at == self.updated_at
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 Application < Ohm::Model
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
include Ohm::ExtraValidations
attribute :name
collection :configurations, Configuration
index :name
def validate
assert_present :name
assert_unique :name
end
def to_hash
arr = []
configurations.sort.each {|c| arr << c.to_hash}
super.merge(:name => name, :updated_at => updated_at, :configurations => arr)
end
def is_new?
self.created_at == self.updated_at
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 Watcher < Ohm::Model #NYI
include Ohm::Typecast
include Ohm::Timestamping
include Ohm::Callbacks
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
# Some pluralized helper models
class Hosts
def self.all(options = {})
options.empty? ? Host.all.sort : Host.find(options).sort
end
end
class Services
def self.all(options = {})
options.empty? ? Service.all.sort : Service.find(options).sort
end
end
class Applications
def self.all(options = {})
options.empty? ? Application.all.sort : Application.find(options).sort
end
end
class Configurations
def self.all(options = {})
options.empty? ? Configuration.all.sort : Configuration.find(options).sort
end
end
end
end
......@@ -20,8 +20,14 @@ class NoahApp < Sinatra::Base
helpers Sinatra::NoahHelpers
config_file = YAML::load File.new(File.join(File.dirname(__FILE__),'config','db.yml')).read
db = config_file["#{environment}"]
Ohm.connect(:url => "redis://#{db["host"]}:#{db["port"]}/#{db["db"]}")
begin
Ohm.connect(:url => "redis://#{db["host"]}:#{db["port"]}/#{db["db"]}")
Ohm.redis.ping
rescue Errno::ECONNREFUSED => e
puts "Unable to connect to Redis. Shutting down...."
puts e.message
exit 1
end
configure do
set :app_file, __FILE__
set :root, File.dirname(__FILE__)
......@@ -30,6 +36,9 @@ class NoahApp < Sinatra::Base
set :logging, true
set :raise_errors, false
set :show_exceptions, false
log = File.new("logs/noah.log", "a")
STDOUT.reopen(log)
STDERR.reopen(log)
end
configure(:development) do
require 'sinatra/reloader'
......
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