diff --git a/libraries/api.rb b/libraries/api.rb index f8006e2..6afa185 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') - else - Mixlib::ShellOut.new('/opt/commvault/Base/qlogin -localadmin -gt') - end + binary = if platform?('windows') + '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 "QSDK #{token.stdout}" end @@ -26,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 @@ -161,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/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..33262ae 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 from platform, bailing for this run' + 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 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