Class: ForemanVirtWhoConfigure::Config

Inherits:
ApplicationRecord
  • Object
show all
Includes:
Authorizable, Encryptable
Defined in:
app/models/foreman_virt_who_configure/config.rb

Constant Summary collapse

PERMITTED_PARAMS =
[
  :interval, :organization_id, :compute_resource_id, :whitelist, :blacklist, :hypervisor_id,
  :hypervisor_type, :hypervisor_server, :hypervisor_username, :hypervisor_password, :debug,
  :satellite_url, :http_proxy_id, :no_proxy, :name,
  # API parameter filtering_mode gets translated to listing_mode in the controller
  # We keep both params permitted for compatibility with 1.11
  :listing_mode, :filtering_mode, :filter_host_parents, :exclude_host_parents, :kubeconfig_path,
  :prism_flavor, :ahv_internal_debug
]
UNLIMITED =
0
WHITELIST =
1
BLACKLIST =
2
FILTERING_MODES =
{
  UNLIMITED.to_s => N_('Unlimited'),
  WHITELIST.to_s => N_('Whitelist'),
  BLACKLIST.to_s => N_('Blacklist'),
}
WIZARD_STEPS =
{
  'general_information' => N_('General information'),
  'schedule' => N_('Schedule'),
  'connection' => N_('Connection')
}
HYPERVISOR_IDS =
['hostname', 'uuid', 'hwuuid']
HYPERVISOR_TYPES =
{
  'esx' => 'VMware vSphere / vCenter (esx)',
  'hyperv' => 'Microsoft Hyper-V (hyperv)',
  'libvirt' => 'libvirt',
  'kubevirt' => 'Container-native virtualization',
  'ahv' => 'Nutanix AHV (ahv)'
}
HYPERVISOR_DEFAULT_TYPE =
'esx'
AVAILABLE_INTERVALS =
{
  '60' => N_('Every hour'),
  '120' => N_('Every 2 hours'),
  '240' => N_('Every 4 hours'),
  '480' => N_('Every 8 hours'),
  '720' => N_('Every 12 hours'),
  '1440' => N_('Every 24 hours'),
  '2880' => N_('Every 2 days'),
  '4320' => N_('Every 3 days'),
}
DEFAULT_INTERVAL =
120
STATUSES =
{
  'ok' => N_('OK'),
  'out_of_date' => N_('No change'),
  'unknown' => N_('Unknown')
}
STATUS_DESCRIPTIONS =
Hash.new(N_('Unknown configuration status, caused by unexpected conditions')).merge(
  {
    :unknown => N_('The configuration was not deployed yet or the virt-who was unable to report the status'),
    :ok => N_('The virt-who report arrived within the interval'),
    :out_of_date => N_('The virt-who report has not arrived within the interval, which indicates there was no change on hypervisor')
  }
)
PRISM_FLAVORS =
{
  'central' => N_('Prism Central'),
  'element' => N_('Prism Element')
}
AHV_VALID_OPTIONS =
%w(prism_flavor ahv_internal_debug)
KUBEVIRT_VALID_OPTIONS =
%w(kubeconfig_path)
KUBEVIRT_INVALID_OPTIONS =
%w(hypervisor_server hypervisor_username)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.search_by_status(key, operator, value) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/foreman_virt_who_configure/config.rb', line 151

def self.search_by_status(key, operator, value)
  condition = case value
                when 'ok'
                  sanitize_sql_for_conditions([' out_of_date_at >= ? ', DateTime.now.utc.to_s(:db)])
                when 'unknown'
                  sanitize_sql_for_conditions([' last_report_at IS NULL'])
                when 'out_of_date'
                  sanitize_sql_for_conditions([' out_of_date_at < ? ', DateTime.now.utc.to_s(:db)])
              end
  { :conditions => condition }
end

Instance Method Details

#create_or_find_service_userObject



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
# File 'app/models/foreman_virt_who_configure/config.rb', line 174

def create_or_find_service_user
  service_user = ServiceUser.find_by(organization_id: organization_id)
  if service_user.present?
    update(service_user_id: service_user.id)
    return service_user
  end

  User.skip_permission_check do
    password = User.random_password
    service_user = build_service_user(organization_id: organization_id)
    user = service_user.build_user
    user.auth_source = AuthSourceHiddenWithAuthentication.default
    user.password = password
    user. = "virt_who_reporter_#{id}"
    user.organizations = [organization]
    user.roles = [Role.find_by(:name => 'Virt-who Reporter')]
    user.valid? # to trigger password hashing
    user.save!(:validate => false)

    service_user.encrypted_password = password
    service_user.save!

    update_attribute :service_user_id, service_user.id
  end
end

