Commit 6773fbb6 authored by John E. Vincent's avatar John E. Vincent

Updating sample data script. Tagging work

parent 5ea50e75
...@@ -40,75 +40,9 @@ end ...@@ -40,75 +40,9 @@ end
desc "Populate database with sample dataset" desc "Populate database with sample dataset"
task :sample, :redis_url do |t, args| task :sample, :redis_url do |t, args|
require 'noah' require 'noah'
Ohm::connect(:url => args.redis_url) Ohm::connect(:url => args.redis_url)
Ohm::redis.flushdb sample_data = File.expand_path(File.join("..", "spec", "support","sample_data.rb"),__FILE__)
puts "Creating watchers..." instance_eval(File.open(sample_data).read)
Noah::Watcher.create :endpoint => "dummy://applications", :pattern => "//noah/applications"
Noah::Watcher.create :endpoint => "dummy://configurations", :pattern => "//noah/configurations"
Noah::Watcher.create :endpoint => "dummy://hosts", :pattern => "//noah/hosts"
Noah::Watcher.create :endpoint => "dummy://services", :pattern => "//noah/services"
Noah::Watcher.create :endpoint => "dummy://ephemerals", :pattern => "//noah/ephemerals"
puts "Creating Host entry for 'localhost'"
h = Noah::Host.create(:name => 'localhost', :status => "up")
if h.save
%w[redis noah].each do |service|
puts "Create Service entry for #{service}"
s = Noah::Service.create(:name => service, :status => "up", :host => h)
h.services << s
end
end
puts "Creating Application entry for 'noah'"
a = Noah::Application.create(:name => 'noah')
if a.save
puts "Creating Configuration entry for 'noah'"
cr = Noah::Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0')
ch = Noah::Configuration.create(:name => 'host', :format => 'string', :body => 'localhost')
cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292')
%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 = Noah::Host.create(:name => host, :status => "up")
if h.save
%w[http https smtp mysql].each do |service|
s = Noah::Service.create(:name => service, :status => "pending", :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 = Noah::Application.create(:name => 'myrailsapp1')
if a1.save
c1 = Noah::Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml)
a1.configurations << c1
end
a2 = Noah::Application.create(:name => 'myrestapp1')
if a2.save
c2 = Noah::Configuration.create(:name => 'config.json', :format => 'json', :body => my_json)
a2.configurations << c2
end
puts "Sample data populated!"
end end
begin begin
......
...@@ -19,6 +19,14 @@ class Noah::App ...@@ -19,6 +19,14 @@ class Noah::App
end end
end end
put '/applications/:appname/tag' do |appname|
required_params = ["tags"]
data = JSON.parse(request.body.read)
(data.keys.sort == required_params.sort) ? (a=Noah::Application.find(:name=>appname).first) : (raise "Missing Parameters")
a.nil? ? (halt 404) : (a.tag!(data['tags']))
a.to_json
end
put '/applications/:appname/watch' do |appname| put '/applications/:appname/watch' do |appname|
required_params = ["endpoint"] required_params = ["endpoint"]
data = JSON.parse(request.body.read) data = JSON.parse(request.body.read)
...@@ -27,6 +35,10 @@ class Noah::App ...@@ -27,6 +35,10 @@ class Noah::App
w.to_json w.to_json
end end
put '/applications/:appname/link/:linkname' do |appname, linkname|
end
put '/applications/:appname/?' do |appname| put '/applications/:appname/?' do |appname|
required_params = ["name"] required_params = ["name"]
data = JSON.parse(request.body.read) data = JSON.parse(request.body.read)
......
class Noah::App class Noah::App
get '/*/?' do get '/:link_name/:model_name/?' do |path, model|
path = params[:splat][0] link_name = Noah::Link.find(:path => "/"+path).first
(halt 404) if link_name.nil?
(halt 404) if link_name.to_hash.has_key?(model.to_sym) == false
link_name.to_hash[model.to_sym].to_json
end
get '/:link_name/?' do |path|
link_name = Noah::Link.find(:path => "/"+path).first link_name = Noah::Link.find(:path => "/"+path).first
(halt 404) if link_name.nil? (halt 404) if link_name.nil?
link_name.to_json link_name.to_json
......
...@@ -37,9 +37,8 @@ module Noah ...@@ -37,9 +37,8 @@ module Noah
path.nil? ? (raise ArgumentError, "Must provide a path") : p=path path.nil? ? (raise ArgumentError, "Must provide a path") : p=path
begin begin
l = Link.new :path => p, :source => base_pattern l = Link.find_or_create :path => p
l.valid? ? l.save : (raise "#{l.errors}") l.nodes = self
l.name
rescue Exception => e rescue Exception => e
e.message e.message
end end
......
...@@ -57,6 +57,19 @@ module Noah ...@@ -57,6 +57,19 @@ module Noah
def name def name
@name = path @name = path
end end
class <<self
def find_or_create(opts={})
begin
find(opts).first.nil? ? obj=new(opts) : obj=find(opts).first
if obj.valid?
obj.save
end
obj
rescue Exception => e
e.message
end
end
end
private private
def node_to_class(node) def node_to_class(node)
node.match(/^Noah::(.*):(\d+)$/) node.match(/^Noah::(.*):(\d+)$/)
......
...@@ -5,14 +5,21 @@ module Noah::Taggable ...@@ -5,14 +5,21 @@ module Noah::Taggable
end end
def tag!(tag_name) def tag!(tag_name)
tags << ::Noah::Tag.find_or_create(:name => tag_name) case tag_name.class.to_s
when "Array"
tag_name.each do |t|
tags << ::Noah::Tag.find_or_create(:name => t)
end
else
tags << Noah::Tag.find_or_create(:name => tag_name)
end
end end
def untag!(tag_name) def untag!(tag_name)
end end
def to_hash def to_hash
tag_arr = [] tag_arr = Array.new
self.tags.sort.each {|t| tag_arr << t.name} if self.tags.size != 0 self.tags.sort.each {|t| tag_arr << t.name} if self.tags.size != 0
super.merge(:tags => tag_arr) super.merge(:tags => tag_arr)
end end
......
require 'noah'
Ohm::redis.flushdb
puts "Creating sample entries - Watchers"
Noah::Watcher.create :endpoint => "dummy://applications", :pattern => "//noah/applications"
Noah::Watcher.create :endpoint => "dummy://configurations", :pattern => "//noah/configurations"
Noah::Watcher.create :endpoint => "dummy://hosts", :pattern => "//noah/hosts"
Noah::Watcher.create :endpoint => "dummy://services", :pattern => "//noah/services"
Noah::Watcher.create :endpoint => "dummy://ephemerals", :pattern => "//noah/ephemerals"
puts "Creating Host entry for 'localhost'"
h = Noah::Host.create(:name => 'localhost', :status => "up")
if h.save
%w[redis noah].each do |service|
puts "Create Service entry for #{service}"
s = Noah::Service.create(:name => service, :status => "up", :host_id => h.id)
h.services << s
end
end
puts "Creating Application entry for 'noah'"
a = Noah::Application.create(:name => 'noah')
if a.save
puts "Creating Configuration entry for 'noah'"
cr = Noah::Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0')
ch = Noah::Configuration.create(:name => 'host', :format => 'string', :body => 'localhost')
cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292')
%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 = Noah::Host.create(:name => host, :status => "up")
if h.save
%w[http https smtp mysql].each do |service|
s = Noah::Service.create(:name => service, :status => "pending", :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 = Noah::Application.create(:name => 'myrailsapp1')
if a1.save
c1 = Noah::Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml)
a1.configurations << c1
end
a2 = Noah::Application.create(:name => 'myrestapp1')
if a2.save
c2 = Noah::Configuration.create(:name => 'config.json', :format => 'json', :body => my_json)
a2.configurations << c2
end
puts "Creating sample entries - Ephemerals"
e1 = Noah::Ephemeral.create(:path => '/some/random/path/item1', :data => 'some_data')
e2 = Noah::Ephemeral.create(:path => '/some/random/path/', :data => '{"children":["item1", "item2"]}')
puts "Creating sample entries - Links and Tags"
l1 = Noah::Link.new
l1.path = "/my_sample_organization"
l1.save
l1.nodes = [a1, c2, Noah::Host.find(:name => 'localhost').first, Noah::Service.find(:name => 'redis').first, e1, e2]
a1.tag! ["production", "sample_data"]
e2.tag! "ephemeral"
puts "Sample data populated!"
...@@ -52,4 +52,10 @@ ...@@ -52,4 +52,10 @@
%a{:href => "configurations/myrailsapp1/database.yml"} database.yml file for myrailsapp1 (should return the proper content-type) %a{:href => "configurations/myrailsapp1/database.yml"} database.yml file for myrailsapp1 (should return the proper content-type)
%li %li
%a{:href => "configurations/myrestapp1/config.json"} config.json file for myrestapp1 %a{:href => "configurations/myrestapp1/config.json"} config.json file for myrestapp1
#header
%h2 Links
%ul
%li
%a{:href => "my_sample_organization"} Sample Organization Link
%li
%a{:href => "my_sample_organization/hosts"} Sample Organization Link - Hosts
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