您如何查看Capistrano中是否存在文件(在远程服务器上)? [英] How can you check to see if a file exists (on the remote server) in Capistrano?
问题描述
就像我在Googleverse中见过的其他许多人一样,我还是 File.exists?
陷阱的受害者,该陷阱当然会检查您的 local 文件系统,而不是要部署到的服务器。
Like many others I've seen in the Googleverse, I fell victim to the File.exists?
trap, which of course checks your local file system, not the server you are deploying to.
我发现了一个使用shell hack的结果,例如:
I found one result that used a shell hack like:
if [[ -d #{shared_path}/images ]]; then ...
但这对我来说并不好,除非它被很好地包裹在一个Ruby方法。
but that doesn't sit well with me, unless it were wrapped nicely in a Ruby method.
有人能解决这个问题吗?
Has anybody solved this elegantly?
推荐答案
@ knocte是正确的,因为 capture
是有问题的,因为通常每个人都将部署定向到多个主机(并且捕获仅从第一个主机获取输出)。为了检查所有主机,您需要使用 invoke_command
代替(这是捕获
在内部使用的功能)。在以下示例中,我检查以确保所有匹配的服务器上都存在文件:
@knocte is correct that capture
is problematic because normally everyone targets deployments to more than one host (and capture only gets the output from the first one). In order to check across all hosts, you'll need to use invoke_command
instead (which is what capture
uses internally). Here is an example where I check to ensure a file exists across all matched servers:
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
-请查看您可以通过的选项以获得更多控制权。
Note that invoke_command
uses run
by default -- check out the options you can pass for more control.
这篇关于您如何查看Capistrano中是否存在文件(在远程服务器上)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!