turn backend into a separate object

Signed-off-by: Dominik Richter <dominik.richter@gmail.com>
This commit is contained in:
Dominik Richter 2015-08-12 19:07:01 -07:00
parent 9ba4fb1d00
commit 6e4381f2d4
8 changed files with 137 additions and 130 deletions

11
lib/utils/modulator.rb Normal file
View file

@ -0,0 +1,11 @@
# encoding: utf-8
module Modulator
def modules
@modules ||= {}
end
def add_module(name, handler)
modules[name] = handler
end
end

View file

@ -1,44 +1,42 @@
# encoding: utf-8
require 'utils/modulator'
module Vulcano
module Backend
class Backend
extend Modulator
def resolve_target_options conf
return if conf[:target].to_s.empty?
uri = URI::parse(conf[:target].to_s)
conf[:backend] = conf[:backend] || uri.scheme
conf[:host] = conf[:host] || uri.host
conf[:port] = conf[:port] || uri.port
conf[:user] = conf[:user] || uri.user
conf[:password] = conf[:password] || uri.password
def initialize(conf)
@conf = conf
end
def configure_shared_options(conf)
def resolve_target_options
return if @conf[:target].to_s.empty?
uri = URI::parse(@conf[:target].to_s)
@conf[:backend] = @conf[:backend] || uri.scheme
@conf[:host] = @conf[:host] || uri.host
@conf[:port] = @conf[:port] || uri.port
@conf[:user] = @conf[:user] || uri.user
@conf[:password] = @conf[:password] || uri.password
end
def configure_shared_options
Specinfra::Backend::Cmd.send(:include, Specinfra::Helper::Set)
si = Specinfra.configuration
si.os = nil
if conf['disable_sudo']
if @conf['disable_sudo']
si.disable_sudo = true
else
si.sudo_password = conf['sudo_password']
si.sudo_options = conf['sudo_options']
si.sudo_password = @conf['sudo_password']
si.sudo_options = @conf['sudo_options']
end
end
def configure_target(conf)
t = conf[:backend] || 'exec'
m = BACKEND_CONFIGS[t]
def configure_target
t = @conf[:backend] || 'exec'
m = Vulcano::Backend.modules[t]
raise "Don't understand backend '#{t}'" if m.nil?
f = method(m)
raise "Couldn't find internal backend method '#{m}'" if f.nil?
f.call(conf)
m.configure(@conf)
end
BACKEND_CONFIGS = {
'exec' => :configure_localhost,
'ssh' => :configure_ssh,
'winrm' => :configure_winrm,
}
end
end

View file

@ -1,11 +1,11 @@
# encoding: utf-8
module Vulcano
module Backend
def configure_localhost(conf)
Specinfra.configuration.backend = :exec
end
module Vulcano::Backend::Exec
def self.configure(conf)
Specinfra.configuration.backend = :exec
end
end
Vulcano::Backend.add_module('exec', Vulcano::Backend::Exec)

View file

@ -1,46 +1,51 @@
# encoding: utf-8
module Vulcano
module Backend
module Vulcano::Backend::SSH
def configure_ssh(conf)
si = Specinfra.configuration
si.backend = :ssh
si.request_pty = true
host = conf['host'].to_s
RSpec.configuration.host = host
ssh_opts = {
port: conf['port'] || 22,
auth_methods: ['none'],
user_known_hosts_file: "/dev/null",
global_known_hosts_file: "/dev/null",
number_of_password_prompts: 0,
user: conf['user'],
password: conf['password'],
keys: [conf['key_file']].compact,
}
if host.empty?
raise "You must configure a target host."
end
unless ssh_opts[:port] > 0
raise "Port must be > 0 (not #{ssh_opts[:port]})"
end
if ssh_opts[:user].to_s.empty?
raise "User must not be empty."
end
unless ssh_opts[:keys].empty?
ssh_opts[:auth_methods].push('publickey')
ssh_opts[:keys_only] = true if ssh_opts[:password].nil?
end
unless ssh_opts[:password].nil?
ssh_opts[:auth_methods].push('password')
end
if ssh_opts[:keys].empty? and ssh_opts[:password].nil?
raise "You must configure at least one authentication method" +
": Password or key."
end
si.ssh_options = ssh_opts
def self.configure(conf)
si = Specinfra.configuration
si.backend = :ssh
si.request_pty = true
host = conf['host'].to_s
RSpec.configuration.host = host
ssh_opts = {
port: conf['port'] || 22,
auth_methods: ['none'],
user_known_hosts_file: "/dev/null",
global_known_hosts_file: "/dev/null",
number_of_password_prompts: 0,
user: conf['user'],
password: conf['password'],
keys: [conf['key_file']].compact,
}
if host.empty?
raise "You must configure a target host."
end
unless ssh_opts[:port] > 0
raise "Port must be > 0 (not #{ssh_opts[:port]})"
end
if ssh_opts[:user].to_s.empty?
raise "User must not be empty."
end
unless ssh_opts[:keys].empty?
ssh_opts[:auth_methods].push('publickey')
ssh_opts[:keys_only] = true if ssh_opts[:password].nil?
end
unless ssh_opts[:password].nil?
ssh_opts[:auth_methods].push('password')
end
if ssh_opts[:keys].empty? and ssh_opts[:password].nil?
raise "You must configure at least one authentication method" +
": Password or key."
end
si.ssh_options = ssh_opts
end
end
Vulcano::Backend.add_module('ssh', Vulcano::Backend::SSH)

