1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
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')
require File.join(File.dirname(__FILE__), 'helpers')
get '/' do
before do
content_type "text/html"
end
haml :index, :format => :html5
end
before do
content_type "application/json"
end
namespace "/h" do
get '/:hostname/s/:servicename/?' do |hostname, servicename|
host_service(hostname, servicename).to_json
end
get '/:hostname/s/?' do |hostname|
hs = host_services(hostname)
hs.map! {|x| x.to_hash}
hs.to_json
end
get '/:hostname/?' do |hostname|
host(:name => hostname).to_json
end
get '/?' do
hosts.map {|h| h.to_hash}
hosts.to_json
end
end
namespace "/s" do
get '/:servicename/h/:hostname/?' do |servicename, hostname|
host_service(hostname, servicename).to_json
end
get '/:servicename/?' do |servicename|
s = services(:name => servicename)
s.map {|x| x.to_hash}
s.to_json
end
get '/?' do
services.map {|s| s.to_hash}
services.to_json
end
end
namespace "/a" do
get '/:appname/:config/?' do
app = Application.find(:name => params[:appname]).first
c = Configuration.find(:name => params[:config], :application_id => app.id).first
"#{c.to_json}"
end
get '/:appname/?' do
app = Application.find(:name => params[:appname]).first
"#{app.to_json}"
end
put '/:appname/?' do
begin
app = Application.find(:name => params[:appname]).first ||= Application.create(:name => params[:appname])
data = JSON.parse(request.body.read)
app.name = data['name']
if app.valid?
app.save
status 200
r = {"result" => "success","id" => "#{app.id}"}
r.to_json
else
status 500
r = {"result" => "failure","error" => "#{app.errors}"}
r.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json
end
end
delete '/:appname/?' do
begin
app = Application.find(:name => params[:appname]).first
if app
Configuration.find(:application_id => app.id).sort.each {|x| x.delete} if app.configurations.size > 0
app.delete
status 200
r = {"result" => "success", "id" => "#{app.id}"}
r.to_json
else
status 404
r = {"result" => "failure","error" => "Application not found"}
r.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure", "error" => "#{e.message}"}
r.to_json
end
end
get '/?' do
apps = []
Application.all.sort.each {|a| apps << a.to_hash}
"#{apps.to_json}"
end
end
namespace '/c' do
get '/:appname/:element/?' do
a = Application.find(:name => params[:appname]).first
c = Configuration.find(:name => params[:element], :application_id => a.id).first
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 '/:appname/?' do
config = []
a = Application.find(:name => params[:appname]).first
Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash}
"#{config.to_json}"
end
get '/?' do
configs = []
Configuration.all.sort.each {|c| configs << c.to_hash}
"#{configs.to_json}"
end
end
# A route for adding a new host
put '/h/:hostname' do
host = params[:hostname]
data = JSON.parse(request.body.read)
begin
h = Host.find(:name => host).first
h.state = 1
rescue
# If we have to register the host, we mark it as inactive
# Feels like the right thing to do in case of accidental creation
# from mispelling
h = Host.create(:name => params[:hostname], :state => 0)
end
if h.save
data['services'].each {|x| h.services << Service.create(:name=> x['name'], :state => x['state'], :host => h)} if data['services']
{"msg" => "success"}.to_json
end
end