From 77529d27c4e8f1af5391518e09c749662c333ea9 Mon Sep 17 00:00:00 2001 From: Paulo Janotti Date: Mon, 10 Jun 2024 14:59:55 -0700 Subject: [PATCH] Use msi capabilities for chef (#4949) * Use latest MSI capabilities instead of custom code in Chef * Lint fixes * Try to see the properties string * Fix creation of msi_install_properties * Remove debug message --- .../recipes/collector_win_config_options.rb | 29 +++++++++++++++++++ .../chef/recipes/collector_win_install.rb | 8 +++++ .../chef/recipes/collector_win_registry.rb | 25 +--------------- deployments/chef/recipes/default.rb | 8 ++++- 4 files changed, 45 insertions(+), 25 deletions(-) create mode 100644 deployments/chef/recipes/collector_win_config_options.rb diff --git a/deployments/chef/recipes/collector_win_config_options.rb b/deployments/chef/recipes/collector_win_config_options.rb new file mode 100644 index 0000000000..49722d8dac --- /dev/null +++ b/deployments/chef/recipes/collector_win_config_options.rb @@ -0,0 +1,29 @@ +# Cookbook:: splunk_otel_collector +# Recipe:: collector_win_config_options + +collector_env_vars = [ + { name: 'SPLUNK_ACCESS_TOKEN', type: :string, data: node['splunk_otel_collector']['splunk_access_token'].to_s }, + { name: 'SPLUNK_API_URL', type: :string, data: node['splunk_otel_collector']['splunk_api_url'].to_s }, + { name: 'SPLUNK_BUNDLE_DIR', type: :string, data: node['splunk_otel_collector']['splunk_bundle_dir'].to_s }, + { name: 'SPLUNK_COLLECTD_DIR', type: :string, data: node['splunk_otel_collector']['splunk_collectd_dir'].to_s }, + { name: 'SPLUNK_CONFIG', type: :string, data: node['splunk_otel_collector']['collector_config_dest'].to_s }, + { name: 'SPLUNK_HEC_TOKEN', type: :string, data: node['splunk_otel_collector']['splunk_hec_token'].to_s }, + { name: 'SPLUNK_HEC_URL', type: :string, data: node['splunk_otel_collector']['splunk_hec_url'].to_s }, + { name: 'SPLUNK_INGEST_URL', type: :string, data: node['splunk_otel_collector']['splunk_ingest_url'].to_s }, + { name: 'SPLUNK_REALM', type: :string, data: node['splunk_otel_collector']['splunk_realm'].to_s }, + { name: 'SPLUNK_MEMORY_TOTAL_MIB', type: :string, data: node['splunk_otel_collector']['splunk_memory_total_mib'].to_s }, + { name: 'SPLUNK_TRACE_URL', type: :string, data: node['splunk_otel_collector']['splunk_trace_url'].to_s }, +] + +unless node['splunk_otel_collector']['gomemlimit'].to_s.strip.empty? + collector_env_vars.push({ name: 'GOMEMLIMIT', type: :string, data: node['splunk_otel_collector']['gomemlimit'].to_s }) +end +unless node['splunk_otel_collector']['splunk_listen_interface'].to_s.strip.empty? + collector_env_vars.push({ name: 'SPLUNK_LISTEN_INTERFACE', type: :string, data: node['splunk_otel_collector']['splunk_listen_interface'].to_s }) +end + +node['splunk_otel_collector']['collector_additional_env_vars'].each do |key, value| + collector_env_vars.push({ name: key, type: :string, data: value.to_s }) +end + +node.default['splunk_otel_collector']['collector_win_env_vars'] = collector_env_vars diff --git a/deployments/chef/recipes/collector_win_install.rb b/deployments/chef/recipes/collector_win_install.rb index fed5d49360..9de90a4e92 100644 --- a/deployments/chef/recipes/collector_win_install.rb +++ b/deployments/chef/recipes/collector_win_install.rb @@ -20,8 +20,16 @@ only_if { !::File.exist?(node['splunk_otel_collector']['collector_version_file']) || (::File.readlines(node['splunk_otel_collector']['collector_version_file']).first.strip != collector_version) } end +msi_is_configurable = Gem::Version.new(collector_version) >= Gem::Version.new('0.98.0') +node.default['splunk_otel_collector']['collector_msi_is_configurable'] = msi_is_configurable +msi_install_properties = node['splunk_otel_collector']['collector_win_env_vars'] + .reject { |item| item[:data].nil? || item[:data] == '' } + .map { |item| "#{item[:name]}=\"#{item[:data]}\"" } + .join(' ') + windows_package 'splunk-otel-collector' do source "#{ENV['TEMP']}/splunk-otel-collector-#{collector_version}-amd64.msi" + options msi_install_properties # If the MSI is not configurable, this will be ignored during installation. action :install notifies :restart, 'windows_service[splunk-otel-collector]', :delayed only_if { !::File.exist?(node['splunk_otel_collector']['collector_version_file']) || (::File.readlines(node['splunk_otel_collector']['collector_version_file']).first.strip != collector_version) } diff --git a/deployments/chef/recipes/collector_win_registry.rb b/deployments/chef/recipes/collector_win_registry.rb index a2e9a0a03d..cec9a7f615 100644 --- a/deployments/chef/recipes/collector_win_registry.rb +++ b/deployments/chef/recipes/collector_win_registry.rb @@ -1,30 +1,7 @@ # Cookbook:: splunk_otel_collector # Recipe:: collector_win_registry -collector_env_vars = [ - { name: 'SPLUNK_ACCESS_TOKEN', type: :string, data: node['splunk_otel_collector']['splunk_access_token'].to_s }, - { name: 'SPLUNK_API_URL', type: :string, data: node['splunk_otel_collector']['splunk_api_url'].to_s }, - { name: 'SPLUNK_BUNDLE_DIR', type: :string, data: node['splunk_otel_collector']['splunk_bundle_dir'].to_s }, - { name: 'SPLUNK_COLLECTD_DIR', type: :string, data: node['splunk_otel_collector']['splunk_collectd_dir'].to_s }, - { name: 'SPLUNK_CONFIG', type: :string, data: node['splunk_otel_collector']['collector_config_dest'].to_s }, - { name: 'SPLUNK_HEC_TOKEN', type: :string, data: node['splunk_otel_collector']['splunk_hec_token'].to_s }, - { name: 'SPLUNK_HEC_URL', type: :string, data: node['splunk_otel_collector']['splunk_hec_url'].to_s }, - { name: 'SPLUNK_INGEST_URL', type: :string, data: node['splunk_otel_collector']['splunk_ingest_url'].to_s }, - { name: 'SPLUNK_REALM', type: :string, data: node['splunk_otel_collector']['splunk_realm'].to_s }, - { name: 'SPLUNK_MEMORY_TOTAL_MIB', type: :string, data: node['splunk_otel_collector']['splunk_memory_total_mib'].to_s }, - { name: 'SPLUNK_TRACE_URL', type: :string, data: node['splunk_otel_collector']['splunk_trace_url'].to_s }, -] - -unless node['splunk_otel_collector']['gomemlimit'].to_s.strip.empty? - collector_env_vars.push({ name: 'GOMEMLIMIT', type: :string, data: node['splunk_otel_collector']['gomemlimit'].to_s }) -end -unless node['splunk_otel_collector']['splunk_listen_interface'].to_s.strip.empty? - collector_env_vars.push({ name: 'SPLUNK_LISTEN_INTERFACE', type: :string, data: node['splunk_otel_collector']['splunk_listen_interface'].to_s }) -end - -node['splunk_otel_collector']['collector_additional_env_vars'].each do |key, value| - collector_env_vars.push({ name: key, type: :string, data: value.to_s }) -end +collector_env_vars = node['splunk_otel_collector']['collector_win_env_vars'] collector_env_vars_strings = [] collector_env_vars.each do |item| diff --git a/deployments/chef/recipes/default.rb b/deployments/chef/recipes/default.rb index c48c878887..cde1906220 100644 --- a/deployments/chef/recipes/default.rb +++ b/deployments/chef/recipes/default.rb @@ -9,8 +9,14 @@ end if platform_family?('windows') + include_recipe 'splunk_otel_collector::collector_win_config_options' include_recipe 'splunk_otel_collector::collector_win_install' - include_recipe 'splunk_otel_collector::collector_win_registry' + + # Older MSI versions can't properly setup the collector configuration + # in this case, we need to use the registry to set the environment variables. + unless node['splunk_otel_collector']['collector_msi_is_configurable'] + include_recipe 'splunk_otel_collector::collector_win_registry' + end directory ::File.dirname(node['splunk_otel_collector']['collector_config_dest']) do action :create