Class: Specinfra::Command::Base::File

Inherits:
Specinfra::Command::Base show all
Defined in:
lib/specinfra/command/base/file.rb

Class Method Summary collapse

Methods inherited from Specinfra::Command::Base

create, escape

Class Method Details

.change_group(file, group) ⇒ Object



121
122
123
# File 'lib/specinfra/command/base/file.rb', line 121

def change_group(file, group)
  "chgrp #{group} #{escape(file)}"
end

.change_mode(file, mode) ⇒ Object



112
113
114
# File 'lib/specinfra/command/base/file.rb', line 112

def change_mode(file, mode)
  "chmod #{mode} #{escape(file)}"
end

.change_owner(file, owner, group = nil) ⇒ Object



116
117
118
119
# File 'lib/specinfra/command/base/file.rb', line 116

def change_owner(file, owner, group=nil)
  owner = "#{owner}:#{group}" if group
  "chown #{owner} #{escape(file)}"
end

.check_contains(file, expected_pattern) ⇒ Object



15
16
17
# File 'lib/specinfra/command/base/file.rb', line 15

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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/specinfra/command/base/file.rb', line 44

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) ⇒ Object



59
60
61
# File 'lib/specinfra/command/base/file.rb', line 59

def check_contains_with_fixed_strings(file, expected_pattern)
  "grep -qFs -- #{escape(expected_pattern)} #{escape(file)}"
end

.check_contains_with_regexp(file, expected_pattern) ⇒ Object



55
56
57
# File 'lib/specinfra/command/base/file.rb', line 55

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) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/specinfra/command/base/file.rb', line 34

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_has_mode(file, mode) ⇒ Object



29
30
31
32
# File 'lib/specinfra/command/base/file.rb', line 29

def check_has_mode(file, mode)
  regexp = "^#{mode}$"
  "stat -c %a #{escape(file)} | grep -- #{escape(regexp)}"
end

.check_is_directory(directory) ⇒ Object



7
8
9
# File 'lib/specinfra/command/base/file.rb', line 7

def check_is_directory(directory)
  "test -d #{escape(directory)}"
end

.check_is_file(file) ⇒ Object



3
4
5
# File 'lib/specinfra/command/base/file.rb', line 3

def check_is_file(file)
  "test -f #{escape(file)}"
end

.check_is_grouped(file, group) ⇒ Object



19
20
21
22
# File 'lib/specinfra/command/base/file.rb', line 19

def check_is_grouped(file, group)
  regexp = "^#{group}$"
  "stat -c %G #{escape(file)} | grep -- #{escape(regexp)}"
end


96
97
98
# File 'lib/specinfra/command/base/file.rb', line 96

def check_is_link(link)
  "test -L #{escape(link)}"
end

.check_is_linked_to(link, target) ⇒ Object



92
93
94
# File 'lib/specinfra/command/base/file.rb', line 92

def check_is_linked_to(link, target)
  "stat -c %N #{escape(link)} | egrep -e \"-> .#{escape(target)}.\""
end

.check_is_mounted(path) ⇒ Object



75
76
77
78
# File 'lib/specinfra/command/base/file.rb', line 75

def check_is_mounted(path)
  regexp = "on #{path} "
  "mount | grep -- '#{escape(regexp)}'"
end

.check_is_owned_by(file, owner) ⇒ Object



24
25
26
27
# File 'lib/specinfra/command/base/file.rb', line 24

def check_is_owned_by(file, owner)
  regexp = "^#{owner}$"
  "stat -c %U #{escape(file)} | grep -- #{escape(regexp)}"
end

.check_is_socket(file) ⇒ Object



11
12
13
# File 'lib/specinfra/command/base/file.rb', line 11

def check_is_socket(file)
  "test -S #{escape(file)}"
end

.copy(src, dest) ⇒ Object



129
130
131
# File 'lib/specinfra/command/base/file.rb', line 129

def copy(src, dest)
  "cp #{escape(src)} #{escape(dest)}"
end

.create_as_directory(file) ⇒ Object



125
126
127
# File 'lib/specinfra/command/base/file.rb', line 125

def create_as_directory(file)
  "mkdir -p #{escape(file)}"
end

.get_content(file) ⇒ Object



71
72
73
# File 'lib/specinfra/command/base/file.rb', line 71

def get_content(file)
  "cat #{file} 2> /dev/null || echo -n"
end


100
101
102
# File 'lib/specinfra/command/base/file.rb', line 100

def get_link_target(link)
  "readlink -f #{escape(link)}"
end

.get_md5sum(file) ⇒ Object



63
64
65
# File 'lib/specinfra/command/base/file.rb', line 63

def get_md5sum(file)
  "md5sum #{escape(file)} | cut -d ' ' -f 1"
end

.get_mode(file) ⇒ Object



80
81
82
# File 'lib/specinfra/command/base/file.rb', line 80

def get_mode(file)
  "stat -c %a #{escape(file)}"
end

.get_mtime(file) ⇒ Object



104
105
106
# File 'lib/specinfra/command/base/file.rb', line 104

def get_mtime(file)
  "stat -c %Y #{escape(file)}"
end

.get_owner_group(file) ⇒ Object



88
89
90
# File 'lib/specinfra/command/base/file.rb', line 88

def get_owner_group(file)
  "stat -c %G #{escape(file)}"
end

.get_owner_user(file) ⇒ Object



84
85
86
# File 'lib/specinfra/command/base/file.rb', line 84

def get_owner_user(file)
  "stat -c %U #{escape(file)}"
end

.get_sha256sum(file) ⇒ Object



67
68
69
# File 'lib/specinfra/command/base/file.rb', line 67

def get_sha256sum(file)
  "sha256sum #{escape(file)} | cut -d ' ' -f 1"
end

.get_size(file) ⇒ Object



108
109
110
# File 'lib/specinfra/command/base/file.rb', line 108

def get_size(file)
  "stat -c %s #{escape(file)}"
end


137
138
139
# File 'lib/specinfra/command/base/file.rb', line 137

def link_to(link, target)
  "ln -s #{escape(target)} #{escape(link)}"
end

.move(src, dest) ⇒ Object



133
134
135
# File 'lib/specinfra/command/base/file.rb', line 133

def move(src, dest)
  "mv #{escape(src)} #{escape(dest)}"
end

.remove(file) ⇒ Object



141
142
143
# File 'lib/specinfra/command/base/file.rb', line 141

def remove(file)
  "rm -rf #{escape(file)}"
end