2015-09-06 15:15:09 +00:00
|
|
|
# encoding: utf-8
|
2015-09-14 08:08:32 +00:00
|
|
|
require 'etc'
|
2015-09-20 11:34:06 +00:00
|
|
|
require 'rbconfig'
|
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'
|
|
|
|
|
2015-09-20 11:34:06 +00:00
|
|
|
attr_reader :os
|
2015-09-06 15:15:09 +00:00
|
|
|
def initialize(conf)
|
|
|
|
@conf = conf
|
|
|
|
@files = {}
|
2015-09-20 11:34:06 +00:00
|
|
|
@os = OS.new(self)
|
2015-09-06 15:15:09 +00:00
|
|
|
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
|
|
|
|
|
2015-09-20 11:34:06 +00:00
|
|
|
class OS < OSCommon
|
|
|
|
def initialize(backend)
|
|
|
|
super(backend, { family: detect_local_os })
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def detect_local_os
|
|
|
|
case ::RbConfig::CONFIG['host_os']
|
|
|
|
when /aix(.+)$/
|
|
|
|
return 'aix'
|
|
|
|
when /darwin(.+)$/
|
|
|
|
return 'darwin'
|
|
|
|
when /hpux(.+)$/
|
|
|
|
return 'hpux'
|
|
|
|
when /linux/
|
|
|
|
return 'linux'
|
|
|
|
when /freebsd(.+)$/
|
|
|
|
return 'freebsd'
|
|
|
|
when /openbsd(.+)$/
|
|
|
|
return 'openbsd'
|
|
|
|
when /netbsd(.*)$/
|
|
|
|
return 'netbsd'
|
|
|
|
when /solaris2/
|
|
|
|
return 'solaris2'
|
|
|
|
when /mswin|mingw32|windows/
|
|
|
|
# After long discussion in IRC the "powers that be" have come to a consensus
|
|
|
|
# that no Windows platform exists that was not based on the
|
|
|
|
# Windows_NT kernel, so we herby decree that "windows" will refer to all
|
|
|
|
# platforms built upon the Windows_NT kernel and have access to win32 or win64
|
|
|
|
# subsystems.
|
|
|
|
return 'windows'
|
|
|
|
else
|
|
|
|
return ::RbConfig::CONFIG['host_os']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-06 15:15:09 +00:00
|
|
|
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
|
2015-09-20 11:34:06 +00:00
|
|
|
|
2015-09-06 15:15:09 +00:00
|
|
|
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
|
2015-09-20 11:34:06 +00:00
|
|
|
rescue Errno::ENOENT => _
|
|
|
|
@exit_status ||= 1
|
2015-09-06 15:15:09 +00:00
|
|
|
end
|
|
|
|
end
|
2015-09-14 08:08:32 +00:00
|
|
|
|
|
|
|
class File < LinuxFile
|
|
|
|
def content
|
2015-09-16 22:50:28 +00:00
|
|
|
@content ||= ::File.read(@path, encoding: 'UTF-8')
|
2015-09-15 11:21:56 +00:00
|
|
|
rescue StandardError => _
|
|
|
|
nil
|
2015-09-14 08:08:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
%w{
|
2015-09-18 10:35:32 +00:00
|
|
|
exist? file? socket? directory? symlink? pipe?
|
2015-09-14 08:08:32 +00:00
|
|
|
}.each do |m|
|
|
|
|
define_method m.to_sym do
|
|
|
|
::File.method(m.to_sym).call(@path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-15 10:18:07 +00:00
|
|
|
def link_path
|
|
|
|
return nil unless symlink?
|
|
|
|
@link_path ||= ::File.readlink(@path)
|
|
|
|
end
|
|
|
|
|
2015-09-14 08:08:32 +00:00
|
|
|
def block_device?
|
|
|
|
::File.blockdev?(@path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def character_device?
|
|
|
|
::File.chardev?(@path)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-09-17 16:04:55 +00:00
|
|
|
def pw_username(uid)
|
|
|
|
Etc.getpwuid(uid).name
|
|
|
|
rescue ArgumentError => _
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def pw_groupname(gid)
|
|
|
|
Etc.getgrgid(gid).name
|
|
|
|
rescue ArgumentError => _
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2015-09-14 08:08:32 +00:00
|
|
|
def stat
|
|
|
|
return @stat unless @stat.nil?
|
|
|
|
|
|
|
|
begin
|
2015-09-15 10:19:36 +00:00
|
|
|
file_stat = ::File.lstat(@path)
|
2015-09-16 00:10:06 +00:00
|
|
|
rescue StandardError => _err
|
2015-09-14 15:20:27 +00:00
|
|
|
return @stat = {}
|
2015-09-14 08:08:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@stat = {
|
2015-09-29 23:42:04 +00:00
|
|
|
type: Stat.find_type(file_stat.mode),
|
|
|
|
mode: file_stat.mode & 00777,
|
2015-09-14 08:08:32 +00:00
|
|
|
mtime: file_stat.mtime.to_i,
|
|
|
|
size: file_stat.size,
|
2015-09-22 12:24:22 +00:00
|
|
|
owner: pw_username(file_stat.uid),
|
2015-09-17 16:04:55 +00:00
|
|
|
group: pw_groupname(file_stat.gid),
|
2015-09-14 08:08:32 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 00:10:06 +00:00
|
|
|
res = @backend.run_command("stat #{@spath} 2>/dev/null --printf '%C'")
|
|
|
|
if res.exit_status == 0 && !res.stdout.empty? && res.stdout != '?'
|
|
|
|
@stat[:selinux_label] = res.stdout.strip
|
|
|
|
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
|