和许多我在谷歌搜索中看到的人一样,我也曾经遭受过File.exists?
陷阱的困扰,这个方法实际上检查的是你本地文件系统而不是你要部署到的服务器。
我找到了一种使用shell hack的解决方案,类似于:
if [[ -d #{shared_path}/images ]]; then ...
但我不太满意这种方法,除非它被包装在一个漂亮的Ruby方法中。
有人解决过这个问题吗?
和许多我在谷歌搜索中看到的人一样,我也曾经遭受过File.exists?
陷阱的困扰,这个方法实际上检查的是你本地文件系统而不是你要部署到的服务器。
我找到了一种使用shell hack的解决方案,类似于:
if [[ -d #{shared_path}/images ]]; then ...
但我不太满意这种方法,除非它被包装在一个漂亮的Ruby方法中。
有人解决过这个问题吗?
在 Capistrano 3 中,您可以执行以下操作:
on roles(:all) do
if test("[ -f /path/to/my/file ]")
# the file exists
else
# the file does not exist
end
end
这很好,因为它会将远程测试的结果返回到您的本地 Ruby 程序中,您可以使用更简单的 shell 命令进行操作。
@knocte是正确的,因为capture
存在问题,通常每个人都会将部署目标定位到多个主机上(而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
-- 查看可传递的选项以获取更多控制。
deploy.rb
中包含它,我会得到一个 undefined method 'remote_file_exists' for #<Capistrano::Configuration::Namespaces::Namespace:0x00000003650f88> (NoMethodError)
错误 - 基本上就像在命名空间中的任务内部调用 sudo "…" if remote_file_exists? "/foo/bar"
。 - slhck受@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"
if [ -d #{dir_path} ]; then echo "yes"; else echo "no"; fi
.strip(确保删除反引号内部的额外空格:我已经添加了它们以帮助 SO 显示) - D_K我以前用Capistrano的run命令(在远程服务器上执行shell命令)完成过这个任务。
例如,下面是一个Capistrano任务,它将检查shared/configs目录中是否存在database.yml文件,如果存在,则会将其链接。
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
unless test("[ -f " + shared_path.to_s + "/a_shared_file.txt ]" )
的东西。 - Lightheadedtest
的文档吗?这是一个很难搜索的单词。谢谢! - Jared Beck[ -p ... ]
。 - Evgenia Karunus