Skip to content

Commit

Permalink
Merge pull request #20759 from djberg96/better_check_connection
Browse files Browse the repository at this point in the history
Add git remote connection check code

(cherry picked from commit 41f40f8)

https://bugzilla.redhat.com/show_bug.cgi?id=1835226
  • Loading branch information
kbrock authored and simaishi committed Nov 9, 2020
1 parent 558a27f commit 0ce905f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
23 changes: 23 additions & 0 deletions app/models/git_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ def self.delete_repo_dir(id, directory_name)
FileUtils.rm_rf(directory_name)
end

# Ping the git repository endpoint to verify that the network connection is still up.
# We use this approach for now over Rugged#check_connection because of a segfault:
#
# https://github.com/libgit2/rugged/issues/859
#
def check_connection?
require 'net/ping/external'

# URI library cannot handle git urls, so just convert it to a standard url.
temp_url = url.dup.to_s
temp_url = temp_url.sub(':', '/').sub('git@', 'https://') if temp_url.start_with?('git@')

host = URI.parse(temp_url).hostname

$log.debug("pinging '#{host}' to verify network connection")
ext_ping = Net::Ping::External.new(host)

result = ext_ping.ping?
$log.debug("ping failed: #{ext_ping.exception}") unless result

result
end

def refresh
update_repo
transaction do
Expand Down
6 changes: 5 additions & 1 deletion app/models/service_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class ServiceAnsiblePlaybook < ServiceGeneric
include AnsibleExtraVarsMixin
include AnsiblePlaybookMixin

delegate :playbook, :to => :service_template, :allow_nil => true
delegate :playbook, :repository, :to => :service_template, :allow_nil => true

# A chance for taking options from automate script to override options from a service dialog
def preprocess(action, add_options = {})
Expand All @@ -18,6 +18,10 @@ def execute(action)
launch_ansible_job_queue(action)
end

def check_connection(action)
repository(action).check_connection?
end

def launch_ansible_job_queue(action)
task_opts = {
:action => "Launching Ansible Job",
Expand Down
4 changes: 4 additions & 0 deletions app/models/service_template_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def playbook(action)
ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Playbook.find(config_info[action.downcase.to_sym][:playbook_id])
end

def repository(action)
GitRepository.find(config_info[action.downcase.to_sym][:repository_id])
end

def update_catalog_item(options, auth_user = nil)
config_info = validate_update_config_info(options)
unless config_info
Expand Down
44 changes: 44 additions & 0 deletions spec/models/git_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -345,5 +345,49 @@
end
end
end

context "check_connection?" do
require 'net/ping/external'
let(:ext_ping) { instance_double(Net::Ping::External) }

before do
allow(Net::Ping::External).to receive(:new).and_return(ext_ping)
allow(ext_ping).to receive(:exception)
end

it "returns true if it can ping the repo" do
allow(ext_ping).to receive(:ping?).and_return(true)
expect($log).to receive(:debug).with(/pinging '.*' to verify network connection/)
expect(repo.check_connection?).to eq(true)
end

it "returns false if it cannot ping the repo" do
allow(ext_ping).to receive(:ping?).and_return(false)
expect($log).to receive(:debug).with(/pinging '.*' to verify network connection/)
expect($log).to receive(:debug).with(/ping failed: .*/)
expect(repo.check_connection?).to eq(false)
end

it "handles git urls without issue" do
allow(repo).to receive(:url).and_return("git@example.com:ManageIQ/manageiq.git")
allow(ext_ping).to receive(:ping?).and_return(true)
expect($log).to receive(:debug).with(/pinging 'example.com' to verify network connection/)
expect(repo.check_connection?).to eq(true)
end

it "handles ssh urls without issue" do
allow(repo).to receive(:url).and_return("ssh://user@example.com:443/manageiq.git")
allow(ext_ping).to receive(:ping?).and_return(true)
expect($log).to receive(:debug).with(/pinging 'example.com' to verify network connection/)
expect(repo.check_connection?).to eq(true)
end

it "handles file urls without issue" do
allow(repo).to receive(:url).and_return("file://example.com/server/manageiq.git")
allow(ext_ping).to receive(:ping?).and_return(true)
expect($log).to receive(:debug).with(/pinging 'example.com' to verify network connection/)
expect(repo.check_connection?).to eq(true)
end
end
end
end

0 comments on commit 0ce905f

Please sign in to comment.