2015-09-06 15:15:09 +00:00
|
|
|
# encoding: utf-8
|
2015-09-14 08:08:32 +00:00
|
|
|
require 'etc'
|
2015-09-06 15:15:09 +00:00
|
|
|
|
|
|
|
if IO.respond_to?(:popen4)
|
|
|
|
def open4(*args)
|
|
|
|
IO.popen4(*args)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
require 'open4'
|
|
|
|
end
|
|
|
|
|
|
|
|
module Vulcano::Backends
|
|
|
|
class Local < Vulcano.backend(1)
|
|
|
|
name 'local'
|
|
|
|
|
|
|
|
def initialize(conf)
|
|
|
|
@conf = conf
|
|
|
|
@files = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def file(path)
|
2015-09-14 08:08:32 +00:00
|
|
|
@files[path] ||= File.new(self, path)
|
2015-09-06 15:15:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_command(cmd)
|
2015-09-06 15:22:31 +00:00
|
|
|
Command.new(cmd)
|
2015-09-06 15:15:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
'Local Command Runner'
|
|
|
|
end
|
|
|
|
|
|
|
|
class Command
|
|
|
|
attr_reader :stdout, :stderr, :exit_status
|
2015-09-06 15:22:31 +00:00
|
|
|
def initialize(cmd)
|
2015-09-06 15:15:09 +00:00
|
|
|
@cmd = cmd
|
|
|
|
pid, stdin, stdout, stderr = open4(cmd)
|
|
|
|
stdin.close
|
|
|
|
|
2015-09-06 15:28:34 +00:00
|
|
|
_, status = Process.waitpid2 pid
|
2015-09-06 15:15:09 +00:00
|
|
|
|
|
|
|
@stdout = stdout.read
|
|
|
|
@stderr = stderr.read
|
|
|
|
@exit_status = status.exitstatus
|
|
|
|
end
|
|
|
|
end
|
2015-09-14 08:08:32 +00:00
|
|
|
|
|
|
|
class File < LinuxFile
|
|
|
|
def content
|
|
|
|
@content ||= ::File.read(@path)
|
|
|
|
end
|
|
|
|
|
|
|
|
%w{
|
|
|
|
exists? file? socket? directory? symlink? pipe?
|
|
|
|
}.each do |m|
|
|
|
|
define_method m.to_sym do
|
|
|
|
::File.method(m.to_sym).call(@path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def block_device?
|
|
|
|
::File.blockdev?(@path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def character_device?
|
|
|
|
::File.chardev?(@path)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def stat
|
|
|
|
return @stat unless @stat.nil?
|
|
|
|
|
|
|
|
begin
|
|
|
|
file_stat = ::File.stat(@path)
|
|
|
|
rescue RuntimeError => _
|
2015-09-14 15:20:27 +00:00
|
|
|
return @stat = {}
|
2015-09-14 08:08:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
tmask = file_stat.mode
|
|
|
|
type = TYPES.find { |_, mask| mask & tmask == mask }
|
|
|
|
type ||= [:unknown]
|
|
|
|
|
|
|
|
@stat = {
|
|
|
|
type: type[0],
|
|
|
|
mode: tmask & 00777,
|
|
|
|
mtime: file_stat.mtime.to_i,
|
|
|
|
size: file_stat.size,
|
|
|
|
}
|
|
|
|
|
|
|
|
begin
|
|
|
|
u = Etc.getpwuid(file_stat.uid)
|
|
|
|
@stat[:owner] = u.name
|
|
|
|
rescue ArgumentError => _
|
|
|
|
@stat[:owner] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
g = Etc.getgrgid(file_stat.gid)
|
|
|
|
@stat[:group] = g.name
|
|
|
|
rescue ArgumentError => _
|
|
|
|
@stat[:group] = nil
|
|
|
|
end
|
2015-09-14 15:20:27 +00:00
|
|
|
|
|
|
|
@stat
|
2015-09-14 08:08:32 +00:00
|
|
|
end
|
|
|
|
end
|
2015-09-06 15:15:09 +00:00
|
|
|
end
|
|
|
|
end
|