From a78985505be27df8f164020e7c17a11cf0e1d649 Mon Sep 17 00:00:00 2001 From: Mike van Goor Date: Thu, 23 Oct 2025 17:00:09 +0200 Subject: [PATCH 1/2] Update logic around obtaining the token --- libraries/api.rb | 9 ++++--- metadata.rb | 2 +- resources/fs_subclient.rb | 54 +++++++++++++++++++-------------------- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/libraries/api.rb b/libraries/api.rb index f8006e2..6b93710 100644 --- a/libraries/api.rb +++ b/libraries/api.rb @@ -10,12 +10,15 @@ module CommVault module Api def cv_token_local - token = if platform?('windows') - Mixlib::ShellOut.new('C:\\Progra~1\\Commvault\\ContentStore\\Base\\QLogin.exe -localadmin -gt') + binary = if platform?('windows') + 'C:\\Progra~1\\Commvault\\ContentStore\\Base\\QLogin.exe' else - Mixlib::ShellOut.new('/opt/commvault/Base/qlogin -localadmin -gt') + '/opt/commvault/Base/qlogin' end + raise "Missing qlogin binary at location: (#{binary})" unless File.exist?(binary) + token = Mixlib::ShellOut.new("#{binary} -localadmin -gt") token.run_command + raise "Did not receive a token from the platform" if token.stdout.length <= 10 "QSDK #{token.stdout}" end diff --git a/metadata.rb b/metadata.rb index 980b107..e9d2fe8 100644 --- a/metadata.rb +++ b/metadata.rb @@ -3,7 +3,7 @@ maintainer_email 'int-dms@schubergphilis.com' license 'Apache-2.0' description 'Installs and Configured CommVault client software' -version '1.2.1' +version '1.3.0' chef_version '>= 14.0' issues_url 'https://github.com/sbp-cookbooks/commvault/issues' diff --git a/resources/fs_subclient.rb b/resources/fs_subclient.rb index 96ca53d..ebbbfd9 100644 --- a/resources/fs_subclient.rb +++ b/resources/fs_subclient.rb @@ -46,38 +46,38 @@ end end - api_token = if new_resource.use_local_login - # Get the api token using qlogin --localadmin - cv_token_local - else - # Get the api token using login to the API - cv_token_api(new_resource.endpoint, new_resource.login_user, new_resource.login_pass) - end - - Chef::Log.debug "Token: [#{api_token}]" - - # Try 3 times (with 10 secs interval) to get the client id (SAFETY PRECAUTION) - counter = 0 + api_token = '' + # Try 3 times (with 30 secs interval) to get the client id (SAFETY PRECAUTION) + counter = 1 loop do begin - cv_client_id(new_resource.endpoint, api_token) + api_token = if new_resource.use_local_login + # Get the api token using qlogin --localadmin + cv_token_local + else + # Get the api token using login to the API + cv_token_api(new_resource.endpoint, new_resource.login_user, new_resource.login_pass) + end + break rescue - Chef::Log.warn "Unable to get client id (counter: #{counter}), retrying after sleep of 10 seconds" - sleep(10) + Chef::Log.warn "Unable to get token (counter: #{counter}), retrying after sleep of 30 seconds" + sleep(30) end + counter += 1 - break if counter > 3 - - # Reinitilize authentication to refresh cache - api_token = if new_resource.use_local_login - # Get the api token using qlogin --localadmin - cv_token_local - else - # Get the api token using login to the API - cv_token_api(new_resource.endpoint, new_resource.login_user, new_resource.login_pass) - end - - Chef::Log.debug "Token: [#{api_token}]" + if counter > 8 + Chef::Log.error "Unable to obtain token, bailing" + return + end + end + + Chef::Log.debug "Token: [#{api_token}]" + + begin + cv_client_id(new_resource.endpoint, api_token) + rescue + Chef::Log.warn "Unable to get client id" + return end # Commvault does not "install" the file system agent anymore if the installation is done in restore only mode, so do it here From 85d3c4075d443067e061c32768b14617082cae34 Mon Sep 17 00:00:00 2001 From: Mike van Goor Date: Thu, 23 Oct 2025 17:05:25 +0200 Subject: [PATCH 2/2] Cookstyle fixes --- libraries/api.rb | 16 ++++++++-------- resources/fs_subclient.rb | 4 ++-- resources/install.rb | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/libraries/api.rb b/libraries/api.rb index 6b93710..6afa185 100644 --- a/libraries/api.rb +++ b/libraries/api.rb @@ -11,14 +11,14 @@ module CommVault module Api def cv_token_local binary = if platform?('windows') - 'C:\\Progra~1\\Commvault\\ContentStore\\Base\\QLogin.exe' - else - '/opt/commvault/Base/qlogin' - end + 'C:\\Progra~1\\Commvault\\ContentStore\\Base\\QLogin.exe' + else + '/opt/commvault/Base/qlogin' + end raise "Missing qlogin binary at location: (#{binary})" unless File.exist?(binary) token = Mixlib::ShellOut.new("#{binary} -localadmin -gt") token.run_command - raise "Did not receive a token from the platform" if token.stdout.length <= 10 + raise 'Did not receive a token from the platform' if token.stdout.length <= 10 "QSDK #{token.stdout}" end @@ -29,14 +29,14 @@ def cv_token_api(endpoint, user, pass) response = _post(url, nil, body) raise "Incorrect output received while logging into REST API endpoint #{endpoint}" unless response raise "API gave error code [#{response.code}] for our request to login\nResponse: [#{response.message}]" if response.code.to_i != 200 - _extract(response.body(), 'token') + _extract(response.body, 'token') end def cv_client_name if platform?('windows') registry_get_values('HKLM\SOFTWARE\CommVault Systems\Galaxy\Instance001').select { |x| x[:name].casecmp?('sPhysicalNodeName') }.first[:data] else - File.readlines('/etc/CommVaultRegistry/Galaxy/Instance001/.properties').grep(/sPhysicalNodeName/)[0].chomp.split[1] + File.readlines('/etc/CommVaultRegistry/Galaxy/Instance001/.properties').grep(/sPhysicalNodeName/).first.chomp.split[1] end end @@ -164,7 +164,7 @@ def cv_client_id(endpoint, cv_token) url = URI("#{endpoint}/GetId?clientname=#{cv_client_name}") response = _get(url, cv_token) raise "Unable to get client id for client name [#{cv_client_name}]" unless response && response.code.to_i == 200 - cid = _extract(response.body(), 'clientId') + cid = _extract(response.body, 'clientId') raise "Incorrect client id received from API -> [#{cid}], code: [#{response.code}]" unless cid && cid.match(/^(\d)+$/) cid.to_i end diff --git a/resources/fs_subclient.rb b/resources/fs_subclient.rb index ebbbfd9..33262ae 100644 --- a/resources/fs_subclient.rb +++ b/resources/fs_subclient.rb @@ -66,7 +66,7 @@ counter += 1 if counter > 8 - Chef::Log.error "Unable to obtain token, bailing" + Chef::Log.error 'Unable to obtain token from platform, bailing for this run' return end end @@ -76,7 +76,7 @@ begin cv_client_id(new_resource.endpoint, api_token) rescue - Chef::Log.warn "Unable to get client id" + Chef::Log.warn 'Unable to get client id' return end diff --git a/resources/install.rb b/resources/install.rb index 8d761f3..4fc2cd8 100644 --- a/resources/install.rb +++ b/resources/install.rb @@ -202,7 +202,7 @@ # Delete cache if it exists ruby_block 'Cache delete' do block do - cache_delete() + cache_delete end end