client_management/app/controllers/admin/plow_controller.rb

73 lines
2.1 KiB
Ruby

class Admin::PlowController < ApplicationController
include ReverseProxy::Controller
SocketFile = "#{Rails.root}/tmp/plow.sock"
Binary = File.expand_path("../../../../bin/plow", __FILE__)
LogFile = "#{Rails.root}/tmp/plow_summary.log"
before_action :setup_setting, only: [:setting, :save_setting]
before_action :check_login
skip_before_action :verify_authenticity_token
def index
reverse_proxy "unix://#{Rails.root}/tmp/plow.sock", path: '/' do |config|
end
end
def show
path = request.env['ORIGINAL_FULLPATH']#.gsub("/admin/plow", "")
reverse_proxy "unix://#{Rails.root}/tmp/plow.sock", path: path do |config|
end
end
def setting
if File.exist?(LogFile)
@logContent = File.read(LogFile).gsub("\n", "<br>").html_safe
end
end
def save_setting
@setting.update_attributes(params[:plow_setting].permit!)
if File.exist?(SocketFile)
if @plow_pid
Process.kill(:INT, @plow_pid)
end
`rm #{SocketFile}`
end
if params['act'] == 'Start Test'
uri = URI.parse(@setting.url)
host = uri.host
ip = @setting.ip
concurrent = @setting.concurrent
rate = @setting.rate
scheme = uri.scheme
Thread.new do
if !ip.blank?
`ulimit -n 100000 && #{Binary} #{scheme}://#{ip}#{uri.request_uri} --host=#{host} --listen=#{SocketFile} -c#{concurrent} --rate #{rate} --summary > #{LogFile}`
else
`ulimit -n 100000 && #{Binary} #{@setting.url} --listen=#{SocketFile} -c#{concurrent} --rate #{rate} --summary > #{LogFile}`
end
end
max_wait = 10
while 1
sleep(1)
setup_setting
max_wait -= 1
if @enable_plow
break
end
end
end
redirect_to admin_plow_setting_path
end
private
def check_login
user = current_user
if user.nil? || !user.is_admin?
return render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found, :formats => [:html]
end
end
def setup_setting
@setting = PlowSetting.first
@plow_pid = `fuser #{SocketFile}`.gsub(/\n/, '').to_i
@enable_plow = @plow_pid > 0
end
end