2011-03-21 09:40:37 +00:00
|
|
|
require 'bushido'
|
|
|
|
require 'jammit'
|
2011-05-27 15:19:20 +00:00
|
|
|
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."
|
2011-05-27 15:19:20 +00:00
|
|
|
task :install => :environment do
|
2011-05-27 22:14:45 +00:00
|
|
|
|
|
|
|
# re-built assets
|
2011-03-21 09:40:37 +00:00
|
|
|
Jammit.package!
|
2011-05-27 15:19:20 +00:00
|
|
|
|
|
|
|
if ENV['BUSHIDO_USER_EMAIL'] && ENV['BUSHIDO_USER_ID']
|
|
|
|
# already logged in in Bushido
|
|
|
|
account = Account.create!({
|
2011-05-27 22:14:45 +00:00
|
|
|
: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)
|
2011-05-27 15:19:20 +00:00
|
|
|
})
|
|
|
|
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}`
|
|
|
|
|
2011-05-27 22:14:45 +00:00
|
|
|
tmp, Locomotive.config.delayed_job = Locomotive.config.delayed_job, false # disable DJ during this import
|
|
|
|
|
2011-05-27 15:19:20 +00:00
|
|
|
Locomotive::Import::Job.run!(File.open(template_local_path), site, { :samples => true })
|
2011-05-27 22:14:45 +00:00
|
|
|
|
|
|
|
Locomotive.config.delayed_job = tmp # restore DJ flag
|
2011-05-27 15:19:20 +00:00
|
|
|
else
|
|
|
|
# do nothing
|
|
|
|
end
|
2011-03-21 09:40:37 +00:00
|
|
|
end
|
|
|
|
|
2011-05-27 22:14:45 +00:00
|
|
|
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
|
2011-05-27 22:14:45 +00:00
|
|
|
|
|
|
|
puts "processing...#{event.inspect}"
|
|
|
|
|
|
|
|
case event.category.to_s
|
2011-05-30 21:17:42 +00:00
|
|
|
when 'app'
|
2011-05-27 22:14:45 +00:00
|
|
|
case event.name.to_s
|
2011-05-30 21:32:34 +00:00
|
|
|
when 'claimed'
|
2011-05-27 22:14:45 +00:00
|
|
|
# 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']
|
2011-05-27 22:14:45 +00:00
|
|
|
|
|
|
|
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
|
2011-05-27 22:14:45 +00:00
|
|
|
# re-built assets
|
2011-03-21 09:40:37 +00:00
|
|
|
Jammit.package!
|
|
|
|
end
|
|
|
|
end
|
2011-05-27 15:19:20 +00:00
|
|
|
|