Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BASE WinRM Module(s) added. Extra classes modified. #470

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -19,3 +19,4 @@ tmp
.rbenv-version
.ruby-version
.project
.DS_Store
2 changes: 2 additions & 0 deletions lib/kitchen.rb
Expand Up @@ -36,13 +36,15 @@
require "kitchen/driver"
require "kitchen/driver/base"
require "kitchen/driver/ssh_base"
require "kitchen/driver/winrm_base"
require "kitchen/driver/proxy"
require "kitchen/instance"
require "kitchen/loader/yaml"
require "kitchen/metadata_chopper"
require "kitchen/platform"
require "kitchen/state_file"
require "kitchen/ssh"
require "kitchen/winrm"
require "kitchen/suite"
require "kitchen/version"

Expand Down
73 changes: 73 additions & 0 deletions lib/kitchen/busser.rb
Expand Up @@ -114,6 +114,24 @@ def setup_cmd
Util.wrap_command(cmd)
end

# Return a command string just like setup_cmd but in PowerShell
#
# @author Salim Afiune <salim@afiunemaya.com.mx>
def setup_cmd_posh
@setup_cmd_posh ||= if local_suite_files.empty?
nil
else
setup_cmd_posh = []
setup_cmd_posh << busser_setup_env_posh
setup_cmd_posh << "If ((gem list busser -i) -eq \"false\") {
gem install #{gem_install_args} }"
# We have to modify Busser::Setup to work with PowerShell
# setup_cmd_posh << "&\"$env:SYSTEMDRIVE/tmp/busser/gems/bin/busser\" setup"
setup_cmd_posh << "#{config[:busser_bin]} plugin install #{plugins.join(' ')}"
setup_cmd_posh.join('; ')
end
end

# Returns a command string which transfers all suite test files to the
# instance.
#
Expand Down Expand Up @@ -141,6 +159,28 @@ def sync_cmd
Util.wrap_command(cmd)
end

# Return a command string just like sync_cmd but in PowerShell
#
# @author Salim Afiune <salim@afiunemaya.com.mx>
def sync_cmd_posh
return if local_suite_files.empty?

cmd = <<-CMD.gsub(/^ {8}/, "")
#{busser_setup_env_posh}

#{config[:busser_bin]} suite cleanup

CMD
local_suite_files.each do |f|
cmd << stream_file(f, remote_file_posh(f, config[:suite_name])).concat("\n")
end
helper_files.each do |f|
cmd << stream_file(f, remote_file_posh(f, "helpers")).concat("\n")
end

cmd
end

# Returns a command string which runs all Busser suite tests for the suite.
#
# If no work needs to be performed, for example if there are no tests for
Expand All @@ -160,6 +200,19 @@ def run_cmd
Util.wrap_command(cmd)
end

# Return a command string just like run_cmd but in PowerShell
#
# @author Salim Afiune <salim@afiunemaya.com.mx>
def run_cmd_posh
return if local_suite_files.empty?
cmd = <<-CMD.gsub(/^ {8}/, "")
#{busser_setup_env_posh}

#{config[:busser_bin]} test
CMD
cmd
end

private

