Skip to content

Commit

Permalink
Fixes #24801 - Always seed default taxonomies
Browse files Browse the repository at this point in the history
This is needed so we can enforce enabling taxonomies. If this seed is
run on an existing system, all existing objects will be associated to
the default taxonomy. For systems that already have taxonomies this
seed will remain a noop.
  • Loading branch information
tbrisker committed Nov 27, 2018
1 parent dda9e30 commit 623fa2a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 35 deletions.
2 changes: 0 additions & 2 deletions app/models/taxonomy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class Taxonomy < ApplicationRecord

serialize :ignore_types, Array

belongs_to :user

before_create :assign_default_templates
after_create :assign_taxonomy_to_user
before_validation :sanitize_ignored_types
Expand Down
3 changes: 0 additions & 3 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
config.consider_all_requests_local = false
config.action_controller.perform_caching = true

# Eager load all classes pre-fork for performance
config.eager_load = true

# Enable Rack::Cache to put a simple HTTP cache in front of your application
# Add `rack-cache` to your Gemfile before enabling this.
# For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
Expand Down
41 changes: 21 additions & 20 deletions db/seeds.d/050-taxonomies.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
# Create an initial organization if specified
if SETTINGS[:organizations_enabled] && ENV['SEED_ORGANIZATION'] && !Organization.any?
Organization.without_auditing do
original_user, User.current = User.current, User.anonymous_admin
@organization = Organization.where(:name => ENV['SEED_ORGANIZATION']).first_or_create
User.current = original_user
end
end
# This runs last since we want to associate all objects we can to the default taxonomies

# Create an initial location if specified
if SETTINGS[:locations_enabled] && ENV['SEED_LOCATION'] && !Location.any?
Location.without_auditing do
original_user, User.current = User.current, User.anonymous_admin
@location = Location.where(:name => ENV['SEED_LOCATION']).first_or_create!
User.current = original_user
end
end
# some associations make no sense to set. Default templates are assigned during creation.
skip_associations = [:associated_audits, :audits, :default_users, :hosts,
:location_parameters, :organization_parameters,
:taxable_taxonomies ] + Template.descendants.map {|type| type.to_s.tableize.to_sym}

User.as_anonymous_admin do
[Location, Organization].select(&:none?).each do |taxonomy|
taxonomy.without_auditing do
tax_name = ENV.fetch("SEED_#{taxonomy.to_s.upcase}", "Default #{taxonomy}")
tax = taxonomy.create!(name: tax_name)
associations = taxonomy.reflect_on_all_associations.reject {|assoc| skip_associations.include?(assoc.name)}
associations.each do |association|
tax.send("#{association.name}=", association.klass.all)
end

# Add the initial location to the initial organization to prevent mismatches
# when a host is created that uses them
if @organization && @location && @organization.locations.exclude?(@location)
@organization.locations << @location
# Mass update when we can
tax_id = "#{taxonomy.to_s.parameterize}_id"
Host::Managed.update_all(tax_id => tax.id)
User.update_all("default_#{tax_id}": tax.id)
end
end
end
4 changes: 2 additions & 2 deletions db/seeds.d/100-installation_media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
next if Medium.unscoped.where(['name = ? OR path = ?', input[:name], input[:path]]).any?
next if SeedHelper.audit_modified? Medium, input[:name]
m = Medium.create input
m.organizations = organizations if SETTINGS[:organizations_enabled]
m.locations = locations if SETTINGS[:locations_enabled]
m.organizations = organizations
m.locations = locations
raise "Unable to create medium: #{format_errors m}" if m.nil? || m.errors.any?
end
end
4 changes: 2 additions & 2 deletions lib/seed_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def import_raw_template(contents, vendor = 'Foreman')
t.vendor = vendor

if !t.persisted?
t.organizations = Organization.unscoped.all if SETTINGS[:organizations_enabled] && t.respond_to?(:organizations=)
t.locations = Location.unscoped.all if SETTINGS[:locations_enabled] && t.respond_to?(:locations=)
t.organizations = Organization.unscoped.all if t.respond_to?(:organizations=)
t.locations = Location.unscoped.all if t.respond_to?(:locations=)
raise "Unable to create template #{t.name}: #{format_errors t}" unless t.valid?
else
raise "Unable to update template #{t.name}: #{format_errors t}" unless t.valid?
Expand Down
12 changes: 6 additions & 6 deletions test/unit/tasks/seeds_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,40 +145,40 @@ def seed(*seed_files)
end

test "seed organization when environment SEED_ORGANIZATION specified" do
Organization.stubs(:any?).returns(false)
Organization.stubs(:none?).returns(true)
with_env('SEED_ORGANIZATION' => 'seed_test') do
seed('030-auth_sources.rb', '035-admin.rb', '050-taxonomies.rb')
end
assert Organization.unscoped.find_by_name('seed_test')
end

test "don't seed organization when an org already exists" do
Organization.stubs(:any?).returns(true)
Organization.stubs(:none?).returns(false)
with_env('SEED_ORGANIZATION' => 'seed_test') do
seed('030-auth_sources.rb', '035-admin.rb', '050-taxonomies.rb')
end
refute Organization.unscoped.find_by_name('seed_test')
end

test "seed location when environment SEED_LOCATION specified" do
Location.stubs(:any?).returns(false)
Location.stubs(:none?).returns(true)
with_env('SEED_LOCATION' => 'seed_test') do
seed('030-auth_sources.rb', '035-admin.rb', '050-taxonomies.rb')
end
assert Location.unscoped.find_by_name('seed_test')
end

test "don't seed location when a location already exists" do
Location.stubs(:any?).returns(true)
Location.stubs(:none?).returns(false)
with_env('SEED_LOCATION' => 'seed_test') do
seed('030-auth_sources.rb', '035-admin.rb', '050-taxonomies.rb')
end
refute Location.unscoped.find_by_name('seed_test')
end

test "seeded organization contains seeded location" do
Location.stubs(:any?).returns(false)
Organization.stubs(:any?).returns(false)
Location.stubs(:none?).returns(true)
Organization.stubs(:none?).returns(true)

org_name = 'seed_org'
loc_name = 'seed_loc'
Expand Down

0 comments on commit 623fa2a

Please sign in to comment.