lib/ridley/host_connector/winrm.rb in ridley-1.0.0.rc1 vs lib/ridley/host_connector/winrm.rb in ridley-1.0.0.rc2

- old
+ new

@@ -9,51 +9,63 @@ module HostConnector # @author Kyle Allan <kallan@riotgames.com> class WinRM < HostConnector::Base require_relative 'winrm/command_uploader' - DEFAULT_PORT = 5985 - EMBEDDED_RUBY_PATH = 'C:\opscode\chef\embedded\bin\ruby'.freeze + DEFAULT_PORT = 5985 + EMBEDDED_RUBY_PATH = 'C:\opscode\chef\embedded\bin\ruby'.freeze + SESSION_TYPE_COMMAND_METHODS = { + powershell: :run_powershell_script, + cmd: :run_cmd + }.freeze # Execute a shell command on a node # # @param [String] host # the host to perform the action on # @param [String] command # + # @option options [Symbol] :session_type (:cmd) + # * :powershell - run the given command in a powershell session + # * :cmd - run the given command in a cmd session # @option options [Hash] :winrm # * :user (String) a user that will login to each node and perform the bootstrap command on # * :password (String) the password for the user that will perform the bootstrap (required) # * :port (Fixnum) the winrm port to connect on the node the bootstrap will be performed on (5985) # # @return [HostConnector::Response] def run(host, command, options = {}) - options = options.reverse_merge(winrm: Hash.new) + options = options.reverse_merge(winrm: Hash.new, session_type: :cmd) options[:winrm].reverse_merge!(port: DEFAULT_PORT) command_uploaders = Array.new user = options[:winrm][:user] password = options[:winrm][:password] port = options[:winrm][:port] connection = winrm(host, port, options[:winrm].slice(:user, :password)) + unless command_method = SESSION_TYPE_COMMAND_METHODS[options[:session_type]] + raise RuntimeError, "unknown session type: #{options[:session_type]}. Known session types " + + "are: #{SESSION_TYPE_COMMAND_METHODS.keys}" + end + HostConnector::Response.new(host).tap do |response| command_uploaders << command_uploader = CommandUploader.new(connection) command = get_command(command, command_uploader) begin log.info "Running WinRM Command: '#{command}' on: '#{host}' as: '#{user}'" - output = connection.run_cmd(command) do |stdout, stderr| - if stdout + output = connection.send(command_method, command) do |stdout, stderr| + if stdout && stdout.present? response.stdout += stdout log.info "[#{host}](WinRM) #{stdout}" end - if stderr - response.stderr += stderr unless stderr.nil? - log.info "[#{host}](WinRM) #{stdout}" + if stderr && stderr.present? + response.stderr += stderr + log.info "[#{host}](WinRM) #{stderr}" end end response.exit_code = output[:exitcode] rescue ::WinRM::WinRMHTTPTransportError => ex @@ -155,9 +167,30 @@ # # @return [HostConnector::Response] def ruby_script(host, command_lines, options = {}) command = "#{EMBEDDED_RUBY_PATH} -e \"#{command_lines.join(';')}\"" run(host, command, options) + end + + # Uninstall Chef from a node + # + # @param [String] host + # the host to perform the action on + # + # @option options [Boolena] :skip_chef (false) + # skip removal of the Chef package and the contents of the installation + # directory. Setting this to true will only remove any data and configurations + # generated by running Chef client. + # @option options [Hash] :winrm + # * :user (String) a user that will login to each node and perform the bootstrap command on + # * :password (String) the password for the user that will perform the bootstrap (required) + # * :port (Fixnum) the winrm port to connect on the node the bootstrap will be performed on (5985) + # + # @return [HostConnector::Response] + def uninstall_chef(host, options = {}) + options[:session_type] = :powershell + log.info "Uninstalling Chef from host: #{host}" + run(host, CommandContext::WindowsUninstall.command(options), options) end private # @param [String] host