DEFAULT_RUBY_BINDIR = "/opt/chef/embedded/bin".freeze
Expand Down Expand Up @@ -248,6 +301,11 @@ def remote_file(file, dir)
concat(file.sub(%r{^#{local_prefix}/}, ""))
end

def remote_file_posh(file, dir)
local_prefix = File.join(config[:test_base_path], dir)
"$env:BUSSER_SUITE_PATH/".
concat(file.sub(%r{^#{local_prefix}/}, ""))
end
# Returns a command string that will, once evaluated, result in the copying
# of a local file to a remote instance.
#
Expand Down Expand Up @@ -298,6 +356,21 @@ def busser_setup_env
].join(" ")
end

# Return a command string just like busser_setup_env but in PowerShell
#
# @author Salim Afiune <salim@afiunemaya.com.mx>
def busser_setup_env_posh
[
%{$env:BUSSER_ROOT="#{config[:root_path]}";},
%{$env:GEM_HOME="#{config[:root_path]}/gems";},
%{$env:GEM_PATH="#{config[:root_path]}/gems";},
%{$env:PATH="$env:PATH;$env:GEM_PATH/bin";},
%{try { $env:BUSSER_SUITE_PATH=@(#{config[:busser_bin]} suite path) }},
%{catch { $env:BUSSER_SUITE_PATH="" };},
%{$env:GEM_CACHE="#{config[:root_path]}/gems/cache"}
].join(" ")
end

# Returns arguments to a `gem install` command, suitable to install the
# Busser gem.
#
Expand Down
1 change: 0 additions & 1 deletion lib/kitchen/cli.rb
Expand Up @@ -50,7 +50,6 @@ def perform(task, command, args = nil, additional_options = {})
:config => @config,
:shell => shell
}.merge(additional_options)

str_const = Thor::Util.camel_case(command)
klass = ::Kitchen::Command.const_get(str_const)
klass.new(args, options, command_options).call
Expand Down
11 changes: 11 additions & 0 deletions lib/kitchen/data_munger.rb
Expand Up @@ -59,6 +59,7 @@ def busser_data_for(suite, platform)
set_kitchen_config_at!(bdata, :kitchen_root)
set_kitchen_config_at!(bdata, :test_base_path)
set_kitchen_config_at!(bdata, :log_level)
override_busser_guest_param!(bdata, driver_data_for(suite, platform))
end
end

Expand Down Expand Up @@ -117,6 +118,16 @@ def suite_data
# @api private
attr_reader :kitchen_config

def override_busser_guest_param!(root, driver)
if driver.has_key?(:guest)
case driver.fetch(:guest)
when ":windows"
root[:sudo] = false
root[:busser_bin] = "busser"
end
end
end

def combine_arrays!(root, key, *namespaces)
if root.key?(key)
root[key] = namespaces.
Expand Down
8 changes: 8 additions & 0 deletions lib/kitchen/driver.rb
Expand Up @@ -37,6 +37,14 @@ module Driver
# @raise [ClientError] if a driver instance could not be created
# @raise [UserError] if the driver's dependencies could not be met
def self.for_plugin(plugin, config)
# What protocol should we load for the instance.
#
# If @param [:guest] exist into .kitchen.yml
# then we use the specific protocol from the driver.
#
# @author Salim Afiune <salim@afiunemaya.com.mx>
plugin = "#{plugin}_#{config[:guest].tr(":","")}" unless config[:guest].nil?

first_load = require("kitchen/driver/#{plugin}")

str_const = Thor::Util.camel_case(plugin)
Expand Down
140 changes: 140 additions & 0 deletions lib/kitchen/driver/winrm_base.rb
@@ -0,0 +1,140 @@
# -*- encoding: utf-8 -*-
#
# Author:: Salim Afiune (<salim@afiunemaya.com.mx>)
#
# Copyright (C) 2014, Salim Afiune
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

module Kitchen

module Driver

# Base class for a driver that uses WinRM to communicate with an instance.
# A subclass must implement the following methods:
# * #create(state)
# * #destroy(state)
#
# @author Salim Afiune <salim@afiunemaya.com.mx>
class WinRMBase < Base

default_config :sudo, false
default_config :port, 5985

def create(state)
raise ClientError, "#{self.class}#create must be implemented"
end

def converge(state)
provisioner = instance.provisioner

# Override sudo config if NOT equals to default for Windows
provisioner.sudo=config[:sudo] if provisioner[:sudo] != config[:sudo]

provisioner.create_sandbox
sandbox_dirs = Dir.glob("#{provisioner.sandbox_path}/*")
Kitchen::WinRM.new(*build_winrm_args(state)) do |conn|
run_remote(provisioner.install_command_posh, conn)
run_remote(provisioner.init_command_posh, conn, false)
transfer_path(sandbox_dirs, provisioner[:root_path], conn)
run_remote(provisioner.prepare_command, conn)
run_remote(provisioner.run_command, conn)
end
ensure
provisioner && provisioner.cleanup_sandbox
end

# (see Base#setup)
def setup(state)
Kitchen::WinRM.new(*build_winrm_args(state)) do |conn|
run_remote(busser.setup_cmd_posh, conn)
end
end

# (see Base#verify)
def verify(state)
Kitchen::WinRM.new(*build_winrm_args(state)) do |conn|
run_remote(busser.sync_cmd_posh, conn)
run_remote(busser.run_cmd_posh, conn)
end
end

def destroy(state)
raise ClientError, "#{self.class}#destroy must be implemented"
end

def login_command(state)
WinRM.new(*build_winrm_args(state)).login_command(vagrant_root)
end

def remote_command(state, command)
Kitchen::WinRM.new(*build_winrm_args(state)) do |conn|
run_remote(command, conn)
end
end

def winrm(winrm_args, command)
Kitchen::WinRM.new(*winrm_args) do |conn|
run_remote(command, conn)
end
end

protected

def build_winrm_args(state)
combined = config.to_hash.merge(state)

opts = Hash.new
opts[:port] = combined[:port] if combined[:port]
opts[:password] = combined[:password] if combined[:password]
opts[:forward_agent] = combined[:forward_agent] if combined.key? :forward_agent
opts[:logger] = logger

[combined[:hostname], combined[:username], opts]
end

def env_cmd(cmd)
#The progress strean may get sent to stderr, so turn it off
env = "$ProgressPreference=\"SilentlyContinue\";"
env << " $env:http_proxy=\"#{config[:http_proxy]}\";" if config[:http_proxy]
env << " $env:https_proxy=\"#{config[:https_proxy]}\";" if config[:https_proxy]

env == "" ? cmd : "#{env} #{cmd}"
end

def run_remote(command, connection, stdout=true)
return if command.nil?

stdout ? connection.exec(env_cmd(command)) : connection.powershell(env_cmd(command))
rescue WinRMFailed, WinRM::WinRMHTTPTransportError,
WinRM::WinRMAuthorizationError, WinRM::WinRMWebServiceError => ex
raise ActionFailed, ex.message
end

def transfer_path(locals, remote, connection)
return if locals.nil? || Array(locals).empty?

info("Transferring files to #{instance.to_str}")
locals.each { |local| connection.upload!(local, remote) }
debug("Transfer complete")
rescue WinRMFailed, ::WinRM::WinRMHTTPTransportError,
::WinRM::WinRMAuthorizationError, ::WinRM::WinRMWebServiceError => ex
raise ActionFailed, ex.message
end

def wait_for_winrm(hostname, username = nil, options = {})
WinRM.new(hostname, username, { :logger => logger }.merge(options)).wait
end
end
end
end
8 changes: 8 additions & 0 deletions lib/kitchen/provisioner/base.rb
Expand Up @@ -70,6 +70,14 @@ def name
def install_command
end

# Override sudo configuration
#
# @param boolean [Boolean] activate/deactivate sudo
# @author Salim Afiune <salim@afiunemaya.com.mx>
def sudo=(boolean)
@config[:sudo]=boolean
end

# Generates a command string which will perform any data initialization
# or configuration required after the provisioner software is installed
# but before the sandbox has been transferred to the instance. If no work
Expand Down