Browse Source

Add support for multiple backends to store config (S3 and File for now)

tags/0.2.9
Torbjörn Norinder 8 years ago
parent
commit
546726ce7d
  1. 40
      Gemfile.lock
  2. 4
      lib/riemann/dash.rb
  3. 1
      lib/riemann/dash/app.rb
  4. 40
      lib/riemann/dash/browser_config.rb
  5. 39
      lib/riemann/dash/browser_config/file.rb
  6. 31
      lib/riemann/dash/browser_config/s3.rb
  7. 15
      lib/riemann/dash/config.rb
  8. 6
      lib/riemann/dash/controller/index.rb
  9. 1
      riemann-dash.gemspec

40
Gemfile.lock

@ -1,8 +1,9 @@
PATH
remote: .
specs:
riemann-dash (0.2.5)
riemann-dash (0.2.8)
erubis (>= 2.7.0)
fog
multi_json (= 1.3.6)
riemann-client (>= 0.0.7)
sass (>= 3.1.14)
@ -12,34 +13,55 @@ PATH
GEM
remote: https://rubygems.org/
specs:
beefcake (0.3.7)
beefcake (0.5.0)
builder (3.2.2)
coderay (1.0.9)
daemons (1.1.9)
erubis (2.7.0)
eventmachine (1.0.3)
excon (0.31.0)
fog (1.19.0)
builder
excon (~> 0.31.0)
formatador (~> 0.2.0)
mime-types
multi_json (~> 1.0)
net-scp (~> 1.1)
net-ssh (>= 2.1.3)
nokogiri (~> 1.5)
ruby-hmac
formatador (0.2.4)
method_source (0.8.1)
mime-types (2.0)
mini_portile (0.5.2)
mtrc (0.0.4)
multi_json (1.3.6)
net-scp (1.1.2)
net-ssh (>= 2.6.5)
net-ssh (2.7.0)
nokogiri (1.6.1)
mini_portile (~> 0.5.0)
pry (0.9.12)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.4)
rack (1.5.2)
rack-protection (1.5.0)
rack-protection (1.5.2)
rack
riemann-client (0.2.2)
beefcake (>= 0.3.5)
mtrc (>= 0.0.4)
trollop (>= 1.16.2)
sass (3.2.7)
sinatra (1.3.6)
ruby-hmac (0.4.0)
sass (3.3.2)
sinatra (1.4.4)
rack (~> 1.4)
rack-protection (~> 1.3)
tilt (~> 1.3, >= 1.3.3)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
slop (3.4.3)
thin (1.5.1)
thin (1.6.2)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
eventmachine (>= 1.0.0)
rack (>= 1.0.0)
tilt (1.4.1)
trollop (2.0)

4
lib/riemann/dash.rb

@ -1,6 +1,8 @@
require 'rubygems'
require 'sinatra/base'
require 'riemann/dash/version'
require 'riemann/dash/config'
require 'riemann/dash/browser_config'
require 'riemann/dash/browser_config/file'
require 'riemann/dash/browser_config/s3'
require 'riemann/dash/config'
require 'riemann/dash/app'

1
lib/riemann/dash/app.rb

@ -26,6 +26,7 @@ module Riemann
config.load_controllers
config.setup_views
config.setup_public_dir
config.setup_ws_backend
end
end
end

40
lib/riemann/dash/browser_config.rb

@ -3,6 +3,14 @@ module Riemann::Dash::BrowserConfig
require 'fileutils'
require 'pp'
def self.backend
@backend
end
def self.backend=(backend)
@backend = backend
end
# TODO: this is gonna take significant restructuring of the dashboard itself,
# but we should move to http://arxiv.org/abs/1201.1784 or equivalent CRDTs.
@ -54,35 +62,11 @@ module Riemann::Dash::BrowserConfig
'workspaces' => merge_workspaces(a['workspaces'], b['workspaces'])
end
def self.read(config)
if File.exists? config.ws_config_file
File.open(config.ws_config_file, 'r') do |f|
f.flock File::LOCK_SH
f.read
end
else
MultiJson.encode({})
end
def self.read
backend.read
end
def self.update(config, update)
update = MultiJson.decode update
# Read old config
old = MultiJson.decode read(config)
new = merge_configs update, old
# Save new config
FileUtils.mkdir_p File.dirname(config.ws_config_file)
begin
File.open(config.ws_config_file, File::RDWR|File::CREAT, 0644) do |f|
f.flock File::LOCK_EX
f.write(MultiJson.encode(new, :pretty => true))
f.flush
f.truncate f.pos
end
end
def self.update(update)
backend.update(update)
end
end

