Class: HammerCLIImport::ImportCommand::RepositoryEnableCommand

Inherits:
BaseCommand
  • Object
show all
Extended by:
ImportTools::Repository::Extend
Includes:
ImportTools::ContentView::Include, ImportTools::Repository::Include
Defined in:
lib/hammer_cli_import/repositoryenable.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, #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

#delete_single_row(row) ⇒ Object



60
61
62
# File 'lib/hammer_cli_import/repositoryenable.rb', line 60

def delete_single_row(row)
  handle_row(row, false)
end

#disable_repos(org, prod_id, repo_set_id, info, channel_label) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/hammer_cli_import/repositoryenable.rb', line 206

def disable_repos(org, prod_id, repo_set_id, info, channel_label)
  repo = lookup_entity_in_cache(
    :redhat_repositories,
    {
      'content_id' => repo_set_id,
      'organization' => {'label' => org['label']}
    })
  # find the repo from reposet to get also the substitutions
  # otherwise it's not possible to disable certain repositories
  repo = find_repo_in_reposet(prod_id, repo_set_id, info) if repo
  unless repo
    error "Unknown repository (#{channel_label} equivalent) to disable."
    return
  end
  info "Disabling #{info['url']} for channel #{channel_label} in org #{org['id']}"
  begin
    unless option_dry_run?
      rc = api_call(
        :repository_sets,
        :disable,
        'product_id' => prod_id,
        'id' => repo_set_id,
        'basearch' => repo['substitutions'].key?('basearch') ? info['arch'] : '',
        'releasever' => repo['substitutions'].key?('releasever') ? info['version'] : '')

      unmap_entity(:redhat_repositories, rc['input']['repository']['id'])
      get_cache(:redhat_repositories).delete(rc['input']['repository']['id'])
      return rc['input']['repository']
    end
  rescue RestClient::Exception  => e
    if e.http_code == 404
      error '...no such repository to disable.'
    else
      error "...repository disable failed with error '#{e.http_code}, #{e.message}' - skipping."
    end
  end
end

#enable_repos(org, prod_id, repo_set_id, info, row) ⇒ Object

Given a repository-set and a channel-to-repo info for that channel, enable the correct repository



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/hammer_cli_import/repositoryenable.rb', line 168

def enable_repos(org, prod_id, repo_set_id, info, row)
  channel_label = row['channel_label']
  channel_id = row ['channel_id'].to_i
  repo = find_repo_in_reposet(prod_id, repo_set_id, info)
  if repo.nil?
    warn "Repository #{info['url']} for (#{info['arch']} x #{info['version']}) not found!"
    return
  end

  return find_enabled_repo(prod_id, repo_set_id, repo['repo_name']) if repo['enabled']

  info "Enabling #{info['url']} for channel #{channel_label} in org #{org['id']}"
  begin
    unless option_dry_run?
      rc = api_call(
        :repository_sets,
        :enable,
        'product_id' => prod_id,
        'id' => repo_set_id,
        'basearch' => repo['substitutions'].key?('basearch') ? info['arch'] : '',
        'releasever' => repo['substitutions'].key?('releasever') ? info['version'] : '')

      original_org_id = get_original_id(:organizations, org['id'])
      map_entity(:redhat_repositories, [original_org_id, channel_id], rc['input']['repository']['id'])
      # store to cache (using lookup_entity, because :redhat_repositories api
      # does not return full repository hash)
      return lookup_entity(:redhat_repositories, rc['input']['repository']['id'], true)
    end
  rescue RestClient::Exception  => e
    if e.http_code == 409
      info '...already enabled.'
      return find_enabled_repo(prod_id, repo_set_id, repo['repo_name'])
    else
      error "...repository enablement failed with error '#{e.http_code}, #{e.message}' - skipping."
    end
  end
end

#find_enabled_repo(product_id, repo_set_id, repo_name) ⇒ Object

and this is quite a pain - to get real ids of the enabled repositories search according to the repository name



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hammer_cli_import/repositoryenable.rb', line 154

def find_enabled_repo(product_id, repo_set_id, repo_name)
  reposet = api_call(
    :repository_sets,
    :show,
    'product_id' => product_id,
    'id' => repo_set_id)

  enabled_repo = lookup_entity_in_array(reposet['repositories'], {'name' => repo_name})
  info "Repository '#{repo_name}' already enabled as #{enabled_repo['id']}."
  return lookup_entity(:redhat_repositories, enabled_repo['id'])
