Skip to content

Commit

Permalink
Use msi capabilities for chef (#4949)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pjanotti authored Jun 10, 2024
1 parent 46ced7c commit 77529d2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
29 changes: 29 additions & 0 deletions deployments/chef/recipes/collector_win_config_options.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions deployments/chef/recipes/collector_win_install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
25 changes: 1 addition & 24 deletions deployments/chef/recipes/collector_win_registry.rb
Original file line number Diff line number Diff line change
@@ -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|
Expand Down
8 changes: 7 additions & 1 deletion deployments/chef/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 77529d2

Please sign in to comment.