class ForemanMaintain::Utils::Hammer

Attributes

settings[R]

Public Class Methods

instance() click to toggle source
# File lib/foreman_maintain/utils/hammer.rb, line 10
def self.instance
  @instance ||= new
end

Public Instance Methods

config_file() click to toggle source
# File lib/foreman_maintain/utils/hammer.rb, line 14
def config_file
  config_dir = File.dirname(ForemanMaintain.config_file)
  File.join(config_dir, 'foreman-maintain-hammer.yml')
end
configured?() click to toggle source
# File lib/foreman_maintain/utils/hammer.rb, line 50
def configured?
  File.exist?(config_file)
end
ready?() click to toggle source
# File lib/foreman_maintain/utils/hammer.rb, line 54
def ready?
  return @ready if defined? @ready
  return false unless configured?
  run_command('architecture list')
  @ready = true
rescue CredentialsError
  @ready = false
end
run_command(args) click to toggle source

Run a hammer command, examples: #run_command('host list')

# File lib/foreman_maintain/utils/hammer.rb, line 40
def run_command(args)
  output = execute("#{command_base} #{args}")
  if output =~ /Invalid username or password/
    raise CredentialsError, 'Invalid hammer credentials: '             'we expect the hammer username/password to be stored'             "in #{config_file}"
  end
  output
end
setup_from_answers(username = nil, password = nil) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/foreman_maintain/utils/hammer.rb, line 33
def setup_from_answers(username = nil, password = nil)
  save_config(:foreman => { :username => username, :password => password })
  ready?
end
setup_from_default() click to toggle source

tries to setup hammer based on default configuration and returns true if it succeeds rubocop:disable Metrics/AbcSize

# File lib/foreman_maintain/utils/hammer.rb, line 22
def setup_from_default
  return unless File.exist?(default_config_file)
  hammer_config = YAML.load_file(default_config_file)
  foreman_config = hammer_config.fetch(:foreman, {})
  if !foreman_config[:username].to_s.empty? && !foreman_config[:password].to_s.empty?
    save_config(hammer_config)
    ready? && default_config_file
  end
end

Private Instance Methods

command_base() click to toggle source
# File lib/foreman_maintain/utils/hammer.rb, line 69
def command_base
  %Q(LANG=en_US.utf-8 hammer -c "#{config_file}" --interactive=no)
end
default_config_file() click to toggle source
# File lib/foreman_maintain/utils/hammer.rb, line 65
def default_config_file
  @default_config_file ||= File.expand_path('~/.hammer/cli.modules.d/foreman.yml')
end
save_config(config) click to toggle source
# File lib/foreman_maintain/utils/hammer.rb, line 73
def save_config(config)
  remove_instance_variable '@ready' if defined? @ready
  File.open(config_file, 'w', 0o600) { |f| f.puts YAML.dump(config) }
end