#author("2016-04-19T15:24:57+09:00","default:wikiwriter","wikiwriter") &tag(Capistrano3); *目次 [#h2c8944f] #contents *関連ページ [#p990e6ae] -[[Capistrano]] -[[./アップグレード]] -[[./基本]] *参考情報 [#i3b863a7] -[[capistrano/CHANGELOG.md at master · capistrano/capistrano:https://github.com/capistrano/capistrano/blob/master/CHANGELOG.md]] -[[Upgrading from v2.x.x:http://capistranorb.com/documentation/upgrading/]] -[[capistrano 3.x系を使ってrailsをデプロイ | iii ThreeTreesLight:http://threetreeslight.com/post/68344998681/capistrano-3-x-rails]] -[[capistrano をバージョン 3 にアップデートして時代の流れに乗る - けんごのお屋敷:http://tkengo.github.io/blog/2013/12/12/version-up-capistrano-v3/]] … v2との違いなど -[[入門 Capistrano 3 ~ 全ての手作業を生まれる前に消し去りたい | GREE Engineers' Blog:http://labs.gree.jp/blog/2013/12/10084/]] … 丁寧な解説。 -Rails用のデプロイツールとしてminaというものもある。しかし依然としてCapistrano3のほうがメジャーな模様。 *Tips [#b1c4e757] **初回データベースやユーザーの生成をどうするか [#m9c90091] -[[Capistrano3で快適デプロイ生活!! - Less is Best:http://yss44.hatenablog.com/entry/2013/12/01/150215]]のように別タスクとして保存しておけばいいかもしれない。 #pre{{ #> dbサーバーのデータベースを生成するタスク。 #> デプロイ前に実行する必要がある。 task :db_create do on roles(:db) do |host| q1 = 'CREATE DATABASE IF NOT EXISTS <app_name>;' q2 = 'GRANT ALL ON <app_name>.* TO <app_user>@localhost IDENTIFIED BY "<app_passsword>";' q3 = "FLUSH PRIVILEGES;" sql = "#{q1}#{q2}#{q3}" execute "mysql --user=<mysql_user> --password=<msql_password> -e '#{sql}' " end end }} **passengerを再起動したい [#kc27af13] -apache passengerはtmp/restart.txtを見て再起動してくれる。 -そこで以下のような設定をdeploy.rbに追加しておくといいらしい。 #pre{{ namespace :deploy do after :restart, :restart_passenger do on roles(:web), in: :groups, limit: 3, wait: 10 do within release_path do execute :touch, 'tmp/restart.txt' end end end after :finishing, 'deploy:restart_passenger' end end }} -passengerは再起動したあとtmp/restart.txtを削除するという説としないという説がある。 -再起動したかどうかログにも書き出されずよくわからない。 **deploy_via:copyしたい(自作gem編) [#a88c0fbe] TODO: -capistrano-scm-copyはtarコマンドを使っているのがちょっと嫌。 -できれば自作gemと置き換えたい。構造は単純でcopy.rakeファイルを該当するLOAD_PATHに設置したgemを作るだけでよいっぽい。 -該当するTar/Zip gemを使いtarコマンドを使わないようにする。 -FileListのexcludeを利用する。 **deploy_via:copyしたい(capistrano-scm-copy編) [#u725bdde] -[[wercker/capistrano-scm-copy:https://github.com/wercker/capistrano-scm-copy]]を使う方法。 ***設定 [#fddf8ec0] -Gemfileに以下を追加する gem "capistrano-scm-copy" -config/deploy.rbの:scmを変更。exclude_dirに指定したディレクトリは無視される。 set :scm, :copy set :exclude_dir, ['vendor/bundle'] -staging.rb、production.rbの変更 -deploy_toでdeploy先ディレクトリを指定する。staging、production環境で異なる場合deploy.rbではなくstaging.rbかproduction.rbで変更しないといけない。 #pre{{ set :deploy_to, '/home/tanaka/www/demoapp' }} -公式ドキュメントによろと、Capfileに以下を追加する必要がある。 require 'capistrano/copy' -しかし、[[Appears to try to upload archive file 2x · Issue #17 · wercker/capistrano-scm-copy:https://github.com/wercker/capistrano-scm-copy/issues/17]]によると、処理が2回走ってしまうためrequireしてはならない。 #pre{{ I had the same problem but could find a proper solution; remove require 'capistrano/copy' anywhere! This problem occurs when we use this module with capistrano/setup because capistrano/setup loads a scm module with the :scm value. https://github.com/capistrano/capistrano/blob/master/lib/capistrano/setup.rb#L18 Good luck! }} **deploy_via:copyしたい(自作編) [#n43b9a84] -デフォルトではできなくなっているので、[[Capistrano3 で deploy_via :copy する - Qiita:http://qiita.com/hidakatsuya/items/4d097416516afc229199]]を参考にする。 -Gemfileにrubyzipを追加。 #pre{{ group :deployment do gem 'capistrano', '~> 3.0', require: false gem 'rubyzip', require: false end }} -lib/capistrano/tasks/copy.rakeを作成。上記リンクからちょっとカスタマイズ。 #pre{{ require 'zip' Zip.setup do |c| c.unicode_names = true c.on_exists_proc = true c.continue_on_exists_proc = true end namespace :copy do task :check do end task :set_current_revision do end task create_release: 'release.zip' do |t| file = t.prerequisites.first on roles(:app) do execute :mkdir, '-p', fetch(:tmp_dir) upload! file, fetch(:tmp_dir) execute :unzip, '-o', "#{fetch(:tmp_dir)}/release.zip", '-d', release_path end File.delete file if File.exists?(file) end file 'release.zip' do |t| release_filename = File.join(Dir.pwd, t.name) Dir.chdir fetch(:copy_dir) do Zip::File.open(release_filename, Zip::File::CREATE) do |zipfile| files = FileList['**/*'] files.exclude(*fetch(:copy_exclude)).each do |file| zipfile.add(file, file) end end end end end }} -deploy.rbを編集して完了。 #pre{{ set :scm, 'copy' set :copy_dir, '.' set :tmp_dir, '/tmp' set :copy_exclude, [ /\.log$/, %r!^files/.+! ] }} -copy_excludeのパターンは、 Rake::FileListの、excludeで指定できるパターン。[[class Rake::FileList:http://docs.ruby-lang.org/ja/2.0.0/class/Rake=3a=3aFileList.html#I_EXCLUDE]]。パターンとして正規表現、グロブパターン、文字列が使用可能。 -FileList['**/*']はプロジェクトルートからの相対パスを返してくるので、例えば railsプロジェクトの、logs、vendor以下を除外したい場合、以下のように指定するとよさそう。 #pre{{ set :copy_exclude, [ /^logs/, /^tmp/, /^vendor/ ] }} -ドットファイル(.で始まるファイル)がコピーされないけど、それはそれでいいのか?(特にruby-versionとか)。 **staging環境で使用したい [#i1307ad0] -staging環境にdeployしたい場合、config/deploy/staging.rbを指定するだけではだめで、Rails側でstaging環境を設定しておかないといけない。[[Capistrano3を最後にもう一度だけ懇切丁寧にまとめてみる - そのねこが学ぶとき:http://chroju89.hatenablog.jp/entry/2014/04/12/215628]] -database.ymlにstaging環境を作成する。 -config/environments/staging.rbを作成。production.rbをコピーして作成する。 -stating.rbでrails_envを指定する set :rails_env, "staging" -Gemfileにgroup:stagingを作成(?) **sharedへのリンク [#mc134145] -deploy.rbでコメントアウトされているlinkd_dirsをコメントアウトする。 set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/uploads') *FAQ [#v91a0669] **ロールってなに? [#p9dcdd23] -サーバーごとに役割を設定するもの。[[Rails - はじめてのCapistrano - Qiita:http://qiita.com/mosson/items/1a4cfd01cb538f2d8f0e]]の説明がわかりやすいかも。Capistrano3もCapistrano2も考え方に大差はない。 -Capistranoではタスクは、デフォルトでは全ロール、全サーバーに対して実行できる。タスクに対し例えばwebロールを割り当てると、webロールのサーバーに対してしか処理が行われなくなる。 **設定ファイルの、role、serverの意味ってなに? [#j2801aa6] -cap installで以下のような設定ファイルが作成される。 #pre{{ role :app, %w{deploy@example.com} role :web, %w{deploy@example.com} role :db, %w{deploy@example.com} # Extended Server Syntax # ====================== # This can be used to drop a more detailed server definition into the # server list. The second argument is a, or duck-types, Hash and is # used to set extended properties on the server. server 'example.com', user: 'deploy', roles: %w{web app}, my_property: :my_value }} -コメントによると、roleは、ロールとサーバーの簡易同時設定。serverはサーバーの詳細設定(既存ロール使用)といったイメージか。 *トラブルシューティング [#h9a9b424] **デプロイ後にassetsがプリコンパイルされない [#ce91609e] -Capfileでrequire 'capistrano/rails/assets'が有効になっているかどうか確認する。 **デプロイ後ExecJS and could not find a JavaScript runtimeエラーが発生 [#ta14004a] -[[ruby on rails 3.1 - ExecJS and could not find a JavaScript runtime - Stack Overflow:http://stackoverflow.com/questions/6282307/execjs-and-could-not-find-a-javascript-runtime]]によると、サーバー環境にNode.jsをインストールする。 #pre{{ sudo apt-get install nodejs sudo yum install nodejs }} **デプロイ後「fatal: Not a git repository (or any of the parent directories): .git」と言われてしまう [#e2931787] -Gemfileにローカルフォルダで管理しているgemファイルを追加していて、そのgemspecで以下のようにgitを使っているから。 gem.files = `git ls-files`.split($\) -deploy先に.gitフォルダが存在しないためこのような減少が発生する模様。 -ローカルフォルダでgemを管理するのはやめたほうが良いかもしれない。 -もしくはgemspecでgit ls-filesを使わないようにするか。 [[`git ls-files` in gemspec template is bad practice. · Issue #2287 · bundler/bundler:https://github.com/bundler/bundler/issues/2287]] **「Command: [ -d /var/www/myapp/releases/20140211033611/public/assets ]」のようなわけのわからないエラーが発生 [#ve91d55a] -[[ruby on rails - Capistrano 3 process failing - Stack Overflow:http://stackoverflow.com/questions/21692601/capistrano-3-process-failing]]によるととりあえず気にしなくても良いのだろうか。 **「Command : [ -L ...]」が exit status 1 (failed).で失敗する [#te9478f2] -[[ruby - Capistrano 3 deploy failed messages - exit status 1 (failed) - Stack Overflow:http://stackoverflow.com/questions/33128623/capistrano-3-deploy-failed-messages-exit-status-1-failed]] -致命的エラーならばCapistranoが止まるはずなので気にしなくてよい(のか?) ** Too many authentication failuresが発生 [#af93b281] -sshをコマンドラインから使うと問題ない。SSHKitの問題?とりあえず以下で一時的に解決できる ssh-add -D