Class: HammerCLIForemanAdmin::LoggingCommand
- Inherits:
-
HammerCLI::AbstractCommand
- Object
- HammerCLI::AbstractCommand
- HammerCLIForemanAdmin::LoggingCommand
- Defined in:
- lib/hammer_cli_foreman_admin/logging_command.rb
Instance Method Summary collapse
- #action_functions ⇒ Object
- #available_components ⇒ Object
- #check_options(hash, action_name, *required_keys) ⇒ Object
- #configure_component(component, level) ⇒ Object
- #execute ⇒ Object
- #new_level ⇒ Object
- #run_command(cmd, raise_on_error = true, verbose = false) ⇒ Object
Instance Method Details
#action_functions ⇒ Object
40 41 42 43 44 45 46 47 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 |
# File 'lib/hammer_cli_foreman_admin/logging_command.rb', line 40 def action_functions @action_functions ||= { :run_command => lambda do |opts| opts, :run_command, :command run_command opts[:command], false end, :run_command_on_file => lambda do |opts| opts, :run_command_on_file, :command, :file run_command "#{opts[:command]} #{opts[:file]}", false end, :create_file => lambda do |opts| 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| opts, :remove_file, :file File.unlink(opts[:file]) if File.exist?(opts[:file]) end, :ensure_line_is_present => lambda do |opts| 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] join_with = opts[:join_with] || ' ' content = File.read(opts[:file]).gsub(/#* ?#{left}\s*#{mid}\s*.*$/, opts[:line].join(join_with)) open(opts[:file], "w") { |file| file << content } end end } end |
#available_components ⇒ Object
111 112 113 |
# 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) ⇒ Object
34 35 36 37 38 |
# File 'lib/hammer_cli_foreman_admin/logging_command.rb', line 34 def (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) ⇒ Object
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 |
# 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 logger.info "Processing #{name} action #{action_name}" func.call(action) unless option_dry_run? 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 ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# 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.('../../../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 ⇒ Object
74 75 76 |
# 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) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/hammer_cli_foreman_admin/logging_command.rb', line 22 def run_command(cmd, raise_on_error = true, verbose = false) output = `#{cmd}` 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 _("Command '%{cmd}' failed: %{e}") % {:cmd => cmd, :e => e} end end |