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, options = {}) ⇒ Object



159
160
161
162
# File 'lib/specinfra/command/base/file.rb', line 159

def change_group(file, group, options = {})
  option = '-R' if options[:recursive]
  "chgrp #{option} #{group} #{escape(file)}".squeeze(' ')
end

.change_mode(file, mode, options = {}) ⇒ Object



148
149
150
151
# 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 = {}) ⇒ Object



153
154
155
156
157
# 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} #{owner} #{escape(file)}".squeeze(' ')
end

.check_contains(file, expected_pattern) ⇒ Object



31
32
33
# 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) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



83
84
85
# 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) ⇒ Object



79
80
81
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



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

def check_exists(file)
  "test -e #{escape(file)}"
end

.check_has_mode(file, mode) ⇒ Object



53
54
55
56
# 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) ⇒ Object



19
20
21
# 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) ⇒ Object



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

def check_is_character_device(file)
  "test -c #{escape(file)}"
end

.check_is_dereferenceable(link) ⇒ Object



136
137
138
# 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) ⇒ 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



35
36
37
38
39
40
41
42
# 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


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

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

.check_is_linked_to(link, target) ⇒ Object



120
121
122
# 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) ⇒ Object



103
104
105
106
# 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) ⇒ Object



44
45
46
47
48
49
50
51
# 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) ⇒ Object



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

def check_is_pipe(file)
  "test -p #{escape(file)}"
end

.check_is_socket(file) ⇒ Object



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

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


27
28
29
# File 'lib/specinfra/command/base/file.rb', line 27

def check_is_symlink(file)
  "test -L #{escape(file)}"
end

.copy(src, dest, options = {}) ⇒ Object



168
169
170
171
172
# 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) ⇒ Object



164
165
166
# File 'lib/specinfra/command/base/file.rb', line 164

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

.download(src, dest) ⇒ Object



189
190
191
# File 'lib/specinfra/command/base/file.rb', line 189

def download(src, dest)
  "curl -sSL #{escape(src)} -o #{escape(dest)}"
end

.get_content(file) ⇒ Object



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

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


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

def get_link_realpath(link)
  "readlink -e #{escape(link)}"
end


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

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

.get_md5sum(file) ⇒ Object



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

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

.get_mode(file) ⇒ Object



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

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

.get_mtime(file) ⇒ Object



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

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

.get_owner_group(file) ⇒ Object



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

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

.get_owner_user(file) ⇒ Object



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

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

.get_sha256sum(file) ⇒ Object



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

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

.get_size(file) ⇒ Object



144
145
146
# File 'lib/specinfra/command/base/file.rb', line 144

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


178
179
180
181
182
183
# File 'lib/specinfra/command/base/file.rb', line 178

def link_to(link, target, options = {})
  option = '-s'
  option << 'f' if options[:force]
  option << 'n' if options[:no_dereference]
  "ln #{option} #{escape(target)} #{escape(link)}"
end

.move(src, dest) ⇒ Object



174
175
176
# File 'lib/specinfra/command/base/file.rb', line 174

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

.remove(file) ⇒ Object



185
186
187
# File 'lib/specinfra/command/base/file.rb', line 185

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