-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/Rails31'
- Loading branch information
Showing
7 changed files
with
185 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module EY | ||
module Serverside | ||
module RailsAssetSupport | ||
def compile_assets | ||
roles :app_master, :app, :solo do | ||
rails_app = "#{c.release_path}/config/application.rb" | ||
asset_dir = "#{c.release_path}/app/assets" | ||
if File.exists?(rails_app) && File.directory?(asset_dir) | ||
return if app_disables_assets?(rails_app) | ||
if app_builds_own_assets? | ||
info "~> public/assets already exists, skipping pre-compilation." | ||
return | ||
end | ||
keep_existing_assets | ||
cmd = "cd #{c.release_path} && PATH=#{c.binstubs_path}:$PATH #{c.framework_envs} rake assets:precompile" | ||
info "~> Precompiling assets for Rails: #{cmd}" | ||
run(cmd) | ||
end | ||
end | ||
end | ||
|
||
def app_disables_assets?(path) | ||
disabled = nil | ||
File.open(path) do |fd| | ||
pattern = /^[^#].*config\.assets\.enabled\s+=\s+(false|nil)/ | ||
contents = fd.read | ||
disabled = contents.match(pattern) | ||
end | ||
disabled | ||
end | ||
|
||
def app_builds_own_assets? | ||
File.directory?(File.join(c.release_path, 'public', 'assets')) | ||
end | ||
|
||
# To support operations like Unicorn's hot reload, it is useful to have | ||
# the prior release's assets as well. Otherwise, while a deploy is running, | ||
# clients may request stale assets that you just deleted. | ||
# Making use of this requires a properly-configured front-end HTTP server. | ||
def keep_existing_assets | ||
current = File.join(c.shared_path, 'assets') | ||
last_asset_path = File.join(c.shared_path, 'last_assets') | ||
# If there are current shared assets, move them under a 'last_assets' directory. | ||
run <<-COMMAND | ||
if [ -d #{current} ]; then | ||
rm -rf #{last_asset_path} && mkdir #{last_asset_path} && mv #{current} #{last_asset_path} && mkdir -p #{current}; | ||
else | ||
mkdir -p #{current} #{last_asset_path}; | ||
fi; | ||
ln -nfs #{current} #{last_asset_path} #{c.release_path}/public | ||
COMMAND | ||
end | ||
|
||
def bundled_rails_version(lockfile_path) | ||
lockfile = File.open(lockfile_path) {|f| f.read} | ||
lockfile.each_line do |line| | ||
# scan for gemname (version) toplevel deps. | ||
# Likely doesn't handle ancient Bundler versions, but | ||
# we only call this when something looks like it is Rails 3. | ||
next unless line =~ /^\s{4}([-\w_.0-9]+)\s*\((.*)\)/ | ||
return $2 if $1 == 'rails' | ||
end | ||
nil | ||
end | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
require 'spec_helper' | ||
|
||
describe "Deploying a Rails 3.1 application" do | ||
def deploy_test_application(assets_enabled = true) | ||
return # pending! | ||
$DISABLE_GEMFILE = false | ||
$DISABLE_LOCKFILE = false | ||
@deploy_dir = File.join(Dir.tmpdir, "serverside-deploy-#{Time.now.to_i}-#{$$}") | ||
|
||
# set up EY::Serverside::Server like we're on a solo | ||
EY::Serverside::Server.reset | ||
EY::Serverside::Server.add(:hostname => 'localhost', :roles => %w[solo]) | ||
|
||
# run a deploy | ||
@config = EY::Serverside::Deploy::Configuration.new({ | ||
"strategy" => "IntegrationSpec", | ||
"deploy_to" => @deploy_dir, | ||
"group" => `id -gn`.strip, | ||
"stack" => 'nginx_passenger', | ||
"migrate" => "ruby -e 'puts ENV[\"PATH\"]' > #{@deploy_dir}/path-when-migrating", | ||
'app' => 'rails31', | ||
'framework_env' => 'staging' | ||
}) | ||
|
||
# pretend there is a shared bundled_gems directory | ||
FileUtils.mkdir_p(File.join(@deploy_dir, 'shared', 'bundled_gems')) | ||
%w(RUBY_VERSION SYSTEM_VERSION).each do |name| | ||
File.open(File.join(@deploy_dir, 'shared', 'bundled_gems', name), "w") { |f| f.write("old\n") } | ||
end | ||
|
||
# Set up the application directory to have the requested asset options. | ||
prepare_rails31_app(assets_enabled) | ||
|
||
@binpath = File.expand_path(File.join(File.dirname(__FILE__), '..', 'bin', 'engineyard-serverside')) | ||
@deployer = FullTestDeploy.new(@config) | ||
yield if block_given? | ||
@deployer.deploy | ||
end | ||
|
||
def prepare_rails31_app(assets_enabled) | ||
FileUtils.mkdir_p(File.join(@config.release_path, 'config')) | ||
app_rb = File.join(@config.release_path, 'config', 'application.rb') | ||
app_rb_contents = <<-EOF | ||
module Rails31 | ||
class Application < Rails::Application | ||
config.assets.enabled = #{assets_enabled ? 'true' : 'false'} | ||
end | ||
end | ||
EOF | ||
File.open(app_rb, 'w') {|f| f.write(app_rb_contents)} | ||
rakefile = File.join(@config.release_path, 'Rakefile') | ||
rakefile_contents = <<-EOF | ||
task 'assets:precompile' do | ||
sh 'touch precompiled' | ||
end | ||
EOF | ||
File.open(rakefile, 'w') {|f| f.write(rakefile_contents)} | ||
FileUtils.mkdir_p(File.join(@config.release_path, 'app', 'assets')) | ||
|
||
@deployer = FullTestDeploy.new(@config) | ||
@deployer.deploy | ||
end | ||
|
||
context "with default production settings" do | ||
before(:all) do | ||
deploy_test_application | ||
end | ||
|
||
it "precompiles assets" do | ||
pending "Bundler is a party" | ||
File.exist?(File.join(@deploy_dir, 'current', 'precompiled')).should be_true | ||
end | ||
end | ||
|
||
context "with asset support disabled in its config" do | ||
before(:all) do | ||
deploy_test_application(with_assets = false) | ||
end | ||
|
||
it "does not precompile assets" do | ||
pending "Bundler is a party" | ||
File.exist?(File.join(@deploy_dir, 'current', 'precompiled')).should be_false | ||
end | ||
end | ||
|
||
context "with existing precompilation in a deploy hook" do | ||
before(:all) do | ||
deploy_test_application do | ||
hook = File.join(@config.release_path, 'deploy', 'before_migrate') | ||
hook_contents = %Q[system 'touch custom_compiled && mkdir public/assets'] | ||
File.open(hook, 'w') {|f| f.puts(hook_contents) } | ||
File.chmod(0755, hook) | ||
end | ||
end | ||
|
||
it "does not replace the public/assets directory" do | ||
pending "Bundler is a party" | ||
File.exist?(File.join(@deploy_dir, 'current', 'custom_compiled')).should be_true | ||
File.exist?(File.join(@deploy_dir, 'current', 'precompiled')).should be_false | ||
File.directory?(File.join(@deploy_dir, 'current', 'public', 'assets')).should be_true | ||
File.symlink?(File.join(@deploy_dir, 'current', 'public', 'assets')).should be_false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters