2018-11-01 08:56:32 -04:00
# frozen_string_literal: true
class ChaosController < ActionController :: Base
2018-11-01 13:20:34 -04:00
before_action :validate_request
2018-11-01 09:16:21 -04:00
def leakmem
2018-11-05 10:34:38 -05:00
memory_mb = ( params [ :memory_mb ] & . to_i || 100 )
duration_s = ( params [ :duration_s ] & . to_i || 30 ) . seconds
start = Time . now
2018-11-01 09:16:21 -04:00
retainer = [ ]
2018-11-05 10:34:38 -05:00
# Add `n` 1mb chunks of memory to the retainer array
memory_mb . times { retainer << " x " * 1 . megabyte }
duration_taken = ( Time . now - start ) . seconds
Kernel . sleep duration_s - duration_taken if duration_s > duration_taken
2018-11-01 09:16:21 -04:00
2018-11-28 13:08:21 -05:00
render plain : " OK "
2018-11-01 09:16:21 -04:00
end
def cpuspin
2018-11-05 10:34:38 -05:00
duration_s = ( params [ :duration_s ] & . to_i || 30 ) . seconds
2018-11-01 14:06:25 -04:00
end_time = Time . now + duration_s . seconds
2018-11-05 10:34:38 -05:00
rand while Time . now < end_time
2018-11-01 09:16:21 -04:00
2018-11-28 13:08:21 -05:00
render plain : " OK "
2018-11-01 09:16:21 -04:00
end
2018-11-01 08:56:32 -04:00
def sleep
2018-11-05 10:34:38 -05:00
duration_s = ( params [ :duration_s ] & . to_i || 30 ) . seconds
2018-11-01 08:56:32 -04:00
Kernel . sleep duration_s
2018-11-05 10:34:38 -05:00
2018-11-28 13:08:21 -05:00
render plain : " OK "
2018-11-01 08:56:32 -04:00
end
2018-11-01 09:16:21 -04:00
def kill
Process . kill ( " KILL " , Process . pid )
end
2018-11-01 13:20:34 -04:00
private
def validate_request
secret = ENV [ 'GITLAB_CHAOS_SECRET' ]
2018-11-05 10:34:38 -05:00
# GITLAB_CHAOS_SECRET is required unless you're running in Development mode
if ! secret && ! Rails . env . development?
2018-11-28 13:08:21 -05:00
render plain : " chaos misconfigured: please configure GITLAB_CHAOS_SECRET when using GITLAB_ENABLE_CHAOS_ENDPOINTS outside of a development environment " , status : :internal_server_error
2018-11-05 10:34:38 -05:00
end
2018-11-01 13:20:34 -04:00
return unless secret
unless request . headers [ " HTTP_X_CHAOS_SECRET " ] == secret
2018-11-28 13:08:21 -05:00
render plain : " To experience chaos, please set X-Chaos-Secret header " , status : :unauthorized
2018-11-01 13:20:34 -04:00
end
end
2018-11-01 08:56:32 -04:00
end