2015-08-29 23:11:23 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2015-09-01 22:50:52 +00:00
|
|
|
require 'digest'
|
2015-08-29 23:11:23 +00:00
|
|
|
|
2015-09-01 22:50:52 +00:00
|
|
|
module Vulcano::Plugins
|
|
|
|
|
|
|
|
class Backend
|
2015-09-05 14:07:54 +00:00
|
|
|
def self.name(name)
|
2015-09-01 22:50:52 +00:00
|
|
|
Vulcano::Plugins::Backend.__register(name, self)
|
|
|
|
end
|
|
|
|
|
2015-09-06 18:26:09 +00:00
|
|
|
# raise errors for all missing methods
|
|
|
|
%w{ file run_command os }.each do |m|
|
|
|
|
define_method(m.to_sym) do |*args|
|
|
|
|
fail NotImplementedError.new("Backend must implement the #{m}() method.")
|
2015-09-02 09:53:25 +00:00
|
|
|
end
|
2015-09-06 18:26:09 +00:00
|
|
|
end
|
2015-09-02 15:30:49 +00:00
|
|
|
|
2015-09-06 18:26:09 +00:00
|
|
|
def self.__register(id, obj)
|
2015-09-03 14:17:52 +00:00
|
|
|
Vulcano::Backend.registry[id] = obj
|
2015-09-01 22:50:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class FileCommon
|
|
|
|
# interface methods: these fields should be implemented by every
|
|
|
|
# backend File
|
|
|
|
%w{
|
2015-09-03 13:43:30 +00:00
|
|
|
exists? mode owner group link_target content mtime size
|
2015-09-01 22:50:52 +00:00
|
|
|
selinux_label product_version file_version
|
|
|
|
}
|
|
|
|
|
|
|
|
def type
|
2015-09-02 02:53:51 +00:00
|
|
|
:unknown
|
2015-08-29 23:11:23 +00:00
|
|
|
end
|
|
|
|
|
2015-09-01 22:50:52 +00:00
|
|
|
# The following methods can be overwritten by a derived class
|
|
|
|
# if desired, to e.g. achieve optimizations.
|
|
|
|
|
|
|
|
def md5sum
|
|
|
|
res = Digest::MD5.new
|
|
|
|
res.update(content)
|
|
|
|
res.hexdigest
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha256sum
|
|
|
|
res = Digest::SHA256.new
|
2015-09-02 02:53:51 +00:00
|
|
|
res.update(content)
|
2015-09-01 22:50:52 +00:00
|
|
|
res.hexdigest
|
|
|
|
end
|
|
|
|
|
|
|
|
# Additional methods for convenience
|
|
|
|
|
|
|
|
def file?
|
|
|
|
type == :file
|
|
|
|
end
|
|
|
|
|
|
|
|
def block_device?
|
2015-09-02 09:53:25 +00:00
|
|
|
type == :block_device
|
2015-09-01 22:50:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def character_device?
|
2015-09-02 09:53:25 +00:00
|
|
|
type == :character_device
|
2015-09-01 22:50:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def socket?
|
2015-09-02 09:53:25 +00:00
|
|
|
type == :socket
|
2015-09-01 22:50:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def directory?
|
2015-09-02 09:53:25 +00:00
|
|
|
type == :directory
|
2015-09-01 22:50:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def symlink?
|
2015-09-02 09:53:25 +00:00
|
|
|
type == :symlink
|
2015-09-01 22:50:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pipe?
|
2015-09-02 09:53:25 +00:00
|
|
|
type == :pipe?
|
2015-09-01 22:50:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def mode?(mode)
|
|
|
|
mode == mode
|
|
|
|
end
|
|
|
|
|
|
|
|
def owned_by?(owner)
|
|
|
|
owner == owner
|
|
|
|
end
|
|
|
|
|
|
|
|
def grouped_into?(group)
|
|
|
|
group == group
|
|
|
|
end
|
|
|
|
|
|
|
|
def linked_to?(dst)
|
|
|
|
link_target == dst
|
|
|
|
end
|
|
|
|
|
|
|
|
def version?(version)
|
|
|
|
product_version == version or
|
|
|
|
file_version == version
|
|
|
|
end
|
|
|
|
|
|
|
|
# helper methods provided to any implementing class
|
2015-09-05 14:07:54 +00:00
|
|
|
|
2015-09-01 22:50:52 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
UNIX_MODE_OWNERS = {
|
|
|
|
owner: 00700,
|
|
|
|
group: 00070,
|
2015-09-05 14:07:54 +00:00
|
|
|
other: 00007
|
2015-09-01 22:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNIX_MODE_TYPES = {
|
|
|
|
r: 00444,
|
|
|
|
w: 00222,
|
2015-09-05 14:07:54 +00:00
|
|
|
x: 00111
|
2015-09-01 22:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def unix_mode_mask(owner, type)
|
|
|
|
o = UNIX_MODE_OWNERS[owner.to_sym]
|
|
|
|
return nil if o.nil?
|
|
|
|
|
|
|
|
t = UNIX_MODE_TYPES[type.to_sym]
|
|
|
|
return nil if t.nil?
|
|
|
|
|
|
|
|
t & o
|
2015-08-29 23:11:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|