Module: HammerCLIKatello::ContentExportHelper

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApipieHelper

#call, #destroy, #index, #show, #update

Class Method Details

.included(base) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 186

def self.included(base)
  if base.command_name.first.to_sym == :version
    setup_version(base)
  elsif base.command_name.first.to_sym == :library
    setup_library(base)
  elsif base.command_name.first.to_sym == :repository
    setup_repository(base)
  end
end

.setup_library(base) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 232

def self.setup_library(base)
  base.action(:library)
  base.success_message _("Library environment is being exported in task %{id}.")
  base.failure_message _("Could not export the library")
  base.option "--fail-on-missing-content", :flag,
                _("Fails if any of the repositories belonging"\
                  " to this organization are unexportable.")

  base.build_options do |o|
    o.expand(:all).including(:organizations)
  end
end

.setup_repository(base) ⇒ Object



196
197
198
199
200
201
202
203
204
205
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
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 196

def self.setup_repository(base)
  base.action(:repository)
  base.success_message _("Repository is being exported in task %{id}.")
  base.failure_message _("Could not export the repository")

  base.option "--name", "NAME", _("Filter repositories by name."),
             :attribute_name => :option_name,
             :required => false

  base.build_options do |o|
    o.expand(:all).including(:products, :organizations)
  end

  base.validate_options do
    any(:option_id, :option_name).required
    unless option(:option_id).exist?
      any(
        :option_product_id,
        :option_product_name
      ).required
      unless option(:option_product_id).exist?
        any(:option_organization_id, :option_organization_name, \
                            :option_organization_label).required
      end
    end
  end

  base.class_eval do
    def request_params
      super.tap do |opts|
        opts["id"] = resolver.repository_id(options)
      end
    end
  end
end

.setup_version(base) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 245

def self.setup_version(base)
  base.action(:version)
  setup_version_options(base)
  base.success_message _("Content view version is being exported in task %{id}.")
  base.failure_message _("Could not export the content view version")

  base.extend_with(
    HammerCLIKatello::CommandExtensions::LifecycleEnvironments.new(only: :option_sources)
  )
  base.include(LifecycleEnvironmentNameMapping)

  base.class_eval do
    def request_params
      super.tap do |opts|
        opts["id"] = resolver.content_view_version_id(options)
      end
    end
  end
end

.setup_version_options(base) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 265

def self.setup_version_options(base)
  base.option "--fail-on-missing-content", :flag,
                _("Fails if any of the repositories belonging"\
                  " to this version are unexportable.")

  base.option "--version", "VERSION", _("Filter versions by version number."),
             :attribute_name => :option_version,
             :required => false

  base.build_options do |o|
    o.expand(:all).including(:content_views, :organizations, :lifecycle_environments)
    o.without(:environment_ids, :environment_id)
  end

  base.validate_options do
    unless option(:option_id).exist?
      any(:option_id, :option_content_view_name, :option_content_view_id).required
      any(
        :option_version,
        :option_lifecycle_environment_id,
        :option_lifecycle_environment_name
      ).required
      unless option(:option_content_view_id).exist?
        any(:option_organization_id, :option_organization_name, \
                            :option_organization_label).required
      end
    end
  end
end

Instance Method Details

#check_export_history_syncable!(export_history) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 73

def check_export_history_syncable!(export_history)
  unless export_history["metadata"]["format"] == "syncable"
    raise _("Cannot generate listing files for this export since "\
      + "it is not syncable. It was not generated with --format=syncable.")
  end

  raise _("Export History does not have the path specified."\
    + " The task may have errored out.") unless export_history["path"]
end

#emit_async_infoObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 30

def emit_async_info
  if syncable?
    output.print_message _("Once the task completes the listing files may be generated "\
        + "with the command:")
    output.print_message(" hammer content-export generate-listing --task-id #{@task['id']}")
  else
    output.print_message _("Once the task completes the export metadata must be generated "\
        + "with the command:")
    output.print_message(" hammer content-export generate-metadata --task-id #{@task['id']}")
  end
end

#executeObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 7

def execute
  warn_unexportable_repositories
  response = super
  if option_async?
    emit_async_info
    HammerCLI::EX_OK
  elsif response != HammerCLI::EX_OK
    response
  else
    export_history = fetch_export_history_from_task(reload_task(@task))
    if syncable?
      make_listing_files(export_history)
      HammerCLI::EX_OK
    elsif export_history
      (export_history)
      HammerCLI::EX_OK
    else
      output.print_error _("Could not fetch the export history")
      HammerCLI::EX_CANTCREAT
    end
  end
end

#fetch_export_history(export_history_id) ⇒ Object



59
60
61
62
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 59

