Class: HammerCLIImport::ImportCommand::LocalRepositoryImportCommand

Inherits:
BaseCommand
  • Object
show all
Extended by:
ImportTools::Repository::Extend
Includes:
ImportTools::ContentView::Include, ImportTools::Repository::Include
Defined in:
lib/hammer_cli_import/contentview.rb

Instance Attribute Summary

Attributes included from PersistentMap::Extend

#map_description, #map_target_entity, #maps

Instance Method Summary collapse

Methods included from ImportTools::Repository::Extend

add_repo_options

Methods included from ImportTools::ContentView::Include

#create_composite_content_view, #delete_content_view, #publish_content_view

Methods included from ImportTools::Repository::Include

#repo_synced?, #sync_repo, #sync_repo2, #with_synced_repo

Methods inherited from BaseCommand

#_compare_hash, #_create_entity, #api_call, api_call, api_init, #create_entity, csv_columns, #cvs_iterate, #data_dir, #delete, #delete_entity, #delete_entity_by_import_id, #execute, #find_uniq, #found_errors, #get_cache, #get_original_id, #get_translated_id, #import, #initialize, #last_in_cache?, #list_server_entities, #load_cache, #lookup_entity, #lookup_entity_in_array, #lookup_entity_in_cache, #map_entity, #mapped_api_call, #post_delete, #post_import, #print_summary, #process_error, #recognizable_error, #report_summary, #split_multival, #to_singular, #unmap_entity, #update_entity, #wait_for_task, #was_translated

Methods included from PersistentMap::Extend

#persistent_map, #persistent_maps

Methods included from ImportTools::ImportLogging::Extend

#add_logging_options

Methods included from AsyncTasksReactor::Extend

#add_async_tasks_reactor_options

Methods included from AsyncTasksReactor::Include

#atr_exit, #atr_init, #postpone_till, #wait_for

Methods included from ImportTools::Exceptional::Include

#handle_missing_and_supress, #silently

Methods included from ImportTools::Task::Include

#annotate_tasks

Methods included from ImportTools::ImportLogging::Include

#debug, #error, #fatal, #info, #log, #logtrace, #progress, #setup_logging, #warn

Methods included from PersistentMap::Include

#load_persistent_maps, #map_target_entity, #maps, #prune_persistent_maps, #save_persistent_maps

Constructor Details

This class inherits a constructor from HammerCLIImport::BaseCommand

Instance Method Details

#add_local_repo(data) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/hammer_cli_import/contentview.rb', line 106

def add_local_repo(data)
  product_name = 'Local-repositories'
  composite_id = [data['org_id'].to_i, product_name]
  product_hash = mk_product_hash data, product_name
  product_id = create_entity(:products, product_hash, composite_id)['id'].to_i

  repo_hash = mk_repo_hash data, product_id
  local_repo = create_entity :local_repositories, repo_hash, [data['org_id'].to_i, data['channel_id'].to_i]
  local_repo
end

#add_repo_filters(content_view_id, nevras) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/hammer_cli_import/contentview.rb', line 117

def add_repo_filters(content_view_id, nevras)
  cw_filter = api_call :content_view_filters,
                       :create,
                       { :content_view_id => content_view_id,
                         :name => 'Satellite 5 channel equivalence filter',
                         :type => 'rpm',
                         :inclusion => true}

  packages = nevras.collect do |package_nevra|
    match = /^([^:]+)-(\d+):([^-]+)-(.*)\.([^.]*)$/.match(package_nevra)
    raise "Bad nevra: #{package_nevra}" unless match

    { :name => match[1],
      :epoch => match[2],
      :version => match[3],
      :release => match[4],
      :architecture => match[5]
    }
  end
  packages.group_by { |package| package[:name] } .each do |name, _packages|
    api_call :content_view_filter_rules,
             :create,
             { :content_view_filter_id => cw_filter['id'],
               :name => name}
  end
end

#delete_single_row(data) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/hammer_cli_import/contentview.rb', line 184

def delete_single_row(data)
  cv_id = data['channel_id'].to_i
  unless @pm[:content_views][cv_id] || @pm[:redhat_content_views][cv_id] || @pm[:system_content_views][cv_id]
    info "#{to_singular(:content_views).capitalize} with id #{cv_id} wasn't imported. Skipping deletion."
    return
  end
  translated = get_translated_id :content_views, cv_id

  # delete_entity :content_views, cv_id
  delete_content_view translated
