Merge pull request #19 from chef/improvements

Improvements
This commit is contained in:
Christoph Hartmann 2015-09-17 21:24:53 +02:00 committed by Dominik Richter
commit e06eed2178
16 changed files with 186 additions and 152 deletions

View file

@ -14,12 +14,8 @@ require 'utils/simpleconfig'
class AuditDaemonConf < Vulcano.resource(1)
name 'audit_daemon_conf'
def initialize
@conf_path = '/etc/audit/auditd.conf'
@files_contents = {}
@content = nil
@params = nil
read_content
def initialize(path = nil)
@conf_path = path || '/etc/audit/auditd.conf'
end
def to_s
@ -27,25 +23,32 @@ class AuditDaemonConf < Vulcano.resource(1)
end
def method_missing(name)
@params || read_content
@params[name.to_s]
read_params[name.to_s]
end
def read_content
private
def read_params
return @params unless @params.nil?
# read the file
file = vulcano.file(@conf_path)
if !file.file?
return skip_resource "Can't find file \"#{@conf_path}\""
skip_resource "Can't find file '#{@conf_path}'"
return @params = {}
end
@content = file.content
if @content.empty? && file.size > 0
return skip_resource "Can't read file \"#{@conf_path}\""
content = file.content
if content.empty? && file.size > 0
skip_resource "Can't read file '#{@conf_path}'"
return @params = {}
end
# parse the file
@params = SimpleConfig.new(
@content,
conf = SimpleConfig.new(
content,
multiple_values: false,
).params
)
@params = conf.params
end
end

View file