def fetch_export_history(export_history_id)
  return unless export_history_id
  index(:content_exports, :id => export_history_id).first
end

#fetch_export_history_from_task(task) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 64

def fetch_export_history_from_task(task)
  # checking this here implies the task object was loaded recently
  if %w(error warning).include?(task['result'])
    raise _("Can not fetch export history from an unfinished task")
  end
  export_history_id = task.dig('output', 'export_history_id')
  fetch_export_history(export_history_id)
end

#fetch_repositoriesObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 138

def fetch_repositories
  if repository_command?
    resp = show(:repositories, id: resolver.repository_id(options))
    return resp["download_policy"] == "immediate" ? [] : [resp]
  end

  repo_options = {
    library: true,
    content_type: 'yum',
    search: 'download_policy != immediate'
  }
  if version_command?
    repo_options[:content_view_version_id] = resolver.content_view_version_id(options)
  else
    repo_options[:organization_id] = options["option_organization_id"]
  end
  index(:repositories, repo_options)
end

#generate_metadata_json(export_history) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 115

def (export_history)
   = export_history["metadata"].to_json
  begin
     = "#{export_history['path']}/metadata.json"
    File.write(, )
    output.print_message _("Generated #{}")
  rescue SystemCallError
    filename = "metadata-#{export_history['id']}.json"
    File.write(filename, )
    output.print_message _("Unable to access/write to '#{export_history['path']}'. "\
                           "Generated '#{Dir.pwd}/#{filename}' instead. "\
                           "This file is necessary to perform an import.")
  end
end

#make_listing_files(export_history) ⇒ Object



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
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 83

def make_listing_files(export_history)
  check_export_history_syncable!(export_history)
  output.print_message _("Generated #{export_history['path']}")

  return unless Dir.exist?("#{export_history['path']}/content")

  begin
    # export history path may look like
    # "/var/lib/pulp/exports/export-12803/apple/3.0//2022-06-30T17-23-06-00-00"
    # Generate listing files for all sub directories of
    # /var/lib/pulp/exports/export-12803/apple/3.0/$date/$org/Library/
    ignorables = Dir.glob("#{export_history['path']}/content/**/repodata").map do |path|
      File.dirname(path)
    end

    paths = Find.find("#{export_history['path']}/content").select do |path|
      File.directory?(path) &&
        ignorables.none? { |ignorable| path.start_with?(ignorable) }
    end

    paths.each do |dir|
      directories = Dir.chdir(dir) { Dir['*'] }
      File.write("#{dir}/listing", directories.join("\n"))
    end
  rescue SystemCallError
    output.print_message _("Unable to access/write listing files"\
                           + " to '#{export_history['path']}'." \
                           + " To generate listing files run the command below as a root user ")
    output.print_message(" hammer content-export generate-listing --id #{export_history['id']}")
  end
end

#reload_task(task) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 50

def reload_task(task)
  task_id = if task.is_a? Hash
              task['id']
            else
              task
            end
  show(:foreman_tasks, id: task_id)
end

#repository_command?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 134

def repository_command?
  self.class.command_name.first.to_sym == :repository
end

#send_requestObject



46
47
48
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 46

def send_request
  @task = super
end

#syncable?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 42

def syncable?
  options.key?("option_format") && option_format == 'syncable'
end

#version_command?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 130

def version_command?
  self.class.command_name.first.to_sym == :version
end

#warn_unexportable_repositoriesObject



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
183
184
# File 'lib/hammer_cli_katello/content_export_helper.rb', line 157

def warn_unexportable_repositories
  repos = fetch_repositories
  unless repos.empty?
    if version_command?
      output.print_message _("NOTE: Unable to fully export this version because"\
        " it contains repositories without the 'immediate' download policy."\
        " Update the download policy and sync affected repositories."\
        " Once synced republish the content view"\
        " and export the generated version.")
    elsif repository_command?
      output.print_message _("NOTE: Unable to fully export this repository because"\
        " it does not have the 'immediate' download policy."\
        " Update the download policy, sync the repository and export.")
    else
      output.print_message _("NOTE: Unable to fully export this organization's library because"\
        " it contains repositories without the 'immediate' download policy."\
        " Update the download policy and sync affected"\
        " repositories to include them in the export.")
    end
    output.print_message _("Use the following command to update the "\
      "download policy of these repositories.")
    output.print_message "hammer repository update --id=<REPOSITORY_ID> "\
      "--download-policy='immediate'"
    output.print_message ""
    print_record(::HammerCLIKatello::Repository::ListCommand.output_definition, repos)
    exit(HammerCLI::EX_SOFTWARE) if repository_command? || option_fail_on_missing_content?
  end
end