Class: HammerCLIImport::ImportCommand::AllCommand

Inherits:
HammerCLI::AbstractCommand
  • Object
show all
Extended by:
AsyncTasksReactor::Extend, ImportTools::ImportLogging::Extend, ImportTools::Repository::Extend
Includes:
ImportTools::ImportLogging::Include
Defined in:
lib/hammer_cli_import/all.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from ImportTools::Repository::Extend

add_repo_options

Methods included from ImportTools::ImportLogging::Extend

add_logging_options

Methods included from AsyncTasksReactor::Extend

add_async_tasks_reactor_options

Methods included from ImportTools::ImportLogging::Include

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

Class Attribute Details

.entity_orderObject

Returns the value of attribute entity_order.



48
49
50
# File 'lib/hammer_cli_import/all.rb', line 48

def entity_order
  @entity_order
end

.knownObject

Returns the value of attribute known.



59
60
61
# File 'lib/hammer_cli_import/all.rb', line 59

def known
  @known
end

Instance Method Details

#build_args(key, filename) ⇒ Object

Some subcommands have their own special args This is the function that will know them all



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
205
# File 'lib/hammer_cli_import/all.rb', line 180

def build_args(key, filename)
  if key == 'content-view'
    csv = ['--csv-file', "#{option_directory}/CHANNELS/export.csv"]
  else
    csv = ['--csv-file', filename]
  end
  return csv << '--delete' if option_delete?

  case key
  when 'config-file'
    args = config_file_args(csv)
  when 'content-host'
    args = content_host_args(csv)
  when 'content-view'
    args = content_view_args(csv)
  when 'organization'
    args = organization_args(csv)
  when 'repository', 'repository-enable'
    args = repository_args(csv)
  when 'user'
    args = user_args(csv)
  else
    args = csv
  end
  return args
end

#config_file_args(args) ⇒ Object

config-file may need –macro-mapping



135
136
137
138
# File 'lib/hammer_cli_import/all.rb', line 135

def config_file_args(args)
  args << '--macro-mapping' << "#{option_macro_mapping}" unless option_macro_mapping.nil?
  return args
end

#content_host_args(args) ⇒ Object

‘content-host needs –export-directory



141
142
143
144
# File 'lib/hammer_cli_import/all.rb', line 141

def content_host_args(args)
  args << '--export-directory' << File.join(File.expand_path('~'), 'rpm-working-dir')
  return args
end

#content_view_args(args) ⇒ Object

‘content-view needs –dir, knows its own –csv-file in that dir, and may need –no-async



147
148
149
150
151
152
153
# File 'lib/hammer_cli_import/all.rb', line 147

def content_view_args(args)
  args << '--dir' << "#{option_directory}/CHANNELS"
  args << '--no-async' if option_no_async?
  args << '--synchronize' if option_synchronize?
  args << '--wait' if option_wait?
  return args
end

#do_listObject



112
113
114
115
116
117
# File 'lib/hammer_cli_import/all.rb', line 112

def do_list
  puts 'Entities I understand:'
  AllCommand.entity_order.each do |an_entity|
    puts "  #{an_entity}"
  end
end

#entitiesObject

Get entities-to-be-processed, in the right order (reversed if deleting)



208
209
210
211
212
213
214
# File 'lib/hammer_cli_import/all.rb', line 208

def entities
  if option_delete?
    return AllCommand.entity_order.reverse
  else
    return AllCommand.entity_order
  end
end

#executeObject



240
241
242
243
244
245
246
247
248
249
# File 'lib/hammer_cli_import/all.rb', line 240

def execute
  setup_logging
  if option_list_entities?
    do_list
  else
    set_import_targets
    import_from
  end
  HammerCLI::EX_OK
end

#import_fromObject

Do the import(s)



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/hammer_cli_import/all.rb', line 217

def import_from
  entities.each do |key|
    a_map = AllCommand.known[key]
    if a_map['import']
      import_file = "#{option_directory}/#{a_map['export-file']}.csv"
      args = build_args(key, import_file)
      if File.exist? import_file
        progress format('Import %-20s with arguments %s', key, args.join(' '))

        #############################################################
        # MAGIC! We create a class from the class-name-string here! #
        #############################################################
        import_class = HammerCLIImport::ImportCommand.const_get(a_map['import-class'])
        unless option_dry_run?
          import_class.new(args).run(args)
        end
      else
        progress "...SKIPPING, no file #{import_file} available."
      end
    end
  end
end

#organization_args(args) ⇒ Object

‘organization’ may need –into-org-id



156
157
158
159
160
# File 'lib/hammer_cli_import/all.rb', line 156

def organization_args(args)
  args << '--into-org-id' << option_into_org_id unless option_into_org_id.nil?
  args << '--upload-manifests-from' << option_manifest_directory unless option_manifest_directory.nil?
  return args
end

#repository_args(args) ⇒ Object

repository and repo-enable may need –synch, –wait, and –no-async



163
164
165
166
167
168
# File 'lib/hammer_cli_import/all.rb', line 163

def repository_args(args)
  args << '--synchronize' if option_synchronize?
  args << '--wait' if option_wait?
  args << '--no-async' if option_no_async?
  return args
end

#set_import_targetsObject

What are we being asked to import? Marks what we asked for, and whatever those things are dependent on, to import



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/hammer_cli_import/all.rb', line 121

def set_import_targets
  to_import = option_entities.split(',')
  AllCommand.known.each_key do |key|
    AllCommand.known[key]['import'] = (to_import.include?(key) || to_import.include?('all'))
    next if AllCommand.known[key]['depends-on'].nil? || !AllCommand.known[key]['import']

    depends_on = AllCommand.known[key]['depends-on'].split(',')
    depends_on.each do |entity_name|
      AllCommand.known[entity_name]['import'] = true
    end
  end
end

#user_args(args) ⇒ Object

‘user’ needs –new-passwords and may need –merge-users



171
172
173
174
175
176
# File 'lib/hammer_cli_import/all.rb', line 171

def user_args(args)
  pwd_filename = "passwords_#{Time.now.utc.iso8601}.csv"
  args << '--new-passwords' << pwd_filename
  args << '--merge-users' if option_merge_users?
  return args
end