diff --git a/lib/embedded_ansible/docker_embedded_ansible.rb b/lib/embedded_ansible/docker_embedded_ansible.rb index 7b589387ead..80b51aa6983 100644 --- a/lib/embedded_ansible/docker_embedded_ansible.rb +++ b/lib/embedded_ansible/docker_embedded_ansible.rb @@ -162,16 +162,13 @@ def awx_env database_auth = find_or_create_database_authentication rabbitmq_auth = find_or_create_rabbitmq_authentication - db_host = database_configuration["host"] || docker_bridge_gateway - db_host = docker_bridge_gateway if db_host == "localhost" - [ "SECRET_KEY=#{find_or_create_secret_key}", "DATABASE_NAME=awx", "DATABASE_USER=#{database_auth.userid}", "DATABASE_PASSWORD=#{database_auth.password}", "DATABASE_PORT=#{database_configuration["port"] || 5432}", - "DATABASE_HOST=#{db_host}", + "DATABASE_HOST=#{database_host}", "RABBITMQ_USER=#{rabbitmq_auth.userid}", "RABBITMQ_PASSWORD=#{rabbitmq_auth.password}", "RABBITMQ_HOST=#{rabbitmq_container_name}", @@ -184,6 +181,13 @@ def awx_env ] end + def database_host + db_host = database_configuration["host"] + return db_host if db_host.presence && db_host != "localhost" + + MiqServer.my_server.ipaddress || docker_bridge_gateway + end + def docker_bridge_gateway br = Docker::Network.get("bridge") br.info["IPAM"]["Config"].first["Gateway"] diff --git a/spec/lib/embedded_ansible/docker_embedded_ansible_spec.rb b/spec/lib/embedded_ansible/docker_embedded_ansible_spec.rb index 6afb20d3adb..6151924ac0b 100644 --- a/spec/lib/embedded_ansible/docker_embedded_ansible_spec.rb +++ b/spec/lib/embedded_ansible/docker_embedded_ansible_spec.rb @@ -32,4 +32,55 @@ expect(subject.alive?).to be false end end + + describe "#database_host (private)" do + let(:my_server) { EvmSpecHelper.local_miq_server } + let(:docker_network_settings) do + settings = { "IPAM" => {"Config" => [{"Gateway" => "192.0.2.1"}]}} + double("Docker::Network settings", :info => settings) + end + + it "returns the active record database host when valid" do + expect(subject).to receive(:database_configuration).and_return("host" => "db.example.com") + expect(subject.send(:database_host)).to eq("db.example.com") + end + + context "the database config doesn't list a host" do + before do + expect(subject).to receive(:database_configuration).and_return("dbname" => "testdatabase") + end + + it "returns the server ip when set" do + my_server.update_attributes(:ipaddress => "192.0.2.123") + + expect(subject.send(:database_host)).to eq("192.0.2.123") + end + + it "returns the docker bridge gateway address when there is no server ip set" do + my_server.update_attributes(:ipaddress => nil) + + expect(Docker::Network).to receive(:get).with("bridge").and_return(docker_network_settings) + expect(subject.send(:database_host)).to eq("192.0.2.1") + end + end + + context "the datbase config containes 'host' => 'localhost'" do + before do + expect(subject).to receive(:database_configuration).and_return("host" => "localhost") + end + + it "returns the server ip when set" do + my_server.update_attributes(:ipaddress => "192.0.2.123") + + expect(subject.send(:database_host)).to eq("192.0.2.123") + end + + it "returns the docker bridge gateway address when there is no server ip set" do + my_server.update_attributes(:ipaddress => nil) + + expect(Docker::Network).to receive(:get).with("bridge").and_return(docker_network_settings) + expect(subject.send(:database_host)).to eq("192.0.2.1") + end + end + end end