class Specinfra::Command::Base::File

Public Class Methods

change_group(file, group, options = {}) click to toggle source
# File lib/specinfra/command/base/file.rb, line 159
def change_group(file, group, options = {})
  option = '-R' if options[:recursive]
  "chgrp #{option} #{escape(group)} #{escape(file)}".squeeze(' ')
end
change_mode(file, mode, options = {}) click to toggle source
# File lib/specinfra/command/base/file.rb, line 148
def change_mode(file, mode, options = {})
  option = '-R' if options[:recursive]
  "chmod #{option} #{mode} #{escape(file)}".squeeze(' ')
end
change_owner(file, owner, group=nil, options = {}) click to toggle source
# File lib/specinfra/command/base/file.rb, line 153
def change_owner(file, owner, group=nil, options = {})
  option = '-R' if options[:recursive]
  owner = "#{owner}:#{group}" if group
  "chown #{option} #{escape(owner)} #{escape(file)}".squeeze(' ')
end
check_contains(file, expected_pattern) click to toggle source
# File lib/specinfra/command/base/file.rb, line 31
def check_contains(file, expected_pattern)
  "#{check_contains_with_regexp(file, expected_pattern)} || #{check_contains_with_fixed_strings(file, expected_pattern)}"
end
check_contains_lines(file, expected_lines, from=nil, to=nil) click to toggle source
# File lib/specinfra/command/base/file.rb, line 68
def check_contains_lines(file, expected_lines, from=nil, to=nil)
  require 'digest/md5'
  from ||= '1'
  to ||= '$'
  sed = "sed -n #{escape(from)},#{escape(to)}p #{escape(file)}"
  head_line = expected_lines.first.chomp
  lines_checksum = Digest::MD5.hexdigest(expected_lines.map(&:chomp).join("\n") + "\n")
  afterwards_length = expected_lines.length - 1
  "#{sed} | grep -A #{escape(afterwards_length)} -F -- #{escape(head_line)} | md5sum | grep -qiw -- #{escape(lines_checksum)}"
end
check_contains_with_fixed_strings(file, expected_pattern) click to toggle source
# File lib/specinfra/command/base/file.rb, line 83
def check_contains_with_fixed_strings(file, expected_pattern)
  "grep -qFs -- #{escape(expected_pattern)} #{escape(file)}"
end
check_contains_with_regexp(file, expected_pattern) click to toggle source
# File lib/specinfra/command/base/file.rb, line 79
def check_contains_with_regexp(file, expected_pattern)
  "grep -qs -- #{escape(expected_pattern)} #{escape(file)}"
end
check_contains_within(file, expected_pattern, from=nil, to=nil) click to toggle source
# File lib/specinfra/command/base/file.rb, line 58
def check_contains_within(file, expected_pattern, from=nil, to=nil)
  from ||= '1'
  to ||= '$'
  sed = "sed -n #{escape(from)},#{escape(to)}p #{escape(file)}"
  sed += " | sed -n 1,#{escape(to)}p" if from != '1' and to != '$'
  checker_with_regexp = check_contains_with_regexp("-", expected_pattern)
  checker_with_fixed = check_contains_with_fixed_strings("-", expected_pattern)
  "#{sed} | #{checker_with_regexp} || #{sed} | #{checker_with_fixed}"
end
check_exists(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 87
def check_exists(file)
  "test -e #{escape(file)}"
end
check_has_mode(file, mode) click to toggle source
# File lib/specinfra/command/base/file.rb, line 53
def check_has_mode(file, mode)
  regexp = "^#{mode}$"
  "stat -c %a #{escape(file)} | grep -- #{escape(regexp)}"
end
check_is_block_device(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 19
def check_is_block_device(file)
  "test -b #{escape(file)}"
end
check_is_character_device(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 23
def check_is_character_device(file)
  "test -c #{escape(file)}"
end
check_is_dereferenceable(link) click to toggle source
# File lib/specinfra/command/base/file.rb, line 136
def check_is_dereferenceable(link)
  %Q|test -n "$(readlink -e #{escape(link)})"|
end
check_is_directory(directory) click to toggle source
# File lib/specinfra/command/base/file.rb, line 7
def check_is_directory(directory)
  "test -d #{escape(directory)}"
end
check_is_file(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 3
def check_is_file(file)
  "test -f #{escape(file)}"
end
check_is_grouped(file, group) click to toggle source
# File lib/specinfra/command/base/file.rb, line 35
def check_is_grouped(file, group)
  regexp = "^#{group}$"
  if group.is_a?(Numeric) || (group =~ /\A\d+\z/ ? true : false)
    "stat -c %g #{escape(file)} | grep -- #{escape(regexp)}"
  else
    "stat -c %G #{escape(file)} | grep -- #{escape(regexp)}"
  end
end
check_is_linked_to(link, target) click to toggle source
# File lib/specinfra/command/base/file.rb, line 120
def check_is_linked_to(link, target)
  %Q|test x"$(readlink #{escape(link)})" = x"#{escape(target)}"|
end
check_is_mounted(path) click to toggle source
# File lib/specinfra/command/base/file.rb, line 103
def check_is_mounted(path)
  regexp = "on #{path} "
  "mount | grep -- '#{escape(regexp)}'"
end
check_is_owned_by(file, owner) click to toggle source
# File lib/specinfra/command/base/file.rb, line 44
def check_is_owned_by(file, owner)
  regexp = "^#{owner}$"
  if owner.is_a?(Numeric) || (owner =~ /\A\d+\z/ ? true : false)
    "stat -c %u #{escape(file)} | grep -- #{escape(regexp)}"
  else
    "stat -c %U #{escape(file)} | grep -- #{escape(regexp)}"
  end
end
check_is_pipe(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 11
def check_is_pipe(file)
  "test -p #{escape(file)}"
end
check_is_socket(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 15
def check_is_socket(file)
  "test -S #{escape(file)}"
end
copy(src, dest, options = {}) click to toggle source
# File lib/specinfra/command/base/file.rb, line 168
def copy(src, dest, options = {})
  option = '-p'
  option << 'R' if options[:recursive]
  "cp #{option} #{escape(src)} #{escape(dest)}"
end
create_as_directory(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 164
def create_as_directory(file)
  "mkdir -p #{escape(file)}"
end
download(src, dest) click to toggle source
# File lib/specinfra/command/base/file.rb, line 189
def download(src, dest)
  "curl -sSL #{escape(src)} -o #{escape(dest)}"
end
get_content(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 99
def get_content(file)
  "cat #{escape(file)} 2> /dev/null || echo -n"
end
get_md5sum(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 91
def get_md5sum(file)
  "md5sum #{escape(file)} | cut -d ' ' -f 1"
end
get_mode(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 108
def get_mode(file)
  "stat -c %a #{escape(file)}"
end
get_mtime(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 140
def get_mtime(file)
  "stat -c %Y #{escape(file)}"
end
get_owner_group(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 116
def get_owner_group(file)
  "stat -c %G #{escape(file)}"
end
get_owner_user(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 112
def get_owner_user(file)
  "stat -c %U #{escape(file)}"
end
get_sha256sum(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 95
def get_sha256sum(file)
  "sha256sum #{escape(file)} | cut -d ' ' -f 1"
end
get_size(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 144
def get_size(file)
  "stat -c %s #{escape(file)}"
end
move(src, dest) click to toggle source
# File lib/specinfra/command/base/file.rb, line 174
def move(src, dest)
  "mv #{escape(src)} #{escape(dest)}"
end
remove(file) click to toggle source
# File lib/specinfra/command/base/file.rb, line 185
def remove(file)
  "rm -rf #{escape(file)}"
end