class ManageIQ::ApplianceConsole::MessageConfiguration

Constants

BASE_DIR
CONFIG_DIR
LOGS_DIR
MIQ_CONFIG_DIR
SAMPLE_CONFIG_DIR

Attributes

ca_cert_path[R]
client_properties_path[R]
config_dir_path[R]
keystore_dir_path[R]
keystore_path[R]
message_keystore_password[R]
message_keystore_username[R]
message_server_host[R]
message_server_port[R]
messaging_yaml_path[R]
messaging_yaml_sample_path[R]
miq_config_dir_path[R]
sample_config_dir_path[R]
truststore_path[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 25
def initialize(options = {})
  @message_server_port        = options[:message_server_port] || 9093
  @message_keystore_username  = options[:message_keystore_username] || "admin"
  @message_keystore_password  = options[:message_keystore_password]

  @miq_config_dir_path        = Pathname.new(MIQ_CONFIG_DIR)
  @config_dir_path            = Pathname.new(CONFIG_DIR)
  @sample_config_dir_path     = Pathname.new(SAMPLE_CONFIG_DIR)

  @client_properties_path     = config_dir_path.join("client.properties")
  @keystore_dir_path          = config_dir_path.join("keystore")
  @truststore_path            = keystore_dir_path.join("truststore.jks")
  @keystore_path              = keystore_dir_path.join("keystore.jks")

  @messaging_yaml_sample_path = miq_config_dir_path.join("messaging.kafka.yml")
  @messaging_yaml_path        = miq_config_dir_path.join("messaging.yml")
  @ca_cert_path               = keystore_dir_path.join("ca-cert")
end

Public Instance Methods

already_configured?() click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 44
def already_configured?
  installed_file_found = false
  installed_files.each do |f|
    if File.exist?(f)
      installed_file_found = true
      say("Installed file #{f} found.")
    end
  end
  installed_file_found
end
ask_questions() click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 55
def ask_questions
  return false unless valid_environment?

  ask_for_parameters
  show_parameters
  return false unless agree("\nProceed? (Y/N): ")

  return false unless host_reachable?(message_server_host, "Message Server Host:")

  true
end
configure_messaging_type(value) click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 182
def configure_messaging_type(value)
  say(__method__.to_s.tr("_", " ").titleize)

  ManageIQ::ApplianceConsole::Utilities.rake_run!("evm:settings:set", ["/prototype/messaging_type=#{value}"])
end
configure_messaging_yaml() click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 100
def configure_messaging_yaml
  say(__method__.to_s.tr("_", " ").titleize)

  return if file_found?(messaging_yaml_path)

  messaging_yaml = YAML.load_file(messaging_yaml_sample_path)

  messaging_yaml["production"].delete("username")
  messaging_yaml["production"].delete("password")

  messaging_yaml["production"]["hostname"]          = message_server_host
  messaging_yaml["production"]["port"]              = message_server_port
  messaging_yaml["production"]["sasl.mechanism"]    = "PLAIN"
  messaging_yaml["production"]["sasl.username"]     = message_keystore_username
  messaging_yaml["production"]["sasl.password"]     = ManageIQ::Password.try_encrypt(message_keystore_password)

  if secure?
    messaging_yaml["production"]["security.protocol"] = "SASL_SSL"
    messaging_yaml["production"]["ssl.ca.location"]   = ca_cert_path.to_path
  else
    messaging_yaml["production"]["security.protocol"] = "PLAINTEXT"
  end

  File.open(messaging_yaml_path, "w") do |f|
    f.write(messaging_yaml.to_yaml)
    f.chown(manageiq_uid, manageiq_gid)
  end
end
create_client_properties() click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 67
def create_client_properties
  say(__method__.to_s.tr("_", " ").titleize)

  return if file_found?(client_properties_path)

  algorithm = message_server_host.ipaddress? ? "" : "HTTPS"
  protocol = secure? ? "SASL_SSL" : "PLAINTEXT"
  content = secure? ? secure_client_properties_content(algorithm, protocol) : unsecure_client_properties_content(algorithm, protocol)

  File.write(client_properties_path, content)
end
file_contains?(path, content) click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 157
def file_contains?(path, content)
  return false unless File.exist?(path)

  content.split("\n").each do |l|
    l.gsub!("/", "\\/")
    l.gsub!(/password=.*$/, "password=") # Remove the password as it can have special characters that grep can not match.
    return false unless File.foreach(path).grep(/#{l}/).any?
  end

  say("Content already exists in #{path}. Taking no action.")
  true
end
file_found?(path) click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 143
def file_found?(path)
  return false unless File.exist?(path)

  say("\tWARNING: #{path} already exists. Taking no action.")
  true
end
files_found?(path_list) click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 150
def files_found?(path_list)
  return false unless path_list.all? { |path| File.exist?(path) }

  path_list.each { |path| file_found?(path) }
  true
end
host_reachable?(host, what) click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 170
def host_reachable?(host, what)
  require 'net/ping'
  say("Checking connectivity to #{host} ... ")
  unless Net::Ping::External.new(host).ping
    say("Failed.\nCould not connect to #{host},")
    say("the #{what} must be reachable by name.")
    return false
  end
  say("Succeeded.")
  true
end
remove_installed_files() click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 129
def remove_installed_files
  say(__method__.to_s.tr("_", " ").titleize)

  installed_files.each { |f| FileUtils.rm_rf(f) }
end
restart_evmserverd() click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 188
def restart_evmserverd
  say("Restart evmserverd if it is running...")
  evmserverd_service = LinuxAdmin::Service.new("evmserverd")
  evmserverd_service.restart if evmserverd_service.running?
end
secure?() click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 200
def secure?
  message_server_port == 9_093
end
secure_client_properties_content(algorithm, protocol) click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 79
      def secure_client_properties_content(algorithm, protocol)
        secure_content = <<~CLIENT_PROPERTIES
          ssl.truststore.location=#{truststore_path}
          ssl.truststore.password=#{message_keystore_password}
        CLIENT_PROPERTIES

        unsecure_client_properties_content(algorithm, protocol) + secure_content
      end
unconfigure() click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 194
def unconfigure
  configure_messaging_type("miq_queue") # Settings.prototype.messaging_type = 'miq_queue'
  restart_evmserverd
  remove_installed_files
end
unsecure_client_properties_content(algorithm, protocol) click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 88
      def unsecure_client_properties_content(algorithm, protocol)
        <<~CLIENT_PROPERTIES
          ssl.endpoint.identification.algorithm=#{algorithm}

          sasl.mechanism=PLAIN
          security.protocol=#{protocol}
          sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \\
            username=#{message_keystore_username} \\
            password=#{message_keystore_password} ;
        CLIENT_PROPERTIES
      end
valid_environment?() click to toggle source
# File lib/manageiq/appliance_console/message_configuration.rb, line 135
def valid_environment?
  if already_configured?
    unconfigure if agree("\nAlready configured on this Appliance, Un-Configure first? (Y/N): ")
    return false unless agree("\nProceed with Configuration? (Y/N): ")
  end
  true
end