2013-06-11 15 views
13

OpsWorksは、展開時にアセットをプリコンパイルしません。 this threadにこのレシピが見つかりましたが、私はそれが完全ではない、または何かが見つからないと思っています。私には、release_pathのエラーが見つかりません。シェフにアセットをプリコンパイルする方法は?

precompile.rb:

Chef::Log.info("Running deploy/before_migrate.rb...") 

Chef::Log.info("Symlinking #{release_path}/public/assets to #{new_resource.deploy_to}/shared/assets") 

link "#{release_path}/public/assets" do 
    to "#{new_resource.deploy_to}/shared/assets" 
end 

rails_env = new_resource.environment["RAILS_ENV"] 
Chef::Log.info("Precompiling assets for RAILS_ENV=#{rails_env}...") 

execute "rake assets:precompile" do 
    cwd release_path 
    command "bundle exec rake assets:precompile" 
    environment "RAILS_ENV" => rails_env 
end 

ログ

undefined local variable or method `release_path' for .... 

任意のアイデア?私はシェフを全く知らず、これをオンザフライで把握しようとしています。

+0

。 – cmur2

+0

これは、アプリケーションをデプロイするたびに変更されます。それは動的なので、私はそれをハードコードすることはできません。 – manafire

答えて

10

OpsWorksがアセットパイプラインをそのまま使用できるようになる前に、これを行うことができます。 レールアプリケーションに次の内容のファイルdeploy/before_symlink.rbを作成します。

run "cd #{release_path} && RAILS_ENV=production bundle exec rake assets:precompile" 

Railsアプリケーションを別の環境に配備する場合は、RAILS_ENVを変更します。

NGINX/Unicornスタックを使用する場合は、/assetsリソースを変更する必要があります。 Cookbookのunicorn/templates/default/nginx_unicorn_web_app.erbというファイルに次のコンテンツをコピーするだけです。

upstream unicorn_<%= @application[:domains].first %> { 
server unix:<%= @application[:deploy_to]%>/shared/sockets/unicorn.sock fail_timeout=0; 
} 

server { 
    listen 80; 
    server_name <%= @application[:domains].join(" ") %> <%= node[:hostname] %>; 
    access_log <%= node[:nginx][:log_dir] %>/<%= @application[:domains].first %>.access.log; 

    keepalive_timeout 5; 

    root <%= @application[:absolute_document_root] %>; 

    <% if @application[:nginx] && @application[:nginx][:client_max_body_size] %> 
    client_max_body_size <%= @application[:nginx][:client_max_body_size] %>; 
    <% end %> 

    location/{ 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 

    # If you don't find the filename in the static files 
    # Then request it from the unicorn server 
    if (!-f $request_filename) { 
     proxy_pass http://unicorn_<%= @application[:domains].first %>; 
     break; 
    } 
    } 

    location /nginx_status { 
    stub_status on; 
    access_log off; 
    allow 127.0.0.1; 
    deny all; 
    } 

    location ~ ^/assets/ { 
    expires 1y; 
    add_header Cache-Control public; 

    add_header ETag ""; 
    break; 
    } 

    error_page 500 502 503 504 /500.html; 
    location = /500.html { 
    root <%= @application[:absolute_document_root] %>; 
    } 
} 

<% if @application[:ssl_support] %> 
server { 
    listen 443; 
    server_name <%= @application[:domains].join(" ") %> <%= node[:hostname] %>; 
    access_log <%= node[:nginx][:log_dir] %>/<%= @application[:domains].first %>-ssl.access.log; 

    ssl on; 
    ssl_certificate /etc/nginx/ssl/<%= @application[:domains].first %>.crt; 
    ssl_certificate_key /etc/nginx/ssl/<%= @application[:domains].first %>.key; 
    <% if @application[:ssl_certificate_ca] -%> 
    ssl_client_certificate /etc/nginx/ssl/<%= @application[:domains].first %>.ca; 
    <% end -%> 

    keepalive_timeout 5; 

    root <%= @application[:absolute_document_root] %>; 

    <% if @application[:nginx] && @application[:nginx][:client_max_body_size] %> 
    client_max_body_size <%= @application[:nginx][:client_max_body_size] %>; 
    <% end %> 

    location/{ 
    proxy_set_header X-Forwarded-Proto https; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 

    # If you don't find the filename in the static files 
    # Then request it from the unicorn server 
    if (!-f $request_filename) { 
     proxy_pass http://unicorn_<%= @application[:domains].first %>; 
     break; 
    } 
    } 

    location ~ ^/assets/ { 
    expires 1y; 
    add_header Cache-Control public; 

    add_header ETag ""; 
    break; 
    } 

    error_page 500 502 503 504 /500.html; 
    location = /500.html { 
    root <%= @application[:absolute_document_root] %>; 
    } 
} 
<% end %> 

あなたがApache2の/旅客スタックを使用する場合は、/assetsリソースを変更する必要があります。 Cookbookの passenger_apache2/templates/default/web_app.conf.erbというファイルに次のコンテンツをコピーするだけです。

<VirtualHost *:80> 
    ServerName <%= @params[:server_name] %> 
    <% if @params[:server_aliases] && [email protected][:server_aliases].empty? -%> 
    ServerAlias <% @params[:server_aliases].each do |a| %><%= "#{a}" %> <% end %> 
    <% end -%> 

    <% if @params[:mounted_at] -%> 
    DocumentRoot /var/www 
    <%= @params[:deploy][:passenger_handler] -%>BaseURI <%= @params[:mounted_at] %> 
    <% else -%> 
    DocumentRoot <%= @params[:docroot] %> 
    <%= @params[:deploy][:passenger_handler] -%>BaseURI/
    <% end -%> 
    <%= @params[:deploy][:passenger_handler] -%>Env <%= @params[:rails_env] %> 

    <Directory <%= @params[:docroot] %>> 
    Options FollowSymLinks 
    AllowOverride None 
    Order allow,deny 
    Allow from all 
    </Directory> 

    <Directory ~ "\.svn"> 
    Order allow,deny 
    Deny from all 
    </Directory> 

    <Directory ~ "\.git"> 
    Order allow,deny 
    Deny from all 
    </Directory> 

    <LocationMatch "^/assets/.*$"> 
    Header unset ETag 
    FileETag None 
    # RFC says only cache for 1 year 
    ExpiresActive On 
    ExpiresDefault "access plus 1 year" 
    </LocationMatch> 

    LogLevel info 
    ErrorLog <%= node[:apache][:log_dir] %>/<%= @params[:name] %>-error.log 
    CustomLog <%= node[:apache][:log_dir] %>/<%= @params[:name] %>-access.log combined 
    CustomLog <%= node[:apache][:log_dir] %>/<%= @params[:name] %>-ganglia.log ganglia 

    FileETag none 

    RewriteEngine On 
    Include <%= @params[:rewrite_config] %>* 
    RewriteLog <%= node[:apache][:log_dir] %>/<%= @application_name %>-rewrite.log 
    RewriteLogLevel 0 

    # Canonical host 
    #RewriteCond %{HTTP_HOST} !^<%= @params[:server_name] %> [NC] 
    #RewriteCond %{HTTP_HOST} !^$ 
    #RewriteRule ^/(.*)$  http://<%= @params[:server_name] %>/$1 [L,R=301] 

    RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|jpeg|png)$ 
    RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f 
    RewriteCond %{SCRIPT_FILENAME} !maintenance.html 
    RewriteRule ^.*$ /system/maintenance.html [L] 

    Include <%= @params[:local_config] %>* 
</VirtualHost> 

<% if node[:deploy][@application_name][:ssl_support] -%> 
<VirtualHost *:443> 
    ServerName <%= @params[:server_name] %> 
    <% if @params[:server_aliases] && [email protected][:server_aliases].empty? -%> 
    ServerAlias <% @params[:server_aliases].each do |a| %><%= "#{a}" %> <% end %> 
    <% end -%> 

    SSLEngine on 
    SSLProxyEngine on 
    SSLCertificateFile <%= node[:apache][:dir] %>/ssl/<%= @params[:server_name] %>.crt 
    SSLCertificateKeyFile <%= node[:apache][:dir] %>/ssl/<%= @params[:server_name] %>.key 
    <% if @params[:ssl_certificate_ca] -%> 
    SSLCACertificateFile <%= node[:apache][:dir] %>/ssl/<%= @params[:server_name] %>.ca 
    <% end -%> 
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0 

    <% if @params[:mounted_at] -%> 
    DocumentRoot /var/www 
    <%= @params[:deploy][:passenger_handler] -%>BaseURI <%= @params[:mounted_at] %> 
    <% else -%> 
    DocumentRoot <%= @params[:docroot] %> 
    <%= @params[:deploy][:passenger_handler] -%>BaseURI/
    <% end -%> 
    <%= @params[:deploy][:passenger_handler] -%>Env <%= @params[:rails_env] %> 

    <Directory <%= @params[:docroot] %>> 
    Options FollowSymLinks 
    AllowOverride All 
    Order allow,deny 
    Allow from all 
    </Directory> 

    <Directory ~ "\.svn"> 
    Order allow,deny 
    Deny from all 
    </Directory> 

    <Directory ~ "\.git"> 
    Order allow,deny 
    Deny from all 
    </Directory> 

    <LocationMatch "^/assets/.*$"> 
    Header unset ETag 
    FileETag None 
    # RFC says only cache for 1 year 
    ExpiresActive On 
    ExpiresDefault "access plus 1 year" 
    </LocationMatch> 

    LogLevel info 
    ErrorLog <%= node[:apache][:log_dir] %>/<%= @params[:name] %>-ssl-error.log 
    CustomLog <%= node[:apache][:log_dir] %>/<%= @params[:name] %>-ssl-access.log combined 
    CustomLog <%= node[:apache][:log_dir] %>/<%= @params[:name] %>-ssl-ganglia.log ganglia 

    FileETag none 

    RewriteEngine On 
    Include <%= @params[:rewrite_config] %>-ssl* 
    RewriteLog <%= node[:apache][:log_dir] %>/<%= @application_name %>-ssl-rewrite.log 
    RewriteLogLevel 0 

    # Canonical host 
    #RewriteCond %{HTTP_HOST} !^<%= @params[:server_name] %> [NC] 
    #RewriteCond %{HTTP_HOST} !^$ 
    #RewriteRule ^/(.*)$  http://<%= @params[:server_name] %>/$1 [L,R=301] 

    RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|jpeg|png)$ 
    RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f 
    RewriteCond %{SCRIPT_FILENAME} !maintenance.html 
    RewriteRule ^.*$ /system/maintenance.html [L] 

    Include <%= @params[:local_config] %>-ssl* 
</VirtualHost> 
<% end -%> 

お気軽にお尋ねください。

ベスト ダニエル

EDIT:

それとも、ただ、これは私が急速に発展https://github.com/neonlex/massive-octo-computing-machineを料理の上にコピーします。 しかし、OpsWorksはデフォルトでこれを将来サポートしなければなりません。

+0

親愛なる、ダニエルありがとう。私は最終的に私のために働いた(以下の答えが答えました)何かを見つけ出すことができました。それが効いているので、私はそれを壊すことを恐れてあなたの料理本を試すのが怖くて怖いです。ユニコーンのステップがあなたの答えにどのようなものか、私が思いついたことについてあなたが思っていることを知りたいのは興味深いです。ありがとう! – manafire

+0

@neonlex私はそれで更新されたレポをうまく使用しました。私はruby shellコマンドをhttps://github.com/neonlex/massive-octo-computing-machine/blob/master/rails/libraries/rails_configuration.rbでレシピdslを実行して置き換えようとしましたが、エラーは未定義のメソッド 'execute 'を返します。それがうまくいかなかったアイデア! – goutham

+0

私は/資産や料理本を変更する必要はありませんでした。単にbefore_symlink.rbが私のために働いただけです。私はnginxとunicornで、既定のOpsworksレシピを使用しています。 – Kirk

5

私はOpsWorksとシェフについてはほとんど分かりませんが、ここでは私がそれを動作させるために何をしましたか?

まず、setupイベント中に実行されるレールレシピを作成して、アセットのシンボリックリンクディレクトリを作成する必要がありました。これは、OpsWorksがアクセスできるパブリック・リポジトリにあります。

料理/レール/レシピ/ symlink_assets.rb:

node[:deploy].each do |application, deploy| 
    Chef::Log.info("Ensuring shared/assets directory for #{application} app...") 

    directory "#{deploy[:deploy_to]}/shared/assets" do 
    group deploy[:group] 
    owner deploy[:user] 
    mode 0775 
    action :create 
    recursive true 
    end 
end 

はその後、私のアプリでは、私がデプロイ/ before_migrate.rbを作成する必要がありました:

Chef::Log.info("Running deploy/before_migrate.rb...") 

Chef::Log.info("Symlinking #{release_path}/public/assets to #{new_resource.deploy_to}/shared/assets") 

link "#{release_path}/public/assets" do 
    to "#{new_resource.deploy_to}/shared/assets" 
end 

rails_env = new_resource.environment["RAILS_ENV"] 
Chef::Log.info("Precompiling assets for RAILS_ENV=#{rails_env}...") 

execute "rake assets:precompile" do 
    cwd release_path 
    command "bundle exec rake assets:precompile" 
    environment "RAILS_ENV" => rails_env 
end 

これは、取得しますデプロイメントプロセス中に呼び出され、アセットをコンパイルします。インスタンスはreleaseパスへの書き込み権の許可が必要ですので、私は、rootとしてこのコマンドを実行

execute 'rake assets:precompile' do 
    cwd "#{node[:deploy_to]}/current" 
    user 'root' 
    command 'bundle exec rake assets:precompile' 
    environment 'RAILS_ENV' => node[:environment_variables][:RAILS_ENV] 
end 

+0

何らかの理由で私が共有資産ディレクトリを探していても、私のスクリプトのいくつかを再構築してください。 –

3

は、AWS Opsworksで、私は以下のレシピを使用します。コマンドをdeployユーザーとして実行すると、アクセス権拒否エラーが表示されます。

+0

OpsWorksが/ shared内のすべての所有者としてデプロイメントユーザーを設定しているようだから、パーミッションが拒否されました。このユーザーは、(ファイルマスク設定で)オーナールートでファイルを作成するので、ログファイルを作成して編集することはできません。 – zrisher

5

new OpsWorks featureを使用してRailsアプリケーションに環境変数を渡している場合は、rake呼び出しにこれらの変数を含める必要があることに注意してください(これらの変数はデプロイメントユーザーの環境に永続的に組み込まれないため)。

私は、次のdeploy/before_migrate.rb中(this articlethis recipeに基づく):

あなたがrelease_path、Railsアプリケーションがdeisgnationホスト上に存在する必要がありますパスを提供するために持っているように見えます
Chef::Log.info("Precompiling assets for RAILS_ENV=" \ 
       "#{new_resource.environment['RAILS_ENV']}...") 

execute 'rake assets:precompile' do 
    cwd release_path 
    command 'bundle exec rake assets:precompile' 
    environment new_resource.environment 
end 
関連する問題