Skip to content

Commit

Permalink
Make sure EmbeddedAnsible.alive? == true after setup
Browse files Browse the repository at this point in the history
Without this we were we were getting "502 Bad Gateway" errors when
trying to remove the demo data through API calls to the embedded
ansible instance.  This was because even though the setup playbook
finished, the server would take just a bit longer to start to serve
requests.
  • Loading branch information
carbonin committed Mar 20, 2017
1 parent f9ae577 commit 5d2d5df
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
10 changes: 10 additions & 0 deletions lib/embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class EmbeddedAnsible
START_EXCLUDE_TAGS = "packages,migrations,firewall".freeze
HTTP_PORT = 54_321
HTTPS_PORT = 54_322
WAIT_FOR_ANSIBLE_SLEEP = 1.second

def self.available?
path = ENV["APPLIANCE_ANSIBLE_DIRECTORY"] || APPLIANCE_ANSIBLE_DIRECTORY
Expand Down Expand Up @@ -50,6 +51,15 @@ def self.configure
def self.start
configure_secret_key
run_setup_script(START_EXCLUDE_TAGS)

5.times do
return if alive?

_log.info("Waiting for EmbeddedAnsible to respond")
sleep WAIT_FOR_ANSIBLE_SLEEP
end

raise "EmbeddedAnsible service is not responding after setup"
end

def self.stop
Expand Down
23 changes: 22 additions & 1 deletion spec/lib/embedded_ansible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,16 @@
end

describe ".start" do
it "runs the setup script with the correct args" do
before do
miq_database.set_ansible_admin_authentication(:password => "adminpassword")
miq_database.set_ansible_rabbitmq_authentication(:userid => "rabbituser", :password => "rabbitpassword")
miq_database.set_ansible_database_authentication(:userid => "databaseuser", :password => "databasepassword")

expect(described_class).to receive(:configure_secret_key)
stub_const("EmbeddedAnsible::WAIT_FOR_ANSIBLE_SLEEP", 0)
end

it "runs the setup script with the correct args" do
expect(AwesomeSpawn).to receive(:run!) do |script_path, options|
params = options[:params]
inventory_file_contents = File.read(params[:inventory=])
Expand All @@ -296,9 +300,26 @@
expect(inventory_file_contents).to include("pg_username='databaseuser'")
expect(inventory_file_contents).to include("pg_password='databasepassword'")
end
expect(described_class).to receive(:alive?).and_return(true)

described_class.start
end

it "waits for Ansible to respond" do
expect(AwesomeSpawn).to receive(:run!)

expect(described_class).to receive(:alive?).exactly(3).times.and_return(false, false, true)

described_class.start
end

it "raises if Ansible doesn't respond" do
expect(AwesomeSpawn).to receive(:run!)

expect(described_class).to receive(:alive?).exactly(5).times.and_return(false)

expect { described_class.start }.to raise_error(RuntimeError)
end
end

describe ".generate_database_authentication (private)" do
Expand Down
21 changes: 20 additions & 1 deletion spec/models/embedded_ansible_worker/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@
expect(provider.default_endpoint.url).to eq("https://boringserver/ansibleapi/v1")
end
end

context "#setup_ansible" do
it "configures EmbeddedAnsible if it is not configured" do
expect(EmbeddedAnsible).to receive(:start)

expect(EmbeddedAnsible).to receive(:configured?).and_return(false)
expect(EmbeddedAnsible).to receive(:configure)

runner.setup_ansible
end

it "doesn't call configure if EmbeddedAnsible is already configured" do
expect(EmbeddedAnsible).to receive(:start)

expect(EmbeddedAnsible).to receive(:configured?).and_return(true)
expect(EmbeddedAnsible).not_to receive(:configure)

runner.setup_ansible
end
end
end
end

0 comments on commit 5d2d5df

Please sign in to comment.