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 as the license will be directly loaded.
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
about the license file being updated if a license is
already installed.
  • Loading branch information
jeremy-clerc committed Apr 19, 2024
1 parent ee404d5 commit 94372f6
Show file tree
Hide file tree
Showing 5 changed files with 79 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
39 changes: 35 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,6 +142,29 @@
end
end

license_file_path = new_resource.properties_variables['nexus.licenseFile']

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 :update, 'nexus3_license[update installed license]', :delayed if license_already_installed
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
Expand All @@ -162,9 +187,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 +201,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 All @@ -189,6 +214,12 @@
config new_resource.outbound_proxy unless new_resource.outbound_proxy.nil?
api_client(lazy { ::Nexus3::Api.local(port, 'admin', new_resource.nexus3_password) })
end

nexus3_license 'update installed license' do
action :nothing
fingerprint new_resource.license_fingerprint
license new_resource.license
end
end

action_class do
Expand Down
39 changes: 39 additions & 0 deletions resources/license.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 JSON.parse(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: ::Base64.decode64(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 94372f6

Please sign in to comment.