Class: Admin::CommandsController

Inherits:
ApplicationController
  • Object
show all
Includes:
AuditLoggable
Defined in:
lib/generators/ruby_cms/templates/controllers/admin/commands_controller.rb

Constant Summary collapse

HISTORY_LIMIT =
30

Instance Method Summary collapse

Instance Method Details

#indexObject



11
12
13
14
15
16
17
18
# File 'lib/generators/ruby_cms/templates/controllers/admin/commands_controller.rb', line 11

def index
  @commands = visible_commands
  @last_runs = CommandRun.last_per_key
                         .where(command_key: @commands.map { |c| c[:key] })
                         .index_by(&:command_key)
  @history = CommandRun.recent.includes(:ran_by).limit(HISTORY_LIMIT).to_a
  @category_counts = build_category_counts(@commands)
end

#runObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/generators/ruby_cms/templates/controllers/admin/commands_controller.rb', line 20

def run
  key = params[:key].presence || params.dig(:command, :key)
  cmd = RubyCms.find_command(key)
  unless cmd
    respond_to do |format|
      format.html { redirect_to admin_settings_commands_path, alert: t("ruby_cms.admin.commands.unknown", default: "Unknown command.") }
      format.json { render json: { error: "Unknown command" }, status: :not_found }
    end
    return
  end

  require_permission!(cmd[:permission])

  cmd_params = sanitized_params_for(cmd)
  started_at = Time.current
  result = CommandRunner.run_rake(cmd[:rake_task], params: cmd_params)
  output = utf8_text(result[:output])

  run_record = CommandRun.create!(
    command_key:   cmd[:key],
    status:        result[:exit_status].to_i.zero? ? "ok" : "failed",
    duration_ms:   result[:duration_ms].to_i,
    output:        output,
    params_used:   cmd_params,
    ran_by:        respond_to?(:current_user, true) ? current_user : nil,
    ran_by_label:  user_label(current_user_cms),
    started_at:    started_at
  )

  audit!(
    run_record.status == "ok" ? :command_completed : :command_failed,
    target: "Command:#{cmd[:key]}",
    summary: "Ran rake #{cmd[:rake_task]} (#{run_record.status}, #{run_record.duration_ms}ms)",
    meta: { key: cmd[:key], rake_task: cmd[:rake_task], status: run_record.status, duration_ms: run_record.duration_ms, params: cmd_params }
  )

  respond_to do |format|
    format.html { redirect_to admin_settings_commands_path(active: cmd[:key]) }
    format.json do
      render json: {
        run: run_payload(run_record),
        command_output: output,
        app_log_tail: utf8_text(CommandRunner.tail_log)
      }
    end
  end
rescue StandardError => e
  Rails.logger.error("[RubyCMS] Commands#run: #{e.class}: #{e.message}")
  audit!(:command_failed, target: "Command:#{key}", summary: "rake #{key} crashed: #{e.message.to_s.truncate(200)}", meta: { key: key, error: e.message })
  begin
    CommandRun.create!(
      command_key:   key.to_s,
      status:        "failed",
      duration_ms:   0,
      error_message: e.message,
      ran_by:        respond_to?(:current_user, true) ? current_user : nil,
      ran_by_label:  user_label(current_user_cms),
      started_at:    Time.current
    )
  rescue StandardError
    nil
  end
  respond_to do |format|
    format.html { redirect_to admin_settings_commands_path, alert: e.message }
    format.json { render json: { error: e.message }, status: :unprocessable_content }
  end
end