Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the server IP for the AWX database host when the rails config is no good #16621

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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