Class: Chef::Knife::Winrm
- Inherits:
-
Chef::Knife
- Object
- Chef::Knife
- Chef::Knife::Winrm
- Defined in:
- lib/chef/knife/winrm.rb
Instance Attribute Summary collapse
-
#password ⇒ Object
writeonly
Sets the attribute password.
Instance Method Summary collapse
- #configure_session ⇒ Object
- #get_password ⇒ Object
- #h ⇒ Object
- #interactive ⇒ Object
- #load_late_dependencies ⇒ Object
- #print_data(host, data, color = :cyan) ⇒ Object
-
#read_line ⇒ Object
Present the prompt and read a single line from the console.
- #reader ⇒ Object
- #run ⇒ Object
- #session ⇒ Object
- #session_from_list(list) ⇒ Object
- #winrm_command(command, subsession = nil) ⇒ Object
Instance Attribute Details
#password=(value) ⇒ Object (writeonly)
Sets the attribute password
31 32 33 |
# File 'lib/chef/knife/winrm.rb', line 31 def password=(value) @password = value end |
Instance Method Details
#configure_session ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/chef/knife/winrm.rb', line 126 def configure_session list = case config[:manual] when true @name_args[0].split(" ") when false r = Array.new q = Chef::Search::Query.new @action_nodes = q.search(:node, @name_args[0])[0] @action_nodes.each do |item| i = format_for_display(item)[config[:attribute]] r.push(i) unless i.nil? end r end (Chef::Log.fatal("No nodes returned from search!"); exit 10) if list.length == 0 session_from_list(list) end |
#get_password ⇒ Object
186 187 188 |
# File 'lib/chef/knife/winrm.rb', line 186 def get_password @password ||= h.ask("Enter your password: ") { |q| q.echo = false } end |
#h ⇒ Object
122 123 124 |
# File 'lib/chef/knife/winrm.rb', line 122 def h @highline ||= HighLine.new end |
#interactive ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/chef/knife/winrm.rb', line 215 def interactive puts "Connected to #{h.list(session.servers.collect { |s| h.color(s.host, :cyan) }, :inline, " and ")}" puts puts "To run a command on a list of servers, do:" puts " on SERVER1 SERVER2 SERVER3; COMMAND" puts " Example: on latte foamy; echo foobar" puts puts "To exit interactive mode, use 'quit!'" puts while 1 command = read_line case command when 'quit!' puts 'Bye!' session.close break when /^on (.+?); (.+)$/ raw_list = $1.split(" ") server_list = Array.new session.servers.each do |session_server| server_list << session_server if raw_list.include?(session_server.host) end command = $2 winrm_command(command, session.on(*server_list)) else winrm_command(command) end end end |
#load_late_dependencies ⇒ Object
260 261 262 263 264 265 |
# File 'lib/chef/knife/winrm.rb', line 260 def load_late_dependencies require 'readline' %w[em-winrm highline].each do |dep| load_late_dependency dep end end |
#print_data(host, data, color = :cyan) ⇒ Object
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/chef/knife/winrm.rb', line 170 def print_data(host, data, color = :cyan) if data =~ /\n/ data.split(/\n/).each { |d| print_data(host, d, color) } else padding = @longest - host.length print h.color(host, color) padding.downto(0) { print " " } puts data.chomp end end |
#read_line ⇒ Object
Present the prompt and read a single line from the console. It also detects ^D and returns “exit” in that case. Adds the input to the history, unless the input is empty. Loops repeatedly until a non-empty line is input.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/chef/knife/winrm.rb', line 194 def read_line loop do command = reader.readline("#{h.color('knife-winrm>', :bold)} ", true) if command.nil? command = "exit" puts(command) else command.strip! end unless command.empty? return command end end end |
#reader ⇒ Object
211 212 213 |
# File 'lib/chef/knife/winrm.rb', line 211 def reader Readline end |
#run ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/chef/knife/winrm.rb', line 245 def run @longest = 0 load_late_dependencies configure_session case @name_args[1] when "interactive" interactive else winrm_command(@name_args[1..-1].join(" ")) session.close end end |
#session ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/chef/knife/winrm.rb', line 102 def session session_opts = {} session_opts[:logger] = Chef::Log.logger if Chef::Log.level == :debug @session ||= begin s = EventMachine::WinRM::Session.new(session_opts) s.on_output do |host, data| print_data(host, data) end s.on_error do |host, err| print_data(host, err, :red) end s.on_command_complete do |host| host = host == :all ? 'All Servers' : host Chef::Log.debug("command complete on #{host}") end s end end |
#session_from_list(list) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/chef/knife/winrm.rb', line 144 def session_from_list(list) list.each do |item| Chef::Log.debug("Adding #{item}") session_opts = {} session_opts[:user] = config[:winrm_user] if config[:winrm_user] session_opts[:password] = config[:winrm_password] if config[:winrm_password] session_opts[:port] = config[:winrm_port] session_opts[:keytab] = config[:kerberos_keytab_file] if config[:kerberos_keytab_file] session_opts[:realm] = config[:kerberos_realm] if config[:kerberos_realm] session_opts[:service] = config[:kerberos_service] if config[:kerberos_service] session_opts[:ca_trust_path] = config[:ca_trust_file] if config[:ca_trust_file] if config.keys.any? {|k| k.to_s =~ /kerberos/ } session_opts[:transport] = :kerberos else session_opts[:transport] = config[:winrm_transport].to_sym end session.use(item, session_opts) @longest = item.length if item.length > @longest end session end |
#winrm_command(command, subsession = nil) ⇒ Object
181 182 183 184 |
# File 'lib/chef/knife/winrm.rb', line 181 def winrm_command(command, subsession=nil) subsession ||= session subsession.relay_command(command) end |