end

#directoryObject



41
42
43
# File 'lib/hammer_cli_import/contentview.rb', line 41

def directory
  File.expand_path(option_dir || File.dirname(option_csv_file))
end

#import_single_row(data) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/hammer_cli_import/contentview.rb', line 144

def import_single_row(data)
  org_id = data['org_id'].to_i

  repo_ids, clone_parents, packages, has_local = load_custom_channel_info org_id, data['channel_id'].to_i

  repo_ids.map! { |id| get_translated_id :repositories, id.to_i }

  if has_local
    local_repo = add_local_repo data
    sync_repo local_repo unless repo_synced? local_repo
    repo_ids.push local_repo['id'].to_i
  end

  clone_parents.collect { |x| Integer(x) } .each do |parent_id|
    begin
      begin
        parent_cv = get_cache(:redhat_content_views)[get_translated_id :redhat_content_views, [org_id, parent_id]]
      rescue
        parent_cv = get_cache(:content_views)[get_translated_id :content_views, parent_id]
      end
      repo_ids += parent_cv['repositories'].collect { |x| x['id'] }
    rescue HammerCLIImport::MissingObjectError
      error "No such {redhat_,}content_view: #{parent_id}"
    end
  end

  repo_ids.collect { |id| lookup_entity :repositories, id } .each do |repo|
    unless repo_synced? repo
      warn "Repository #{repo['label']} is not (fully) synchronized. Retry once synchronization has completed."
      report_summary :skipped, :content_views
      return nil
    end
  end
  content_view = mk_content_view_hash data, repo_ids

  cw = create_entity :content_views, content_view, data['channel_id'].to_i
  add_repo_filters cw['id'], packages if option_filter?
  publish_content_view cw['id'] if newer_repositories cw
end

#load_custom_channel_info(org_id, channel_id) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/hammer_cli_import/contentview.rb', line 85

def load_custom_channel_info(org_id, channel_id)
  headers = %w(org_id channel_id package_nevra package_rpm_name in_repo in_parent_channel)
  file = File.join directory, org_id.to_s, channel_id.to_s + '.csv'

  packages_in_channel = Set[]
  repo_ids = Set[]
  parent_channel_ids = Set[]
  has_local_packages = false

  CSVHelper.csv_each file, headers do |data|
    packages_in_channel << data['package_nevra']
    push_unless_nil parent_channel_ids, data['in_parent_channel']
    push_unless_nil repo_ids, data['in_repo']
    has_local_packages ||= data['in_repo'].nil? && data['in_parent_channel'].nil?
  end

  raise "Multiple parents for channel #{channel_id}?" unless parent_channel_ids.size.between? 0, 1

  [repo_ids.to_a, parent_channel_ids.to_a, packages_in_channel.to_a, has_local_packages]
end

#mk_content_view_hash(data, repo_ids) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/hammer_cli_import/contentview.rb', line 61

def mk_content_view_hash(data, repo_ids)
  {
    :name => data['channel_name'],

    :description => 'Channel migrated from Satellite 5',

    :organization_id => get_translated_id(:organizations, data['org_id'].to_i),
    :repository_ids  => repo_ids
  }
end

#mk_product_hash(data, product_name) ⇒ Object



45
46
47
48
49
50
# File 'lib/hammer_cli_import/contentview.rb', line 45

def mk_product_hash(data, product_name)
  {
    :name => product_name,
    :organization_id => get_translated_id(:organizations, data['org_id'].to_i)
  }
end

#mk_repo_hash(data, product_id) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/hammer_cli_import/contentview.rb', line 52

def mk_repo_hash(data, product_id)
  {
    :name => "Local repository for #{data['channel_label']}",
    :product_id => product_id,
    :url => 'file://' + File.join(directory, data['org_id'], data['channel_id']),
    :content_type => 'yum'
  }
end

#newer_repositories(cw) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/hammer_cli_import/contentview.rb', line 72

def newer_repositories(cw)
  last = cw['last_published']
  return true unless last
  last = Time.parse(last)
  cw['repositories'].any? do |repo|
    repo['last_sync'].nil? || last < Time.parse(repo['last_sync'])
  end
end

#push_unless_nil(col, obj) ⇒ Object



81
82
83
# File 'lib/hammer_cli_import/contentview.rb', line 81

def push_unless_nil(col, obj)
  col << obj unless obj.nil?
end