39
lib/riemann/dash/browser_config/file.rb

@ -0,0 +1,39 @@
class Riemann::Dash::BrowserConfig::File
require 'multi_json'
require 'fileutils'
def initialize(path)
@path = path
end
def read
if ::File.exists? @path
::File.open(@path, 'r') do |f|
f.flock ::File::LOCK_SH
f.read
end
else
MultiJson.encode({})
end
end
def update(update)
update = MultiJson.decode update
# Read old config
old = MultiJson.decode read
new = Riemann::Dash::BrowserConfig.merge_configs update, old
# Save new config
FileUtils.mkdir_p ::File.dirname(@path)
begin
::File.open(@path, ::File::RDWR|::File::CREAT, 0644) do |f|
f.flock ::File::LOCK_EX
f.write(MultiJson.encode(new, :pretty => true))
f.flush
f.truncate f.pos
end
end
end
end

31
lib/riemann/dash/browser_config/s3.rb

@ -0,0 +1,31 @@
class Riemann::Dash::BrowserConfig::S3
require 'multi_json'
require 'fog'
def initialize(bucket, path, config = {})
@bucket = bucket
@path = path
@config = config
@storage = Fog::Storage.new(:region => config[:region],
:provider => 'AWS')
end
def read
begin
@storage.get_object(@bucket, @path).body
rescue Excon::Errors::NotFound
MultiJson.encode({})
end
end
def update(update)
update = MultiJson.decode update
# Read old config
old = MultiJson.decode read
new = Riemann::Dash::BrowserConfig.merge_configs update, old
@storage.put_object @bucket, @path, MultiJson.encode(new, :pretty => true)
end
end

15
lib/riemann/dash/config.rb

@ -1,4 +1,6 @@
class Riemann::Dash::Config
require 'uri'
attr_accessor :config_path
attr_accessor :store
@ -62,6 +64,19 @@ class Riemann::Dash::Config
Riemann::Dash::App.use Riemann::Dash::Static, :root => store[:public]
end
def setup_ws_backend
uri = URI.parse(ws_config_file)
backend = case uri.scheme
when "s3"
Riemann::Dash::BrowserConfig::S3.new(uri.host, uri.path.sub(/^\//, ''), store[:s3_config])
when nil, "file"
Riemann::Dash::BrowserConfig::File.new(uri.path)
else
raise Exception.new "Unknown backend for #{ws_config_file}"
end
Riemann::Dash::BrowserConfig.backend = backend
end
# Load controllers.
def load_controllers_from(dir)
sorted_controller_list(dir).each do |r|

6
lib/riemann/dash/controller/index.rb

@ -5,16 +5,16 @@ class Riemann::Dash::App
get '/config', :provides => 'json' do
content_type "application/json"
Riemann::Dash::BrowserConfig.read config
Riemann::Dash::BrowserConfig.read
end
post '/config' do
# Read update
request.body.rewind
Riemann::Dash::BrowserConfig.update config, request.body.read
Riemann::Dash::BrowserConfig.update request.body.read
# Return current config
content_type "application/json"
Riemann::Dash::BrowserConfig.read config
Riemann::Dash::BrowserConfig.read
end
end

1
riemann-dash.gemspec

@ -21,6 +21,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'sass', '>= 3.1.14'
gem.add_dependency 'thin', '>= 1.3.1'
gem.add_dependency 'multi_json', '1.3.6'
gem.add_dependency 'fog'
gem.files = `git ls-files`.split($/)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})

Loading…
Cancel
Save