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

A sample start page

parent c21fb79d
......@@ -3,3 +3,4 @@ gem "sinatra", "1.1.2"
gem "sinatra-reloader", "0.5.0"
gem "ohm", "0.1.3"
gem "ohm-contrib", "0.1.0"
gem "haml", "3.0.25"
......@@ -2,6 +2,7 @@ GEM
remote: http://rubygems.org/
specs:
backports (1.18.2)
haml (3.0.25)
monkey-lib (0.5.4)
backports
nest (1.1.0)
......@@ -31,6 +32,7 @@ PLATFORMS
ruby
DEPENDENCIES
haml (= 3.0.25)
ohm (= 0.1.3)
ohm-contrib (= 0.1.0)
sinatra (= 1.1.2)
......
......@@ -20,6 +20,11 @@ _rackup config.ru_
[2011-01-17 08:00:30] INFO ruby 1.9.2 (2010-12-25) [x86_64-linux]
[2011-01-17 08:00:30] INFO WEBrick::HTTPServer#start: pid=15349 port=9292
## Example links
[Noah Start Page](http://localhost:9292/)
If you have Noah running, you can hit the above link for some links created by the setup samples.
## All configs
_curl http://localhost:9292/c/_
......
require 'sinatra/reloader' if development?
require 'ohm'
require 'json'
require 'haml'
require File.join(File.dirname(__FILE__), 'config/db')
require File.join(File.dirname(__FILE__), 'models')
# Read operations
## Hosts
### Still not sold on this pathing structure
# Specifc Host/Specific Service
get '/h/:hostname/s/:servicename/?' do
host = Host.find(:name => params[:hostname]).first
service = Service.find(:name => params[:servicename], :host_id => host.id).first
"#{service.to_json}"
end
# Specific Host/All Services
get '/h/:hostname/s/?' do
host = Host.find(:name => params[:hostname]).first
services = []
......@@ -21,13 +23,13 @@ get '/h/:hostname/s/?' do
"#{services.to_json}"
end
# Specific Host
get '/h/:hostname/?' do
host = Host.find(:name => "#{params[:hostname]}").first
"#{host.to_json}"
end
# Catchall
# All Hosts
get '/h/?' do
hosts = []
Host.all.sort.each {|h| hosts << h.to_hash}
......@@ -36,9 +38,9 @@ end
## Services
get '/s/:servicename/h/:hostname/?' do
s = Service.find(:name => "#{params[:servicename]}").first
h = Host.find(:name => "#{params[:hostname]}", :service_id => s.id).first
"#{h.to_json}"
h = Host.find(:name => params[:hostname]).first.id
s = Service.find(:name => params[:servicename], :host_id => h).first
"#{s.to_json}"
end
get '/s/:servicename/?' do
......@@ -75,7 +77,17 @@ end
get '/c/:appname/:element/?' do
a = Application.find(:name => params[:appname]).first
c = Configuration.find(:name => params[:element], :application_id => a.id).first
"#{c.to_json}"
case c.format
when "json"
content_type 'application/json'
when "xml"
content_type 'text/xml'
when "yaml"
content_type 'text/x-yaml'
else
content_type 'text/plain'
end
"#{c.body}"
end
get '/c/:appname/?' do
......@@ -91,6 +103,9 @@ get '/c/?' do
"#{configs.to_json}"
end
get '/' do
haml :index, :format => :html5
end
# A route for adding a new host
put '/h/:hostname' do
......
......@@ -87,7 +87,9 @@ class Application < Ohm::Model
end
def to_hash
super.merge(:name => name, :updated_at => updated_at)
arr = []
configurations.sort.each {|c| arr << c.to_hash}
super.merge(:name => name, :updated_at => updated_at, :configurations => arr)
end
end
......
......@@ -4,6 +4,8 @@ require 'json'
require File.join(File.dirname(__FILE__), 'config/db')
require File.join(File.dirname(__FILE__), 'models')
# Flush the DB
Ohm.redis.flushdb
# Add an entry for my localhost
puts "Creating Host entry for 'localhost'"
h = Host.create(:name => 'localhost', :state => 1)
......@@ -19,8 +21,49 @@ puts "Creating Application entry for 'noah'"
a = Application.create(:name => 'noah')
if a.save
puts "Creating Configuration entry for 'noah'"
c = Configuration.create(:name => 'db', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a)
a.configurations << c
cr = Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a)
ch = Configuration.create(:name => 'host', :format => 'string', :body => 'localhost', :application => a)
cp = Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a)
%w[cr ch cp].each do |c|
a.configurations << eval(c)
end
end
puts "Creating sample entries - Host and Service"
%w[host1.domain.com host2.domain.com host3.domain.com].each do |host|
h = Host.create(:name => host, :state => 0)
if h.save
%w[http https smtp mysql].each do |service|
s = Service.create(:name => service, :state =>0, :host => h)
h.services << s
end
end
end
puts "Creating sample entries - Application and Configuration"
my_yaml = <<EOY
development:
database: development_database
adapter: mysql
username: dev_user
password: dev_password
EOY
my_json = <<EOJ
{
"id":"hostname",
"data":"localhost"
}
EOJ
a1 = Application.create(:name => 'myrailsapp1')
if a1.save
c1 = Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1)
a1.configurations << c1
end
a2 = Application.create(:name => 'myrestapp1')
if a2.save
c2 = Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2)
a2.configurations << c2
end
puts "Setup successful!"
%html
%head
%title Noah Start Page
#header
%h1 Sample links
#header
%h2 Hosts
%ul
%li
%a{:href => "/h/"} All registered Hosts
%li
%a{:href => "/h/localhost"} localhost (this server)
%li
%a{:href => "/h/localhost/s"} localhost services
%li
%a{:href => "/h/localhost/s/noah"} localhost noah service
#header
%h2 Services
%ul
%li
%a{:href => "/s/"} All registered Services
%li
%a{:href => "/s/noah/h/localhost"} localhost noah service
%li
%a{:href => "/s/http"} All hosts providing 'http'
#header
%h2 Applications
%ul
%li
%a{:href => "/a/"} All registered Applications
%li
%a{:href => "/a/noah"} Noah Application entry
%li
%a{:href => "/a/noah/redis"} Noah Redis configuration entry
#header
%h2 Configurations
%ul
%li
%a{:href => "/c/"} All registered Configurations
%li
%a{:href => "/c/noah"} Noah Configuration entry
%li
%a{:href => "/c/myrailsapp1"} myrailsapp1 Configuration entry
%li
%a{:href => "/c/myrestapp1"} myrestapp1 Configuration entry
%li
%a{:href => "/c/myrailsapp1/database.yml"} database.yml file for myrailsapp1 (should return the proper content-type)
%li
%a{:href => "/c/myrestapp1/config.json"} config.json file for myrestapp1
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