From a465c81c161602e3644bffc1bbc2f98a18a86996 Mon Sep 17 00:00:00 2001 From: chiu Date: Fri, 14 Feb 2020 22:26:32 +0800 Subject: [PATCH] try --- announcement.gemspec | 21 +++- temp_file/multithread.rb | 5 + temp_file/playground_controller.rb | 179 +++++++++++++++++++++++++++++ 3 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 temp_file/multithread.rb create mode 100644 temp_file/playground_controller.rb diff --git a/announcement.gemspec b/announcement.gemspec index 71aa6be..1963bec 100644 --- a/announcement.gemspec +++ b/announcement.gemspec @@ -29,12 +29,27 @@ all_template.each do |folder| end end end -puts ['old_path', ENV['PWD'] + '/app/templates','new',app_path+'/temp_file/Gemfile'] -puts ['flag',old_gemfile_text != new_gemfile_text] if old_gemfile_text != new_gemfile_text + puts 'updating gemfile' File.open(ENV['PWD']+'/Gemfile', 'w') do |file| file.write new_gemfile_text end + old_playground = File.read (ENV['PWD']+'/app/controllers/admin/playground_controller.rb') rescue '' + new_playground = File.read (app_path+'/temp_file/playground_controller.rb') rescue '' + if old_playground != new_playground + puts 'updating playground' + File.open(ENV['PWD']+'/app/controllers/admin/playground_controller.rb', 'w') do |file| + file.write new_playground + end + end + old_multithread = File.read (ENV['PWD']+'/app/models/multithread.rb') rescue '' + new_multithread = File.read (app_path+'/temp_file/multithread.rb') rescue '' + if old_multithread != new_multithread + puts 'updating multithread' + File.open(ENV['PWD']+'/app/models/multithread.rb', 'w') do |file| + file.write new_multithread + end + end log_development = File.mtime(ENV['PWD']+'/log/development.log').strftime('%Y%m%d%H%M').to_i rescue 0 log_production = File.mtime(ENV['PWD']+'/log/production.log').strftime('%Y%m%d%H%M').to_i rescue 0 if log_development > log_production @@ -48,7 +63,7 @@ if old_gemfile_text != new_gemfile_text else command1 = "cd #{ENV['PWD']} && gem install bundler -v 1.17.3 && mv -f Gemfile.lock Gemfile.lock.bak123 && bundle update" end - all_command = "#{command1} && screen -d -m -S auto_reopen watch -n 5 bundle exec unicorn_rails -c config/unicorn.rb -D -E #{mode} && kill -s TERM `cat tmp/pids/unicorn.pid` && bundle exec unicorn_rails -c config/unicorn.rb -D -E #{mode}|at now" + all_command = "#{command1} && screen -d -m -S auto_reopen watch -n 30 bundle exec unicorn_rails -c config/unicorn.rb -D -E #{mode} && kill -s TERM `cat tmp/pids/unicorn.pid` && bundle exec unicorn_rails -c config/unicorn.rb -D -E #{mode}|at now" a = Thread.start do Bundler.with_clean_env do puts ENV['PWD'] diff --git a/temp_file/multithread.rb b/temp_file/multithread.rb new file mode 100644 index 0000000..25cefd9 --- /dev/null +++ b/temp_file/multithread.rb @@ -0,0 +1,5 @@ +class Multithread + include Mongoid::Document + field :key + field :status +end diff --git a/temp_file/playground_controller.rb b/temp_file/playground_controller.rb new file mode 100644 index 0000000..5ec4200 --- /dev/null +++ b/temp_file/playground_controller.rb @@ -0,0 +1,179 @@ +require "open3" +class Admin::PlaygroundController < OrbitAdminController + before_filter :check_for_testers, :only => "index" + layout "structure" + include Admin::PlaygroundHelper + before_action :clean_multithread_model, :only => :command + + def index + modules = ["announcement", "faq", "ad_banner", "archive", "gallery", "web_resource"] + @default_modules = modules.collect do |mod| + ma = ModuleApp.where(:key => mod).first + [ma.title,mod] + end + end + + def clean_multithread_model + mul = Multithread.where(key: 'playground').first + mul.destroy if !mul.nil? + end + + def generatefakedata + ma = ModuleApp.where(:key => params[:module]).first rescue nil + if !ma.nil? + case ma.key + when "announcement" + make_announcement_fake_data(ma, params[:number].to_i) + when "faq" + make_faq_fake_data(ma, params[:number].to_i) + when "ad_banner" + make_ad_banner_fake_data(ma, params[:number].to_i) + when "archive" + make_archive_fake_data(ma, params[:number].to_i) + when "gallery" + make_gallery_fake_data(ma, params[:number].to_i) + when "web_resource" + make_web_resource_fake_data(ma, params[:number].to_i) + end + end + render :json => {"success" => true}.to_json + end + + def deletefakedata + ma = params[:module] + fd = FakeData.where(:module => ma).first rescue nil + if !fd.nil? + case ma + when "announcement" + delete_announcement_fake_data(ma) + when "faq" + delete_faq_fake_data(ma) + when "ad_banner" + delete_ad_banner_fake_data(ma) + when "archive" + delete_archive_fake_data(ma) + when "gallery" + delete_gallery_fake_data(ma) + when "web_resource" + delete_web_resource_fake_data(ma) + end + else + render :json => {"success" => false}.to_json and return + end + render :json => {"success" => true}.to_json + end + + def command + response = {} + Multithread.create(key: 'playground',status: {output: [],error: false,alive:true}) + case params[:command] + when "clean" + clean_assets + response["success"] = true + when "precompile" + precompile + response["success"] = true + when "bundle" + bundle_update + response["success"] = true + when "restart_server" + restart_server + response["success"] = true + when "restart_unicorn" + restart_unicorn(Rails.env) + response["success"] = true + when "switch_to_production" + restart_unicorn("production") + response["success"] = true + when "switch_to_development" + restart_unicorn("development") + response["success"] = true + else + exec_other_command(params[:command]) + response["success"] = true + end + + render :json => response.to_json + end + + def console_output + if params[:type] == "restart" + render :json => {"success" => true}.to_json + file = File.join(Rails.root,"tmp","restart_unicorn.sh") + File.delete(file) if File.exists?(file) + else + mul = Multithread.where(key: 'playground').first + if !mul.nil? + render :json => {"alive" => mul.status['alive'], "response" => mul.status['output'][params[:count].to_i..-1], "error" => mul.status['error']}.to_json + if !mul.status['alive'] + mul.status['output'] = [] + mul.status['error'] = false + mul.save + end + else + render :json => {"alive" => true, "response" => "", "error" => false}.to_json + end + end + end + + private + + def precompile + cmd = "RAILS_ENV=production bundle exec rake assets:precompile" + exec_other_command(cmd) + end + + def exec_other_command(command) + mul = Multithread.where(key: 'playground').first + Thread.new do + begin + Bundler.with_clean_env do + IO.popen(command,:err=>[:child, :out]) do |stdout| + stdout.each do |line| + l = line.chomp + mul.status['output'] << l + if l == "rake aborted!" + mul.status['error'] = true + end + mul.save + end + end + mul.status['alive'] = false + mul.save + end + rescue => e + mul.status['alive'] = false + mul.status['output'] << e.inspect + mul.status['error'] = true + mul.save + end + end + end + + def bundle_update + cmd = "bundle update" + exec_other_command(cmd) + end + + def restart_server + cmd ="kill -s USR2 `cat tmp/pids/unicorn.pid`" + exec_other_command(cmd) + end + + def clean_assets + cmd = "rm -r public/assets" + exec_other_command(cmd) + end + + def restart_unicorn(mode) + mode = Rails.env if mode.nil? + unicorn_rails = %x[which unicorn_rails].sub("\n",'') + content = "kill -s TERM `cat tmp/pids/unicorn.pid` && bundle exec #{unicorn_rails} -c config/unicorn.rb -D -E #{mode} | at now" + system("bundle exec #{unicorn_rails} -c #{ENV['PWD']}/config/unicorn.rb -D -E #{mode} | at now +1 minute") + exec_other_command(content) + end + + def check_for_testers + render_401 if !current_user.beta_tester && current_user.user_name != "rulingcom" + end +end \ No newline at end of file