engine/lib/tasks/bushido.rake

88 lines
2.8 KiB
Ruby
Raw Permalink Normal View History

2011-03-21 09:40:37 +00:00
require 'bushido'
require 'jammit'
require 'net/http'
2011-03-21 09:40:37 +00:00
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
2011-03-21 09:40:37 +00:00
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,
:bushido_user_id => ENV['BUSHIDO_USER_ID'],
:password => ActiveSupport::SecureRandom.base64(6)
})
else
# create an anonymous account right away
account = Account.create!({
:email => "#{ENV['BUSHIDO_APP']}@#{ENV['BUSHIDO_HOST']}",
:name => ENV['BUSHIDO_APP'],
:password => ActiveSupport::SecureRandom.base64(6)
})
end
# create the site
site = Site.create_first_one(:name => ENV['LOCOMOTIVE_SITE_NAME'] || ENV['BUSHIDO_APP'])
# fetch the site template
template_url = ENV['SITE_TEMPLATE_URL'] || Locomotive::Import::DEFAULT_SITE_TEMPLATE
template_url = "http://#{template_url}" unless template_url =~ /^https?:\/\//
template_local_path = "#{Rails.root}/permanent/site_template.zip"
uri = URI.parse(template_url)
http = Net::HTTP.new(uri.host, uri.port)
if template_url.starts_with?('https') # ssl request ?
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
case http.request(Net::HTTP::Get.new(uri.request_uri))
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
2011-03-21 09:40:37 +00:00
end
desc "Perform custom actions triggered by the Bushido hosting platform."
2011-05-29 22:25:36 +00:00
task :event => :environment do
2011-05-30 21:17:42 +00:00
event = ::Bushido::Event.last
puts "processing...#{event.inspect}"
case event.category.to_s
2011-05-30 21:17:42 +00:00
when 'app'
case event.name.to_s
2011-05-30 21:32:34 +00:00
when 'claimed'
# an user has just claimed his application
account = Account.order_by(:created_at).first
2011-05-30 21:17:42 +00:00
account.email = event.data['bushido_user_email']
account.bushido_user_id = event.data['bushido_user_id']
account.save!
end
end
end
2011-03-21 09:40:37 +00:00
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
2011-03-21 09:40:37 +00:00
Jammit.package!
end
end