inspec/lib/vulcano/backend/specinfra.rb

116 lines
2.7 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2015-09-04 07:59:30 +00:00
require 'shellwords'
2015-09-14 12:50:08 +00:00
require 'specinfra'
require 'specinfra/helper'
require 'specinfra/helper/set'
2015-09-14 14:36:54 +00:00
module Specinfra
module Helper
module Os
def os
2015-09-25 17:46:46 +00:00
property[:os] = {} if !property[:os]
if !property[:os].include?(:family)
2015-09-14 14:36:54 +00:00
property[:os] = detect_os
end
property[:os]
end
private
2015-09-25 17:46:46 +00:00
2015-09-14 14:36:54 +00:00
def detect_os
backend = Specinfra.configuration.backend
if backend == :cmd || backend == :winrm
2015-09-25 17:46:46 +00:00
return { family: 'windows', release: nil, arch: nil }
2015-09-14 14:36:54 +00:00
end
Specinfra::Helper::DetectOs.subclasses.each do |c|
res = c.detect
if res
res[:arch] ||= Specinfra.backend.run_command('uname -m').stdout.strip
return res
end
end
2015-09-25 17:46:46 +00:00
fail NotImplementedError, 'Specinfra failed os detection.'
2015-09-14 14:36:54 +00:00
end
end
end
end
module Vulcano::Backends
class SpecinfraHelper < Vulcano.backend(1)
name 'specinfra'
autoload :Docker, 'vulcano/backend/specinfra_docker'
autoload :Exec, 'vulcano/backend/specinfra_exec'
autoload :Ssh, 'vulcano/backend/specinfra_ssh'
autoload :Winrm, 'vulcano/backend/specinfra_winrm'
autoload :File, 'vulcano/backend/specinfra_file'
def initialize(conf)
@conf = conf
@files = {}
configure_shared_options
type = @conf['backend'].to_s
case type
when 'docker'
spec_backend = Specinfra::Backend::Docker
backend_helper = Docker
when 'exec'
spec_backend = Specinfra::Backend::Exec
backend_helper = Exec
when 'ssh'
spec_backend = Specinfra::Backend::Ssh
backend_helper = Ssh
when 'winrm'
spec_backend = Specinfra::Backend::Winrm
backend_helper = Winrm
else
2015-09-03 21:24:42 +00:00
fail "Cannot configure Specinfra backend #{type}: it isn't supported yet."
end
reset_backend(spec_backend)
backend_helper.configure(@conf)
end
2015-09-14 14:36:54 +00:00
def os
Specinfra::Helper::Os.os
end
def file(path)
@files[path] ||= File.new(self, path)
end
def run_command(cmd)
Specinfra::Runner.run_command(cmd)
end
def to_s
'SpecInfra Backend Runner'
end
def configure_shared_options
Specinfra::Backend::Cmd.send(:include, Specinfra::Helper::Set)
# Force specinfra to disregard any locally detected backend and instead
# retry backend detection.
Specinfra::Properties.instance.properties({})
si = Specinfra.configuration
if @conf['disable_sudo']
si.disable_sudo = true
else
si.sudo_password = @conf['sudo_password']
si.sudo_options = @conf['sudo_options']
end
end
def reset_backend(x)
x.instance_variable_set(:@instance, nil)
end
end
end