Class: Chef::Knife::Core::WindowsBootstrapContext
- Inherits:
-
BootstrapContext
- Object
- BootstrapContext
- Chef::Knife::Core::WindowsBootstrapContext
- Defined in:
- lib/chef/knife/core/windows_bootstrap_context.rb
Overview
Instances of BootstrapContext are the context objects (i.e., self) for bootstrap templates. For backwards compatability, they must set the following instance variables:
-
@config - a hash of knife’s config values
-
@run_list - the run list for the node to boostrap
Constant Summary collapse
- PathHelper =
::Knife::Windows::PathHelper
Instance Attribute Summary collapse
-
#client_pem ⇒ Object
Returns the value of attribute client_pem.
Instance Method Summary collapse
- #bootstrap_directory ⇒ Object
- #config_content ⇒ Object
-
#escape_and_echo(file_contents) ⇒ Object
escape WIN BATCH special chars and prefixes each line with an echo.
- #first_boot ⇒ Object
-
#initialize(config, run_list, chef_config, secret = nil) ⇒ WindowsBootstrapContext
constructor
A new instance of WindowsBootstrapContext.
- #install_chef ⇒ Object
- #latest_current_windows_chef_version_query ⇒ Object
- #local_download_path ⇒ Object
- #msi_url(machine_os = nil, machine_arch = nil, download_context = nil) ⇒ Object
- #secret ⇒ Object
- #start_chef ⇒ Object
- #trusted_certs_script ⇒ Object
- #validation_key ⇒ Object
- #win_wget ⇒ Object
- #win_wget_ps ⇒ Object
Constructor Details
#initialize(config, run_list, chef_config, secret = nil) ⇒ WindowsBootstrapContext
Returns a new instance of WindowsBootstrapContext.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 40 def initialize(config, run_list, chef_config, secret=nil) @config = config @run_list = run_list @chef_config = chef_config @secret = secret # Compatibility with Chef 12 and Chef 11 versions begin # Pass along the secret parameter for Chef 12 super(config, run_list, chef_config, secret) rescue ArgumentError # The Chef 11 base class only has parameters for initialize super(config, run_list, chef_config) end end |
Instance Attribute Details
#client_pem ⇒ Object
Returns the value of attribute client_pem.
38 39 40 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 38 def client_pem @client_pem end |
Instance Method Details
#bootstrap_directory ⇒ Object
260 261 262 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 260 def bootstrap_directory bootstrap_directory = "C:\\chef" end |
#config_content ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 71 def config_content client_rb = "log_level :info\nlog_location STDOUT\n\nchef_server_url \"\#{@chef_config[:chef_server_url]}\"\nvalidation_client_name \"\#{@chef_config[:validation_client_name]}\"\n\nfile_cache_path \"c:/chef/cache\"\nfile_backup_path \"c:/chef/backup\"\ncache_options ({:path => \"c:/chef/cache/checksums\", :skip_expires => true})\n\n" if @config[:chef_node_name] client_rb << %Q{node_name "#{@config[:chef_node_name]}"\n} else client_rb << "# Using default node name (fqdn)\n" end # We configure :verify_api_cert only when it's overridden on the CLI # or when specified in the knife config. if !@config[:node_verify_api_cert].nil? || knife_config.has_key?(:verify_api_cert) value = @config[:node_verify_api_cert].nil? ? knife_config[:verify_api_cert] : @config[:node_verify_api_cert] client_rb << %Q{verify_api_cert #{value}\n} end # We configure :ssl_verify_mode only when it's overridden on the CLI # or when specified in the knife config. if @config[:node_ssl_verify_mode] || knife_config.has_key?(:ssl_verify_mode) value = case @config[:node_ssl_verify_mode] when "peer" :verify_peer when "none" :verify_none when nil knife_config[:ssl_verify_mode] else nil end if value client_rb << %Q{ssl_verify_mode :#{value}\n} end end if @config[:ssl_verify_mode] client_rb << %Q{ssl_verify_mode :#{knife_config[:ssl_verify_mode]}\n} end if knife_config[:bootstrap_proxy] client_rb << "\n" client_rb << %Q{http_proxy "#{knife_config[:bootstrap_proxy]}"\n} client_rb << %Q{https_proxy "#{knife_config[:bootstrap_proxy]}"\n} client_rb << %Q{no_proxy "#{knife_config[:bootstrap_no_proxy]}"\n} if knife_config[:bootstrap_no_proxy] end if knife_config[:bootstrap_no_proxy] client_rb << %Q{no_proxy "#{knife_config[:bootstrap_no_proxy]}"\n} end if @config[:secret] client_rb << %Q{encrypted_data_bag_secret "c:/chef/encrypted_data_bag_secret"\n} end unless trusted_certs_script.empty? client_rb << %Q{trusted_certs_dir "c:/chef/trusted_certs"\n} end escape_and_echo(client_rb) end |
#escape_and_echo(file_contents) ⇒ Object
escape WIN BATCH special chars and prefixes each line with an echo
290 291 292 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 290 def escape_and_echo(file_contents) file_contents.gsub(/^(.*)$/, 'echo.\1').gsub(/([(<|>)^])/, '^\1') end |
#first_boot ⇒ Object
283 284 285 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 283 def first_boot escape_and_echo(super.to_json) end |
#install_chef ⇒ Object
254 255 256 257 258 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 254 def install_chef # The normal install command uses regular double quotes in # the install command, so request such a string from install_command install_chef = install_command('"') + "\n" + fallback_install_task_command end |
#latest_current_windows_chef_version_query ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 147 def latest_current_windows_chef_version_query installer_version_string = nil if @config[:prerelease] installer_version_string = "&prerelease=true" else chef_version_string = if knife_config[:bootstrap_version] knife_config[:bootstrap_version] else Chef::VERSION.split(".").first end installer_version_string = "&v=#{chef_version_string}" # If bootstrapping a pre-release version add the prerelease query string if chef_version_string.split(".").length > 3 installer_version_string << "&prerelease=true" end end installer_version_string end |
#local_download_path ⇒ Object
264 265 266 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 264 def local_download_path local_download_path = "%TEMP%\\chef-client-latest.msi" end |
#msi_url(machine_os = nil, machine_arch = nil, download_context = nil) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 268 def msi_url(machine_os=nil, machine_arch=nil, download_context=nil) # The default msi path has a number of url query parameters - we attempt to substitute # such parameters in as long as they are provided by the template. if @config[:msi_url].nil? || @config[:msi_url].empty? url = "https://www.chef.io/chef/download?p=windows" url += "&pv=#{machine_os}" unless machine_os.nil? url += "&m=#{machine_arch}" unless machine_arch.nil? url += "&DownloadContext=#{download_context}" unless download_context.nil? url += latest_current_windows_chef_version_query else @config[:msi_url] end end |
#secret ⇒ Object
63 64 65 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 63 def secret escape_and_echo(@config[:secret]) end |
#start_chef ⇒ Object
142 143 144 145 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 142 def start_chef start_chef = "SET \"PATH=%PATH%;C:\\ruby\\bin;C:\\opscode\\chef\\bin;C:\\opscode\\chef\\embedded\\bin\"\n" start_chef << "chef-client -c c:/chef/client.rb -j c:/chef/first-boot.json -E #{bootstrap_environment}\n" end |
#trusted_certs_script ⇒ Object
67 68 69 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 67 def trusted_certs_script @trusted_certs_script ||= trusted_certs_content end |
#validation_key ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 55 def validation_key if File.exist?(File.(@chef_config[:validation_key])) IO.read(File.(@chef_config[:validation_key])) else false end end |
#win_wget ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 169 def win_wget # I tried my best to figure out how to properly url decode and switch / to # but this is VBScript - so I don't really care that badly. win_wget = "url = WScript.Arguments.Named(\"url\")\npath = WScript.Arguments.Named(\"path\")\nproxy = null\n'* Vaguely attempt to handle file:// scheme urls by url unescaping and switching all\n'* / into \\. Also assume that file:/// is a local absolute path and that file://<foo>\n'* is possibly a network file path.\nIf InStr(url, \"file://\") = 1 Then\nurl = Unescape(url)\nIf InStr(url, \"file:///\") = 1 Then\nsourcePath = Mid(url, Len(\"file:///\") + 1)\nElse\nsourcePath = Mid(url, Len(\"file:\") + 1)\nEnd If\nsourcePath = Replace(sourcePath, \"/\", \"\\\\\")\n\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\")\nIf objFSO.Fileexists(path) Then objFSO.DeleteFile path\nobjFSO.CopyFile sourcePath, path, true\nSet objFSO = Nothing\n\nElse\nSet objXMLHTTP = CreateObject(\"MSXML2.ServerXMLHTTP\")\nSet wshShell = CreateObject( \"WScript.Shell\" )\nSet objUserVariables = wshShell.Environment(\"USER\")\n\nrem http proxy is optional\nrem attempt to read from HTTP_PROXY env var first\nOn Error Resume Next\n\nIf NOT (objUserVariables(\"HTTP_PROXY\") = \"\") Then\nproxy = objUserVariables(\"HTTP_PROXY\")\n\nrem fall back to named arg\nElseIf NOT (WScript.Arguments.Named(\"proxy\") = \"\") Then\nproxy = WScript.Arguments.Named(\"proxy\")\nEnd If\n\nIf NOT isNull(proxy) Then\nrem setProxy method is only available on ServerXMLHTTP 6.0+\nSet objXMLHTTP = CreateObject(\"MSXML2.ServerXMLHTTP.6.0\")\nobjXMLHTTP.setProxy 2, proxy\nEnd If\n\nOn Error Goto 0\n\nobjXMLHTTP.open \"GET\", url, false\nobjXMLHTTP.send()\nIf objXMLHTTP.Status = 200 Then\nSet objADOStream = CreateObject(\"ADODB.Stream\")\nobjADOStream.Open\nobjADOStream.Type = 1\nobjADOStream.Write objXMLHTTP.ResponseBody\nobjADOStream.Position = 0\nSet objFSO = Createobject(\"Scripting.FileSystemObject\")\nIf objFSO.Fileexists(path) Then objFSO.DeleteFile path\nSet objFSO = Nothing\nobjADOStream.SaveToFile path\nobjADOStream.Close\nSet objADOStream = Nothing\nEnd If\nSet objXMLHTTP = Nothing\nEnd If\n" escape_and_echo(win_wget) end |
#win_wget_ps ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 239 def win_wget_ps win_wget_ps = "param(\n [String] $remoteUrl,\n [String] $localPath\n)\n\n$webClient = new-object System.Net.WebClient;\n\n$webClient.DownloadFile($remoteUrl, $localPath);\n" escape_and_echo(win_wget_ps) end |