Skip to content

Commit

Permalink
Merge pull request #277 from ailisp/cli-internal-db-disk
Browse files Browse the repository at this point in the history
internal database must be set with dbdisk option in ap and in cli
  • Loading branch information
carbonin authored Oct 13, 2017
2 parents 2c8eb68 + 99937e2 commit 367b50d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
4 changes: 4 additions & 0 deletions lib/gems/pending/appliance_console/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def set_internal_db
:disk => disk_from_string(options[:dbdisk]),
:run_as_evm_server => !options[:standalone]
}.delete_if { |_n, v| v.nil? })
config.check_disk_is_mount_point

# create partition, pv, vg, lv, ext4, update fstab, mount disk
# initdb, relabel log directory for selinux, update configs,
Expand All @@ -212,6 +213,9 @@ def set_internal_db

# enable/start related services
config.post_activation
rescue RuntimeError => e
say e.message
say "Failed to configure internal database"
end

def set_external_db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "pathname"
require "util/postgres_admin"
require "pg"
require "linux_admin"

RAILS_ROOT ||= Pathname.new(__dir__).join("../../../")

Expand Down Expand Up @@ -49,6 +50,7 @@ def activate

def ask_questions
choose_disk
check_disk_is_mount_point
self.run_as_evm_server = !ask_yn?(<<-EOS.gsub!(/^ +/m, ""), "N")
Should this appliance run as a standalone database server?
Expand All @@ -69,6 +71,11 @@ def choose_disk
@disk = ask_for_disk("database disk")
end

def check_disk_is_mount_point
error_message = "The disk for database must be a mount point"
raise error_message unless disk || pg_mount_point?
end

def initialize_postgresql_disk
log_and_feedback(__method__) do
LogicalVolumeManagement.new(:disk => disk,
Expand Down Expand Up @@ -108,7 +115,7 @@ def post_activation
private

def mount_point
Pathname.new(ENV.fetch("APPLIANCE_PG_MOUNT_POINT"))
PostgresAdmin.mount_point
end

def copy_template(src, src_dir = self.class.postgresql_template, dest_dir = PostgresAdmin.data_directory)
Expand All @@ -121,6 +128,10 @@ def copy_template(src, src_dir = self.class.postgresql_template, dest_dir = Post
end
end

def pg_mount_point?
LinuxAdmin::LogicalVolume.mount_point_exists?(mount_point)
end

def run_initdb
AwesomeSpawn.run!("service", :params => {nil => [PostgresAdmin.service_name, "initdb"]})
end
Expand Down
28 changes: 24 additions & 4 deletions spec/appliance_console/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
:interactive => false,
:disk => '/dev/x',
:run_as_evm_server => true)
.and_return(double(:activate => true, :post_activation => true))
.and_return(double(:check_disk_is_mount_point => true, :activate => true, :post_activation => true))
expect(subject.key_configuration).not_to receive(:activate)
subject.run
end
Expand All @@ -61,7 +61,7 @@
:interactive => false,
:disk => 'x',
:run_as_evm_server => true)
.and_return(double(:activate => true, :post_activation => true))
.and_return(double(:check_disk_is_mount_point => true, :activate => true, :post_activation => true))

subject.run
end
Expand All @@ -79,7 +79,7 @@
:interactive => false,
:disk => 'x',
:run_as_evm_server => false)
.and_return(double(:activate => true, :post_activation => true))
.and_return(double(:check_disk_is_mount_point => true, :activate => true, :post_activation => true))
subject.run
end

Expand All @@ -88,7 +88,7 @@
expect_v2_key
expect(subject).to receive(:disk_from_string).and_return('x')
expect(subject).to receive(:say).twice
config_double = double(:activate => false)
config_double = double(:check_disk_is_mount_point => true, :activate => false)
expect(ApplianceConsole::InternalDatabaseConfiguration).to receive(:new)
.with(:region => 1,
:database => 'vmdb_production',
Expand All @@ -103,6 +103,26 @@
subject.run
end

it "should not run activation if internal database not setting in a separate mount point" do
subject.parse(%w(--internal --username user --password pass -r 1))
expect_v2_key
expect(subject).to receive(:disk_from_string).and_return(nil)
expect(subject).to receive(:say).exactly(3).times
config_double = double
expect(ApplianceConsole::InternalDatabaseConfiguration).to receive(:new)
.with(:region => 1,
:database => 'vmdb_production',
:username => 'user',
:password => 'pass',
:interactive => false,
:run_as_evm_server => true)
.and_return(config_double)
expect(config_double).to receive(:check_disk_is_mount_point).and_raise("The disk for database must be a mount point")
expect(config_double).not_to receive(:activation)

subject.run
end

it "should not run post activation if external database activation fails" do
subject.parse(%w(--hostname host --dbname db --username user --password pass -r 1))
expect_v2_key
Expand Down
19 changes: 19 additions & 0 deletions spec/appliance_console/internal_database_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@
@config.choose_disk
end

context "#check_disk_is_mount_point" do
it "not raise error if disk is given" do
expect(@config).to receive(:disk).and_return("/x")
@config.check_disk_is_mount_point
end

it "not raise error if no disk given but mount point for database is really a mount point" do
expect(@config).to receive(:disk).and_return(nil)
expect(@config).to receive(:pg_mount_point?).and_return(true)
@config.check_disk_is_mount_point
end

it "raise error if no disk given and not a mount point" do
expect(@config).to receive(:disk).and_return(nil)
expect(@config).to receive(:pg_mount_point?).and_return(false)
expect { @config.check_disk_is_mount_point }.to raise_error(RuntimeError, "The disk for database must be a mount point")
end
end

it ".postgresql_template" do
allow(PostgresAdmin).to receive_messages(:data_directory => Pathname.new("/var/lib/pgsql/data"))
allow(PostgresAdmin).to receive_messages(:template_directory => Pathname.new("/opt/manageiq/manageiq-appliance/TEMPLATE"))
Expand Down

0 comments on commit 367b50d

Please sign in to comment.