Skip to content

Commit

Permalink
Auto-create and auto-delete servers in zones when podified
Browse files Browse the repository at this point in the history
This is required to manage servers when running in containers.
Without this change a user would be stuck with a single server
and would not be able to make use of the features that zones
provide.
  • Loading branch information
carbonin committed Jan 27, 2020
1 parent eaf2420 commit 5e87435
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
15 changes: 15 additions & 0 deletions app/models/zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class Zone < ApplicationRecord
has_many :containers, :through => :container_managers
virtual_has_many :active_miq_servers, :class_name => "MiqServer"

before_destroy :remove_servers_if_podified
before_destroy :check_zone_in_use_on_destroy
after_create :create_server_if_podified

include AuthenticationMixin

Expand Down Expand Up @@ -241,6 +243,19 @@ def ntp_reload_queue

protected

def remove_servers_if_podified
return unless MiqEnvironment::Command.is_podified?

miq_servers.destroy_all
end

def create_server_if_podified
return unless MiqEnvironment::Command.is_podified?
return if name == "default" || !visible

miq_servers.create!(:name => name)
end

def check_zone_in_use_on_destroy
raise _("cannot delete default zone") if name == "default"
raise _("cannot delete maintenance zone") if self == miq_region.maintenance_zone
Expand Down
53 changes: 51 additions & 2 deletions spec/models/zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@

context "#ntp_reload_queue" do
it "queues a ntp reload for all active servers in the zone" do
expect(MiqEnvironment::Command).to receive(:is_appliance?).and_return(true)
expect(MiqEnvironment::Command).to receive(:is_container?).and_return(false)
allow(MiqEnvironment::Command).to receive(:is_appliance?).and_return(true)
allow(MiqEnvironment::Command).to receive(:is_container?).and_return(false)
zone = FactoryBot.create(:zone)
server_1 = FactoryBot.create(:miq_server, :zone => zone)
FactoryBot.create(:miq_server, :zone => zone, :status => "stopped")
Expand Down Expand Up @@ -224,4 +224,53 @@
zone.destroy!
expect(MiqQueue.where(:zone => zone.name).count).to eq(0)
end

it "doesn't create a server for the zone when not podified" do
zone = Zone.create!(:name => "my_zone", :description => "some zone")
expect(zone.miq_servers.count).to eq(0)
end

it "fails to destroy a zone with servers when not podified" do
MiqRegion.seed
zone = Zone.create!(:name => "my_zone", :description => "some zone")
zone.miq_servers.create!(:name => "my_server")
expect { zone.destroy! }.to raise_error(RuntimeError)
end

context "when podified" do
before do
allow(MiqEnvironment::Command).to receive(:is_podified?).and_return(true)
end

describe ".create" do
it "automatically creates a server" do
zone = Zone.create!(:name => "my_zone", :description => "some zone")
expect(zone.miq_servers.count).to eq(1)

server = zone.miq_servers.first
expect(server.name).to eq("my_zone")
end

it "doesn't create a server for non-visible zones" do
zone = Zone.create!(:name => "my_zone", :description => "some zone", :visible => false)
expect(zone.miq_servers.count).to eq(0)
end

it "doesn't create a server for the default zone" do
zone = Zone.create!(:name => "default", :description => "Default Zone")
expect(zone.miq_servers.count).to eq(0)
end
end



it ".destroy deletes the server in the zone" do
MiqRegion.seed
zone = Zone.create!(:name => "my_zone", :description => "some zone")
server = zone.miq_servers.first
zone.destroy!

expect(MiqServer.find_by(:id => server.id)).to be_nil
end
end
end

0 comments on commit 5e87435

Please sign in to comment.