Skip to content

Commit

Permalink
HostSettings specifies storage_roots by hostname for sharding
Browse files Browse the repository at this point in the history
  • Loading branch information
tallenaz committed Mar 30, 2018
1 parent 6a9ea28 commit b58843d
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ task :m2c_exist_single_root, [:storage_root, :profile] => [:environment] do |_t,
exit
end
root = args[:storage_root].to_sym
storage_dir = "#{Settings.moab.storage_roots[root]}/#{Settings.moab.storage_trunk}"
storage_dir = "#{HostSettings.storage_roots[root]}/#{Settings.moab.storage_trunk}"
puts "#{Time.now.utc.iso8601} Running Moab to Catalog Existence Check for #{storage_dir}"
$stdout.flush # sometimes above is not visible (flushed) until last puts (when run finishes)
if args[:profile] == 'profile'
Expand Down Expand Up @@ -127,7 +127,7 @@ task :c2m_check_version_on_dir, [:last_checked_b4_date, :storage_root, :profile]
exit
end
root = args[:storage_root].to_sym
storage_dir = "#{Settings.moab.storage_roots[root]}/#{Settings.moab.storage_trunk}"
storage_dir = "#{HostSettings.storage_roots[root]}/#{Settings.moab.storage_trunk}"
last_checked = args[:last_checked_b4_date].to_s
begin
if args[:profile] == 'profile'
Expand Down
2 changes: 1 addition & 1 deletion app/models/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Endpoint < ApplicationRecord
# NOTE: this adds new entries from the config, and leaves existing entries alone, but won't delete anything.
# TODO: figure out deletion based on config?
def self.seed_storage_root_endpoints_from_config(endpoint_type, preservation_policies)
Settings.moab.storage_roots.map do |storage_root_name, storage_root_location|
HostSettings.storage_roots.map do |storage_root_name, storage_root_location|
find_or_create_by!(endpoint_name: storage_root_name.to_s) do |endpoint|
endpoint.endpoint_type = endpoint_type
endpoint.endpoint_node = Settings.endpoints.storage_root_defaults.endpoint_node
Expand Down
17 changes: 17 additions & 0 deletions app/models/host_settings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Lets us consolidate shared_configs for almost identical machines
class HostSettings
# @return [Config::Options] key/value pairs of name: file_path
def self.storage_roots
Settings.storage_root_map[storage_root_lookup_name]
end

# @return [String] hostname depending on environment
def self.storage_root_lookup_name
Rails.env.production? ? parsed_hostname : 'default'
end

# @return [String] modified hostname
def self.parsed_hostname
Socket.gethostname.underscore.remove('.stanford.edu')
end
end
2 changes: 1 addition & 1 deletion config/initializers/moab_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'moab/stanford'

