Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for backend stores for config #57

Merged
merged 3 commits into from Apr 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/riemann/dash.rb
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions lib/riemann/dash/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def self.load(filename)
config.load_controllers
config.setup_views
config.setup_public_dir
config.setup_storage_backend
end
end
end
Expand Down
40 changes: 12 additions & 28 deletions lib/riemann/dash/browser_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -54,35 +62,11 @@ def self.merge_configs(a, b)
'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 changes: 39 additions & 0 deletions lib/riemann/dash/browser_config/file.rb
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions lib/riemann/dash/browser_config/s3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Riemann::Dash::BrowserConfig::S3
require 'multi_json'
require 'fog'

def initialize(bucket, path, config = {})
@bucket = bucket
@path = path
@config = config

@storage = Fog::Storage::AWS.new(config)
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 changes: 15 additions & 0 deletions lib/riemann/dash/config.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Riemann::Dash::Config
require 'uri'

attr_accessor :config_path
attr_accessor :store

Expand Down Expand Up @@ -62,6 +64,19 @@ def setup_public_dir
Riemann::Dash::App.use Riemann::Dash::Static, :root => store[:public]
end

def setup_storage_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|
Expand Down
6 changes: 3 additions & 3 deletions lib/riemann/dash/controller/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions riemann-dash.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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)/})
Expand Down