Class: ForemanSeeder

Inherits:
Object show all
Defined in:
app/services/foreman_seeder.rb

Constant Summary collapse

FOREMAN_INTERNAL_KEY =
'database_seed'.freeze
ADVISORY_LOCK =
'database_seed'.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeForemanSeeder

Returns a new instance of ForemanSeeder.


13
14
15
16
# File 'app/services/foreman_seeder.rb', line 13

def initialize
  @seeds = (foreman_seeds + plugin_seeds).sort_by { |seed| seed.split("/").last }
  @hashed_files = @seeds + templates
end

Class Attribute Details

.is_seedingObject

Returns the value of attribute is_seeding


10
11
12
# File 'app/services/foreman_seeder.rb', line 10

def is_seeding
  @is_seeding
end

Instance Attribute Details

#seedsObject (readonly)

Returns the value of attribute seeds


7
8
9
# File 'app/services/foreman_seeder.rb', line 7

def seeds
  @seeds
end

Instance Method Details

#executeObject


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/foreman_seeder.rb', line 37

def execute
  Foreman::AdvisoryLockManager.with_transaction_lock(ADVISORY_LOCK) do
    # if we had to wait for the lock it is likely that the seeding has already been done, no need to seed again
    return unless hash_changed?

    self.class.is_seeding = true
    begin
      @seeds.each do |seed|
        Rails.logger.info("Seeding #{seed}") unless Rails.env.test?

        admin = User.unscoped.(User::ANONYMOUS_ADMIN)
        # anonymous admin does not exist until some of seed step creates it, therefore we use it only when it exists
        if admin.present?
          User.as_anonymous_admin do
            load seed
          end
        else
          load seed
        end
      end
    ensure
      self.class.is_seeding = false
    end
    save_hash

    Rails.logger.info("All seed files executed") unless Rails.env.test?
  end
end

#foreman_seedsObject


18
19
20
# File 'app/services/foreman_seeder.rb', line 18

def foreman_seeds
  Dir.glob(Rails.root + 'db/seeds.d/*.rb')
end

#hashObject


32
33
34
35
# File 'app/services/foreman_seeder.rb', line 32

def hash
  hashes = @hashed_files.collect { |seed| Digest::SHA256.file(seed).base64digest }
  Digest::SHA256.base64digest(hashes.join)
end

#hash_changed?Boolean

Returns:

  • (Boolean)

74
75
76
# File 'app/services/foreman_seeder.rb', line 74

def hash_changed?
  old_hash != hash
end

#old_hashObject


70
71
72
# File 'app/services/foreman_seeder.rb', line 70

def old_hash
  ForemanInternal.find_or_create_by(key: FOREMAN_INTERNAL_KEY).value
end

#plugin_seedsObject


22
23
24
25
26
# File 'app/services/foreman_seeder.rb', line 22

def plugin_seeds
  Foreman::Plugin.registered_plugins.collect do |name, plugin|
    Dir.glob(plugin.engine.root + 'db/seeds.d/*.rb') if plugin.engine
  end.flatten.compact
end

#save_hashObject


66
67
68
# File 'app/services/foreman_seeder.rb', line 66

def save_hash
  ForemanInternal.find_or_create_by(key: FOREMAN_INTERNAL_KEY).update_attribute(:value, hash)
end

#templatesObject


28
29
30
# File 'app/services/foreman_seeder.rb', line 28

def templates
  SeedHelper.report_templates + SeedHelper.provisioning_templates + SeedHelper.partition_tables_templates
end