From 8880188001da23a5f0960e3970c4eb9bbd448306 Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Sun, 23 Feb 2014 19:11:29 +0200 Subject: Migrate to sinatra/puma from webrick As I can't do IO#select on sinatra/puma to run it when I have time, I have to run it on separate thread. This means Nodes container needs to be thread safe, it now has ghetto mutex locking, but I probably need to be be more focused what are the external methods that can be called and wrap those in @mutex.synchronize Provide also HTML UI not just JSON for ghetto UI to people who don't want to integrate --- lib/oxidized/api/web/public/css/oxidized.css | 35 ++++++++++ lib/oxidized/api/web/views/default.haml | 4 ++ lib/oxidized/api/web/views/head.haml | 3 + lib/oxidized/api/web/views/node.haml | 6 ++ lib/oxidized/api/web/views/nodes.haml | 27 ++++++++ lib/oxidized/api/web/webapp.rb | 98 ++++++++++++++++++++++++++++ 6 files changed, 173 insertions(+) create mode 100644 lib/oxidized/api/web/public/css/oxidized.css create mode 100644 lib/oxidized/api/web/views/default.haml create mode 100644 lib/oxidized/api/web/views/head.haml create mode 100644 lib/oxidized/api/web/views/node.haml create mode 100644 lib/oxidized/api/web/views/nodes.haml create mode 100644 lib/oxidized/api/web/webapp.rb (limited to 'lib/oxidized/api/web') diff --git a/lib/oxidized/api/web/public/css/oxidized.css b/lib/oxidized/api/web/public/css/oxidized.css new file mode 100644 index 0000000..8455513 --- /dev/null +++ b/lib/oxidized/api/web/public/css/oxidized.css @@ -0,0 +1,35 @@ +body { + background: #fdf6e3; + color: #002b36; +} + +a:link { + /* color: #268bd2; */ + color: #dc322f; + text-decoration: none; +} + +a:visited { +} + +a:hover { + color: #d33682; + text-decoration: underline; +} + +a:active { +} + +tr.odd { + background: #eee8d5; +} + +tr.even { + background: #fdf6e3; +} + +tr:hover { + background: #586e75; + color: #fdf6e3; +} + diff --git a/lib/oxidized/api/web/views/default.haml b/lib/oxidized/api/web/views/default.haml new file mode 100644 index 0000000..1ff1f4b --- /dev/null +++ b/lib/oxidized/api/web/views/default.haml @@ -0,0 +1,4 @@ +%html + !=haml :head + %body + =@data diff --git a/lib/oxidized/api/web/views/head.haml b/lib/oxidized/api/web/views/head.haml new file mode 100644 index 0000000..1619512 --- /dev/null +++ b/lib/oxidized/api/web/views/head.haml @@ -0,0 +1,3 @@ +%head + %title oxidized + %link{:rel=>'stylesheet', :href=>'/css/oxidized.css'} diff --git a/lib/oxidized/api/web/views/node.haml b/lib/oxidized/api/web/views/node.haml new file mode 100644 index 0000000..6c68e07 --- /dev/null +++ b/lib/oxidized/api/web/views/node.haml @@ -0,0 +1,6 @@ +%html + !=haml :head + %body + -out='';PP.pp(@data,out) + %pre + =out diff --git a/lib/oxidized/api/web/views/nodes.haml b/lib/oxidized/api/web/views/nodes.haml new file mode 100644 index 0000000..4bc0b14 --- /dev/null +++ b/lib/oxidized/api/web/views/nodes.haml @@ -0,0 +1,27 @@ +%html + !=haml :head + %body + %a(href="/reload") reload list of nodes + %table + %tr + %th Name + %th IP + %th Group + %th Last Status + %th Last Time + %th Config + %th Update + -trclass = %w(even odd) + -@data.sort_by{|e|e[:name]}.each do |node| + -klass = trclass.rotate!.first + %tr{:class=>klass} + %td + %a(href="/node/show/#{node[:name]}") #{node[:name]} + %td= node[:ip] + %td= node[:group] + %td= node[:status] + %td= node[:time] + %td + %a(href="/node/fetch/#{node[:full_name]}") config + %td + %a(href="/node/next/#{node[:full_name]}") update diff --git a/lib/oxidized/api/web/webapp.rb b/lib/oxidized/api/web/webapp.rb new file mode 100644 index 0000000..13bd0ab --- /dev/null +++ b/lib/oxidized/api/web/webapp.rb @@ -0,0 +1,98 @@ +require 'sinatra/base' +require 'sinatra/json' +require 'haml' +require 'pp' +module Oxidized + module API + class WebApp < Sinatra::Base + + get '/nodes.?:format?' do + @data = nodes.list.map do |node| + node[:status] = 'never' + node[:time] = 'never' + node[:group] = 'default' unless node[:group] + if node[:last] + node[:status] = node[:last][:status] + node[:time] = node[:last][:end] + end + node + end + out :nodes + end + + get '/reload.?:format?' do + nodes.load + @data = 'reloaded list of nodes' + out + end + + + get '/node/fetch/:node' do + node, @json = route_parse :node + @data = nodes.fetch node, nil + out :text + end + + get '/node/fetch/:group/:node' do + node, @json = route_parse :node + @data = nodes.fetch node, params[:group] + out :text + end + + + get '/node/next/:node' do + node, @json = route_parse :node + nodes.next node + redirect '/nodes' unless @json + @data = 'ok' + out + end + + # use this to attach author/email/message to commit + put '/node/next/:node' do + node, @json = route_parse :node + opt = JSON.load request.body.read + nodes.next node, opt + redirect '/nodes' unless @json + @data = 'ok' + out + end + + get '/node/show/:node' do + node, @json = route_parse :node + @data = nodes.show node + out :node + end + + #get '/node/:node' do + # @data = nodes.show params[:node] + # out + #end + + def out template=:default + if @json or params[:format] == 'json' + json @data + elsif template == :text + content_type :text + @data + else + haml template + end + end + + def nodes + settings.nodes + end + + def route_parse param + json = false + e = params[param].split '.' + if e.last == 'json' + e.pop + json = true + end + [e.join('.'), json] + end + end + end +end -- cgit v1.2.1