Class: HammerCLIImport::ImportCommand::ContentHostImportCommand

Inherits:
BaseCommand
  • Object
show all
Includes:
ImportTools::ContentView::Include, ImportTools::LifecycleEnvironment::Include
Defined in:
lib/hammer_cli_import/contenthost.rb

Instance Attribute Summary

Attributes included from PersistentMap::Extend

#map_description, #map_target_entity, #maps

Instance Method Summary collapse

Methods included from ImportTools::LifecycleEnvironment::Include

#get_env

Methods included from ImportTools::ContentView::Include

#create_composite_content_view, #delete_content_view, #publish_content_view

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, #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

#_build_composite_cv_label(data, cvs) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/hammer_cli_import/contenthost.rb', line 49

def _build_composite_cv_label(data, cvs)
  label = ''
  label += data['base_channel_label'] + '_' if data['base_channel_label']
  label += cvs.sort.join('_')
  label.gsub!(/[^0-9a-z_-]/i, '_')
  return label
end

#_create_dir(dir_name) ⇒ Object



180
181
182
# File 'lib/hammer_cli_import/contenthost.rb', line 180

def _create_dir(dir_name)
  Dir.mkdir(dir_name) unless File.directory?(dir_name)
end

#create_rpmbuild_structureObject



184
185
186
187
188
# File 'lib/hammer_cli_import/contenthost.rb', line 184

def create_rpmbuild_structure
  _create_dir option_export_directory
  _create_dir File.join(option_export_directory, 'SPECS')
  _create_dir File.join(option_export_directory, 'SOURCES')
end

#delete_single_row(data) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/hammer_cli_import/contenthost.rb', line 157

def delete_single_row(data)
  @composite_cvs ||= Set.new
  profile_id = data['server_id'].to_i
  unless @pm[:hosts][profile_id]
    info "#{to_singular(:hosts).capitalize} with id #{profile_id} wasn't imported. Skipping deletion."
    return
  end
  profile = get_cache(:hosts)[@pm[:hosts][profile_id]]
  cv = get_cache(:content_views)[profile['content_view_id']]
  @composite_cvs << cv['id'] if cv && cv['composite']
  delete_entity_by_import_id(:hosts, get_translated_id(:hosts, profile_id), 'id')
end

#import_single_row(data) ⇒ Object



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
# File 'lib/hammer_cli_import/contenthost.rb', line 74

def import_single_row(data)
  @vguests ||= {}
  @map ||= Set.new
  cvs = (split_multival(data['base_channel_id']) + split_multival(data['child_channel_id'])).collect do |channel_id|
    begin
      get_translated_id(:redhat_content_views, [data['organization_id'].to_i, channel_id])
    rescue HammerCLIImport::MissingObjectError
      get_translated_id(:content_views, channel_id)
    end
  end
  cv_id = create_composite_content_view(
    :system_content_views,
    get_translated_id(:organizations, data['organization_id'].to_i),
    _build_composite_cv_label(data, cvs),
    'Composite content view for content hosts',
    cvs)
  profile = mk_profile_hash data, cv_id
  c_host = create_entity(:hosts, profile, data['server_id'].to_i)
  # store processed system profiles to a set according to the organization
  @map << {
    :org_id => data['organization_id'].to_i,
    :system_id => data['server_id'].to_i,
    :host_id => c_host['id'],
    :uuid => c_host['subscription_facet_attributes']['uuid']
  }
  # associate virtual guests in post_import to make sure, all the guests
  # are already imported (and known to sat6)
  @vguests[data['server_id'].to_i] = split_multival(data['virtual_guest']) if data['virtual_host'] == data['server_id']
  debug "vguests: #{@vguests[data['server_id'].to_i].inspect}" if @vguests[data['server_id'].to_i]
end

#mk_profile_hash(data, cv_id) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/hammer_cli_import/contenthost.rb', line 57

def mk_profile_hash(data, cv_id)
  hcollections = split_multival(data['system_group_id']).collect do |sg_id|
    get_translated_id(:host_collections, sg_id)
  end
  org_id = get_translated_id(:organizations, data['organization_id'].to_i)
  {
    :name => data['profile_name'],
    :comment => "#{data['description']}\nsat5_system_id: #{data['server_id']}",
    :facts => {'release' => data['release'], 'architecture' => data['architecture']},
    # :guest_ids => [],
    :organization_id => org_id,
    :lifecycle_environment_id => get_env(org_id)['id'],
    :content_view_id => cv_id,
    :host_collection_ids => hcollections
  }
