views:

855

answers:

4

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.

I found one result that used a shell hack like if [[ -d #{shared_path}/images ]]; then ... but that doesn't sit well with me, unless it were wrapped nicely in a Ruby method.

Has anybody solved this elegantly?

+1  A: 

May be you want to do is:

isFileExist = 'if [ -d #{dir_path} ]; then echo "yes"; else echo "no"; fi'.strip
puts "File exist" if isFileExist == "yes"
bhups
Thanks. I'm assuming you mean to wrap that with the "capture" method? http://www.capify.org/index.php/Capture
Teflon Ted
+1  A: 

Inspired by @bhups response, with tests:

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
Teflon Ted
A: 

How about deferring to SFTP?

(off-the-cuff code, not tested)

require 'net/sftp'

Net::SFTP.start("server", "user") do |sftp|
  stfp.dir.glob("mypath", "myfile") do |entry|
    p "File exists at #{entry}"
  end
end
Craig Walker
Too bad to connect a second time when capistrano is already connected once.
Damien MATHIEU
A: 

I have done that before using the run command in capistrano (which execute a shell command on the remote server)

For example here is one capistrano task which will check if a database.yml exists in the shared/configs directory and link it if it exists.

  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
Aurélien Bottazzini