Skip to content

Commit

Permalink
Merge pull request #16621 from carbonin/use_server_ip_for_awx_databas…
Browse files Browse the repository at this point in the history
…e_host

Use the server IP for the AWX database host when the rails config is no good
  • Loading branch information
bdunne authored Dec 8, 2017
2 parents 514ce3e + a8caa1f commit 5842a94
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/embedded_ansible/docker_embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand All @@ -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"]
Expand Down
51 changes: 51 additions & 0 deletions spec/lib/embedded_ansible/docker_embedded_ansible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5842a94

Please sign in to comment.