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
Instance Method Summary collapse
- #bootstrap_directory ⇒ Object
- #config_content ⇒ Object
- #encrypted_data_bag_secret ⇒ 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) ⇒ WindowsBootstrapContext
constructor
A new instance of WindowsBootstrapContext.
- #install_chef ⇒ Object
- #local_download_path ⇒ Object
- #start_chef ⇒ Object
- #validation_key ⇒ Object
- #win_wget ⇒ Object
- #win_wget_ps ⇒ Object
Constructor Details
#initialize(config, run_list, chef_config) ⇒ WindowsBootstrapContext
Returns a new instance of WindowsBootstrapContext.
32 33 34 35 36 37 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 32 def initialize(config, run_list, chef_config) @config = config @run_list = run_list @chef_config = chef_config super(config, run_list, chef_config) end |
Instance Method Details
#bootstrap_directory ⇒ Object
157 158 159 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 157 def bootstrap_directory bootstrap_directory = "C:\\chef" end |
#config_content ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 47 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]}\"\nclient_key \"c:/chef/client.pem\"\nvalidation_key \"c:/chef/validation.pem\"\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 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 @config[:encrypted_data_bag_secret] client_rb << %Q{encrypted_data_bag_secret "c:/chef/encrypted_data_bag_secret"\n} end escape_and_echo(client_rb) end |
#encrypted_data_bag_secret ⇒ Object
43 44 45 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 43 def encrypted_data_bag_secret escape_and_echo(@config[:encrypted_data_bag_secret]) end |
#escape_and_echo(file_contents) ⇒ Object
escape WIN BATCH special chars and prefixes each line with an echo
173 174 175 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 173 def escape_and_echo(file_contents) file_contents.gsub(/^(.*)$/, 'echo.\1').gsub(/([(<|>)^])/, '^\1') end |
#first_boot ⇒ Object
165 166 167 168 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 165 def first_boot first_boot_attributes_and_run_list = (@config[:first_boot_attributes] || {}).merge(:run_list => @run_list) escape_and_echo(first_boot_attributes_and_run_list.to_json) end |
#install_chef ⇒ Object
151 152 153 154 155 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 151 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 |
#local_download_path ⇒ Object
161 162 163 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 161 def local_download_path local_download_path = "%TEMP%\\chef-client-latest.msi" end |
#start_chef ⇒ Object
82 83 84 85 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 82 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 |
#validation_key ⇒ Object
39 40 41 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 39 def validation_key escape_and_echo(super) end |
#win_wget ⇒ Object
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 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 87 def win_wget win_wget = "url = WScript.Arguments.Named(\"url\")\npath = WScript.Arguments.Named(\"path\")\nproxy = null\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\n" escape_and_echo(win_wget) end |
#win_wget_ps ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/chef/knife/core/windows_bootstrap_context.rb', line 136 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 |