179 lines
4.6 KiB
Ruby
179 lines
4.6 KiB
Ruby
|
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
|