Class: HammerCLICsv::CsvCommand::SpliceCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/hammer_cli_csv/splice.rb

Constant Summary collapse

UUID =
'server_id'
ORGANIZATION =
'organization'
ORGANIZATION_ID =
'org_id'
NAME =
'name'
HOSTNAME =
'hostname'
IP_ADDRESS =
'ip_address'
IPV6_ADDRESS =
'ipv6_address'
REGISTERED_BY =
'registered_by'
REGISTRATION_TIME =
'registration_time'
LAST_CHECKIN_TIME =
'last_checkin_time'
PRODUCTS =
'software_channel'
ENTITLEMENTS =
'entitlements'
HOSTCOLLECTIONS =
'system_group'
VIRTUAL_HOST =
'virtual_host'
ARCHITECTURE =
'architecture'
HARDWARE =
'hardware'
MEMORY =
'memory'
SOCKETS =
'sockets'
IS_VIRTUALIZED =
'is_virtualized'

Constants inherited from BaseCommand

BaseCommand::COUNT

Instance Method Summary collapse

Methods inherited from BaseCommand

#associate_locations, #associate_organizations, #build_os_name, #check_server_status, #collect_column, #execute, #export_column, #foreman_architecture, #foreman_domain, #foreman_environment, #foreman_filter, #foreman_location, #foreman_operatingsystem, #foreman_organization, #foreman_partitiontable, #foreman_permission, #foreman_role, #foreman_template_kind, #hammer, #hammer_context, #katello_contentview, #katello_contentviewversion, #katello_hostcollection, #katello_product, #katello_repository, #katello_subscription, #labelize, #lifecycle_environment, #namify, #pluralize, #split_os_name, #thread_import

Instance Method Details

#create_content_hosts_from_csv(line) ⇒ Object



61
62
63
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/hammer_cli_csv/splice.rb', line 61

def create_content_hosts_from_csv(line)
  return if option_organization && line[ORGANIZATION] != option_organization

  if !@existing[line[ORGANIZATION]]
    create_organization(line)
    @existing[line[ORGANIZATION]] = {}

    # Fetching all content hosts is too slow and times out due to the complexity of the data
    # rendered in the json.
    # http://projects.theforeman.org/issues/6307
    total = @api.resource(:systems).call(:index, {
        'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
        'per_page' => 1
    })['total'].to_i
    (total / 20 + 2).to_i.times do |page|
      @api.resource(:systems).call(:index, {
          'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
          'page' => page,
          'per_page' => 20
      })['results'].each do |host|
        @existing[line[ORGANIZATION]][host['name']] = host['uuid'] if host
      end
    end
  end

  name = "#{line[NAME]}-#{line[UUID]}"
  #checkin_time = Time.parse(line[LAST_CHECKIN_TIME]).strftime("%a, %d %b %Y %H:%M:%S %z")
  checkin_time = if line[LAST_CHECKIN_TIME].casecmp('now').zero?
                   DateTime.now.strftime("%a, %d %b %Y %H:%M:%S %z")
                 else
                   DateTime.parse(line[LAST_CHECKIN_TIME]).strftime("%a, %d %b %Y %H:%M:%S %z")
                 end

  if !@existing[line[ORGANIZATION]].include? name
    print(_("Creating content host '%{name}'...") % {:name => name}) if option_verbose?
    host_id = @api.resource(:systems).call(:create, {
        'name' => name,
        'organization_id' => foreman_organization(:name => line[ORGANIZATION]),
        'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => 'Library'),
        'content_view_id' => katello_contentview(line[ORGANIZATION], :name => 'Default Organization View'),
        'last_checkin' => checkin_time,
        'facts' => facts(name, line),
        'installed_products' => products(line),
        'type' => 'system'
    })['uuid']

    # last_checkin is not updated in candlepin on creation
    # https://bugzilla.redhat.com/show_bug.cgi?id=1212122
    @api.resource(:systems).call(:update, {
      'id' => host_id,
      'system' => {
          'last_checkin' => checkin_time
      },
      'last_checkin' => checkin_time
    })

  else
    print(_("Updating content host '%{name}'...") % {:name => name}) if option_verbose?
    host_id = @api.resource(:systems).call(:update, {
        'id' => @existing[line[ORGANIZATION]][name],
        'system' => {
            'name' => name,
            'environment_id' => lifecycle_environment(line[ORGANIZATION], :name => 'Library'),
            'content_view_id' => katello_contentview(line[ORGANIZATION], :name => 'Default Organization View'),
            'last_checkin' => checkin_time,
            'facts' => facts(name, line),
            'installed_products' => products(line)
        },
        'installed_products' => products(line),  # TODO: http://projects.theforeman.org/issues/9191
        'last_checkin' => checkin_time
    })['uuid']

    @existing[line[ORGANIZATION]].delete(name) # Remove to indicate found
  end

  if @hosts.include? line[UUID]
    @hosts[line[UUID]] = host_id
  elsif @guests.include? line[UUID]
    @guests[line[UUID]] = "#{line[ORGANIZATION]}/#{name}"
  end

  update_host_collections(host_id, line)

  puts _('done') if option_verbose?
rescue RuntimeError => e
  raise "#{e}\n       #{line}"
end

#importObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/hammer_cli_csv/splice.rb', line 47

def import
  @existing = {}
  load_product_mapping
  preload_host_guests

  filename = option_dir + '/splice-export'
  thread_import(false, filename, NAME) do |line|
    create_content_hosts_from_csv(line) unless line[UUID][0] == '#'
  end

  update_host_guests
  delete_unfound_hosts(@existing)
end