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.19'
LIBVIRT_FAKE_PASSWORD =
'libvirt_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_chkconfig_issue', N_('Unable to enable virt-who service using chkconfig')),
  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 satellite-tools repository')),
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ OutputGenerator

Returns a new instance of OutputGenerator.



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

def initialize(config)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



24
25
26
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 24

def config
  @config
end

Instance Method Details

#config_file_pathObject



164
165
166
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 164

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

#crObject



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

def cr
  config.compute_resource
end

#cr_passwordObject



197
198
199
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 197

def cr_password
  config.hypervisor_type == 'libvirt' && config.hypervisor_password.blank? ? LIBVIRT_FAKE_PASSWORD : config.hypervisor_password
end

#cr_serverObject

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



189
190
191
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 189

def cr_server
  config.hypervisor_server
end

#cr_usernameObject



193
194
195
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 193

def cr_username
  config.hypervisor_username
end

#error_code(error_name) ⇒ Object



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

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

#error_handlingObject



139
140
141
142
143
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 139

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

#filteringObject



145
146
147
148
149
150
151
152
153
154
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 145

def filtering
  case config.listing_mode.to_i
    when ForemanVirtWhoConfigure::Config::UNLIMITED
      return ''
    when ForemanVirtWhoConfigure::Config::WHITELIST
      return filtering_line_sanitized('filter_hosts', config.whitelist)
    when ForemanVirtWhoConfigure::Config::BLACKLIST
      return filtering_line_sanitized('exclude_hosts', config.blacklist)
  end
end

#filtering_line_sanitized(filter, list) ⇒ Object



156
157
158
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 156

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

#hypervisor_idObject



176
177
178
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 176

def hypervisor_id
  config.hypervisor_id
end

#identifierObject



172
173
174
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 172

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

#missing_virt_who_input_messagesObject



39
40
41
42
43
44
45
46
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 39

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



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

def no_proxy
  config.no_proxy
end

#ownerObject



180
181
182
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 180

def owner
  config.organization.label
end

#proxyObject



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

def proxy
  config.proxy
end

#proxy_stringsObject



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

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

#ready_for_virt_who_output?Boolean

Returns:

  • (Boolean)


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

def ready_for_virt_who_output?
  missing_virt_who_input_messages.empty?
end

#sanitize(string) ⇒ Object



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

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

#sanitize_filter(list) ⇒ Object



160
161
162
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 160

def sanitize_filter(list)
  sanitize(list)
end

#sanitize_proxy(string) ⇒ Object



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

def sanitize_proxy(string)
  sanitize(string)
end

#satellite_urlObject



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

def satellite_url
  config.satellite_url
end

#service_user_passwordObject



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

def service_user_password
  config.service_user.encrypted_password
end

#service_user_usernameObject



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

def service_user_username
  config.service_user.username
end

#sysconfig_file_pathObject



168
169
170
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 168

def sysconfig_file_path
  '/etc/sysconfig/virt-who'
end

#typeObject



184
185
186
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 184

def type
  config.hypervisor_type
end

#virt_who_output(format = nil) ⇒ Object



48
49
50
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
# File 'app/models/foreman_virt_who_configure/output_generator.rb', line 48

def virt_who_output(format = nil)
  result = ''
  result += "#!/bin/bash\n" if format == :bash_script
  result += <<EOS
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 satellite-tools repository, minimal virt-who version is $minimal_version"
return 1
  else
return 0
  fi
}

result_code=#{error_code(:success)}
step 1 "Installing virt-who"
yum install -y virt-who || result_code=$(($result_code|#{error_code(:virt_who_installation)}))

if verify_minimal_version; then
  step 2 "Encrypting password"
  cr_password=`virt-who-password --password "#{cr_password}" 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}
env=Library
server=#{cr_server}
username=#{cr_username}
encrypted_password=$cr_password#{filtering}
rhsm_hostname=#{satellite_url}
rhsm_username=#{service_user_username}
rhsm_encrypted_password=$user_password
rhsm_prefix=/rhsm
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 > #{sysconfig_file_path} << EOF
### This configuration file is managed via the virt-who configure plugin
### manual edits will be deleted.
VIRTWHO_SATELLITE6=1
VIRTWHO_DEBUG=#{config.debug? ? 1 : 0}
VIRTWHO_INTERVAL=#{config.interval * 60}#{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"
  chkconfig virt-who on || result_code=$(($result_code|#{error_code(:virt_who_chkconfig_issue)}))
  service virt-who restart || 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 successully"
fi

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