Moab::Config.configure do
storage_roots(Settings.moab.storage_roots.map { |_storage_root_name, storage_root_location| storage_root_location })
storage_roots(HostSettings.storage_roots.map { |_storage_root_name, storage_root_location| storage_root_location })
storage_trunk(Settings.moab.storage_trunk)
path_method(Settings.moab.path_method.to_sym)
end
13 changes: 9 additions & 4 deletions config/settings/development.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
moab:
# this is a list of named storage roots. the names are used to seed the endpoints table.
storage_roots:
# name: '/location/on/disk' # e.g.
storage_root_map:
default:
fixture_sr1: 'spec/fixtures/storage_root01'
fixture_sr2: 'spec/fixtures/storage_root02'
fixture_sr3: 'spec/fixtures/checksum_root01'
preservation_catalog_prod:
fixture_sr1: 'spec/fixtures/storage_root01'
fixture_sr2: 'spec/fixtures/storage_root02'
fixture_sr3: 'spec/fixtures/checksum_root01'
preservation_catalog_prod_a:
fixture_sr1: 'spec/fixtures/storage_root01'
fixture_sr2: 'spec/fixtures/storage_root02'
workflow_services_url: 'https://sul-lyberservices-test.stanford.edu/workflow/'
13 changes: 9 additions & 4 deletions config/settings/test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
moab:
# this is a list of named storage roots. the names are used to seed the endpoints table.
storage_roots:
# name: '/location/on/disk' # e.g.
storage_root_map:
default:
fixture_sr1: 'spec/fixtures/storage_root01'
fixture_sr2: 'spec/fixtures/storage_root02'
fixture_sr3: 'spec/fixtures/checksum_root01'
fixture_empty: 'spec/fixtures/empty'
preservation_catalog_prod:
fixture_sr1: 'spec/fixtures/storage_root01'
fixture_sr2: 'spec/fixtures/storage_root02'
fixture_sr3: 'spec/fixtures/checksum_root01'
preservation_catalog_prod_a:
fixture_sr1: 'spec/fixtures/storage_root01'
fixture_sr2: 'spec/fixtures/storage_root02'
2 changes: 1 addition & 1 deletion lib/audit/catalog_to_moab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def self.check_version_all_dirs(last_checked_b4_date)
start_msg = "#{Time.now.utc.iso8601} C2M check_version_all_dirs starting"
puts start_msg
Rails.logger.info start_msg
Settings.moab.storage_roots.each do |_strg_root_name, strg_root_location|
HostSettings.storage_roots.each do |_strg_root_name, strg_root_location|
check_version_on_dir(last_checked_b4_date, "#{strg_root_location}/#{Settings.moab.storage_trunk}")
end
end_msg = "#{Time.now.utc.iso8601} C2M check_version_all_dirs ended"
Expand Down
2 changes: 1 addition & 1 deletion lib/audit/checksum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.validate_disk_all_endpoints
start_msg = "#{Time.now.utc.iso8601} CV validate_disk_all_endpoints starting"
puts start_msg
Rails.logger.info start_msg
Settings.moab.storage_roots.each do |strg_root_name, _strg_root_location|
HostSettings.storage_roots.each do |strg_root_name, _strg_root_location|
validate_disk(strg_root_name)
end
end_msg = "#{Time.now.utc.iso8601} CV validate_disk_all_endpoints ended"
Expand Down
4 changes: 2 additions & 2 deletions lib/audit/moab_to_catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def self.seed_catalog_for_all_storage_roots
start_msg = "#{Time.now.utc.iso8601} Seeding for all storage roots starting"
puts start_msg
Rails.logger.info start_msg
Settings.moab.storage_roots.each do |_strg_root_name, strg_root_location|
HostSettings.storage_roots.each do |_strg_root_name, strg_root_location|
seed_catalog_for_dir("#{strg_root_location}/#{Settings.moab.storage_trunk}")
end
end_msg = "#{Time.now.utc.iso8601} Seeding for all storage roots ended'"
Expand All @@ -71,7 +71,7 @@ def self.check_existence_for_all_storage_roots
start_msg = "#{Time.now.utc.iso8601} M2C check_existence for all storage roots starting'"
puts start_msg
Rails.logger.info start_msg
Settings.moab.storage_roots.each do |_strg_root_name, strg_root_location|
HostSettings.storage_roots.each do |_strg_root_name, strg_root_location|
check_existence_for_dir("#{strg_root_location}/#{Settings.moab.storage_trunk}")
end
end_msg = "#{Time.now.utc.iso8601} M2C check_existence for all storage roots ended'"
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/audit/catalog_to_moab_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@
let(:subject) { described_class.check_version_all_dirs(last_checked_version_b4_date) }

it 'calls .check_version_for_dir once per storage root' do
expect(described_class).to receive(:check_version_on_dir).exactly(Settings.moab.storage_roots.entries.count).times
expect(described_class).to receive(:check_version_on_dir).exactly(HostSettings.storage_roots.entries.count).times
subject
end

