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



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

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

.change_mode(file, mode) ⇒ Object



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

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

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



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

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

.check_contains(file, expected_pattern) ⇒ Object



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

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



60
61
62
63
64
65
66
67
68
69
# File 'lib/specinfra/command/base/file.rb', line 60

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



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

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



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

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



50
51
52
53
54
55
56
57
58
# File 'lib/specinfra/command/base/file.rb', line 50

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



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

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

.check_has_mode(file, mode) ⇒ Object



45
46
47
48
# File 'lib/specinfra/command/base/file.rb', line 45

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



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

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


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

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

.check_is_linked_to(link, target) ⇒ Object



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

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

.check_is_mounted(path) ⇒ Object



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

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

.check_is_owned_by(file, owner) ⇒ Object



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

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


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

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

.copy(src, dest) ⇒ Object



149
150
151
# File 'lib/specinfra/command/base/file.rb', line 149

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

.create_as_directory(file) ⇒ Object



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

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

.get_content(file) ⇒ Object



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

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


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

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

.get_md5sum(file) ⇒ Object



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

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

.get_mode(file) ⇒ Object



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

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

.get_mtime(file) ⇒ Object



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

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

.get_owner_group(file) ⇒ Object



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

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

.get_owner_user(file) ⇒ Object



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

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

.get_sha256sum(file) ⇒ Object



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

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

.get_size(file) ⇒ Object



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

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


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

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

.move(src, dest) ⇒ Object



153
154
155
# File 'lib/specinfra/command/base/file.rb', line 153

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

.remove(file) ⇒ Object



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

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