debian-mirror-gitlab/app/controllers/chaos_controller.rb

98 lines
2.1 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2022-10-11 01:57:18 +05:30
# rubocop:disable Rails/ApplicationController
2018-12-13 13:39:08 +05:30
class ChaosController < ActionController::Base
2019-10-12 21:52:04 +05:30
before_action :validate_chaos_secret, unless: :development_or_test?
2018-12-13 13:39:08 +05:30
def leakmem
2019-10-12 21:52:04 +05:30
do_chaos :leak_mem, Chaos::LeakMemWorker, memory_mb, duration_s
2018-12-13 13:39:08 +05:30
end
2019-09-30 21:07:59 +05:30
def cpu_spin
2019-10-12 21:52:04 +05:30
do_chaos :cpu_spin, Chaos::CpuSpinWorker, duration_s
2018-12-13 13:39:08 +05:30
end
2019-09-30 21:07:59 +05:30
def db_spin
2019-10-12 21:52:04 +05:30
do_chaos :db_spin, Chaos::DbSpinWorker, duration_s, interval_s
2019-09-30 21:07:59 +05:30
end
2018-12-13 13:39:08 +05:30
def sleep
2019-10-12 21:52:04 +05:30
do_chaos :sleep, Chaos::SleepWorker, duration_s
2018-12-13 13:39:08 +05:30
end
def kill
2021-04-29 21:17:54 +05:30
do_chaos :kill, Chaos::KillWorker, 'KILL'
end
def quit
do_chaos :kill, Chaos::KillWorker, 'QUIT'
2018-12-13 13:39:08 +05:30
end
2021-03-11 19:13:27 +05:30
def gc
gc_stat = Gitlab::Chaos.run_gc
render json: {
2021-09-30 23:02:18 +05:30
worker_id: ::Prometheus::PidProvider.worker_id,
2021-03-11 19:13:27 +05:30
gc_stat: gc_stat
}
end
2018-12-13 13:39:08 +05:30
private
2019-10-12 21:52:04 +05:30
def do_chaos(method, worker, *args)
if async
worker.perform_async(*args)
else
Gitlab::Chaos.public_send(method, *args) # rubocop: disable GitlabSecurity/PublicSend
end
2018-12-13 13:39:08 +05:30
2019-10-12 21:52:04 +05:30
render plain: "OK"
2019-09-30 21:07:59 +05:30
end
def validate_chaos_secret
unless chaos_secret_configured
render plain: "chaos misconfigured: please configure GITLAB_CHAOS_SECRET",
status: :internal_server_error
return
end
2018-12-13 13:39:08 +05:30
2019-09-30 21:07:59 +05:30
unless Devise.secure_compare(chaos_secret_configured, chaos_secret_request)
render plain: "To experience chaos, please set a valid `X-Chaos-Secret` header or `token` param",
status: :unauthorized
2018-12-13 13:39:08 +05:30
end
end
2019-09-30 21:07:59 +05:30
def chaos_secret_configured
ENV['GITLAB_CHAOS_SECRET']
end
def chaos_secret_request
request.headers["HTTP_X_CHAOS_SECRET"] || params[:token]
end
def interval_s
interval_s = params[:interval_s] || 1
interval_s.to_f.seconds
end
def duration_s
duration_s = params[:duration_s] || 30
duration_s.to_i.seconds
end
def memory_mb
memory_mb = params[:memory_mb] || 100
memory_mb.to_i
end
2019-10-12 21:52:04 +05:30
def async
async = params[:async] || false
Gitlab::Utils.to_boolean(async)
end
def development_or_test?
Rails.env.development? || Rails.env.test?
2019-09-30 21:07:59 +05:30
end
2018-12-13 13:39:08 +05:30
end
2022-10-11 01:57:18 +05:30
# rubocop:enable Rails/ApplicationController