Skip to content

Commit

Permalink
Add priority to the EmbeddedAnsible subclasses
Browse files Browse the repository at this point in the history
This sorts the subclasses and instantiates the first available one
  • Loading branch information
carbonin committed Nov 28, 2017
1 parent d934730 commit e42f543
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.new
end

def self.detect_available_platform
subclasses.detect(&:available?) || NullEmbeddedAnsible
subclasses.sort.detect(&:available?) || NullEmbeddedAnsible
end

def self.available?
Expand All @@ -21,6 +21,14 @@ def self.enabled?
MiqServer.my_server(true).has_active_role?(ANSIBLE_ROLE)
end

def self.priority
0
end

def self.<=>(other_embedded_ansible)
other_embedded_ansible.priority <=> priority
end

def alive?
return false unless configured? && running?
begin
Expand Down
4 changes: 4 additions & 0 deletions lib/embedded_ansible/appliance_embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def self.available?
required_rpms.subset?(LinuxAdmin::Rpm.list_installed.keys.to_set)
end

def self.priority
30
end

def initialize
require "linux_admin"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/embedded_ansible/container_embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ def self.available?
ContainerOrchestrator.available?
end

def self.priority
20
end

def start
miq_database.set_ansible_admin_authentication(:password => ENV["ANSIBLE_ADMIN_PASSWORD"])
ContainerOrchestrator.new.scale(ANSIBLE_DC_NAME, 1)
Expand Down
4 changes: 4 additions & 0 deletions lib/embedded_ansible/docker_embedded_ansible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def self.available?
false
end

def self.priority
10
end

def initialize
super
require 'docker'
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/embedded_ansible_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require 'docker'

describe EmbeddedAnsible do
describe ".<=>" do
it "allows classes to be sorted by priority" do
expect(EmbeddedAnsible.subclasses.sort).to eq([ApplianceEmbeddedAnsible, ContainerEmbeddedAnsible, DockerEmbeddedAnsible, NullEmbeddedAnsible])
end
end

context "with no available subclass" do
before do
expect(MiqEnvironment::Command).to receive(:is_appliance?).and_return(false)
Expand Down

0 comments on commit e42f543

Please sign in to comment.