197 lines
5.0 KiB
Ruby
197 lines
5.0 KiB
Ruby
require "open3"
|
|
class Admin::PlaygroundController < OrbitAdminController
|
|
before_filter :check_for_testers, :only => "index"
|
|
layout "structure"
|
|
include Admin::PlaygroundHelper
|
|
begin
|
|
include BundlerHelper
|
|
rescue
|
|
def bundler_with_clean_env
|
|
if block_given?
|
|
if Bundler.respond_to?(:with_unbundled_env)
|
|
Bundler.with_unbundled_env(&Proc.new)
|
|
else
|
|
Bundler.with_clean_env(&Proc.new)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
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" => "finish", "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
|
|
if command == 'bundle update'
|
|
system('sleep 2')
|
|
bundler_with_clean_env{system("#{RESTART_CMD}")}
|
|
end
|
|
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 ="#{RESTART_CMD}"
|
|
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 = "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}"
|
|
Thread.new do
|
|
bundler_with_clean_env{system(content)}
|
|
end
|
|
end
|
|
|
|
def check_for_testers
|
|
render_401 if !current_user.beta_tester
|
|
end
|
|
end |