Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only run the setup playbook the first time we start embedded ansible #15225

Merged
merged 2 commits into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app/models/embedded_ansible_worker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def before_exit(*_)
def setup_ansible
raise_role_notification(:role_activate_start)

_log.info("calling EmbeddedAnsible.configure")
EmbeddedAnsible.configure unless EmbeddedAnsible.configured?

_log.info("calling EmbeddedAnsible.start")
EmbeddedAnsible.start
_log.info("calling EmbeddedAnsible.start finished")
Expand Down
29 changes: 13 additions & 16 deletions lib/embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
require "ansible_tower_client"

class EmbeddedAnsible
ANSIBLE_ROLE = "embedded_ansible".freeze
SETUP_SCRIPT = "ansible-tower-setup".freeze
SECRET_KEY_FILE = "/etc/tower/SECRET_KEY".freeze
CONFIGURE_EXCLUDE_TAGS = "packages,migrations,firewall,supervisor".freeze
START_EXCLUDE_TAGS = "packages,migrations,firewall".freeze
HTTP_PORT = 54_321
HTTPS_PORT = 54_322
WAIT_FOR_ANSIBLE_SLEEP = 1.second
ANSIBLE_ROLE = "embedded_ansible".freeze
SETUP_SCRIPT = "ansible-tower-setup".freeze
SECRET_KEY_FILE = "/etc/tower/SECRET_KEY".freeze
EXCLUDE_TAGS = "packages,migrations,firewall".freeze
HTTP_PORT = 54_321
HTTPS_PORT = 54_322
WAIT_FOR_ANSIBLE_SLEEP = 1.second

def self.available?
return false unless MiqEnvironment::Command.is_appliance?
Expand Down Expand Up @@ -43,15 +42,13 @@ def self.alive?
true
end

def self.configure
configure_secret_key
run_setup_script(CONFIGURE_EXCLUDE_TAGS)
stop
end

def self.start
configure_secret_key
run_setup_script(START_EXCLUDE_TAGS)
if configured?
services.each { |service| LinuxAdmin::Service.new(service).start.enable }
else
configure_secret_key
run_setup_script(EXCLUDE_TAGS)
end

5.times do
return if alive?
Expand Down
85 changes: 33 additions & 52 deletions spec/lib/embedded_ansible_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@
described_class.disable
end
end

describe ".start when configured" do
before do
stub_const("EmbeddedAnsible::WAIT_FOR_ANSIBLE_SLEEP", 0)

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

expect(nginx_service).to receive(:start).and_return(nginx_service)
expect(supervisord_service).to receive(:start).and_return(supervisord_service)
expect(rabbitmq_service).to receive(:start).and_return(rabbitmq_service)

expect(nginx_service).to receive(:enable).and_return(nginx_service)
expect(supervisord_service).to receive(:enable).and_return(supervisord_service)
expect(rabbitmq_service).to receive(:enable).and_return(rabbitmq_service)
end

it "waits for Ansible to respond" do
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(described_class).to receive(:alive?).exactly(5).times.and_return(false)

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

context "with an miq_databases row" do
Expand Down Expand Up @@ -224,10 +252,11 @@
end
end

describe ".configure" do
describe ".start when not configured" do
before do
expect(described_class).to receive(:configured?).and_return(false)
expect(described_class).to receive(:configure_secret_key)
expect(described_class).to receive(:stop)
expect(described_class).to receive(:alive?).and_return(true)
end

it "generates new passwords with no passwords set" do
Expand All @@ -239,7 +268,7 @@
expect(script_path).to eq("ansible-tower-setup")
expect(params["--"]).to be_nil
expect(params[:extra_vars=]).to eq(extra_vars)
expect(params[:skip_tags=]).to eq("packages,migrations,firewall,supervisor")
expect(params[:skip_tags=]).to eq("packages,migrations,firewall")

new_admin_auth = miq_database.ansible_admin_authentication
new_rabbit_auth = miq_database.ansible_rabbitmq_authentication
Expand All @@ -251,45 +280,14 @@
expect(inventory_file_contents).to include("pg_password='databasepassword'")
end

described_class.configure
described_class.start
end

it "uses the existing passwords when they are set in the database" 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(AwesomeSpawn).to receive(:run!) do |script_path, options|
params = options[:params]
inventory_file_contents = File.read(params[:inventory=])

expect(script_path).to eq("ansible-tower-setup")
expect(params["--"]).to be_nil
expect(params[:extra_vars=]).to eq(extra_vars)
expect(params[:skip_tags=]).to eq("packages,migrations,firewall,supervisor")

expect(inventory_file_contents).to include("admin_password='adminpassword'")
expect(inventory_file_contents).to include("rabbitmq_username='rabbituser'")
expect(inventory_file_contents).to include("rabbitmq_password='rabbitpassword'")
expect(inventory_file_contents).to include("pg_username='databaseuser'")
expect(inventory_file_contents).to include("pg_password='databasepassword'")
end

described_class.configure
end
end

describe ".start" 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 @@ -305,26 +303,9 @@
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
20 changes: 0 additions & 20 deletions spec/models/embedded_ansible_worker/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,7 @@
NotificationType.seed
end

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

it "creates a notification to inform the user that the service has started" do
expect(EmbeddedAnsible).to receive(:configured?).and_return(true)
expect(EmbeddedAnsible).to receive(:start)

runner.setup_ansible
Expand All @@ -104,7 +85,6 @@
end

it "creates a notification to inform the user that the role has been assigned" do
expect(EmbeddedAnsible).to receive(:configured?).and_return(true)
expect(EmbeddedAnsible).to receive(:start)

runner.setup_ansible
Expand Down