class HammerCLIForemanAdmin::LoggingCommand
Public Instance Methods
action_functions()
click to toggle source
# File lib/hammer_cli_foreman_admin/logging_command.rb, line 40 def action_functions @action_functions ||= { :run_command => lambda do |opts| check_options opts, :run_command, :command run_command opts[:command], false end, :run_command_on_file => lambda do |opts| check_options opts, :run_command_on_file, :command, :file run_command "#{opts[:command]} #{opts[:file]}", false end, :create_file => lambda do |opts| check_options opts, :create_file, :file, :contents FileUtils.mkdir_p(File.dirname(opts[:file])) open(opts[:file], 'w') { |f| f.puts opts[:contents] } end, :remove_file => lambda do |opts| check_options opts, :remove_file, :file File.unlink(opts[:file]) if File.exist?(opts[:file]) end, :ensure_line_is_present => lambda do |opts| check_options opts, :ensure_line_is_present, :file, :line if File.foreach(opts[:file]).grep(/#{opts[:line][0]}/).empty? open(opts[:file], 'a') { |f| f.puts "\n" + opts[:line].join } else left = opts[:line][0] mid = opts[:line][1] right = opts[:line][2] content = File.read(opts[:file]).gsub(/#*#{left}\s*#{mid}\s*.*$/, opts[:line].join(' ')) open(opts[:file], "w") { |file| file << content } end end } end
available_components()
click to toggle source
# File lib/hammer_cli_foreman_admin/logging_command.rb, line 111 def available_components HammerCLI::Settings.get(:admin)[:logging][:component] end
check_options(hash, action_name, *required_keys)
click to toggle source
# File lib/hammer_cli_foreman_admin/logging_command.rb, line 34 def check_options(hash, action_name, *required_keys) required_keys.each do |key| raise("Missing option '#{key}' in action '#{action_name}' component '#{hash[:name]}'") unless hash[key] end end
configure_component(component, level)
click to toggle source
# File lib/hammer_cli_foreman_admin/logging_command.rb, line 78 def configure_component(component, level) name = component[:name] friendly_name = component[:friendly_name] file = component[:file] file = option_prefix + file if option_prefix backup_suffix = Time.now.utc.to_i.to_s(36) if File.exists?(file) unless option_no_backup? || option_dry_run? backup_file = "#{file}.#{backup_suffix}~" logger.info "Creating backup #{backup_file}" FileUtils.cp(file, backup_file) end component[level].each do |action| action_name = action[:action] action[:name] = name if action[:file] action[:file] = option_prefix + action[:file] if option_prefix else action[:file] = file end func = action_functions[action_name.to_sym] if func && ! option_dry_run? logger.info "Processing #{name} action #{action_name}" func.call(action) else raise "Unknown action #{action_name} for component #{name}" end end else logger.info "Skipped component #{name}, file #{file} does not exist" end end
execute()
click to toggle source
# File lib/hammer_cli_foreman_admin/logging_command.rb, line 115 def execute # FIXME Workaround until https://github.com/theforeman/hammer-cli/pull/192/files HammerCLI::Settings.load_from_paths([File.expand_path('../../../config', __FILE__)]) unless HammerCLI::Settings.get(:admin) configuration = HammerCLI::Settings.get(:admin)[:logging][:component] rescue raise("Missing logging YAML definitions (foreman_admin_logging_*.yml)") if option_list? output_definition = HammerCLI::Output::Definition.new output_definition.fields << Fields::Field.new(:label => _('Component'), :path => ["name"]) output_definition.fields << Fields::Field.new(:label => _('Auto-detected by existence of'), :path => ["file"]) output_definition.fields << Fields::Field.new(:label => _('Destinations'), :path => ["destinations"]) output = HammerCLI::Output::Output.new(context, :default_adapter => :table) output.print_collection(output_definition, HammerCLI::Settings.get(:admin)[:logging][:component]) else if option_all? components = configuration else raise("Unknown component provided, use --list to find them") unless option_components.all? { |c| available_components.map{|x| x[:name]}.include? c } components = configuration.select{ |x| option_components.include? x[:name] } end components.each { |component| configure_component(component, new_level) } end HammerCLI::EX_OK end
new_level()
click to toggle source
# File lib/hammer_cli_foreman_admin/logging_command.rb, line 74 def new_level option_level_debug? ? :debug : :production end
run_command(cmd, raise_on_error = true, verbose = false)
click to toggle source
# File lib/hammer_cli_foreman_admin/logging_command.rb, line 22 def run_command(cmd, raise_on_error = true, verbose = false) output = %x`#{cmd}` print_message output if verbose && output.chomp != '' raise("return value = #{$?.to_i}") if $?.to_i != 0 && raise_on_error rescue Exception => e if raise_on_error raise "Command '#{cmd}' failed: #{e}" else print_message _("Command '%{cmd}' failed: %{e}") % {:cmd => cmd, :e => e} end end