it 'calls check_version_for_dir with the right arguments' do
Settings.moab.storage_roots.each do |storage_root|
HostSettings.storage_roots.each do |storage_root|
expect(described_class).to receive(:check_version_on_dir).with(
last_checked_version_b4_date,
"#{storage_root[1]}/#{Settings.moab.storage_trunk}"
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/audit/checksum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@
let(:subject) { described_class.validate_disk_all_endpoints }

it 'calls validate_disk once per storage root' do
expect(described_class).to receive(:validate_disk).exactly(Settings.moab.storage_roots.entries.count).times
expect(described_class).to receive(:validate_disk).exactly(HostSettings.storage_roots.entries.count).times
subject
end

it 'calls validate_disk with the right arguments' do
Settings.moab.storage_roots.each_key do |storage_name|
HostSettings.storage_roots.each_key do |storage_name|
expect(described_class).to receive(:validate_disk).with(
storage_name
)
Expand Down
8 changes: 4 additions & 4 deletions spec/lib/audit/moab_to_catalog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
let(:subject) { described_class.check_existence_for_all_storage_roots }

it 'calls check_existence_for_dir once per storage root' do
expect(described_class).to receive(:check_existence_for_dir).exactly(Settings.moab.storage_roots.entries.count).times
expect(described_class).to receive(:check_existence_for_dir).exactly(HostSettings.storage_roots.entries.count).times
subject
end

it 'calls check_existence_for_dir with the right arguments' do
Settings.moab.storage_roots.each do |storage_root|
HostSettings.storage_roots.each do |storage_root|
expect(described_class).to receive(:check_existence_for_dir).with("#{storage_root[1]}/#{Settings.moab.storage_trunk}")
end
subject
Expand All @@ -43,12 +43,12 @@
let(:subject) { described_class.seed_catalog_for_all_storage_roots }

it 'calls seed_catalog_for_dir once per storage root' do
expect(described_class).to receive(:seed_catalog_for_dir).exactly(Settings.moab.storage_roots.entries.count).times
expect(described_class).to receive(:seed_catalog_for_dir).exactly(HostSettings.storage_roots.entries.count).times
subject
end

it 'calls seed_catalog_for_dir with the right arguments' do
Settings.moab.storage_roots.each do |storage_root|
HostSettings.storage_roots.each do |storage_root|
expect(described_class).to receive(:seed_catalog_for_dir).with("#{storage_root[1]}/#{Settings.moab.storage_trunk}")
end
subject
Expand Down
4 changes: 2 additions & 2 deletions spec/load_fixtures_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
end
after do
# TODO: danger - if there are objects in the test db we want to keep
Settings.moab.storage_roots.each_value do |storage_root|
HostSettings.storage_roots.each_value do |storage_root|
storage_dir = File.join(storage_root, Settings.moab.storage_trunk)
PreservedCopy.where(endpoint_id: Endpoint.find_by(storage_location: storage_dir).id).each do |pc|
po_id = pc.preserved_object_id
Expand Down Expand Up @@ -50,7 +50,7 @@ def setup
@storage_dir_to_endpoint_id = {}
# FIXME: I couldn't get .each_value to work ... try again?
# rubocop:disable Performance/HashEachMethods
Settings.moab.storage_roots.each do |_name, storage_root|
HostSettings.storage_roots.each do |_name, storage_root|
storage_dir = File.join(storage_root, Settings.moab.storage_trunk)
@moab_storage_dirs << storage_dir
@storage_dir_to_endpoint_id[storage_dir] = Endpoint.find_by(storage_location: storage_dir).id
Expand Down
4 changes: 2 additions & 2 deletions spec/models/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
let(:default_pres_policies) { [PreservationPolicy.default_policy] }

it 'creates a local online endpoint for each storage root' do
Settings.moab.storage_roots.each do |storage_root_name, storage_root_location|
HostSettings.storage_roots.each do |storage_root_name, storage_root_location|
storage_root_attrs = {
endpoint_type: strg_rt_endpoint_type,
endpoint_node: Settings.endpoints.storage_root_defaults.endpoint_node,
Expand All @@ -89,7 +89,7 @@
fixture_sr2: 'spec/fixtures/storage_root02',
fixture_srTest: 'spec/fixtures/storage_root_unit_test'
)
allow(Settings.moab).to receive(:storage_roots).and_return(storage_roots_setting)
allow(HostSettings).to receive(:storage_roots).and_return(storage_roots_setting)

# run it a second time
Endpoint.seed_storage_root_endpoints_from_config(strg_rt_endpoint_type, default_pres_policies)
Expand Down
31 changes: 31 additions & 0 deletions spec/models/host_settings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rails_helper'

RSpec.describe HostSettings do
describe '.storage_roots' do
it 'gets the correct values' do
expect(described_class).to receive(:storage_root_lookup_name).and_call_original
expect(described_class.storage_roots.to_h).to eq(fixture_sr1: 'spec/fixtures/storage_root01',
fixture_sr2: 'spec/fixtures/storage_root02',
fixture_sr3: 'spec/fixtures/checksum_root01',
fixture_empty: 'spec/fixtures/empty')
end
end

describe '.storage_root_lookup_name' do
it "returns 'default' when not in production" do
expect(described_class.storage_root_lookup_name).to eq('default')
end
it 'returns parsed hostname when in production' do
expect(Rails.env).to receive(:production?).and_return(true)
expect(described_class).to receive(:parsed_hostname).and_return('preservation_catalog_prod_a')
expect(described_class.storage_root_lookup_name).to eq('preservation_catalog_prod_a')
end
end

describe '.parsed_hostname' do
it 'makes hyphens into underscores and trims the domain down' do
expect(Socket).to receive(:gethostname).and_return('preservation-catalog-prod-a.stanford.edu')
expect(described_class.parsed_hostname).to eq('preservation_catalog_prod_a')
end
end
end

0 comments on commit b58843d

Please sign in to comment.