Class: SpecInfra::Backend::Exec
- Defined in:
- lib/specinfra/backend/exec.rb
Instance Method Summary collapse
- #add_pre_command(cmd) ⇒ Object
- #build_command(cmd) ⇒ Object
- #check_executable(file, by_whom) ⇒ Object
- #check_monitored_by_monit(process) ⇒ Object
- #check_mounted(path, expected_attr, only_with) ⇒ Object
- #check_os ⇒ Object
- #check_readable(file, by_whom) ⇒ Object
- #check_routing_table(expected_attr) ⇒ Object
- #check_running(process) ⇒ Object
- #check_writable(file, by_whom) ⇒ Object
- #copy_file(from, to) ⇒ Object
- #run_command(cmd, opts = {}) ⇒ Object
- #run_with_no_ruby_environment ⇒ Object
Methods inherited from Base
#check_zero, #commands, #method_missing, #set_commands, #set_example
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class SpecInfra::Backend::Base
Instance Method Details
#add_pre_command(cmd) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/specinfra/backend/exec.rb', line 45 def add_pre_command(cmd) path = SpecInfra.configuration.path if SpecInfra.configuration.pre_command cmd = "#{SpecInfra.configuration.pre_command} && #{cmd}" cmd = "env PATH=#{path}:$PATH #{cmd}" if path end cmd end |
#build_command(cmd) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/specinfra/backend/exec.rb', line 35 def build_command(cmd) path = SpecInfra.configuration.path if path cmd = "env PATH=#{path}:$PATH #{cmd}" cmd.gsub!(/(\&\&\s*!?\(?\s*)/, "\\1env PATH=#{path}:$PATH ") cmd.gsub!(/(\|\|\s*!?\(?\s*)/, "\\1env PATH=#{path}:$PATH ") end cmd end |
#check_executable(file, by_whom) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/specinfra/backend/exec.rb', line 113 def check_executable(file, by_whom) mode = sprintf('%04s',run_command(commands.get_mode(file)).stdout.strip) mode = mode.split('') mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1 case by_whom when nil mode_octal & 0111 != 0 when 'owner' mode_octal & 0100 != 0 when 'group' mode_octal & 0010 != 0 when 'others' mode_octal & 0001 != 0 end end |
#check_monitored_by_monit(process) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/specinfra/backend/exec.rb', line 70 def check_monitored_by_monit(process) ret = run_command(commands.check_monitored_by_monit(process)) return false unless ret.stdout != nil && ret.success? retlines = ret.stdout.split(/[\r\n]+/).map(&:strip) proc_index = retlines.index("Process '#{process}'") return false unless proc_index retlines[proc_index+2].match(/\Amonitoring status\s+monitored\Z/i) != nil end |
#check_mounted(path, expected_attr, only_with) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/specinfra/backend/exec.rb', line 129 def check_mounted(path, expected_attr, only_with) ret = run_command(commands.check_mounted(path)) if expected_attr.nil? || ret.failure? return ret.success? end mount = ret.stdout.scan(/\S+/) actual_attr = { :device => mount[0], :type => mount[4] } mount[5].gsub(/\(|\)/, '').split(',').each do |option| name, val = option.split('=') if val.nil? actual_attr[name.to_sym] = true else val = val.to_i if val.match(/^\d+$/) actual_attr[name.to_sym] = val end end if ! expected_attr[:options].nil? expected_attr.merge!(expected_attr[:options]) expected_attr.delete(:options) end if only_with actual_attr == expected_attr else expected_attr.each do |key, val| return false if actual_attr[key] != val end true end end |
#check_os ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/specinfra/backend/exec.rb', line 182 def check_os return SpecInfra.configuration.os if SpecInfra.configuration.os arch = run_command('uname -m').stdout.strip # Fedora also has an /etc/redhat-release so the Fedora check must # come before the RedHat check if run_command('ls /etc/fedora-release').success? line = run_command('cat /etc/redhat-release').stdout if line =~ /release (\d[\d]*)/ release = $1 end { :family => 'Fedora', :release => release } elsif run_command('ls /etc/redhat-release').success? line = run_command('cat /etc/redhat-release').stdout if line =~ /release (\d[\d.]*)/ release = $1 end if release =~ /7./ { :family => 'RedHat7', :release => release, :arch => arch } else { :family => 'RedHat', :release => release, :arch => arch } end elsif run_command('ls /etc/system-release').success? { :family => 'RedHat', :release => nil, :arch => arch } # Amazon Linux elsif run_command('ls /etc/SuSE-release').success? line = run_command('cat /etc/SuSE-release').stdout if line =~ /SUSE Linux Enterprise Server (\d+)/ release = $1 end { :family => 'SuSE', :release => release, :arch => arch } elsif run_command('ls /etc/debian_version').success? lsb_release = run_command("lsb_release -ir") if lsb_release.success? if lsb_release.stdout =~ /:/ distro = lsb_release.stdout.split("\n").first.split(':').last release = lsb_release.stdout.split("\n").last.split(':').last.strip end else lsb_release = run_command("cat /etc/lsb-release") if lsb_release.success? lsb_release.stdout.each_line do |line| distro = line.split('=').last if line =~ /^DISTRIB_ID=/ release = line.split('=').last.strip if line =~ /^DISTRIB_RELEASE=/ end end end distro ||= 'Debian' release ||= nil { :family => distro.strip, :release => release, :arch => arch } elsif run_command('ls /etc/gentoo-release').success? { :family => 'Gentoo', :release => nil, :arch => arch } elsif run_command('ls /usr/lib/setup/Plamo-*').success? { :family => 'Plamo', :release => nil, :arch => arch } elsif run_command('uname -s').stdout =~ /AIX/i { :family => 'AIX', :release => nil, :arch => arch } elsif ( uname = run_command('uname -sr').stdout) && uname =~ /SunOS/i if uname =~ /5.10/ { :family => 'Solaris10', :release => nil, :arch => arch } elsif run_command('grep -q "Oracle Solaris 11" /etc/release').success? { :family => 'Solaris11', :release => nil, :arch => arch } elsif run_command('grep -q SmartOS /etc/release').success? { :family => 'SmartOS', :release => nil, :arch => arch } else { :family => 'Solaris', :release => nil, :arch => arch } end elsif run_command('uname -s').stdout =~ /Darwin/i { :family => 'Darwin', :release => nil } elsif ( uname = run_command('uname -sr').stdout ) && uname =~ /FreeBSD/i if uname =~ /10./ { :family => 'FreeBSD10', :release => nil, :arch => arch } else { :family => 'FreeBSD', :release => nil, :arch => arch } end elsif run_command('uname -sr').stdout =~ /Arch/i { :family => 'Arch', :release => nil, :arch => arch } elsif run_command('uname -s').stdout =~ /OpenBSD/i { :family => 'OpenBSD', :release => nil, :arch => arch } else { :family => 'Base', :release => nil, :arch => arch } end end |
#check_readable(file, by_whom) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/specinfra/backend/exec.rb', line 81 def check_readable(file, by_whom) mode = sprintf('%04s',run_command(commands.get_mode(file)).stdout.strip) mode = mode.split('') mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1 case by_whom when nil mode_octal & 0444 != 0 when 'owner' mode_octal & 0400 != 0 when 'group' mode_octal & 0040 != 0 when 'others' mode_octal & 0004 != 0 end end |
#check_routing_table(expected_attr) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/specinfra/backend/exec.rb', line 162 def check_routing_table(expected_attr) return false if ! expected_attr[:destination] ret = run_command(commands.check_routing_table(expected_attr[:destination])) return false if ret.failure? ret.stdout.gsub!(/\r\n/, "\n") ret.stdout =~ /^(\S+)(?: via (\S+))? dev (\S+).+\n(?:default via (\S+))?/ actual_attr = { :destination => $1, :gateway => $2 ? $2 : $4, :interface => expected_attr[:interface] ? $3 : nil } expected_attr.each do |key, val| return false if actual_attr[key] != val end true end |
#check_running(process) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/specinfra/backend/exec.rb', line 54 def check_running(process) ret = run_command(commands.check_running(process)) # In Ubuntu, some services are under upstart and "service foo status" returns # exit status 0 even though they are stopped. # So return false if stdout contains "stopped/waiting". return false if ret.stdout =~ /stopped\/waiting/ # If the service is not registered, check by ps command if ret.exit_status == 1 ret = run_command(commands.check_process(process)) end ret.success? end |
#check_writable(file, by_whom) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/specinfra/backend/exec.rb', line 97 def check_writable(file, by_whom) mode = sprintf('%04s',run_command(commands.get_mode(file)).stdout.strip) mode = mode.split('') mode_octal = mode[0].to_i * 512 + mode[1].to_i * 64 + mode[2].to_i * 8 + mode[3].to_i * 1 case by_whom when nil mode_octal & 0222 != 0 when 'owner' mode_octal & 0200 != 0 when 'group' mode_octal & 0020 != 0 when 'others' mode_octal & 0002 != 0 end end |
#copy_file(from, to) ⇒ Object
264 265 266 267 268 269 270 271 |
# File 'lib/specinfra/backend/exec.rb', line 264 def copy_file(from, to) begin FileUtils.cp(from, to) rescue => e return false end true end |
#run_command(cmd, opts = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/specinfra/backend/exec.rb', line 8 def run_command(cmd, opts={}) cmd = build_command(cmd) cmd = add_pre_command(cmd) stdout = run_with_no_ruby_environment do `#{build_command(cmd)} 2>&1` end # In ruby 1.9, it is possible to use Open3.capture3, but not in 1.8 # stdout, stderr, status = Open3.capture3(cmd) if @example @example.[:command] = cmd @example.[:stdout] = stdout end CommandResult.new :stdout => stdout, :exit_status => $?.exitstatus end |
#run_with_no_ruby_environment ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/specinfra/backend/exec.rb', line 25 def run_with_no_ruby_environment keys = %w[BUNDLER_EDITOR BUNDLE_BIN_PATH BUNDLE_GEMFILE RUBYOPT GEM_HOME GEM_PATH GEM_CACHE] keys.each { |key| ENV["_SPECINFRA_#{key}"] = ENV[key] ; ENV.delete(key) } yield ensure keys.each { |key| ENV[key] = ENV.delete("_SPECINFRA_#{key}") } end |