From ef7ac0e721f0096cc3ffbc6a8b1c79396d650bb7 Mon Sep 17 00:00:00 2001 From: did Date: Fri, 27 May 2011 15:14:45 -0700 Subject: [PATCH] make the bushido installation process the smoothest --- .gitignore | 1 + Gemfile | 2 +- Gemfile.lock | 5 +- lib/locomotive/hosting/bushido.rb | 13 ++++- lib/locomotive/hosting/bushido/account_ext.rb | 19 +++++++ lib/locomotive/hosting/bushido/middleware.rb | 51 +++++++++++++++++++ lib/tasks/bushido.rake | 35 +++++++++++-- 7 files changed, 118 insertions(+), 8 deletions(-) create mode 100644 lib/locomotive/hosting/bushido/account_ext.rb create mode 100644 lib/locomotive/hosting/bushido/middleware.rb diff --git a/.gitignore b/.gitignore index 977de89e..7852ac7c 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ perf/*.rb gem_graph.png sites/ permanent +doc/bushido diff --git a/Gemfile b/Gemfile index 5a494b87..b8615c5d 100644 --- a/Gemfile +++ b/Gemfile @@ -40,12 +40,12 @@ gem 'SystemTimer', :platforms => :ruby_18 group :development do # Using unicorn_rails instead of webrick (default server) gem 'unicorn' - gem 'bushido_stub', :path => '../gems/bushido_stub' end group :test, :development do gem 'ruby-debug', :platforms => :mri_18 gem 'ruby-debug19', :platforms => :mri_19 + gem 'bushido_stub', :path => '../gems/bushido_stub' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 38270433..9d25c6b9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: git://github.com/floehopper/mocha.git - revision: adeb8ae0189963cf3024f2b37d5348a7feaae11e + revision: 6da1242f26b12a24c4fcf67bf5921a25bc1bc88d specs: mocha (0.9.12.20110213002255) @@ -15,7 +15,6 @@ PATH remote: ../gems/bushido_stub specs: bushido_stub (0.0.1) - activesupport GEM remote: http://rubygems.org/ @@ -64,7 +63,7 @@ GEM bson (1.3.1) bson_ext (1.3.1) builder (2.1.2) - bushido (0.0.12) + bushido (0.0.14) highline (>= 1.6.1) json (>= 1.4.6) rest-client (>= 1.6.1) diff --git a/lib/locomotive/hosting/bushido.rb b/lib/locomotive/hosting/bushido.rb index 6e437b2f..3977424c 100644 --- a/lib/locomotive/hosting/bushido.rb +++ b/lib/locomotive/hosting/bushido.rb @@ -1,6 +1,8 @@ require 'bushido' require 'locomotive/hosting/bushido/custom_domain' require 'locomotive/hosting/bushido/first_installation' +require 'locomotive/hosting/bushido/account_ext' +require 'locomotive/hosting/bushido/middleware' module Locomotive module Hosting @@ -29,7 +31,9 @@ module Locomotive self.setup_smtp_settings - self.config.delayed_job = false #true # force the use of delayed_job + self.add_middleware + + self.config.delayed_job = true # force the use of delayed_job self.bushido_domains = ::Bushido::App.domains self.bushido_subdomain = ::Bushido::App.subdomain @@ -38,6 +42,7 @@ module Locomotive def enhance_site_model_with_bushido Site.send :include, Locomotive::Hosting::Bushido::CustomDomain Site.send :include, Locomotive::Hosting::Bushido::FirstInstallation + Account.send :include, Locomotive::Hosting::Bushido::AccountExt end def setup_smtp_settings @@ -53,6 +58,12 @@ module Locomotive } end + def add_middleware + ::Locomotive::Application.configure do |config| + config.middleware.use '::Locomotive::Hosting::Bushido::Middleware' + end + end + # manage domains def add_bushido_domain(name) diff --git a/lib/locomotive/hosting/bushido/account_ext.rb b/lib/locomotive/hosting/bushido/account_ext.rb new file mode 100644 index 00000000..8b257959 --- /dev/null +++ b/lib/locomotive/hosting/bushido/account_ext.rb @@ -0,0 +1,19 @@ +module Locomotive + module Hosting + module Bushido + + module AccountExt + + extend ActiveSupport::Concern + + included do + + field :bushido_user_id, :type => Integer + + end + + end + + end + end +end \ No newline at end of file diff --git a/lib/locomotive/hosting/bushido/middleware.rb b/lib/locomotive/hosting/bushido/middleware.rb new file mode 100644 index 00000000..d22e5b5b --- /dev/null +++ b/lib/locomotive/hosting/bushido/middleware.rb @@ -0,0 +1,51 @@ +require 'rack/utils' + +module Locomotive + module Hosting + module Bushido + class Middleware + + BUSHIDO_JS_URL = 'http://localhost:4567/javascripts/bushido.js' + + include Rack::Utils + + def initialize(app, opts = {}) + @app = app + @bushido_app_name = ENV['BUSHIDO_APP'] + @bushido_claimed = ENV['BUSHIDO_CLAIMED'] && ENV['BUSHIDO_CLAIMED'].to_s.downcase == 'true' + end + + def call(env) + status, headers, response = @app.call(env) + + if env["PATH_INFO"] =~ /^\/admin\// + content = "" + response.each { |part| content += part } + + # "claiming" bar + stats ? + content.gsub!(/<\/body>/i, <<-STR + + + STR + ) + + headers['content-length'] = bytesize(content).to_s + + [status, headers, [content]] + else + [status, headers, response] + end + end + + end + end + end +end diff --git a/lib/tasks/bushido.rake b/lib/tasks/bushido.rake index 07f99450..a2bdcfd2 100644 --- a/lib/tasks/bushido.rake +++ b/lib/tasks/bushido.rake @@ -5,14 +5,17 @@ require 'net/http' namespace :bushido do desc "Prepare an app to run on the Bushido hosting platform, only called during the initial installation. Called just before booting the app." task :install => :environment do + + # re-built assets Jammit.package! if ENV['BUSHIDO_USER_EMAIL'] && ENV['BUSHIDO_USER_ID'] # already logged in in Bushido account = Account.create!({ - :email => ENV['BUSHIDO_USER_EMAIL'], - :name => ENV['BUSHIDO_USER_NAME'] || ENV['BUSHIDO_USER_EMAIL'].split('@').first, - :password => ActiveSupport::SecureRandom.base64(6) + :email => ENV['BUSHIDO_USER_EMAIL'], + :name => ENV['BUSHIDO_USER_NAME'] || ENV['BUSHIDO_USER_EMAIL'].split('@').first, + :bushido_user_id => ENV['BUSHIDO_USER_ID'], + :password => ActiveSupport::SecureRandom.base64(6) }) else # create an anonymous account right away @@ -44,14 +47,40 @@ namespace :bushido do when Net::HTTPSuccess, Net::HTTPFound `curl -L -s -o #{template_local_path} #{template_url}` + tmp, Locomotive.config.delayed_job = Locomotive.config.delayed_job, false # disable DJ during this import + Locomotive::Import::Job.run!(File.open(template_local_path), site, { :samples => true }) + + Locomotive.config.delayed_job = tmp # restore DJ flag else # do nothing end end + desc "Perform custom actions triggered by the Bushido hosting platform." + task :message => :environment do + event = ::Bushido::App.last_event + + puts "processing...#{event.inspect}" + + case event.category.to_s + when 'user' + case event.name.to_s + when 'create' + # an user has just claimed his application + account = Account.order_by(:created_at).first + + account.email = event.data['email'] + account.bushido_user_id = event.data['id'] + + account.save! + end + end + end + desc "Prepare an app to run on the Bushido hosting platform, called on every update. Called just before booting the app." task :update do + # re-built assets Jammit.package! end end