Skip to content

Commit

Permalink
Use the built-in service environment variables
Browse files Browse the repository at this point in the history
Before this change we were providing the memcached and ansible
service names as environment variables.

These names are DNS entries that resolve to the already present
values in <SVCNAME>_SERVICE_HOST. If we just use those directly,
we can eliminate some template overhead.

ref: https://docs.openshift.org/latest/dev_guide/environment_variables.html#automatically-added-environment-variables
  • Loading branch information
carbonin committed Sep 20, 2017
1 parent 68fc994 commit 4fe0ec4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/models/embedded_ansible_worker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def provider_url
server = MiqServer.my_server(true)

if MiqEnvironment::Command.is_container?
host = ENV["ANSIBLE_SERVICE_NAME"]
host = ENV["ANSIBLE_SERVICE_HOST"]
path = "/api/v1"
else
host = server.hostname || server.ipaddress
Expand Down
4 changes: 2 additions & 2 deletions lib/embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def self.services

def self.api_connection
if MiqEnvironment::Command.is_container?
host = ENV["ANSIBLE_SERVICE_NAME"]
port = 80
host = ENV["ANSIBLE_SERVICE_HOST"]
port = ENV["ANSIBLE_SERVICE_PORT_HTTP"]
else
host = "localhost"
port = HTTP_PORT
Expand Down
6 changes: 5 additions & 1 deletion lib/miq_memcached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

module MiqMemcached
def self.server_address
ENV["MEMCACHED_SERVER"] || ::Settings.session.memcache_server
if ENV["MEMCACHED_SERVICE_HOST"] && ENV["MEMCACHED_SERVICE_PORT"]
"#{ENV["MEMCACHED_SERVICE_HOST"]}:#{ENV["MEMCACHED_SERVICE_PORT"]}"
else
::Settings.session.memcache_server
end
end

class Error < RuntimeError; end
Expand Down
8 changes: 5 additions & 3 deletions spec/lib/embedded_ansible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,19 @@

describe ".api_connection" do
around do |example|
ENV["ANSIBLE_SERVICE_NAME"] = "ansible-service"
ENV["ANSIBLE_SERVICE_HOST"] = "192.0.2.1"
ENV["ANSIBLE_SERVICE_PORT_HTTP"] = "1234"
example.run
ENV.delete("ANSIBLE_SERVICE_NAME")
ENV.delete("ANSIBLE_SERVICE_HOST")
ENV.delete("ANSIBLE_SERVICE_PORT_HTTP")
end

it "connects to the ansible service when running in a container" do
miq_database.set_ansible_admin_authentication(:password => "adminpassword")
expect(MiqEnvironment::Command).to receive(:is_container?).and_return(true)

expect(AnsibleTowerClient::Connection).to receive(:new).with(
:base_url => "http://ansible-service/api/v1",
:base_url => "http://192.0.2.1:1234/api/v1",
:username => "admin",
:password => "adminpassword",
:verify_ssl => 0
Expand Down
6 changes: 3 additions & 3 deletions spec/models/embedded_ansible_worker/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@
end

around do |example|
ENV["ANSIBLE_SERVICE_NAME"] = "ansible-service"
ENV["ANSIBLE_SERVICE_HOST"] = "192.0.2.1"
example.run
ENV.delete("ANSIBLE_SERVICE_NAME")
ENV.delete("ANSIBLE_SERVICE_HOST")
end

it "creates the provider with the service name for the URL" do
Expand All @@ -84,7 +84,7 @@

provider = ManageIQ::Providers::EmbeddedAnsible::Provider.first
expect(provider.zone).to eq(miq_server.zone)
expect(provider.default_endpoint.url).to eq("https://ansible-service/api/v1")
expect(provider.default_endpoint.url).to eq("https://192.0.2.1/api/v1")
userid, password = provider.auth_user_pwd
expect(userid).to eq("admin")
expect(password).to eq("secret")
Expand Down

0 comments on commit 4fe0ec4

Please sign in to comment.