Skip to content

Instantly share code, notes, and snippets.

@andrewandante
Last active December 18, 2017 16:43
Show Gist options
  • Save andrewandante/9561cba4f4e877ab97397902fdb2684d to your computer and use it in GitHub Desktop.
Save andrewandante/9561cba4f4e877ab97397902fdb2684d to your computer and use it in GitHub Desktop.
Testing File.exist? in Capistrano scripts
namespace :file_tests do
task :exists do
user = capture("whoami")
logger.debug "User is #{user}"
release_dir = capture("readlink /sites/dash/www").strip
logger.debug "Release directory is #{release_dir}"
file_exists("/etc/environment")
file_exists("/etc/not_really_there")
file_exists("/sites/dash/www/framework/sake")
file_exists("#{release_dir}")
file_exists("#{release_dir}/framework/sake")
end
def file_exists(file_path)
exists = false
if File.exist?(file_path)
logger.debug "File.exist? says that #{file_path} exists!"
exists = true
end
if "true" == capture("if [ -e '#{file_path}' ]; then echo 'true'; fi").strip
logger.debug "Inline bash says that #{file_path} exists!"
exists = true
end
stat = capture("stat '#{file_path}' || true")
if !stat.include?("No such file or directory")
logger.debug "Stat says that #{file_path} exists!"
exists = true
end
if !exists
logger.debug "No checks return true for #{file_path}."
end
end
end
## Trimmed for readability
aafincham@aafincham-dell:~/home/devPublic/cwp-projects/template/dash$ cap dash:deploynaut:vagrant file_tests:exists
* User is dash
* Release directory is /sites/dash/releases/20171218144524
* File.exist? says that /etc/environment exists!
* Inline bash says that /etc/environment exists!
* Stat says that /etc/environment exists!
* No checks return true for /etc/not_really_there.
* Inline bash says that /sites/dash/www/framework/sake exists!
* Stat says that /sites/dash/www/framework/sake exists!
* Inline bash says that /sites/dash/releases/20171218144524 exists!
* Stat says that /sites/dash/releases/20171218144524 exists!
* Inline bash says that /sites/dash/releases/20171218144524/framework/sake exists!
* Stat says that /sites/dash/releases/20171218144524/framework/sake exists!
File: ‘/etc/environment’
* Size: 74 Blocks: 8 IO Block: 4096 regular file
* Device: fe00h/65024d Inode: 788431 Links: 1
* Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
* Access: 2017-12-19 03:42:01.828415491 +1300
* Modify: 2017-12-07 23:19:27.776305117 +1300
* Change: 2017-12-07 23:19:27.776305117 +1300
* Birth: -
File: ‘/sites/dash/www/framework/sake’
* Size: 2814 Blocks: 8 IO Block: 4096 regular file
* Device: fe00h/65024d Inode: 533472 Links: 1
* Access: (0775/-rwxrwxr-x) Uid: ( 1002/ dash) Gid: ( 1002/ dash)
* Access: 2017-12-19 03:49:05.720255491 +1300
* Modify: 2017-12-07 13:35:49.000000000 +1300
* Change: 2017-12-19 03:49:05.680235496 +1300
* Birth: -
File: ‘/sites/dash/releases/20171218144524’
* Size: 4096 Blocks: 8 IO Block: 4096 directory
* Device: fe00h/65024d Inode: 406875 Links: 25
* Access: (2775/drwxrwsr-x) Uid: ( 1002/ dash) Gid: ( 1002/ dash)
* Access: 2017-12-19 03:49:12.379583491 +1300
* Modify: 2017-12-19 03:49:12.339563491 +1300
* Change: 2017-12-19 03:49:12.339563491 +1300
* Birth: -
File: ‘/sites/dash/releases/20171218144524/framework/sake’
* Size: 2814 Blocks: 8 IO Block: 4096 regular file
* Device: fe00h/65024d Inode: 533472 Links: 1
* Access: (0775/-rwxrwxr-x) Uid: ( 1002/ dash) Gid: ( 1002/ dash)
* Access: 2017-12-19 03:49:05.720255491 +1300
* Modify: 2017-12-07 13:35:49.000000000 +1300
* Change: 2017-12-19 03:49:05.680235496 +1300
* Birth: -
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment