class ForemanMaintain::Config

Attributes

backup_dir[RW]
completion_cache_file[RW]
config_file[RW]
db_backup_dir[RW]
definitions_dirs[RW]
disable_commands[RW]
foreman_port[RW]
foreman_proxy_cert_path[RW]
foreman_url[RW]
log_dir[RW]
log_file_size[RW]
log_filename[RW]
log_level[RW]
manage_crond[RW]
pre_setup_log_messages[RW]
storage_file[RW]

Public Class Methods

new(options) click to toggle source
# File lib/foreman_maintain/config.rb, line 10
def initialize(options)
  @pre_setup_log_messages = []
  @config_file = options.fetch(:config_file, config_file_path)
  @options = load_config
  @definitions_dirs = @options.fetch(:definitions_dirs,
    [File.join(source_path, 'definitions')])
  load_log_configs
  load_backup_dir_paths
  load_cron_option
  @foreman_proxy_cert_path = @options.fetch(:foreman_proxy_cert_path, '/etc/foreman')
  @completion_cache_file = File.expand_path(
    @options.fetch(:completion_cache_file, '~/.cache/foreman_maintain_completion.yml')
  )
  @disable_commands = @options.fetch(:disable_commands, [])
  @foreman_url = @options.fetch(:foreman_url) { `hostname -f`.chomp }
  @foreman_port = @options.fetch(:foreman_port, 443)
end

Public Instance Methods

use_color?() click to toggle source
# File lib/foreman_maintain/config.rb, line 28
def use_color?
  ENV['TERM'] && ENV.fetch('NO_COLOR', '') == '' && \
    system('command -v tput', out: File.open('/dev/null')) && `tput colors`.to_i > 0
end

Private Instance Methods

boolean?(value) click to toggle source
# File lib/foreman_maintain/config.rb, line 94
def boolean?(value)
  [true, false].include? value
end
config_file_path() click to toggle source
# File lib/foreman_maintain/config.rb, line 71
def config_file_path
  if defined?(CONFIG_FILE) && File.exist?(CONFIG_FILE)
    CONFIG_FILE
  else
    File.join(source_path, 'config/foreman_maintain.yml')
  end
end
find_dir_path(dir_path_str) click to toggle source
# File lib/foreman_maintain/config.rb, line 83
def find_dir_path(dir_path_str)
  dir_path = File.expand_path(dir_path_str)
  begin
    FileUtils.mkdir_p(dir_path, :mode => 0o750) unless File.exist?(dir_path)
  rescue StandardError => e
    warn "No permissions to create dir #{dir_path_str}"
    warn e.message.inspect
  end
  dir_path
end
load_backup_dir_paths() click to toggle source
# File lib/foreman_maintain/config.rb, line 44
def load_backup_dir_paths
  @storage_file = @options.fetch(:storage_file, 'data.yml')
  @backup_dir = find_dir_path(
    @options.fetch(:backup_dir, '/var/lib/foreman-maintain')
  )
  @db_backup_dir = find_dir_path(
    @options.fetch(:db_backup_dir, '/var/lib/foreman-maintain/db-backups')
  )
end
load_config() click to toggle source
# File lib/foreman_maintain/config.rb, line 59
def load_config
  if File.exist?(config_file)
    YAML.load(File.open(config_file)) || {}
  else
    @pre_setup_log_messages <<
      "Config file #{config_file} not found, using default configuration"
    {}
  end
rescue StandardError => e
  raise "Couldn't load configuration file. Error: #{e.message}"
end
load_cron_option() click to toggle source
# File lib/foreman_maintain/config.rb, line 54
def load_cron_option
  opt_val = @options.fetch(:manage_crond, true)
  @manage_crond = boolean?(opt_val) ? opt_val : true
end
load_log_configs() click to toggle source
# File lib/foreman_maintain/config.rb, line 35
def load_log_configs
  @log_level = @options.fetch(:log_level, ::Logger::INFO)
  @log_dir = find_dir_path(@options.fetch(:log_dir, 'log'))
  @log_file_size = @options.fetch(:log_file_size, 10_000)
  # Note - If timestamp added to filename then number of log files i.e second
  # argument to Logger.new will not work as expected
  @log_filename = File.expand_path("#{@log_dir}/foreman-maintain.log")
end
source_path() click to toggle source
# File lib/foreman_maintain/config.rb, line 79
def source_path
  File.expand_path('../..', __dir__)
end