end

#find_repo_in_reposet(product_id, repo_set_id, info) ⇒ Object

this way we’re able to get from the server, what repositories within a repository-set are enabled



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hammer_cli_import/repositoryenable.rb', line 137

def find_repo_in_reposet(product_id, repo_set_id, info)
  repos = api_call(
    :repository_sets,
    :available_repositories,
    'product_id' => product_id,
    'id' => repo_set_id)

  if repos['results'][0] && repos['results'][0]['substitutions'].size == 1
    return lookup_entity_in_array(repos['results'],
                                  {'substitutions' => {'basearch' => info['arch']}})
  end
  return lookup_entity_in_array(repos['results'],
                                {'substitutions' => {'basearch' => info['arch'], 'releasever' => info['version']}})
end

#handle_row(row, enable) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/hammer_cli_import/repositoryenable.rb', line 64

def handle_row(row, enable)
  if row['org_id'] # Not a Red Hat channel
    info " Skipping #{row['channel_label']} in organization #{row['org_id']}"
    return
  end

  # read_channel_mapping_data will be called only once per subcommand
  @channel_to_repo ||= read_channel_mapping_data(option_repository_map)
  channel_label = row['channel_label']
  channel_id = row['channel_id'].to_i
  repo_set_info = @channel_to_repo[channel_label]

  if repo_set_info.nil? # not mapped channel (like proxy)
    info " Skipping nontransferable #{row['channel_label']}"
    return
  end

  # rely on we see only products in imported organizations
  get_cache(:products).each do |product_id, product|
    rsets = list_server_entities(:repository_sets, {:product_id => product_id}, true)

    rsets.each do |rs|
      next if repo_set_info['set-url'] != rs['contentUrl'] &&
              repo_set_info['set-url'].gsub('$releasever', repo_set_info['version']) != rs['contentUrl']

      product_org = lookup_entity_in_cache(:organizations, {'label' => product['organization']['label']})
      composite_rhcv_id = [get_original_id(:organizations, product_org['id']), channel_id]
      if enable
        # Turn on the specific repository
        rh_repo = enable_repos(product_org, product_id, rs['id'], repo_set_info, row)
        next if rh_repo.nil? || option_dry_run?

        # Finally, if requested, kick off a sync
        with_synced_repo rh_repo do
          cv = create_entity(
            :redhat_content_views,
            {
              :organization_id => product_org['id'],
              :name => row['channel_name'],
              :description => 'Red Hat channel migrated from Satellite 5',
              :repository_ids  => [rh_repo['id']]
            },
            composite_rhcv_id)
          begin
            publish_content_view(cv['id'], :redhat_content_views)
          rescue RestClient::Exception => e
            msg = JSON.parse(e.response)['displayMessage']
            error "#{e.http_code} trying to publish content-view #{row['channel_name']} :\n #{msg}\n"
            next
          end
        end
      else
        if @pm[:redhat_content_views][composite_rhcv_id]
          delete_content_view(get_translated_id(:redhat_content_views, composite_rhcv_id), :redhat_content_views)
        end
        disable_repos(product_org, product_id, rs['id'], repo_set_info, channel_label)
      end
    end
  end
end

#import_single_row(row) ⇒ Object

BaseCommand will read our channel-csv for us



56
57
58
# File 'lib/hammer_cli_import/repositoryenable.rb', line 56

def import_single_row(row)
  handle_row(row, true)
end

#post_import(_file) ⇒ Object



244
245
246
# File 'lib/hammer_cli_import/repositoryenable.rb', line 244

def post_import(_file)
  HammerCLI::EX_OK
end

#read_channel_mapping_data(filename) ⇒ Object

Hydrate the channel-to-repository-data mapping struct



126
127
128
129
130
131
132
133
134
# File 'lib/hammer_cli_import/repositoryenable.rb', line 126

def read_channel_mapping_data(filename)
  channel_map = {}

  File.open(filename, 'r') do |f|
    json = f.read
    channel_map = JSON.parse(json)
  end
  return channel_map
end