Class: ForemanVirtWhoConfigure::OutputGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/models/foreman_virt_who_configure/output_generator.rb

Defined Under Namespace

Classes: ConfigurationResult

Constant Summary collapse

MINIMUM_VIRT_WHO_VERSION =
'0.24.2'
LIBVIRT_FAKE_PASSWORD =
'libvirt_fake_password'
KUBEVIRT_FAKE_PASSWORD =
'kubevirt_fake_password'
CONFIGURATION_RESULTS =
[
  ConfigurationResult.new(0, 'success', N_('Success')),
  ConfigurationResult.new(1, 'virt_who_too_old', N_('Newer version of virt-who is required, minimum version is %s') % MINIMUM_VIRT_WHO_VERSION),
  ConfigurationResult.new(2, 'virt_who_config_file_issue', N_('Unable to create virt-who config file')),
  ConfigurationResult.new(4, 'virt_who_sysconfig_file_issue', N_('Unable to create sysconfig file')),
  ConfigurationResult.new(8, 'virt_who_systemctl_issue', N_('Unable to enable virt-who service using systemctl')),
  ConfigurationResult.new(16, 'virt_who_service_issue', N_('Unable to start virt-who service, please see virt-who logs for more details')),
  ConfigurationResult.new(32, 'virt_who_installation', N_('Unable to install virt-who package, make sure the host is properly subscribed and has access to katello-host-tools repository')),
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ OutputGenerator

Returns a new instance of OutputGenerator.



29
30
31
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 29

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



27
28
29
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 27

def config
  @config
end

Instance Method Details

#ahv_config_optionsObject



174
175
176
177
178
179
180
181
182
183
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 174

def ahv_config_options
  if config.hypervisor_type == 'ahv'
    prism_central = config.prism_flavor == "central"
    internal_debug = config.ahv_internal_debug.present? ? "\nahv_internal_debug=#{config.ahv_internal_debug}" : nil

    "\nprism_central=#{prism_central}#{internal_debug}"
  else
    ""
  end
end

#config_file_pathObject



214
215
216
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 214

def config_file_path
  "/etc/virt-who.d/#{identifier}.conf"
end

#connection_detailsObject



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

def connection_details
  if config.hypervisor_type == 'kubevirt'
    ""
  elsif config.hypervisor_type == 'libvirt'
    "\nserver=#{cr_server}
username=#{cr_username}"
  else
    "\nserver=#{cr_server}
username=#{cr_username}
encrypted_password=$cr_password"
  end
end

#crObject



270
271
272
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 270

def cr
  config.compute_resource
end

#cr_passwordObject



247
248
249
250
251
252
253
254
255
256
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 247

def cr_password
  case config.hypervisor_type
  when 'libvirt'
    config.hypervisor_password.presence || LIBVIRT_FAKE_PASSWORD
  when 'kubevirt'
    config.hypervisor_password.presence || KUBEVIRT_FAKE_PASSWORD
  else
    config.hypervisor_password
  end
end

#cr_serverObject

returns nil if url could not be parsed or is invalid, e.g. qemu:///system



239
240
241
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 239

def cr_server
  config.hypervisor_server
end

#cr_usernameObject



243
244
245
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 243

def cr_username
  Shellwords.escape(config.hypervisor_username)
end

#default_config_pathObject



218
219
220
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 218

def default_config_path
  '/etc/virt-who.conf'
end

#enabled_filters(filter) ⇒ Object



202
203
204
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 202

def enabled_filters(filter)
  filter.reject { |_, list| list.blank? }.map { |f, list| filtering_line_sanitized(f, list) }.join
end

#error_code(error_name) ⇒ Object



33
34
35
36
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 33

def error_code(error_name)
  result = CONFIGURATION_RESULTS.find { |r| r.identifier == error_name.to_s }
  result.try(:code)
end

#error_handlingObject



185
186
187
188
189
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 185

def error_handling
  CONFIGURATION_RESULTS.map do |result|
    "[ $(($result_code&#{result.code})) -ge 1 ] && echo '#{result.message}'"
  end.join("\n  ")
end

#filteringObject



191
192
193
194
195
196
197
198
199
200
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 191

def filtering
  case config.listing_mode.to_i
  when ForemanVirtWhoConfigure::Config::UNLIMITED
    ''
  when ForemanVirtWhoConfigure::Config::WHITELIST
    enabled_filters({'filter_hosts' => config.whitelist, 'filter_host_parents' => config.filter_host_parents})
  when ForemanVirtWhoConfigure::Config::BLACKLIST
    enabled_filters({'exclude_hosts' => config.blacklist, 'exclude_host_parents' => config.exclude_host_parents})
  end
end

#filtering_line_sanitized(filter, list) ⇒ Object



206
207
208
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 206

def filtering_line_sanitized(filter, list)
  "\n" + filter + '=' + sanitize_filter(list.to_s)
end

#hypervisor_idObject



226
227
228
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 226

def hypervisor_id
  config.hypervisor_id
end

#identifierObject



222
223
224
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 222

def identifier
  "virt-who-config-#{config.id}"
end

#missing_virt_who_input_messagesObject



42
43
44
45
46
47
48
49
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 42

def missing_virt_who_input_messages
  messages = []
  messages.push _('Owner was not provided') unless config.organization_id?
  messages.push _('Interval was not provided') unless config.interval.present?
  # messages.push _('Cofiguration not stored') unless config.persisted?
  # messages.push _('Service user was not provided') unless config.service_user_id.present?
  messages
end

#no_proxyObject



278
279
280
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 278

def no_proxy
  config.no_proxy
end

#ownerObject



230
231
232
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 230

def owner
  config.organization.label
end

#proxyObject



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

def proxy
  config.http_proxy
end

#proxy_stringsObject



290
291
292
293
294
295
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 290

def proxy_strings
  output = ''
  output << "\n#{proxy_type}=#{sanitize_proxy(proxy.full_url)}" if proxy.present?
  output << "\nno_proxy=#{sanitize_proxy(no_proxy)}" if no_proxy.present?
  output
end

#proxy_typeObject



286
287
288
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 286

def proxy_type
  "#{URI(proxy.url).scheme}_proxy"
end

#ready_for_virt_who_output?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 38

def ready_for_virt_who_output?
  missing_virt_who_input_messages.empty?
end

#sanitize(string) ⇒ Object



297
298
299
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 297

def sanitize(string)
  string.tr("\r\n", '').strip.chomp(",")
end

#sanitize_filter(list) ⇒ Object



210
211
212
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 210

def sanitize_filter(list)
  sanitize(list)
end

#sanitize_proxy(string) ⇒ Object



282
283
284
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 282

def sanitize_proxy(string)
  sanitize(string)
end

#satellite_urlObject



258
259
260
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 258

def satellite_url
  config.satellite_url
end

#service_user_passwordObject



266
267
268
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 266

def service_user_password
  config.service_user.encrypted_password
end

#service_user_usernameObject



262
263
264
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 262

def service_user_username
  config.service_user.username
end

#typeObject



234
235
236
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 234

def type
  config.hypervisor_type
end

#virt_who_output(format = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
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
148
149
150
151
152
153
154
155
156
157
158
159
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 51

def virt_who_output(format = nil)
  kubeconfig = config.hypervisor_type == 'kubevirt' ? "\nkubeconfig=#{config.kubeconfig_path}" : ''
  result = ''
  cr_password_base64 = Base64.strict_encode64(cr_password.to_s)
  result += "#!/bin/bash\n" if format == :bash_script
  result += <<~SCRIPT
    heading() {
      echo -e "\\n== $1 =="
    }

    step() {
      step_count=5
      heading "[$1/$step_count] $2"
    }

    version_lte() {
      [ "$1" = "`echo -e "$1\\n$2" | sort -V | head -n1`" ]
    }

    version_lt() {
      [ "$1" = "$2" ] && return 1 || version_lte $1 $2
    }

    verify_minimal_version() {
      minimal_version=#{MINIMUM_VIRT_WHO_VERSION}
      installed_version=`rpm -q --queryformat '%{VERSION}' virt-who`

      if version_lt $installed_version $minimal_version; then
        echo "virt-who $installed_version does not meet minimum requirements, please make sure this host is properly subscribed and has access to katello-host-tools repository, minimal virt-who version is $minimal_version"
        return 1
      else
        return 0
      fi
    }

    result_code=#{error_code(:success)}

    compose_install_command() {
      $1 packages unlock
      $1 advanced procedure run packages-install --packages virt-who --assumeyes || result_code=$(($result_code|#{error_code(:virt_who_installation)}))
      $1 packages lock
    }

    install_virt_who() {
      if `rpm -q satellite-maintain > /dev/null`; then
        compose_install_command satellite-maintain
      elif `rpm -q rubygem-foreman_maintain > /dev/null`; then
        compose_install_command foreman-maintain
      else
        yum install -y virt-who || result_code=$(($result_code|#{error_code(:virt_who_installation)}))
      fi
    }

    step 1 "Installing virt-who"
    install_virt_who

    if verify_minimal_version; then
      step 2 "Encrypting password"
      cr_password=$(virt-who-password --password $(echo #{cr_password_base64}|base64 -d) 2> /dev/null)
      user_password=$(virt-who-password --password '#{service_user_password}' 2> /dev/null)

      step 3 "Creating virt-who configuration"
      cat > #{config_file_path} << EOF
    ### This configuration file is managed via the virt-who configure plugin
    ### manual edits will be deleted.
    [#{identifier}]
    type=#{type}
    hypervisor_id=#{hypervisor_id}
    owner=#{owner}
    #{connection_details}#{filtering}
    rhsm_hostname=#{satellite_url}
    rhsm_username=#{service_user_username}
    rhsm_encrypted_password=$user_password
    rhsm_prefix=/rhsm#{kubeconfig}#{ahv_config_options}
    EOF
      if [ $? -ne 0 ]; then result_code=$(($result_code|#{error_code(:virt_who_config_file_issue)})); fi

      step 4 "Creating sysconfig virt-who configuration"
      cat > #{default_config_path} << EOF
    ### This configuration file is managed via the virt-who configure plugin
    ### manual edits will be deleted.
    [global]
    debug=#{config.debug? ? 1 : 0}
    interval=#{config.interval * 60}
    oneshot=False

    [system_environment]#{proxy_strings}
    EOF
      if [ $? -ne 0 ]; then result_code=$(($result_code|#{error_code(:virt_who_sysconfig_file_issue)})); fi

      step 5 "Enabling and restarting the virt-who service"
      systemctl enable virt-who || result_code=$(($result_code|#{error_code(:virt_who_systemctl_issue)}))
      systemctl restart virt-who || result_code=$(($result_code|#{error_code(:virt_who_service_issue)}))
    else
      result_code=$(($result_code|#{error_code(:virt_who_too_old)}))
    fi

    heading "Finished"
    if [ $result_code -ne 0 ]; then
      echo "There were some errors during configuration:"
      #{error_handling}
    else
      echo "Finished successfully"
    fi

  SCRIPT
  result += "exit $result_code\n" if format == :bash_script
  result
end