end

#post_delete(_file) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/hammer_cli_import/contenthost.rb', line 170

def post_delete(_file)
  # let's 'try' to delete the system content views
  # there's no chance to find out, whether some other content hosts are associated with them
  @composite_cvs.each do |cv_id|
    silently do
      delete_content_view(cv_id, :system_content_views)
    end
  end
end

#post_import(_file) ⇒ Object



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
148
149
150
151
152
153
154
155
# File 'lib/hammer_cli_import/contenthost.rb', line 105

def post_import(_file)
  @vguests.each do |system_id, guest_ids|
    handle_missing_and_supress "setting guests for #{system_id}" do
      uuid = get_translated_id(:hosts, system_id)
      vguest_uuids = guest_ids.collect do |id|
        get_translated_id(:hosts, id)
      end if guest_ids
      debug "Setting virtual guests for #{uuid}: #{vguest_uuids.inspect}"
      update_entity(
        :hosts,
        uuid,
        {:guest_ids => vguest_uuids}
      ) if uuid && vguest_uuids
    end
  end
  return if @map.empty?
  # create rpmbuild directories
  create_rpmbuild_structure
  # create mapping files
  version = '0.0.1'
  now = Time.now
  rpm_name = "system-profile-transition-#{Socket.gethostname}-#{now.to_i}"
  tar_name = "#{rpm_name}-#{version}"
  dir_name = File.join(option_export_directory, tar_name)
  # create SOURCES id_to_uuid.map file
  FileUtils.rm_rf(dir_name) if File.directory?(dir_name)
  Dir.mkdir dir_name
  CSVHelper.csv_write_hashes(
    File.join(dir_name, "system-id_to_uuid-#{now.to_i}.map"),
    [:system_id, :uuid, :org_id],
    @map.sort_by { |x| [x[:org_id], x[:system_id], x[:uuid]] })

  sources_dir = File.join(option_export_directory, 'SOURCES')
  # debug("tar -C #{option_export_directory} -czf #{sources_dir}/#{tar_name}.tar.gz #{tar_name}")
  system("tar -C #{option_export_directory} -czf #{sources_dir}/#{tar_name}.tar.gz #{tar_name}")
  FileUtils.rm_rf(dir_name)
  # store spec file
  File.open(
    File.join(option_export_directory, 'SPECS', "#{tar_name}.spec"), 'w') do |file|
    file.write(rpm_spec(rpm_name, version, now))
  end
  abs_export_directory = File.expand_path(option_export_directory)
  progress ''
  progress 'To build the system-profile-transition rpm, run:'
  progress ''
  progress "\tcd #{abs_export_directory}/SPECS && "
  progress "\t  rpmbuild -ba --define \"_topdir #{abs_export_directory}\" #{tar_name}.spec"
  progress ''
  progress "Then find your #{rpm_name} package"
  progress "\tin #{File.join(abs_export_directory, 'RPMS/noarch/')} directory."
end

#rpm_spec(rpm_name, version, date) ⇒ Object



190
191
192
193
194
195
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
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/hammer_cli_import/contenthost.rb', line 190

def rpm_spec(rpm_name, version, date)
  "
Name:       #{rpm_name}
Version:    #{version}
Release:    1
Summary:    System profile transition data

Group:      Applications/Productivity
License:    GPLv3
URL:        https://github.com/Katello/hammer-cli-import
Source0:    #{rpm_name}-#{version}.tar.gz
BuildRoot:  %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
BuildArch: noarch

%define _binary_filedigest_algorithm 1
%define _binary_payload w9.gzdio

%define  debug_package %{nil}

%description
This package contains mapping information, how system profiles managed by Red Hat Satellite 5
get translated to content hosts on Red Hat Satellite 6

%prep
%setup -q


%build


%install
install -m 755 -d $RPM_BUILD_ROOT/%{_datarootdir}/rhn/transition
install -m 644 system-id_to_uuid-#{date.to_i}.map $RPM_BUILD_ROOT/%{_datarootdir}/rhn/transition/


%post
# run register here

%clean
rm -rf %{buildroot}


%files
%defattr(-,root,root,-)
/usr/share/rhn/transition/
/usr/share/rhn/transition/system-id_to_uuid-#{date.to_i}.map
%doc


%changelog
* #{date.strftime('%a %b %e %Y')} root <root@localhost> initial package build
- using system profile to content host mapping data
"
end