View file

@ -1,54 +1,54 @@
# encoding: utf-8
module Vulcano
module Backend
module Vulcano::Backend::WinRM
def configure_winrm(conf)
si = Specinfra.configuration
si.backend = :winrm
si.os = { family: 'windows'}
def self.configure(conf)
si = Specinfra.configuration
si.backend = :winrm
si.os = { family: 'windows'}
# common options
host = conf['host'].to_s
port = conf['port']
user = conf['user'].to_s
pass = conf['pass'].tp_s
# common options
host = conf['host'].to_s
port = conf['port']
user = conf['user'].to_s
pass = conf['pass'].tp_s
# SSL configuration
if conf['winrm_ssl']
scheme = 'https'
port = port || 5986
else
scheme = 'http'
port = port || 5985
end
# validation
if host.empty?
raise "You must configure a target host."
end
unless port > 0
raise "Port must be > 0 (not #{port})"
end
if user.empty?
raise "You must configure a WinRM user for login."
end
if pass.empty?
raise "You must configure a WinRM password."
end
# create the connection
endpoint = "#{scheme}://#{host}:#{port}/wsman"
winrm = ::WinRM::WinRMWebService.new(
endpoint,
:ssl,
user: user,
pass: pass,
basic_auth_only: true,
no_ssl_peer_verification: conf['winrm_self_signed'],
)
si.winrm = winrm
# SSL configuration
if conf['winrm_ssl']
scheme = 'https'
port = port || 5986
else
scheme = 'http'
port = port || 5985
end
# validation
if host.empty?
raise "You must configure a target host."
end
unless port > 0
raise "Port must be > 0 (not #{port})"
end
if user.empty?
raise "You must configure a WinRM user for login."
end
if pass.empty?
raise "You must configure a WinRM password."
end
# create the connection
endpoint = "#{scheme}://#{host}:#{port}/wsman"
winrm = ::WinRM::WinRMWebService.new(
endpoint,
:ssl,
user: user,
pass: pass,
basic_auth_only: true,
no_ssl_peer_verification: conf['winrm_self_signed'],
)
si.winrm = winrm
end
end
Vulcano::Backend.add_module('winrm', Vulcano::Backend::WinRM)

View file

@ -20,8 +20,6 @@ module Vulcano
class Runner
include Vulcano::Backend
def initialize(profile_id, conf)
@rules = []
@profile_id = profile_id
@ -32,9 +30,10 @@ module Vulcano
RSpec.configuration.add_formatter(:json)
# specinfra
resolve_target_options(@conf)
configure_shared_options(@conf)
configure_target(@conf)
backend = Vulcano::Backend.new(@conf)
backend.resolve_target_options
backend.configure_shared_options
backend.configure_target
end
def add_resources(resources)

View file

@ -1,19 +1,13 @@
# encoding: utf-8
require 'utils/modulator'
module Vulcano
module Targets
def self.modules
@modules ||= []
end
def self.add_module(handler)
modules.push(handler)
end
extend Modulator
def self.resolve(targets)
Array(targets).map do |target|
handler = @modules.find{|m| m.handles?(target)}
handler = modules.values.find{|m| m.handles?(target)}
if handler.nil?
raise "Don't know how to handle target: #{target}"
end

View file

@ -10,4 +10,4 @@ class FileTarget
end
end
Vulcano::Targets.add_module(FileTarget.new)
Vulcano::Targets.add_module('file', FileTarget.new)