Class: ForemanOpenscap::DataMigration

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_openscap/data_migration.rb

Instance Method Summary collapse

Constructor Details

#initialize(proxy_id) ⇒ DataMigration

Returns a new instance of DataMigration.



8
9
10
11
12
# File 'lib/foreman_openscap/data_migration.rb', line 8

def initialize(proxy_id)
  @proxy = ::SmartProxy.find(proxy_id)
  puts "Found proxy #{@proxy.to_label}"
  @url = @proxy.url
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/foreman_openscap/data_migration.rb', line 14

def available?
  return false unless @proxy && @url
  ::ProxyAPI::AvailableProxy.new(:url => @url).available? && foreman_available?
end

#migrateObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/foreman_openscap/data_migration.rb', line 19

def migrate
  ForemanOpenscap::Asset.where(:assetable_type => "Host::Base").select { |a| a.host.nil? }.map(&:destroy)
  ForemanOpenscap::Asset.where(:assetable_type => "Hostgroup").select { |a| a.hostgroup.nil? }.map(&:destroy)

  old_arf_reports = ActiveRecord::Migration.execute("SELECT * FROM foreman_openscap_arf_reports;")
  old_arf_reports.select do |report|
    policy_id = report["policy_id"]
    date = DateTime.strptime(report["created_at"], "%Y-%m-%d %H:%M:%S")
    date += 1.second until arfs_by_reported(date).empty?

    host_name = fetch_host_name(report["asset_id"])
    arf_file = fetch_xml_file(report["id"])

    next if arf_file.blank?
    migrator = ::ProxyAPI::Migration.new(:url => @url)

    migrated_id = migrator.migrate_arf_report(arf_file, host_name, policy_id, date.to_i)

    migrated_arf = ForemanOpenscap::ArfReport.find(migrated_id["arf_id"])
    migrated_arf.update_attribute(:openscap_proxy_id, @proxy.id)
    puts "Migrated Old arf_report #{report['id']} as arf: #{migrated_arf.id}"
    delete_old_records(report["id"]) if migrated_arf
  end
end