Class: Chef::Knife::WinrmBootstrap

Inherits:
Bootstrap
  • Object
show all
Defined in:
lib/chef/knife/winrm_bootstrap.rb

Instance Method Summary collapse

Instance Method Details

#bootstrap_bat_fileObject



184
185
186
# File 'lib/chef/knife/winrm_bootstrap.rb', line 184

def bootstrap_bat_file
  "%TEMP%\\bootstrap.bat"
end

#bootstrap_commandObject



164
165
166
# File 'lib/chef/knife/winrm_bootstrap.rb', line 164

def bootstrap_command
  @bootstrap_command ||= "cmd /C #{bootstrap_bat_file}"
end

#create_bootstrap_bat_command {|bootstrap_bat.join(" && "), chunk_num += 1| ... } ⇒ Object

Yields:

  • (bootstrap_bat.join(" && "), chunk_num += 1)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/chef/knife/winrm_bootstrap.rb', line 168

def create_bootstrap_bat_command(&block)
  bootstrap_bat = []
  chunk_num = 0
  render_template(load_template(config[:bootstrap_template])).each_line do |line|
    # escape WIN BATCH special chars
    line.gsub!(/[(<|>)^]/).each{|m| "^#{m}"}
    # windows commands are limited to 2047 characters
    if((bootstrap_bat + [line]).join(" && ").size > 2047 )
      yield bootstrap_bat.join(" && "), chunk_num += 1
      bootstrap_bat = []
    end
    bootstrap_bat << ">> #{bootstrap_bat_file} (echo.#{line.chomp.strip})"
  end
  yield bootstrap_bat.join(" && "), chunk_num += 1
end

#knife_winrm(command = '') ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/chef/knife/winrm_bootstrap.rb', line 150

def knife_winrm(command = '')
  winrm = Chef::Knife::Winrm.new
  winrm.name_args = [ server_name, command ]
  winrm.config[:winrm_user] = config[:winrm_user] 
  winrm.config[:winrm_password] = config[:winrm_password]
  winrm.config[:winrm_transport] = config[:winrm_transport]
  winrm.config[:kerberos_keytab_file] = config[:kerberos_keytab_file] if config[:kerberos_keytab_file]
  winrm.config[:kerberos_realm] = config[:kerberos_realm] if config[:kerberos_realm]
  winrm.config[:kerberos_service] = config[:kerberos_service] if config[:kerberos_service]
  winrm.config[:ca_trust_file] = config[:ca_trust_file] if config[:ca_trust_file]
  winrm.config[:manual] = true
  winrm
end

#load_late_dependenciesObject



188
189
190
191
# File 'lib/chef/knife/winrm_bootstrap.rb', line 188

def load_late_dependencies
  super
  require 'chef/knife/winrm'
end

#load_template(template = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chef/knife/winrm_bootstrap.rb', line 102

def load_template(template=nil)
  # Are we bootstrapping using an already shipped template?
  if config[:template_file]
    bootstrap_files = config[:template_file]
  else
    bootstrap_files = []
    bootstrap_files << File.join(File.dirname(__FILE__), 'bootstrap', "#{config[:distro]}.erb")
    bootstrap_files << File.join(Dir.pwd, ".chef", "bootstrap", "#{config[:distro]}.erb")
    bootstrap_files << File.join(ENV['HOME'], '.chef', 'bootstrap', "#{config[:distro]}.erb")
    bootstrap_files << Gem.find_files(File.join("chef","knife","bootstrap","#{config[:distro]}.erb")) 
  end

  template = Array(bootstrap_files).find do |bootstrap_template|
    Chef::Log.debug("Looking for bootstrap template in #{File.dirname(bootstrap_template)}")
    File.exists?(bootstrap_template)
  end

  unless template
    Chef::Log.info("Can not find bootstrap definition for #{config[:distro]}")
    raise Errno::ENOENT
  end

  Chef::Log.debug("Found bootstrap template in #{File.dirname(template)}")
  
  IO.read(template).chomp
end

#runObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/chef/knife/winrm_bootstrap.rb', line 129

def run 
  require 'highline'

  validate_name_args!

  $stdout.sync = true

  Chef::Log.info("Bootstrapping Chef on #{h.color(config[:server_name], :bold)}")

  knife_winrm.load_late_dependencies

  # create a bootstrap.bat file on the node
  # we have to run the remote commands in 2047 char chunks
  create_bootstrap_bat_command do |command_chunk, chunk_num|
    knife_winrm("echo \"Rendering bootstrap.bat chunk #{chunk_num}\" && #{command_chunk}").run
  end

  # execute the bootstrap.bat file
  knife_winrm(bootstrap_command).run
end