#destroy_service_userObject



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/models/foreman_virt_who_configure/config.rb', line 200

def destroy_service_user
  return unless service_user.configs.count == 0

  User.skip_permission_check do
    # skip validation that prevents hidden user deletion
    user = User.unscoped.find_by_id(service_user.user_id)
    service_user.destroy
    user.notification_recipients.delete_all
    # we can't use destroy, because internal users can't be deleted
    user.tasks.update_all :user_id => nil
    user.delete
  end
end

#humanized_intervalObject

mapping of supported CR types case config.compute_resource

when Foreman::Model::Libvirt
  'libvirt'
when Foreman::Model::Vmware
  'esx'
else
  raise 'unsupported compute resource type'

end



224
225
226
# File 'app/models/foreman_virt_who_configure/config.rb', line 224

def humanized_interval
  _("every %s hours") % (interval / 60)
end

#out_of_date?(deadline = DateTime.now.utc) ⇒ Boolean

Returns:

  • (Boolean)


228
229
230
# File 'app/models/foreman_virt_who_configure/config.rb', line 228

def out_of_date?(deadline = DateTime.now.utc)
  out_of_date_at.present? && out_of_date_at < deadline
end

#permission_name(action) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'app/models/foreman_virt_who_configure/config.rb', line 163

def permission_name(action)
  case action
    when :create
      'create_virt_who_config'
    when :edit
      'edit_virt_who_config'
    else
      raise "unknown permission for action #{action}"
  end
end

#statusObject



263
264
265
266
267
268
269
270
271
272
# File 'app/models/foreman_virt_who_configure/config.rb', line 263

def status
  if last_report_at.nil?
    :unknown
  elsif !out_of_date?
    :ok
  else
    # out of date is currently considered ok too, virt-who does not send any report if there's no change on hypervisor
    :out_of_date
  end
end

#status_descriptionObject



274
275
276
# File 'app/models/foreman_virt_who_configure/config.rb', line 274

def status_description
  _(STATUS_DESCRIPTIONS[status])
end

#step_name(step_key) ⇒ Object



232
233
234
# File 'app/models/foreman_virt_who_configure/config.rb', line 232

def step_name(step_key)
  _(WIZARD_STEPS[step_key])
end

#stepsObject



236
237
238
# File 'app/models/foreman_virt_who_configure/config.rb', line 236

def steps
  WIZARD_STEPS.keys
end

#validates_debug_settingsObject



134
135
136
137
138
# File 'app/models/foreman_virt_who_configure/config.rb', line 134

def validates_debug_settings
  if ahv_internal_debug && !debug
    errors.add(:ahv_internal_debug, "Enable debugging output is required for Enable AHV debug")
  end
end

#validates_hypervisor_optionsObject



140
141
142
# File 'app/models/foreman_virt_who_configure/config.rb', line 140

def validates_hypervisor_options
  invalid_fields.each { |f| errors.add(f, "Invalid option for hypervisor [#{hypervisor_type}]") if send(f).present? }
end

#validates_whitelist_blacklistObject



121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/foreman_virt_who_configure/config.rb', line 121

def validates_whitelist_blacklist
  case listing_mode.to_i
    when WHITELIST
      unless whitelist.present? || filter_host_parents.present?
        [:whitelist, :filter_host_parents].each { |f| errors.add(f, "Filter hosts or Filter host parents is required") }
      end
    when BLACKLIST
      unless blacklist.present? || exclude_host_parents.present?
        [:blacklist, :exclude_host_parents].each { |f| errors.add(f, "Exclude hosts or Exclude host parents is required") }
      end
  end
end

#virt_who_bash_scriptObject



240
241
242
# File 'app/models/foreman_virt_who_configure/config.rb', line 240

def virt_who_bash_script
  virt_who_config_script(:bash_script)
end

#virt_who_config_commandObject



253
254
255
# File 'app/models/foreman_virt_who_configure/config.rb', line 253

def virt_who_config_command
  "hammer virt-who-config deploy --id #{id} --organization-id #{organization_id}"
end

#virt_who_config_script(format = nil) ⇒ Object



244
245
246
247
248
249
250
251
# File 'app/models/foreman_virt_who_configure/config.rb', line 244

def virt_who_config_script(format = nil)
  generator = OutputGenerator.new(self)
  if generator.ready_for_virt_who_output?
    generator.virt_who_output(format)
  else
    generator.missing_virt_who_input_messages.join("\n")
  end
end

#virt_who_touch!Object



257
258
259
260
261
# File 'app/models/foreman_virt_who_configure/config.rb', line 257

def virt_who_touch!
  self.last_report_at = DateTime.now.utc
  self.out_of_date_at = last_report_at + interval.minutes
  self.class.skip_permission_check { save! }
end