Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for license installation #144

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
40 changes: 36 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,30 @@
end
end

license_file_path = new_resource.properties_variables['nexus.licenseFile']
license_already_installed = !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
jeremy-clerc marked this conversation as resolved.
Show resolved Hide resolved
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 +188,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 +202,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 +215,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