どのようにファイルがカピストラーノに(リモートサーバ上)が存在するかどうかを確認することができますか?
-
12-09-2019 - |
質問
私はGoogleverseで見てきた多くの人と同じように、私はもちろん、あなたののローカルのファイルシステムではなく、あなたが展開するサーバーをチェックしFile.exists?
トラップ、犠牲になった。
私のようなハックシェルを使用し、結果を1つ見つけます:
if [[ -d #{shared_path}/images ]]; then ...
しかし、それはRubyのメソッドできれいに包まれていない限りそれは、私とよく座っていません。
誰もがこれを解決したエレガントな?
解決
@knocteは通常誰もが複数のホストに配備をターゲット(第1の出力を取得するのみキャプチャ)ためcapture
が問題であることが正しいです。すべてのホスト間でチェックするためには、を使用する必要がありますinvoke_command
の代わりに(capture
が内部で使用するものです)。ここで私は、ファイルは全体の存在を確認するためにチェックする例です。のすべてののマッチサーバます:
def remote_file_exists?(path)
results = []
invoke_command("if [ -e '#{path}' ]; then echo -n 'true'; fi") do |ch, stream, out|
results << (out == 'true')
end
results.all?
end
そのinvoke_command
がデフォルトでrun
を使用します - <のhref = "https://github.com/capistrano/capistrano/blob/23452d6390d2ff59240f71966409850a7646320f/lib/capistrano/configuration/actions/invocation.rb#L92-をチェックしてください148" >オプションを使用すると、より多くの制御のためにを渡すことができます。
他のヒント
カピストラーノ3では、あなたが行うことができます:
on roles(:all) do
if test("[ -f /path/to/my/file ]")
# the file exists
else
# the file does not exist
end
end
それはあなたの地元のRubyプログラムに戻って、リモートテストの結果を返し、あなたはシンプルなシェルコマンドで作業することができますので、これはいいです。
のテストで、@bhups応答に触発されます:
def remote_file_exists?(full_path)
'true' == capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
end
namespace :remote do
namespace :file do
desc "test existence of missing file"
task :missing do
if remote_file_exists?('/dev/mull')
raise "It's there!?"
end
end
desc "test existence of present file"
task :exists do
unless remote_file_exists?('/dev/null')
raise "It's missing!?"
end
end
end
end
あなたがやりたいことかもしれあります:
isFileExist = 'if [ -d #{dir_path} ]; then echo "yes"; else echo "no"; fi'.strip
puts "File exist" if isFileExist == "yes"
私が行っていること(リモートサーバー上でシェルコマンドを実行する)カピストラーノでrunコマンドを使用する前に、
たとえば、ここでdatabase.ymlの共有/ configsディレクトリに存在するかどうかをチェックし、それが存在する場合はそれをリンクする1つのカピストラーノタスクがある。
desc "link shared database.yml"
task :link_shared_database_config do
run "test -f #{shared_path}/configs/database.yml && ln -sf
#{shared_path}/configs/database.yml #{current_path}/config/database.yml ||
echo 'no database.yml in shared/configs'"
end