@ -12,26 +12,29 @@ class GemPackage < Vulcano.resource(1)
end
def info
return @info unless @info.nil?
cmd = vulcano.run_command("gem list --local -a -q \^#{@package_name}\$")
return nil if cmd.exit_status != 0
@info = {
installed: cmd.exit_status == 0,
type: 'gem',
}
return @info unless @info[:installed]
# extract package name and version
# parses data like winrm (1.3.4, 1.3.3)
params = /^\s*([^\(]*?)\s*\((.*?)\)\s*$/.match(cmd.stdout.chomp)
versions = params[2].split(',')
@cache = {
name: params[1],
version: versions[0],
type: 'gem',
}
@info[:name] = params[1]
@info[:version] = versions[0]
@info
end
def installed?
!info.nil?
info[:installed] == true
end
def version
return nil if info.nil?
info[:version]
end

View file

@ -17,10 +17,6 @@ class InetdConf < Vulcano.resource(1)
def initialize(path = nil)
@conf_path = path || '/etc/inetd.conf'
@files_contents = {}
@content = nil
@params = nil
read_content
end
def to_s
@ -28,27 +24,31 @@ class InetdConf < Vulcano.resource(1)
end
def method_missing(name)
@params || read_content
@params[name.to_s]
read_params[name.to_s]
end
def read_content
def read_params
return @params unless @params.nil?
# read the file
file = vulcano.file(@conf_path)
if !file.file?
return skip_resource "Can't find file \"#{@conf_path}\""
skip_resource "Can't find file \"#{@conf_path}\""
return @params = {}
end
@content = file.content
if @content.empty? && file.size > 0
return skip_resource "Can't read file \"#{@conf_path}\""
content = file.content
if content.empty? && file.size > 0
skip_resource "Can't read file \"#{@conf_path}\""
return @params = {}
end
# parse the file
@params = SimpleConfig.new(
@content,
conf = SimpleConfig.new(
content,
assignment_re: /^\s*(\S+?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$/,
key_vals: 6,
multiple_values: false,
).params
@content
)
@params = conf.params
end
end

View file

@ -15,10 +15,6 @@ class LimitsConf < Vulcano.resource(1)
def initialize(path = nil)
@conf_path = path || '/etc/security/limits.conf'
@files_contents = {}
@content = nil
@params = nil
read_content
end
def to_s
@ -26,27 +22,32 @@ class LimitsConf < Vulcano.resource(1)
end
def method_missing(name)
@params || read_content
@params[name.to_s]
read_params[name.to_s]
end
def read_content
def read_params
return @params unless @params.nil?
# read the file
file = vulcano.file(@conf_path)
if !file.file?
return skip_resource "Can't find file \"#{@conf_path}\""
skip_resource "Can't find file \"#{@conf_path}\""
return @params = {}
end
@content = file.content
if @content.empty? && file.size > 0
return skip_resource "Can't read file \"#{@conf_path}\""
content = file.content
if content.empty? && file.size > 0
skip_resource "Can't read file \"#{@conf_path}\""
return @params = {}
end
# parse the file
@params = SimpleConfig.new(
@content,
conf = SimpleConfig.new(
content,
assignment_re: /^\s*(\S+?)\s+(.*?)\s+(.*?)\s+(.*?)\s*$/,
key_vals: 3,
multiple_values: true,
).params
@content
)
@params = conf.params
end
end

View file

@ -21,10 +21,6 @@ class LoginDef < Vulcano.resource(1)
def initialize(path = nil)
@conf_path = path || '/etc/login.defs'
@files_contents = {}
@content = nil
@params = nil
read_content
end
def to_s
@ -32,26 +28,31 @@ class LoginDef < Vulcano.resource(1)
end
def method_missing(name)
@params || read_content
@params[name.to_s]
read_params[name.to_s]
end
def read_content
def read_params
return @params unless @params.nil?
# read the file
file = vulcano.file(@conf_path)
if !file.file?
return skip_resource "Can't find file \"#{@conf_path}\""
skip_resource "Can't find file \"#{@conf_path}\""
return @params = {}
end
@content = file.content
if @content.empty? && file.size > 0
return skip_resource "Can't read file \"#{@conf_path}\""
content = file.content
if content.empty? && file.size > 0
skip_resource "Can't read file \"#{@conf_path}\""
return @params = {}
end
# parse the file
@params = SimpleConfig.new(
@content,
conf = SimpleConfig.new(
content,
assignment_re: /^\s*(\S+)\s+(\S*)\s*$/,
multiple_values: false,
).params
@content
)
@params = conf.params
end
end

View file

@ -13,23 +13,26 @@ class NpmPackage < Vulcano.resource(1)
end
def info
return @cache if !@cache.nil?
return @info unless @info.nil?
cmd = vulcano.run_command("npm ls -g --json #{@package_name}")
return nil if cmd.exit_status != 0
pkgs = JSON.parse(cmd.stdout)
@cache = {
@info = {
name: @package_name,
version: pkgs['dependencies'][@package_name]['version'],
type: 'npm',
installed: cmd.exit_status == 0,
}
return @info unless @info[:installed]
pkgs = JSON.parse(cmd.stdout)
@info[:version] = pkgs['dependencies'][@package_name]['version']
@info
end
def installed?
!info.nil?
info[:installed] == true
end
def version
return nil if info.nil?
info[:version]
end

View file

@ -16,10 +16,6 @@ class NtpConf < Vulcano.resource(1)
def initialize(path = nil)
@conf_path = path || '/etc/ntp.conf'
@files_contents = {}
@content = nil
@params = nil
read_content
end
def to_s
@ -27,28 +23,34 @@ class NtpConf < Vulcano.resource(1)
end
def method_missing(name)
@params || read_content
param = @params[name.to_s]
param = read_params[name.to_s]
# extract first value if we have only one value in array
param = param[0] if !param.nil? && param.length == 1
return param[0] if param.is_a?(Array) and param.length == 1
param
end
def read_content
# read the file
private
def read_params
return @params unless @params.nil?
if !vulcano.file(@conf_path).file?
return skip_resource "Can't find file \"#{@conf_path}\""
skip_resource "Can't find file \"#{@conf_path}\""
return @params = {}
end
@content = vulcano.file(@conf_path).content
if @content.empty? && vulcano.file(@conf_path).size > 0
return skip_resource "Can't read file \"#{@conf_path}\""
content = vulcano.file(@conf_path).content
if content.empty? && vulcano.file(@conf_path).size > 0
skip_resource "Can't read file \"#{@conf_path}\""
return @params = {}
end
# parse the file
@params = SimpleConfig.new(
@content,
conf = SimpleConfig.new(
content,
assignment_re: /^\s*(\S+)\s+(.*)\s*$/,
multiple_values: true,
).params
@content
)
@params = conf.params
end
end

View file

@ -12,33 +12,36 @@ class OneGetPackage < Vulcano.resource(1)
def initialize(package_name)
@package_name = package_name
@cache = nil
end
def info
return @cache if !@cache.nil?
return @info unless @info.nil?
@info = {}
@info[:type] = 'oneget'
cmd = vulcano.run_command("Get-Package -Name '#{@package_name}' | ConvertTo-Json")
# cannot rely on exit code for now, successful command returns exit code 1
# return nil if cmd.exit_status != 0
# try to parse json
begin
pkgs = JSON.parse(cmd.stdout)
rescue JSON::ParserError => _e
return nil
return @info
end
@cache = {
name: pkgs['Name'],
version: pkgs['Version'],
type: 'oneget',
}
@info[:name] = pkgs['Name']
@info[:version] = pkgs['Version']
@info[:installed] = true
@info
end
def installed?
!info.nil?
info[:installed] == true
end
def version
return nil if info.nil?
info[:version]
end

View file

@ -26,7 +26,7 @@ class Package < Vulcano.resource(1)
when 'darwin'
@pkgman = Brew.new(vulcano)
else
fail 'The `package` resource is not supported on your OS yet. Please open an issue on Github.'
return skip_resource 'The `package` resource is not supported on your OS yet.'
end
end

View file

@ -12,28 +12,30 @@ class PipPackage < Vulcano.resource(1)
end
def info
return @info unless @info.nil?
@info = {}
@info[:type] = 'pip'
cmd = vulcano.run_command("pip show #{@package_name}")
return nil if cmd.exit_status != 0
return @info if cmd.exit_status != 0
params = SimpleConfig.new(
cmd.stdout,
assignment_re: /^\s*([^:]*?)\s*:\s*(.*?)\s*$/,
multiple_values: false,
).params
@cache = {
name: params['Name'],
installed: true,
version: params['Version'],
type: 'pip',
}
@info[:name] = params['Name']
@info[:version] = params['Version']
@info[:installed] = true
@info
end
def installed?
!info.nil?
info[:installed] == true
end
def version
return nil if info.nil?
info[:version]
end

View file

@ -11,7 +11,6 @@ class SshConf < Vulcano.resource(1)
@conf_path = conf_path || '/etc/ssh/ssh_config'
typename = (@conf_path.include?('sshd') ? 'Server' : 'Client')
@type = type || "SSH #{typename} configuration #{conf_path}"
read_content
end
def to_s
@ -19,43 +18,50 @@ class SshConf < Vulcano.resource(1)
end
def content
@conf.content
read_content
end
def params(*opts)
res = @params
opts.each do |opt|
res = res[opt] unless res.nil?
opts.inject(read_params) do |res, nxt|
res.respond_to?(:key) ? res[nxt] : nil
end
res
end
def method_missing(name)
param = @params[name.to_s]
param = read_params[name.to_s]
return nil if param.nil?
# extract first value if we have only one value in array
param = param[0] if !param.nil? && param.length == 1
return param[0] if param.length == 1
param
end
private
def read_content
@conf = vulcano.file(@conf_path)
# read the file
if !@conf.file?
return @content if @content_read
file = vulcano.file(@conf_path)
if !file.file?
return skip_resource "Can't find file \"#{@conf_path}\""
end
if @conf.content.empty? && @conf.size > 0
@content = file.content
if @content.empty? && file.size > 0
return skip_resource "Can't read file \"#{@conf_path}\""
end
# parse the file
@params = SimpleConfig.new(
@conf.content,
@content_read = true
@content
end
def read_params
return @params unless @params.nil?
return @params = {} if read_content.nil?
conf = SimpleConfig.new(
read_content,
assignment_re: /^\s*(\S+?)\s+(.*?)\s*$/,
multiple_values: true,
).params
)
@params = conf.params
end
end

View file

@ -101,14 +101,13 @@ class YumRepo
def initialize(yum, reponame)
@yum = yum
@reponame = reponame
@cache = nil
end
# extracts the shortname from a repo id
# e.g. extras/7/x86_64 -> extras
def shortname(id)
val = %r{^\s*([^/]*?)/(.*?)\s*$}.match(id)
val[1]
val.nil? ? nil : val[1]
end
def info
@ -150,10 +149,6 @@ module Vulcano::Resources
@repository.enabled?
end
def method_missing
fail 'Not supported'
end
def deprecated
warn '[DEPRECATION] `yumrepo(reponame)` is deprecated. Please use `yum.repo(reponame)` instead.'
end

View file

@ -75,6 +75,18 @@ module Vulcano::Backends
private
def pw_username(uid)
Etc.getpwuid(uid).name
rescue ArgumentError => _
nil
end
def pw_groupname(gid)
Etc.getgrgid(gid).name
rescue ArgumentError => _
nil
end
def stat
return @stat unless @stat.nil?
@ -93,22 +105,10 @@ module Vulcano::Backends
mode: tmask & 00777,
mtime: file_stat.mtime.to_i,
size: file_stat.size,
user: pw_username(file_stat.uid),
group: pw_groupname(file_stat.gid),
}
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
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

View file

@ -8,7 +8,12 @@ describe 'Vulcano::Resources::Passwd' do
let(:resource) { loadResource('gem', 'rubocop') }
it 'verify gem package detail parsing' do
pkg = { name: 'rubocop', version: '0.33.0', type: 'gem' }
pkg = {
name: 'rubocop',
version: '0.33.0',
type: 'gem',
installed: true,
}
_(resource.installed?).must_equal true
_(resource.info).must_equal pkg
end

View file

@ -8,7 +8,12 @@ describe 'Vulcano::Resources::Passwd' do
let(:resource) { loadResource('npm', 'bower') }
it 'verify npm package detail parsing' do
pkg = { name: 'bower', version: '1.4.1', type: 'npm'}
pkg = {
name: 'bower',
version: '1.4.1',
type: 'npm',
installed: true,
}
_(resource.installed?).must_equal true
_(resource.info).must_equal pkg
end

View file

@ -8,7 +8,12 @@ describe 'Vulcano::Resources::Passwd' do
let(:resource) { loadResource('oneget', 'Mozilla Firefox') }
it 'verify oneget package detail parsing' do
pkg = { name: 'Mozilla Firefox 40.0.3 (x86 en-US)', version: '40.0.3', type: 'oneget'}
pkg = {
name: 'Mozilla Firefox 40.0.3 (x86 en-US)',
version: '40.0.3',
type: 'oneget',
installed: true,
}
_(resource.installed?).must_equal true
_(resource.info).must_equal pkg
end