Skip to content

Commit

Permalink
Merge pull request #13802 from carbonin/add_alive_method_to_embedded_…
Browse files Browse the repository at this point in the history
…ansible

Add an .alive? method to the EmbeddedAnsible class
  • Loading branch information
jrafanie authored Feb 8, 2017
2 parents eee805b + 2b58858 commit 943ecd4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
29 changes: 25 additions & 4 deletions lib/embedded_ansible.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "securerandom"
require "awesome_spawn"
require "linux_admin"
require "ansible_tower_client"

class EmbeddedAnsible
APPLIANCE_ANSIBLE_DIRECTORY = "/opt/ansible-installer".freeze
Expand Down Expand Up @@ -30,6 +31,16 @@ def self.configured?
key.present? && key == File.read(SECRET_KEY_FILE)
end

def self.alive?
return false unless configured? && running?
begin
api_connection.api.verify_credentials
rescue Faraday::ConnectionFailed, Faraday::SSLError, AnsibleTowerClient::ConnectionError, AnsibleTowerClient::ClientError
return false
end
true
end

def self.configure
configure_secret_key
run_setup_script(playbook_extra_variables.merge(:k => CONFIGURE_EXCLUDE_TAGS))
Expand Down Expand Up @@ -104,8 +115,8 @@ def self.generate_rabbitmq_authentication

def self.generate_database_authentication
auth = miq_database.set_ansible_database_authentication(:password => generate_password)
connection.select_value("CREATE ROLE #{connection.quote_column_name(auth.userid)} WITH LOGIN PASSWORD #{connection.quote(auth.password)}")
connection.select_value("CREATE DATABASE awx OWNER #{connection.quote_column_name(auth.userid)} ENCODING 'utf8'")
database_connection.select_value("CREATE ROLE #{database_connection.quote_column_name(auth.userid)} WITH LOGIN PASSWORD #{database_connection.quote(auth.password)}")
database_connection.select_value("CREATE DATABASE awx OWNER #{database_connection.quote_column_name(auth.userid)} ENCODING 'utf8'")
auth
end
private_class_method :generate_database_authentication
Expand Down Expand Up @@ -153,8 +164,18 @@ def self.generate_password
end
private_class_method :generate_password

def self.connection
def self.database_connection
ActiveRecord::Base.connection
end
private_class_method :connection
private_class_method :database_connection

def self.api_connection
admin_auth = miq_database.ansible_admin_authentication
AnsibleTowerClient::Connection.new(
:base_url => URI::HTTP.build(:host => "localhost", :path => "/api/v1", :port => NGINX_HTTP_PORT).to_s,
:username => admin_auth.userid,
:password => admin_auth.password
)
end
private_class_method :api_connection
end
66 changes: 65 additions & 1 deletion spec/lib/embedded_ansible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,70 @@
EvmSpecHelper.create_guid_miq_server_zone
end

describe ".alive?" do
it "returns false if the service is not configured" do
expect(described_class).to receive(:configured?).and_return false
expect(described_class.alive?).to be false
end

it "returns false if the service is not running" do
expect(described_class).to receive(:configured?).and_return true
expect(described_class).to receive(:running?).and_return false
expect(described_class.alive?).to be false
end

context "when a connection is attempted" do
let(:api_conn) { double("AnsibleAPIConnection") }
let(:api) { double("AnsibleAPIResource") }

before do
expect(described_class).to receive(:configured?).and_return true
expect(described_class).to receive(:running?).and_return true

miq_database.set_ansible_admin_authentication(:password => "adminpassword")

expect(AnsibleTowerClient::Connection).to receive(:new).with(
:base_url => "http://localhost:54321/api/v1",
:username => "admin",
:password => "adminpassword"
).and_return(api_conn)
expect(api_conn).to receive(:api).and_return(api)
end

it "returns false when a Faraday::ConnectionFailed is raised" do
error = Faraday::ConnectionFailed.new("error")
expect(api).to receive(:verify_credentials).and_raise(error)
expect(described_class.alive?).to be false
end

it "returns false when a Faraday::SSLError is raised" do
error = Faraday::SSLError.new("error")
expect(api).to receive(:verify_credentials).and_raise(error)
expect(described_class.alive?).to be false
end

it "returns false when an AnsibleTowerClient::ConnectionError is raised" do
expect(api).to receive(:verify_credentials).and_raise(AnsibleTowerClient::ConnectionError)
expect(described_class.alive?).to be false
end

it "returns false when an AnsibleTowerClient::ClientError is raised" do
expect(api).to receive(:verify_credentials).and_raise(AnsibleTowerClient::ClientError)
expect(described_class.alive?).to be false
end

it "raises when other errors are raised" do
expect(api).to receive(:verify_credentials).and_raise(RuntimeError)
expect { described_class.alive? }.to raise_error(RuntimeError)
end

it "returns true when no error is raised" do
expect(api).to receive(:verify_credentials)
expect(described_class.alive?).to be true
end
end
end

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

Expand Down Expand Up @@ -245,7 +309,7 @@
end

it "creates the database" do
allow(described_class).to receive(:connection).and_return(connection)
allow(described_class).to receive(:database_connection).and_return(connection)
expect(described_class).to receive(:generate_password).and_return(password)
expect(connection).to receive(:select_value).with("CREATE ROLE \"awx\" WITH LOGIN PASSWORD #{quoted_password}")
expect(connection).to receive(:select_value).with("CREATE DATABASE awx OWNER \"awx\" ENCODING 'utf8'")
Expand Down

0 comments on commit 943ecd4

Please sign in to comment.