Skip to content

Commit

Permalink
Merge pull request ManageIQ#15642 from carbonin/add_complete_marker_f…
Browse files Browse the repository at this point in the history
…or_embedded_ansible_setup

Add a marker file for determining when the ansible setup has been run
  • Loading branch information
gtanzillo committed Jul 24, 2017
2 parents 71d244b + cd863fd commit 9e0fe66
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 9e0fe66

Please sign in to comment.