diff --git a/app/models/container_node.rb b/app/models/container_node.rb index 7985e422c9a..1a01a917b9b 100644 --- a/app/models/container_node.rb +++ b/app/models/container_node.rb @@ -6,6 +6,7 @@ class ContainerNode < ApplicationRecord include TenantIdentityMixin include SupportsFeatureMixin include ArchivedMixin + include CockpitMixin include_concern 'Purging' EXTERNAL_LOGGING_PATH = "/#/discover?_g=()&_a=(columns:!(hostname,level,kubernetes.pod_name,message),filters:!((meta:(disabled:!f,index:'%{index}',key:hostname,negate:!f),%{query})),index:'%{index}',interval:auto,query:(query_string:(analyze_wildcard:!t,query:'*')),sort:!(time,desc))".freeze @@ -81,13 +82,7 @@ def kubernetes_hostname def cockpit_url URI::HTTP.build(:host => kubernetes_hostname, :port => 9090) address = kubernetes_hostname || name - miq_server = ext_management_system.nil? ? nil : ext_management_system.zone.remote_cockpit_ws_miq_server - if miq_server - - end - MiqCockpit::WS.url(miq_server, - MiqCockpitWsWorker.fetch_worker_settings_from_server(miq_server), - address) + MiqCockpit::WS.url(cockpit_server, cockpit_worker, address) end def evaluate_alert(_alert_id, _event) diff --git a/app/models/mixins/cockpit_mixin.rb b/app/models/mixins/cockpit_mixin.rb new file mode 100644 index 00000000000..a91cb1a4070 --- /dev/null +++ b/app/models/mixins/cockpit_mixin.rb @@ -0,0 +1,10 @@ +module CockpitMixin + extend ActiveSupport::Concern + def cockpit_server + ext_management_system.try(:zone).try(:remote_cockpit_ws_miq_server) + end + + def cockpit_worker + cockpit_server.nil? ? nil : MiqCockpitWsWorker.fetch_worker_settings_from_server(cockpit_server) + end +end diff --git a/app/models/vm/operations.rb b/app/models/vm/operations.rb index f53c406d525..0599c16b717 100644 --- a/app/models/vm/operations.rb +++ b/app/models/vm/operations.rb @@ -1,6 +1,8 @@ module Vm::Operations extend ActiveSupport::Concern + include CockpitMixin + include_concern 'Guest' include_concern 'Power' include_concern 'Lifecycle' @@ -15,14 +17,16 @@ module Vm::Operations def cockpit_url return if ipaddresses.blank? - miq_server = ext_management_system.nil? ? nil : ext_management_system.zone.remote_cockpit_ws_miq_server - MiqCockpit::WS.url(miq_server, - MiqCockpitWsWorker.fetch_worker_settings_from_server(miq_server), - ipv4_address || ipaddresses.first) + MiqCockpit::WS.url(cockpit_server, cockpit_worker, ipv4_address || ipaddresses.first) end def ipv4_address - ipaddresses.find { |ip| IPAddr.new(ip).ipv4? } + return public_address unless public_address.nil? + ipaddresses.find { |ip| IPAddr.new(ip).ipv4? && !ip.starts_with?('192') } + end + + def public_address + ipaddresses.find { |ip| !Addrinfo.tcp(ip, 80).ipv4_private? && IPAddr.new(ip).ipv4? } end def validate_collect_running_processes diff --git a/spec/models/vm/operations_spec.rb b/spec/models/vm/operations_spec.rb index e7ffd6f3f2d..68c81182107 100644 --- a/spec/models/vm/operations_spec.rb +++ b/spec/models/vm/operations_spec.rb @@ -1,25 +1,62 @@ describe 'VM::Operations' do before(:each) do - miq_server = EvmSpecHelper.local_miq_server - @ems = FactoryGirl.create(:ems_vmware, :zone => miq_server.zone) - @vm = FactoryGirl.create(:vm_vmware, :ems_id => @ems.id) - allow(@vm).to receive(:ipaddresses).and_return(@ipaddresses) + @miq_server = EvmSpecHelper.local_miq_server + @ems = FactoryGirl.create(:ems_vmware, :zone => @miq_server.zone) + @vm = FactoryGirl.create(:vm_vmware, :ems_id => @ems.id) + ipaddresses = %w(fe80::21a:4aff:fe22:dde5 127.0.0.1) + allow(@vm).to receive(:ipaddresses).and_return(ipaddresses) + + @hardware = FactoryGirl.create(:hardware) + @hardware.ipaddresses << '10.142.0.2' + @hardware.ipaddresses << '35.190.140.48' end context '#cockpit_url' do it '#returns a valid Cockpit url' do - @ipaddresses = %w(fe80::21a:4aff:fe22:dde5 127.0.0.1) - expect(@vm).to receive(:cockpit_url).and_return('http://127.0.0.1:9090') - @vm.send(:cockpit_url) + url = @vm.send(:cockpit_url) + expect(url).to eq(URI::HTTP.build(:host => "127.0.0.1", :port => 9090)) end end context '#ipv4_address' do - after(:each) { @vm.send(:return_ipv4_address) } - it 'returns the existing ipv4 address' do - @ipaddresses = %w(fe80::21a:4aff:fe22:dde5 127.0.0.1) - expect(@vm).to receive(:return_ipv4_address).and_return('127.0.0.1') + url = @vm.send(:ipv4_address) + expect(url).to eq('127.0.0.1') + end + + context 'cloud providers' do + before(:each) { @ipaddresses = %w(10.10.1.121 35.190.140.48) } + it 'returns the public ipv4 address for AWS' do + ems = FactoryGirl.create(:ems_google, :project => 'manageiq-dev') + az = FactoryGirl.create(:availability_zone_google) + vm = FactoryGirl.create(:vm_google, + :ext_management_system => ems, + :ems_ref => 123, + :availability_zone => az, + :hardware => @hardware) + allow(vm).to receive(:ipaddresses).and_return(@ipaddresses) + url = vm.send(:ipv4_address) + expect(url).to eq('35.190.140.48') + end + + it 'returns the public ipv4 address for GCE' do + ems = FactoryGirl.create(:ems_amazon) + vm = FactoryGirl.create(:vm_amazon, :ext_management_system => ems, :hardware => @hardware) + allow(vm).to receive(:ipaddresses).and_return(@ipaddresses) + url = vm.send(:ipv4_address) + expect(url).to eq('35.190.140.48') + end + end + end + + context '#public_address' do + it 'returns a public ipv4 address' do + ipaddresses = %w(10.10.1.121 35.190.140.48) + ems = FactoryGirl.create(:ems_amazon) + vm = FactoryGirl.create(:vm_amazon, :ext_management_system => ems, :hardware => @hardware) + allow(vm).to receive(:ipaddresses).and_return(ipaddresses) + url = vm.send(:public_address) + expect(url).to eq('35.190.140.48') end end end