Skip to content

Commit

Permalink
Add a marker file for determining when the ansible setup has been run
Browse files Browse the repository at this point in the history
This ensures that we have a definitive way to determine if we have
successfully run the setup for embedded ansible on a particular server

Before this change, the thread running the setup could be killed
and when we tried to start the service again, we would see #configured?
as true even if we didn't run through the whole playbook.

Now, we will not write this marker file until we finish running
the playbook so the next worker will know to attempt the setup if
the previous one was killed.

https://bugzilla.redhat.com/show_bug.cgi?id=1474427
  • Loading branch information
carbonin committed Jul 24, 2017
1 parent 7faa944 commit cd863fd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
21 changes: 21 additions & 0 deletions lib/embedded_ansible.rb
Expand Up @@ -2,6 +2,7 @@
require "awesome_spawn"
require "linux_admin"
require "ansible_tower_client"
require "fileutils"

class EmbeddedAnsible
ANSIBLE_ROLE = "embedded_ansible".freeze
Expand Down Expand Up @@ -33,7 +34,10 @@ def self.running?

def self.configured?
return true if MiqEnvironment::Command.is_container?

return false unless File.exist?(SECRET_KEY_FILE)
return false unless setup_completed?

key = miq_database.ansible_secret_key
key.present? && key == File.read(SECRET_KEY_FILE)
end
Expand Down Expand Up @@ -149,9 +153,11 @@ def self.run_setup_script(exclude_tags)
}
AwesomeSpawn.run!(SETUP_SCRIPT, :params => params)
end
write_setup_complete_file
rescue AwesomeSpawn::CommandResultError => e
_log.error("EmbeddedAnsible setup script failed with: #{e.message}")
miq_database.ansible_secret_key = nil
FileUtils.rm_f(setup_complete_file)
raise
end
private_class_method :run_setup_script
Expand Down Expand Up @@ -254,4 +260,19 @@ def self.tower_rpm_version
LinuxAdmin::Rpm.info("ansible-tower-server")["version"]
end
private_class_method :tower_rpm_version

def self.write_setup_complete_file
FileUtils.touch(setup_complete_file)
end
private_class_method :write_setup_complete_file

def self.setup_completed?
File.exist?(setup_complete_file)
end
private_class_method :setup_completed?

def self.setup_complete_file
Rails.root.join("tmp", "embedded_ansible_setup_complete")
end
private_class_method :setup_complete_file
end
15 changes: 14 additions & 1 deletion spec/lib/embedded_ansible_spec.rb
Expand Up @@ -242,10 +242,12 @@
end

context "with a key file" do
let(:key_file) { Tempfile.new("SECRET_KEY") }
let(:key_file) { Tempfile.new("SECRET_KEY") }
let(:complete_file) { Tempfile.new("embedded_ansible_setup_complete") }

before do
stub_const("EmbeddedAnsible::SECRET_KEY_FILE", key_file.path)
allow(described_class).to receive(:setup_complete_file).and_return(complete_file.path)
end

after do
Expand All @@ -262,6 +264,17 @@
expect(described_class.configured?).to be true
end

it "returns false when the key is configured but the complete file is missing" do
key = "verysecret"
key_file.write(key)
key_file.close
miq_database.ansible_secret_key = key

complete_file.unlink

expect(described_class.configured?).to be false
end

it "returns false when there is no key in the database" do
key_file.write("asdf")
key_file.close
Expand Down

0 comments on commit cd863fd

Please sign in to comment.