Skip to content

Commit

Permalink
Support license installation
Browse files Browse the repository at this point in the history
Via system property path so we do not require a restart on
new install or upgrade:
cf: https://help.sonatype.com/en/installing-and-updating-licenses.html#installing-or-updating-a-license-using-a-system-property

But also via the API directly for when the license needs
to be updated as the system property path does not care
if about a new license if there is already on installed
  • Loading branch information
jeremy-clerc committed Apr 17, 2024
1 parent ee404d5 commit c6bb2a7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ Gemfile.lock
.kitchen/
.kitchen.local.yml
.bundle/
bundle/
6 changes: 3 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ AllCops:
Layout/LineLength:
Max: 150

Layout/MethodLength:
Metrics/MethodLength:
Max: 60

Layout/AbcSize:
Metrics/AbcSize:
Max: 60

Layout/BlockLength:
Metrics/BlockLength:
Max: 130

Naming/FileName:
Expand Down
45 changes: 41 additions & 4 deletions resources/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
property :properties_variables, Hash, default: lazy { node['nexus3']['properties_variables'] }
property :vmoptions_variables, Hash, default: lazy { node['nexus3']['vmoptions_variables'] }
property :outbound_proxy, [Hash, NilClass], sensitive: true, default: lazy { node['nexus3']['outbound_proxy'] }
property :license_fingerprint, [String, NilClass], default: lazy { node['nexus3']['license_fingerprint'] }
property :license, [String, NilClass], sensitive: true, default: lazy { node['nexus3']['license'] }
property :plugins, Hash, default: lazy { node['nexus3']['plugins'] }
property :logback_variables, Hash, default: lazy { node['nexus3']['logback_variables'] }

Expand Down Expand Up @@ -140,12 +142,47 @@
end
end

nexus3_license 'update installed license' do
action :nothing
fingerprint new_resource.license_fingerprint
license new_resource.license
api_client(lazy { ::Nexus3::Api.local(port, 'admin', new_resource.nexus3_password) })
end

license_file_path = new_resource.properties_variables['nexus.licenseFile']
restart_for_license = !license_file_path.nil? && !::File.exist?(license_file_path)

directory "license directory for #{new_resource.instance_name}" do
path(lazy { ::File.dirname(license_file_path) })
recursive true
owner 'root'
group 'root'
mode '0755'
only_if { !new_resource.license.nil? && !license_file_path.nil? }
end

file "license for #{new_resource.instance_name}" do
action :create
path license_file_path
owner 'root'
group new_resource.nexus3_group
mode '0640'
sensitive true
content(lazy { ::Base64.decode64(new_resource.license) })
notifies :restart, "nexus3_service[#{new_resource.service_name}]", :delayed if restart_for_license
notifies :run, "ruby_block[#{blocker}]", :delayed if restart_for_license
notifies :update, 'nexus3_license[update installed license]', :delayed
only_if { !new_resource.license.nil? && !license_file_path.nil? }
end

link new_resource.nexus3_home do
to install_dir
owner new_resource.nexus3_user
group new_resource.nexus3_group
end

# With unified mode, always make sure this resource is kept between install steps (conf files, ...) and
# steps depending on a running instance, so we can have a "restart point".
nexus3_service new_resource.service_name.to_s do
install_dir install_dir
nexus3_user new_resource.nexus3_user
Expand All @@ -162,9 +199,9 @@
wait_until_ready!(::Nexus3::Api.endpoint(port), node['nexus3']['api']['wait'])
end
action :nothing
notifies :create, "nexus3_api[#{pwchanger}]"
notifies :run, "nexus3_api[#{pwchanger}]"
notifies new_resource.outbound_proxy ? :create : :delete, 'nexus3_outbound_proxy[default]'
notifies :create, "nexus3_api[#{pwchanger}]", :delayed
notifies :run, "nexus3_api[#{pwchanger}]", :delayed
notifies new_resource.outbound_proxy ? :create : :delete, 'nexus3_outbound_proxy[default]', :delayed
end

passwd_file = ::File.join(new_resource.data, 'admin.password')
Expand All @@ -176,7 +213,7 @@
api_client(lazy { ::Nexus3::Api.local(port, 'admin', ::File.read(passwd_file)) })
only_if { ::File.exist? passwd_file }
action :nothing
notifies :delete, "file[#{passwd_file}]"
notifies :delete, "file[#{passwd_file}]", :delayed
end

file passwd_file do
Expand Down
37 changes: 37 additions & 0 deletions resources/license.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
property :fingerprint, String, default: lazy { node['nexus3']['license_fingerprint'] }
property :license, String, sensitive: true, default: lazy { node['nexus3']['license'] }
property :api_client, ::Nexus3::Api, identity: true, desired_state: false, default: lazy { ::Nexus3::Api.default(node) }

load_current_value do |_desired|
begin
config = api_client.request(:get, 'system/license')
current_value_does_not_exist! if config.nil?
fingerprint config['fingerprint']
# Nexus returns a 402 Payment Required when there is no license installed, we get an ApiError
rescue ::LoadError, ::Nexus3::ApiError => e
::Chef::Log.warn "A '#{e.class}' occured: #{e.message}"
current_value_does_not_exist!
end
end

action :update do
converge_if_changed :fingerprint do
converge_by('Uploading license') do
new_resource.api_client.request(:post, 'system/license', data: new_resource.license, content_type: 'application/octet-stream')
end
end
end

action :delete do
unless current_resource.nil?
converge_by('Unregistering license') do
new_resource.api_client.request(:delete, 'system/license')
end
end
end

action_class do
def whyrun_supported?
true
end
end
2 changes: 1 addition & 1 deletion spec/unit/resource_validation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'

describe 'nexus3_resources_test::default' do
NON_TESTED_RESOURCES = %w[service_systemd service_windows default service_sysvinit].freeze
NON_TESTED_RESOURCES = %w[default license service_systemd service_sysvinit service_windows].freeze
cached(:chef_run) do
::ChefSpec::SoloRunner.new(platform: 'centos', version: CENTOS_VERSION).converge(described_recipe)
end
Expand Down

0 comments on commit c6bb2a7

Please sign in to comment.