inspec/lib/vulcano/backend.rb

81 lines
2.1 KiB
Ruby
Raw Normal View History

# encoding: utf-8
2015-10-06 16:55:44 +00:00
# author: Dominik Richter
# author: Christoph Hartmann
require 'uri'
require 'vulcano/plugins'
require 'vulcano/resource'
module Vulcano
class Backend
# Expose all registered backends
def self.registry
@registry ||= {}
end
# Resolve target configuration in URI-scheme into
# all respective fields and merge with existing configuration.
# e.g. ssh://bob@remote => backend: ssh, user: bob, host: remote
2015-10-02 20:29:40 +00:00
def self.target_config(config = nil) # rubocop:disable Metrics/AbcSize
conf = config.nil? ? {} : config.dup
2015-10-02 20:29:40 +00:00
# in case the user specified a key-file, register it that way
key = conf['key']
2015-10-02 20:29:40 +00:00
if !key.nil? and File.file?(key)
conf['key_file'] = key
end
return conf if conf['target'].to_s.empty?
2015-09-05 14:07:54 +00:00
uri = URI.parse(conf['target'].to_s)
unless uri.host.nil? and uri.scheme.nil?
conf['backend'] ||= uri.scheme
conf['host'] ||= uri.host
conf['port'] ||= uri.port
conf['user'] ||= uri.user
conf['password'] ||= uri.password
conf['path'] ||= uri.path
end
# ensure path is nil, if its empty; e.g. required to reset defaults for winrm
conf['path'] = nil if !conf['path'].nil? && conf['path'].to_s.empty?
# return the updated config
conf
end
def self.create(name, conf)
backend_class = @registry[name]
return nil if backend_class.nil?
backend_instance = backend_class.new(conf)
# Create wrapper class with all resources
cls = Class.new do
define_method :backend do
backend_instance
end
Vulcano::Resource.registry.each do |id, r|
define_method id.to_sym do |*args|
r.new(self, id.to_s, *args)
end
end
end
cls.new
end
end
def self.backend(version = 1)
if version != 1
2015-09-03 21:18:28 +00:00
fail 'Only backend version 1 is supported!'
end
Vulcano::Plugins::Backend
end
end
require 'vulcano/backend/docker'
require 'vulcano/backend/local'
require 'vulcano/backend/mock'
require 'vulcano/backend/specinfra'
require 'vulcano/backend/ssh'