From 2236dc08ec1ef01e54a51b0529e86f9e5e941603 Mon Sep 17 00:00:00 2001 From: Julien Lebot Date: Thu, 27 Feb 2020 12:01:21 +0100 Subject: [PATCH 1/4] Add cleanup for Windows env vars --- libraries/windows_cleanup.rb | 15 +++++++++++++++ recipes/_install-windows.rb | 21 +++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 libraries/windows_cleanup.rb diff --git a/libraries/windows_cleanup.rb b/libraries/windows_cleanup.rb new file mode 100644 index 00000000..e0042cc9 --- /dev/null +++ b/libraries/windows_cleanup.rb @@ -0,0 +1,15 @@ +require 'chef/handler' +require 'chef/log' + +module Windows + class Helper + def do_cleanup(context) + Chef::Log.info 'Cleanup started.' + resource = context.resource_collection.lookup("windows_env[DDAGENTUSER_NAME]") + resource.run_action(:delete) if resource + resource = context.resource_collection.lookup("windows_env[DDAGENTUSER_PASSWORD]") + resource.run_action(:delete) if resource + Chef::Log.info 'Cleanup finished.' + end + end +end \ No newline at end of file diff --git a/recipes/_install-windows.rb b/recipes/_install-windows.rb index 1c4aa0c1..d2b909d3 100644 --- a/recipes/_install-windows.rb +++ b/recipes/_install-windows.rb @@ -17,6 +17,19 @@ # limitations under the License. # +Chef.event_handler do + on :run_failed do + Windows::Helper.new.do_cleanup( + Chef.run_context + ) + end + on :run_completed do + Windows::Helper.new.do_cleanup( + Chef.run_context + ) + end +end + dd_agent_version = Chef::Datadog.agent_version(node) if dd_agent_version.nil? @@ -141,11 +154,3 @@ unsafe end end - -windows_env 'DDAGENTUSER_NAME' do - action :delete -end - -windows_env 'DDAGENTUSER_PASSWORD' do - action :delete -end From 262ce209c94a27bacce4b9cc022e0280b61139b1 Mon Sep 17 00:00:00 2001 From: Julien Lebot Date: Thu, 27 Feb 2020 13:57:04 +0100 Subject: [PATCH 2/4] Move code to recipe. Keep delete env var resources. --- libraries/windows_cleanup.rb | 15 --------------- recipes/_install-windows.rb | 30 +++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 20 deletions(-) delete mode 100644 libraries/windows_cleanup.rb diff --git a/libraries/windows_cleanup.rb b/libraries/windows_cleanup.rb deleted file mode 100644 index e0042cc9..00000000 --- a/libraries/windows_cleanup.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'chef/handler' -require 'chef/log' - -module Windows - class Helper - def do_cleanup(context) - Chef::Log.info 'Cleanup started.' - resource = context.resource_collection.lookup("windows_env[DDAGENTUSER_NAME]") - resource.run_action(:delete) if resource - resource = context.resource_collection.lookup("windows_env[DDAGENTUSER_PASSWORD]") - resource.run_action(:delete) if resource - Chef::Log.info 'Cleanup finished.' - end - end -end \ No newline at end of file diff --git a/recipes/_install-windows.rb b/recipes/_install-windows.rb index d2b909d3..c3a68f1d 100644 --- a/recipes/_install-windows.rb +++ b/recipes/_install-windows.rb @@ -17,17 +17,27 @@ # limitations under the License. # +include_recipe 'chef_handler' + +module Windows + class Helper + def do_cleanup(context) + Chef::Log.info 'Windows environment vars cleanup started.' + resource = context.resource_collection.lookup("windows_env[DDAGENTUSER_NAME]") + resource.run_action(:delete) if resource + resource = context.resource_collection.lookup("windows_env[DDAGENTUSER_PASSWORD]") + resource.run_action(:delete) if resource + Chef::Log.info 'Windows environment vars cleanup finished.' + end + end +end + Chef.event_handler do on :run_failed do Windows::Helper.new.do_cleanup( Chef.run_context ) end - on :run_completed do - Windows::Helper.new.do_cleanup( - Chef.run_context - ) - end end dd_agent_version = Chef::Datadog.agent_version(node) @@ -154,3 +164,13 @@ unsafe end end + +windows_env 'DDAGENTUSER_NAME' do + action :delete + sensitive true +end + +windows_env 'DDAGENTUSER_PASSWORD' do + action :delete + sensitive true +end From 32108c7e1aac55cd51d84b6b7345ce28e7cbead2 Mon Sep 17 00:00:00 2001 From: Julien Lebot Date: Thu, 27 Feb 2020 13:58:17 +0100 Subject: [PATCH 3/4] Stop offending Rubocop --- recipes/_install-windows.rb | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/recipes/_install-windows.rb b/recipes/_install-windows.rb index c3a68f1d..819f7f5c 100644 --- a/recipes/_install-windows.rb +++ b/recipes/_install-windows.rb @@ -23,9 +23,9 @@ module Windows class Helper def do_cleanup(context) Chef::Log.info 'Windows environment vars cleanup started.' - resource = context.resource_collection.lookup("windows_env[DDAGENTUSER_NAME]") + resource = context.resource_collection.lookup('windows_env[DDAGENTUSER_NAME]') resource.run_action(:delete) if resource - resource = context.resource_collection.lookup("windows_env[DDAGENTUSER_PASSWORD]") + resource = context.resource_collection.lookup('windows_env[DDAGENTUSER_PASSWORD]') resource.run_action(:delete) if resource Chef::Log.info 'Windows environment vars cleanup finished.' end @@ -57,14 +57,14 @@ def do_cleanup(context) if node['datadog']['windows_agent_use_exe'] dd_agent_installer = "#{dd_agent_installer_basename}.exe" temp_file = "#{temp_file_basename}.exe" - resolved_installer_type = :custom + installer_type = :custom install_options = '/q' else dd_agent_installer = "#{dd_agent_installer_basename}.msi" temp_file = "#{temp_file_basename}.msi" - resolved_installer_type = :msi + installer_type = :msi # Agent >= 5.12.0 installs per-machine by default, but specifying ALLUSERS=1 shouldn't affect the install - install_options = '/norestart ALLUSERS=1' + install_options = '/norestart ALLUSERS=1 /Log C:\\msi.log' # Since 6.11.0, the core and APM/trace components of the Windows Agent run under # a specific user instead of LOCAL_SYSTEM, check whether the user has provided @@ -89,8 +89,6 @@ def do_cleanup(context) fix_message = 'The file downloaded matches a known unsafe MSI - Agent versions 6.14.0/1 have been blacklisted. please use a different release. '\ 'See http://dtdg.co/win-614-fix' -must_reinstall = Chef::Datadog::WindowsInstallHelpers.must_reinstall?(node) - # Download the installer to a temp location remote_file temp_file do source node['datadog']['windows_agent_url'] + dd_agent_installer @@ -110,10 +108,8 @@ def do_cleanup(context) end unless node['datadog']['windows_blacklist_silent_fail'] # these are notified in order - if must_reinstall - notifies :create, "remote_file[#{temp_fix_file}]", :immediately - notifies :run, 'powershell_script[datadog_6.14.x_fix]', :immediately - end + notifies :create, "remote_file[#{temp_fix_file}]", :immediately + notifies :run, 'powershell_script[datadog_6.14.x_fix]', :immediately end remote_file temp_fix_file do @@ -144,7 +140,7 @@ def do_cleanup(context) # Install the package windows_package 'Datadog Agent' do # ~FC009 source temp_file - installer_type resolved_installer_type + installer_type installer_type options install_options timeout node['datadog']['windows_msi_timeout'] action :install From 25f6f1a02756712203016b523e9f0c9d982c2448 Mon Sep 17 00:00:00 2001 From: Julien Lebot Date: Thu, 27 Feb 2020 14:03:09 +0100 Subject: [PATCH 4/4] Undo unwanted changes --- recipes/_install-windows.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/recipes/_install-windows.rb b/recipes/_install-windows.rb index 819f7f5c..feda4c58 100644 --- a/recipes/_install-windows.rb +++ b/recipes/_install-windows.rb @@ -57,14 +57,14 @@ def do_cleanup(context) if node['datadog']['windows_agent_use_exe'] dd_agent_installer = "#{dd_agent_installer_basename}.exe" temp_file = "#{temp_file_basename}.exe" - installer_type = :custom + resolved_installer_type = :custom install_options = '/q' else dd_agent_installer = "#{dd_agent_installer_basename}.msi" temp_file = "#{temp_file_basename}.msi" - installer_type = :msi + resolved_installer_type = :msi # Agent >= 5.12.0 installs per-machine by default, but specifying ALLUSERS=1 shouldn't affect the install - install_options = '/norestart ALLUSERS=1 /Log C:\\msi.log' + install_options = '/norestart ALLUSERS=1' # Since 6.11.0, the core and APM/trace components of the Windows Agent run under # a specific user instead of LOCAL_SYSTEM, check whether the user has provided @@ -89,6 +89,8 @@ def do_cleanup(context) fix_message = 'The file downloaded matches a known unsafe MSI - Agent versions 6.14.0/1 have been blacklisted. please use a different release. '\ 'See http://dtdg.co/win-614-fix' +must_reinstall = Chef::Datadog::WindowsInstallHelpers.must_reinstall?(node) + # Download the installer to a temp location remote_file temp_file do source node['datadog']['windows_agent_url'] + dd_agent_installer @@ -108,8 +110,10 @@ def do_cleanup(context) end unless node['datadog']['windows_blacklist_silent_fail'] # these are notified in order - notifies :create, "remote_file[#{temp_fix_file}]", :immediately - notifies :run, 'powershell_script[datadog_6.14.x_fix]', :immediately + if must_reinstall + notifies :create, "remote_file[#{temp_fix_file}]", :immediately + notifies :run, 'powershell_script[datadog_6.14.x_fix]', :immediately + end end remote_file temp_fix_file do @@ -140,7 +144,7 @@ def do_cleanup(context) # Install the package windows_package 'Datadog Agent' do # ~FC009 source temp_file - installer_type installer_type + installer_type resolved_installer_type options install_options timeout node['datadog']['windows_msi_timeout'] action :install