diff --git a/app/controllers/admin/plow_controller.rb b/app/controllers/admin/plow_controller.rb index dbbb6ae..2ea2c39 100644 --- a/app/controllers/admin/plow_controller.rb +++ b/app/controllers/admin/plow_controller.rb @@ -1,29 +1,64 @@ -class Admin::PlowController < ApplicationController +class Admin::PlowController < OrbitAdminController 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] def index - # Assuming the WordPress server is being hosted on port 8080 reverse_proxy "unix://#{Rails.root}/tmp/plow.sock", path: '/' do |config| - # We got a 404! - # config.on_missing do |code, response| - # redirect_to root_url and return - # end - - # There's also other callbacks: - # - on_set_cookies - # - on_connect - # - on_response - # - on_set_cookies - # - on_success - # - on_redirect - # - on_missing - # - on_error - # - on_complete end end def show - path = request.env['ORIGINAL_FULLPATH'] - reverse_proxy "unix://#{Rails.root}/tmp/plow.sock", path: '/' do |config| + 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", "
").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 + scheme = uri.scheme + Thread.new do + if !ip.blank? + `#{Binary} #{scheme}://#{ip}#{uri.request_uri} --host=#{host} --listen=#{SocketFile} -c#{concurrent} --summary > #{LogFile}` + else + `#{Binary} #{@setting.url} --listen=#{SocketFile} -c#{concurrent} --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 action: :setting + end + +private + def setup_setting + @setting = PlowSetting.first + @plow_pid = `fuser #{SocketFile}`.gsub(/\n/, '').to_i + @enable_plow = @plow_pid > 0 + + end end \ No newline at end of file diff --git a/app/models/plow_setting.rb b/app/models/plow_setting.rb new file mode 100644 index 0000000..26eac36 --- /dev/null +++ b/app/models/plow_setting.rb @@ -0,0 +1,8 @@ +class PlowSetting + include Mongoid::Document + include Mongoid::Timestamps + + field :concurrent, type: Integer, :default => 1 + field :ip, type: String + field :url, type: String +end \ No newline at end of file diff --git a/app/views/admin/plow/setting.html.erb b/app/views/admin/plow/setting.html.erb new file mode 100644 index 0000000..3b8b97c --- /dev/null +++ b/app/views/admin/plow/setting.html.erb @@ -0,0 +1,45 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "lib/main-forms" %> +<% end %> +

<%= t('client_management.stress_test') %>

+<%= form_for @setting, :url => {:action => :save_setting}, :html => {:class => "form-horizontal contract-form main-forms"} do |f| %> +
+
+ +
+ <%= f.text_field :url, :class => 'input-large' %> +
+
+ +
+ +
+ <%= f.text_field :ip, :class =>'input-large' %> +
+
+ +
+ +
+ <%= f.number_field :concurrent, :class => 'input-large' %> +
+
+ + <% if !@logContent.blank? %> +
+ +
+ <%= @logContent %> +
+
+ <% end %> +
+ <% if @enable_plow %> + <%= f.submit 'Stop Test', :name => 'act', :class => 'btn btn-primary' %> + + <% else %> + <%= f.submit 'Start Test', :name => 'act', :class => 'btn btn-primary' %> + <% end %> +
+
+<% end %> \ No newline at end of file diff --git a/bin/plow b/bin/plow new file mode 100644 index 0000000..6924cda Binary files /dev/null and b/bin/plow differ diff --git a/config/locales/en.yml b/config/locales/en.yml index 1e1b82f..287f5b6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -7,6 +7,7 @@ en: upload_cert: Upload Cert cert_management: Cert Management client_management: + stress_test: Stress Test select_all: "Select All" deselect_all: "Deselect All" get_template: "Get Template" diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index d2db12c..ef47536 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -7,6 +7,7 @@ zh_tw: upload_cert: 上傳憑證 cert_management: 憑證管理 client_management: + stress_test: 壓力測試 select_all: "全選" deselect_all: "取消全選" get_template: "獲取版型" diff --git a/config/routes.rb b/config/routes.rb index b407e5e..d69a6d1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,8 @@ Rails.application.routes.draw do # get "client_managements/contracts", to: 'client_managements#contracts' match 'plow' => 'plow#index', via: [:get, :post, :put, :patch, :delete] + get 'plow/setting' => 'plow#setting' + patch 'plow/save_setting' => 'plow#save_setting' match 'plow/*path' => 'plow#show', via: [:get, :post, :put, :patch, :delete] resources :client_managements do member do diff --git a/lib/client_management/engine.rb b/lib/client_management/engine.rb index 8aabbb6..761376f 100644 --- a/lib/client_management/engine.rb +++ b/lib/client_management/engine.rb @@ -10,9 +10,13 @@ module ClientManagement # frontend_enabled # data_count 1..30 require File.expand_path('../../../app/models/server_ability', __FILE__) + require File.expand_path('../../../app/models/plow_setting', __FILE__) if ServerAbility.count==0 ServerAbility.create() end + if PlowSetting.count==0 + PlowSetting.create() + end set_keyword_contstraints ["/cpanel/"] side_bar do head_label_i18n 'client_management.client_management', icon_class: "icons-users" @@ -65,6 +69,12 @@ module ClientManagement :priority=>1, :active_for_action=>{'admin/site_panel'=>"server_manager"}, :available_for => 'admin' + + context_link 'client_management.stress_test', + :link_path=>"admin_plow_setting_path" , + :priority=>1, + :active_for_action=>{'admin/plow'=>"setting"}, + :available_for => 'admin' end end end