update site controller for old version kernel

This commit is contained in:
邱博亞 2022-10-01 14:34:46 +08:00
parent aee56f782c
commit 394304bd17
1 changed files with 76 additions and 36 deletions

View File

@ -1,7 +1,7 @@
class Admin::SitesController < OrbitAdminController
before_filter :get_site
before_filter :set_git_branch, :only=>[:check_updates, :update_orbit]
include Admin::GmailHelper
include BundlerHelper
helper_method :GeneratePermissionUrl
def get_all_databases
nginx_exe = %x[ps -o args -C nginx| grep 'daemon'| awk '{print $4}'].split("\n")[0]
@ -46,7 +46,7 @@ class Admin::SitesController < OrbitAdminController
end
layout "structure"
def send_email
params_to_send = {'store_token' => current_site.store_token}
params_to_send = {'store_token' => @site.store_token}
uri = URI.parse(OrbitStore::URL)
http = Net::HTTP.new(uri.host,uri.port)
request = Net::HTTP::Get.new("/site/send_email")
@ -60,7 +60,7 @@ class Admin::SitesController < OrbitAdminController
response.body = {'success'=>true}.to_json
end
data = JSON.parse(response.body)
Bundler.with_clean_env{system("UNICORN_PID=\"`fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` `cat tmp/pids/unicorn.pid `\" && kill -s USR2 $UNICORN_PID ; n=20; while (kill -0 $UNICORN_PID > /dev/null 2>&1) && test $n -ge 0; do printf '.' && sleep 1 && n=$(( $n - 1 )); done ; if test $n -lt 0; then kill -s TERM $UNICORN_PID; sleep 3; bundle exec unicorn_rails -c config/unicorn.rb -D -E #{Rails.env}; else kill -s QUIT $UNICORN_PID; fi")}
%x(#{RESTART_CMD})
sleep 5
end
def index
@ -89,7 +89,7 @@ class Admin::SitesController < OrbitAdminController
end
user_name = current_user.user_name rescue ''
network = ONetwork.new(OrbitStore::URL,"post")
response = network.request("/xhr/site/re_register_url",{"store_token" => current_site.store_token, 'site_domain' => request.host_with_port,'user' => user_name})
response = network.request("/xhr/site/re_register_url",{"store_token" => @site.store_token, 'site_domain' => request.host_with_port,'user' => user_name})
@data = JSON.parse(response.body) rescue {}
end
@ -123,7 +123,42 @@ class Admin::SitesController < OrbitAdminController
@mongo_version = `mongod --version`.split("\n")[0].html_safe
@linux_version = `lsb_release -d`.split(":")[1].html_safe rescue "Not Applicable"
@user_actions = UserAction.all.desc(:created_at).page(params[:page]).per(10)
if !params[:user_logs].nil?
@user_page = params[:page].to_i
@system_page = 0
@mongo_page = 0
@nginx_page = 0
elsif !params[:system_logs].nil?
@system_page = params[:page].to_i
@user_page = 0
@mongo_page = 0
@nginx_page = 0
elsif !params[:mongo_logs].nil?
@mongo_page = params[:page].to_i
@user_page = 0
@system_page = 0
@nginx_page = 0
elsif !params[:nginx_logs].nil?
@nginx_page = params[:page].to_i
@user_page = 0
@system_page = 0
@mongo_page = 0
else
@user_page = 1
@system_page = 1
@mongo_page = 1
@nginx_page = 1
end
@max_system_page = 1000
limit_num = 100
@user_actions = UserAction.all.desc(:created_at).page(@user_page).per(10) if @user_page != 0
@params = params
search_grep = params[:keywords].blank? ? nil : "|grep '#{params[:keywords]}'"
system_head = search_grep ? "-n #{@max_system_page*limit_num} #{search_grep} | head -n #{@system_page*limit_num||limit_num}" : "-n #{@system_page*limit_num}"
@system_logs = Kaminari.paginate_array(%x[journalctl -x --no-pager -r -o json #{system_head} | tail -#{limit_num}].split("\n").collect{|v| JSON.parse(v)}).page(1).per(limit_num) if @system_page != 0
@mongo_logs = Kaminari.paginate_array(%x[cat /var/log/mongodb/mongod.log*|grep error#{search_grep}|tac].split("\n").collect{|v| v.scan(/^((?:(?! ).)*) (.*)/)[0]}).page(@mongo_page).per(limit_num) if @mongo_page != 0
@nginx_logs = Kaminari.paginate_array(%x[cat /var/log/nginx/error.log#{search_grep}|tac].split("\n").collect{|v| v.scan(/^((?:(?!\[).)*)\[error\] (.*)/)[0]}.compact).page(@nginx_page).per(limit_num) if @nginx_page != 0
@mail_crons = Email.can_deliver.desc(:created_at)
@ -139,7 +174,7 @@ class Admin::SitesController < OrbitAdminController
if params[:ids]
EmailLog.any_in(:_id => params[:ids]).destroy
end
render :nothing => true
render :body => nil
end
def preference
@ -158,10 +193,10 @@ class Admin::SitesController < OrbitAdminController
def update
@site.update_attributes(site_params)
if params[:site][:enable_language_detection].eql?("0")
tmp = params[:site]
if tmp[:enable_language_detection].eql?("0")
Site.update_all({:enable_language_detection => false})
elsif params[:site][:enable_language_detection].eql?("1")
elsif tmp[:enable_language_detection].eql?("1")
if @site.in_use_locales.include?(:en)
Site.update_all({:default_locale => nil})
else
@ -170,13 +205,17 @@ class Admin::SitesController < OrbitAdminController
end
if !@site.in_use_locales.include? I18n.locale
I18n.locale = @site.in_use_locales.first
redirect_to admin_site_preference_path(current_site)
redirect_to admin_site_preference_path(@site)
else
redirect_to :back
begin
redirect_to :back
rescue
redirect_to admin_site_preference_path(@site)
end
end
Thread.new do
sleep 1
Bundler.with_clean_env{system("UNICORN_PID=\"`fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` `cat tmp/pids/unicorn.pid `\" && kill -s USR2 $UNICORN_PID ; n=20; while (kill -0 $UNICORN_PID > /dev/null 2>&1) && test $n -ge 0; do printf '.' && sleep 1 && n=$(( $n - 1 )); done ; if test $n -lt 0; then kill -s TERM $UNICORN_PID; sleep 3; bundle exec unicorn_rails -c config/unicorn.rb -D -E #{Rails.env}; else kill -s QUIT $UNICORN_PID; fi")}
%x(#{RESTART_CMD})
end
end
@ -224,7 +263,7 @@ class Admin::SitesController < OrbitAdminController
git_add_custom = (Dir['*'].select{|v| v !="config.ru" && v !='app' && v != 'lib' && v != 'config' && v != 'public' && v!= 'log' && v != 'dump' && v != 'tmp'} + ['app/templates','config/mongoid.yml','config/extra_lang.txt']).collect do |v|
"#{git} add -f --all --ignore-errors '#{v}'"
end.join(' ; ')
git_restore = "#{git} checkout ."
git_restore = "#{git} checkout -- `git ls-tree HEAD --name-only|sed 's/.ruby-version//g'|xargs`"
time_now = Time.now.strftime('%Y_%m_%d_%H_%M')
if %x[#{git} config user.name].empty?
%x[#{git} config --global user.name "rulingcom"]
@ -233,7 +272,7 @@ class Admin::SitesController < OrbitAdminController
%x[#{git} config --global user.email "orbit@rulingcom.com"]
end
site = Site.first
Bundler.with_clean_env{system("#{git_add_except_public} ; #{git} commit -m auto_backup_before_#{type}_#{time_now} --allow-empty && #{git} reset #{commit} --mixed ; #{git_add_custom} ; #{git_restore} ; #{git_add_except_public} ; #{git} clean -f -- app/models ; #{git} commit -m complete_#{type}_#{time_now} --allow-empty")}
bundler_with_clean_env{system("#{git_add_except_public} ; #{git} commit -m auto_backup_before_#{type}_#{time_now} --allow-empty && #{git} reset #{commit} --mixed ; #{git_add_custom} ; #{git_restore} ; #{git_add_except_public} ; #{git} clean -f -- app/models ; #{git} commit -m complete_#{type}_#{time_now} --allow-empty")}
site.update_attributes(update_flag: true) rescue nil
mul.update_attributes(status: 'finish')
end
@ -243,63 +282,64 @@ class Admin::SitesController < OrbitAdminController
if params['type'] == 'update'
if store_permissions["permission_granted"]
git_reset('origin','update')
render :text => 'waiting'
render :plain => 'waiting'
else
render :json => store_permissions.to_json
end
elsif params['type'] == 'restore'
git_reset(params['id'],'restore')
render :text => 'waiting'
render :plain => 'waiting'
elsif params['type'] == 'get_result'
render :text => Multithread.where(key: 'update_manager').first.status rescue 'running'
render :plain => Multithread.where(key: 'update_manager').first.status rescue 'running'
end
end
def bundle_install
Bundler.with_clean_env { system("cd #{Rails.root} && bundle update") }
Bundler.with_clean_env{system("UNICORN_PID=\"`fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` `cat tmp/pids/unicorn.pid `\" && kill -s USR2 $UNICORN_PID ; n=20; while (kill -0 $UNICORN_PID > /dev/null 2>&1) && test $n -ge 0; do printf '.' && sleep 1 && n=$(( $n - 1 )); done ; if test $n -lt 0; then kill -s TERM $UNICORN_PID; sleep 3; bundle exec unicorn_rails -c config/unicorn.rb -D -E #{Rails.env}; else kill -s QUIT $UNICORN_PID; fi")}
bundler_with_clean_env{system("cd #{Rails.root} && bundle update") }
%x(#{RESTART_CMD})
sleep 2
render :nothing => true
render :body => nil
end
def restart_server
mode = Rails.env
unicorn_rails = %x[which unicorn_rails].sub("\n",'')
render :nothing => true
Bundler.with_clean_env{system("UNICORN_PID=\"`fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` `cat tmp/pids/unicorn.pid `\" && kill -s TERM $UNICORN_PID ; while (kill -0 $UNICORN_PID > /dev/null 2>&1) ; do printf '.' && sleep 1 ; done ; unset UNICORN_FD; bundle exec unicorn_rails -c config/unicorn.rb -D -E #{mode}")}
render :body => nil
bundler_with_clean_env{system("UNICORN_PID=\"`fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` `cat tmp/pids/unicorn.pid `\" && kill -s TERM $UNICORN_PID ; while (kill -0 $UNICORN_PID > /dev/null 2>&1) ; do printf '.' && sleep 1 ; done ; unset UNICORN_FD; bundle exec unicorn_rails -c config/unicorn.rb -D -E #{mode}")}
end
private
def get_site
@site = @current_site.nil? ? Site.first : @current_site
end
private
def dashboard_is_first_run?
!current_user.is_tour_completed?("tickets")
end
def site_params
if params[:site][:default_bar_color]
params[:site][:mobile_bar_color] = []
tmp = params[:site]
if tmp[:default_bar_color].present?
tmp[:mobile_bar_color] = []
end
# if params[:site][:enable_language_detection]
# if tmp[:enable_language_detection]
# Site.update_all({:default_locale => nil})
# end
unless params[:site][:in_use_locales].nil?
unless tmp[:in_use_locales].nil?
in_user_locales = []
I18n.available_locales.each do |locale|
in_user_locales << locale if params[:site][:in_use_locales][locale].eql?("1")
in_user_locales << locale if tmp[:in_use_locales][locale].eql?("1")
end
params[:site][:in_use_locales] = in_user_locales
tmp[:in_use_locales] = in_user_locales
end
if params[:site][:phone_number].nil?
params[:site][:phone_number] = []
if tmp[:phone_number].nil?
tmp[:phone_number] = []
# else
# params[:site][:phone_number] = params[:site][:phone_number]
# tmp[:phone_number] = tmp[:phone_number]
end
tmp[:sign_up_roles] = [] if !tmp[:sign_up_roles].present?
if tmp[:privileged_ip].present?
tmp[:privileged_ip] = tmp[:privileged_ip].values rescue []
end
params[:site][:sign_up_roles] = [] if !params[:site][:sign_up_roles].present?
params.require(:site).permit!
end