mirror of
https://github.com/inspec/inspec
synced 2024-11-15 01:17:08 +00:00
commit
1570e8e7af
4 changed files with 122 additions and 0 deletions
|
@ -37,6 +37,8 @@ module Vulcano
|
|||
end
|
||||
end
|
||||
|
||||
require 'vulcano/backend/docker'
|
||||
require 'vulcano/backend/local'
|
||||
require 'vulcano/backend/mock'
|
||||
require 'vulcano/backend/specinfra'
|
||||
require 'vulcano/backend/ssh'
|
||||
|
|
33
lib/vulcano/backend/docker.rb
Normal file
33
lib/vulcano/backend/docker.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
# encoding: utf-8
|
||||
require 'docker'
|
||||
|
||||
module Vulcano::Backends
|
||||
class Docker < Vulcano.backend(1)
|
||||
name 'docker'
|
||||
|
||||
def initialize(conf)
|
||||
@conf = conf
|
||||
@files = {}
|
||||
id = @conf['host'] ||
|
||||
fail("You must specify a docker container ID.")
|
||||
@container = ::Docker::Container.get(id) ||
|
||||
fail("Can't find Docker container #{id}")
|
||||
end
|
||||
|
||||
def file(path)
|
||||
@files[path] ||= LinuxFile.new(self, path)
|
||||
end
|
||||
|
||||
def run_command(cmd)
|
||||
stdout, stderr, exit_status = @container.exec([
|
||||
'/bin/sh', '-c', cmd
|
||||
])
|
||||
CommandResult.new(stdout.join, stderr.join, exit_status)
|
||||
rescue ::Docker::Error::DockerError => err
|
||||
raise
|
||||
rescue => err
|
||||
# @TODO: differentiate any other error
|
||||
raise
|
||||
end
|
||||
end
|
||||
end
|
86
lib/vulcano/backend/ssh.rb
Normal file
86
lib/vulcano/backend/ssh.rb
Normal file
|
@ -0,0 +1,86 @@
|
|||
# encoding: utf-8
|
||||
|
||||
module Vulcano::Backends
|
||||
class SSH < Vulcano.backend(1)
|
||||
name 'ssh'
|
||||
|
||||
def initialize(conf)
|
||||
@conf = conf
|
||||
@files = {}
|
||||
@conf['host'] ||
|
||||
fail("You must specify a SSH host.")
|
||||
@ssh = start_ssh
|
||||
end
|
||||
|
||||
def file(path)
|
||||
@files[path] ||= LinuxFile.new(self, path)
|
||||
end
|
||||
|
||||
def run_command(cmd)
|
||||
stdout = stderr = ''
|
||||
exit_status = nil
|
||||
|
||||
@ssh.open_channel do |channel|
|
||||
channel.exec(cmd) do |ch, success|
|
||||
unless success
|
||||
fail "Couldn't execute command on SSH."
|
||||
end
|
||||
|
||||
channel.on_data do |ch,data|
|
||||
stdout += data
|
||||
end
|
||||
|
||||
channel.on_extended_data do |ch,type,data|
|
||||
stderr += data
|
||||
end
|
||||
|
||||
channel.on_request("exit-status") do |ch,data|
|
||||
exit_status = data.read_long
|
||||
end
|
||||
|
||||
channel.on_request("exit-signal") do |ch, data|
|
||||
exit_status = data.read_long
|
||||
end
|
||||
end
|
||||
end
|
||||
@ssh.loop
|
||||
|
||||
CommandResult.new(stdout, stderr, exit_status)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def start_ssh
|
||||
host = @conf['host']
|
||||
ssh_config = Net::SSH.configuration_for(host)
|
||||
|
||||
user = @conf['user'] || ssh_config[:user]
|
||||
keys = [ @conf['key_file'], ssh_config[:keys] ].flatten.compact
|
||||
options = {
|
||||
port: @conf['port'] || ssh_config[:port] || 22,
|
||||
auth_methods: ['none'],
|
||||
user_known_hosts_file: '/dev/null',
|
||||
global_known_hosts_file: '/dev/null',
|
||||
number_of_password_prompts: 0,
|
||||
password: @conf['password'] || ssh_config[:password],
|
||||
keys: keys,
|
||||
}
|
||||
|
||||
unless options[:keys].empty?
|
||||
options[:auth_methods].push('publickey')
|
||||
options[:keys_only] = true if options[:password].nil?
|
||||
end
|
||||
|
||||
unless options[:password].nil?
|
||||
options[:auth_methods].push('password')
|
||||
end
|
||||
|
||||
if options[:keys].empty? and options[:password].nil?
|
||||
fail('You must configure at least one authentication method for SSH:' +
|
||||
' Password or key.')
|
||||
end
|
||||
|
||||
Net::SSH.start( host, user, options )
|
||||
end
|
||||
end
|
||||
end
|
|
@ -6,6 +6,7 @@ module Vulcano::Plugins
|
|||
class Backend
|
||||
autoload :FileCommon, 'vulcano/plugins/backend_file_common'
|
||||
autoload :LinuxFile, 'vulcano/plugins/backend_linux_file'
|
||||
CommandResult = Struct.new(:stdout, :stderr, :exit_status)
|
||||
|
||||
def self.name(name)
|
||||
Vulcano::Plugins::Backend.__register(name, self)
|
||||
|
|
Loading…
Reference in a new issue