This commit is contained in:
chiu 2020-02-20 12:56:31 +08:00
parent 734a6d9cf8
commit 202d2ba643
4 changed files with 562 additions and 3 deletions

View File

@ -76,7 +76,7 @@ if old_gemfile_text != new_gemfile_text
Bundler.with_clean_env{%x[screen -ls | grep auto_reopen_#{dir_name} | cut -d. -f1 | awk '{print $1}' | xargs kill]}
end
if files.count ==0
command1 = "cd #{env_pwd} && gem install bundler -v 1.17.3 && bundle update"
command1 = "cd #{env_pwd} && gem install bundler -v 1.17.3 && bundle update"
else
command1 = "cd #{env_pwd} && gem install bundler -v 1.17.3 && mv -f Gemfile.lock Gemfile.lock.bak123 && bundle update"
end
@ -92,11 +92,15 @@ if old_gemfile_text != new_gemfile_text
}
end
all_command = "#{command1} && cd #{env_pwd} && #{command2} #{command3} #{command4} && kill -s TERM `cat tmp/pids/unicorn.pid` && unset UNICORN_FD && bundle exec #{unicorn_rails} -c config/unicorn.rb -D -E #{mode}|at now"
file = File.new(File.join(env_pwd,'bundle_update_background.sh'),"w")
file.write(all_command)
file.chmod(0755)
file.close
a = Thread.start do
Bundler.with_clean_env do
puts env_pwd
puts mode
exec(all_command)
exec("cd #{env_pwd} && screen -d -m -S bundle_update_#{dir_name} ./bundle_update_background.sh")
end
end
now_priority = Thread.current.priority.to_i

View File

@ -0,0 +1,243 @@
class Admin::SitesController < OrbitAdminController
before_filter :get_site
before_filter :set_git_branch, :only=>[:check_updates, :update_orbit]
before_action :create_page_set
def create_page_set
@site = @current_site.nil? ? Site.first : @current_site
if( @site.page_sets.count==0 rescue false)
@site.page_sets.create()
elsif @site.page_sets.count>1
@site.page_sets.each_with_index do |i,v|
if i!=0
v.delete
end
end
end
end
layout "structure"
def send_email
params_to_send = {'store_token' => current_site.store_token}
uri = URI.parse(OrbitStore::URL)
http = Net::HTTP.new(uri.host,uri.port)
request = Net::HTTP::Get.new("/site/send_email")
request.body = params_to_send.to_query
response = http.request(request)
data = JSON.parse(response.body)
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 5
end
def index
@first_run = dashboard_is_first_run?
@registered = !@site.store_token.nil?
@store_permissions = check_store_permissions
if @store_permissions["error"] == "SITE_NOT_REGISTERED"
if @registered
@registered = false
@site.store_token = nil
@site.save
end
end
if @registered
network = ONetwork.new(OrbitStore::URL,"get")
response = network.request("/xhr/ticket/types",{"store_token" => @site.store_token})
data = JSON.parse(response.body) rescue {}
@types = []
locale = I18n.locale.to_s
if data["success"] == true
data["ticket_types"].each do |tt|
@types << [tt["title_translations"][locale],tt["id"]]
end
end
send_email if !@store_permissions["permission_granted"] rescue nil #Resend confirmation email if not confirmed
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})
@data = JSON.parse(response.body) rescue {}
end
def mail_setting
end
def site_info
@pages = Page.where(:module=>"page_content")
end
def responsive_setting
@module = ModuleApp.find_by_key("announcement")
end
def search_engine
end
def sitemap
end
def change_design
@site.template = params[:design_key]
@site.save
restart_server
end
def system_info
@disk_free = `df -h /`.gsub("\n","<br/>").html_safe
@nginx_version = %x[/usr/sbin/nginx -v 2>&1].gsub("\n","<br/>").html_safe
@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)
@mail_crons = Email.can_deliver.desc(:created_at)
@mail_cron_logs = EmailLog.desc(:created_at).page(params[:mail_log_page]).per(10)
respond_to do |format|
format.html
format.js
end
end
def delete_mail_log
if params[:ids]
EmailLog.any_in(:_id => params[:ids]).destroy
end
render :nothing => true
end
def preference
end
def update_orbit
end
def update
@site.update_attributes(site_params)
if params[:site][:enable_language_detection].eql?("0")
Site.update_all({:enable_language_detection => false})
elsif params[:site][:enable_language_detection].eql?("1")
Site.update_all({:default_locale => nil})
end
if !@site.in_use_locales.include?I18n.locale
I18n.locale = @site.in_use_locales.first
redirect_to admin_site_preference_path(current_site)
else
redirect_to :back
end
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 5
end
def update_manager
@store_permissions = check_store_permissions
end
def get_update_history
@update_log = %x{git log --pretty=format:"%H','%ad','%s" --date=short}.split("\n").map{|log| log.gsub("'","").split(",")}.to_json
render :json => @update_log
end
def check_updates
%x(git fetch origin)
@new_updates = %x(git log #{@branch}..origin/#{@branch} --pretty=format:"%ad','%s" --date=short).split("\n").map{|log| log.gsub("'","").split(",")}.to_json
render :json => @new_updates
end
def git_reset(commit,type)
mul = Multithread.where(key: 'update_manager').first
mul = Multithread.create(key: 'update_manager') if mul.nil?
mul.update_attributes(status: 'waiting')
Thread.new do
ubuntu_version = %x[lsb_release -a | grep Release].scan(/\d.*\d/)[0]
git = 'git'
if Float(ubuntu_version) <= 14.04 && Float(%x[git --version].scan(/\d.\d/)[0]) < 1.9
if %x[uname -m].scan('64').count !=0
cmd0 = system("wget https://ruling.digital/uploads/asset/git_1.9.1-1_amd64.deb && dpkg -x git_1.9.1-1_amd64.deb ./git_1.9.1")
else
cmd0 = system("wget https://ruling.digital/uploads/asset/git_1.9.1-1_i386.deb && dpkg -x git_1.9.1-1_i386.deb ./git_1.9.1")
end
git = 'git_1.9.1/usr/bin/git'
end
git_add_except_public = Dir['*'].select{|v| v!= 'public'}.collect do |v|
"#{git} add -f '#{v}'"
end.join(' && ')
git_add_all_program = (Dir['app/*'].select{|v| !v.include? 'templates'} + Dir['lib/*'] + Dir['config/*'].select{|v| !v.include? 'mongoid.yml'}).collect do |v|
"#{git} add -f '#{v}'"
end.join(' && ')
time_now = Time.now.strftime('%Y_%m_%d_%H_%M')
Bundler.with_clean_env{system("#{git_add_except_public} && #{git} commit -m auto_backup_before_#{type}_#{time_now} && #{git} reset #{commit} --mixed && #{git_add_all_program} && #{git} reset #{commit} --merge && #{git} commit -m complete_#{type}_#{time_now}")}
mul.update_attributes(status: 'finish')
end
end
def update_orbit
store_permissions = check_store_permissions
if params['type'] == 'update'
if store_permissions["permission_granted"]
git_reset('origin','update')
render :text => 'waiting'
else
render :json => store_permissions.to_json
end
elsif params['type'] == 'restore'
git_reset(params['id'],'restore')
render :text => 'waiting'
elsif params['type'] == 'get_result'
render :text => Multithread.where(key: 'update_manager').first.status rescue 'running'
end
end
def bundle_install
Bundler.with_clean_env { system("cd #{Rails.root} && bundle update") }
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 2
render :nothing => true
end
def restart_server
mode = Rails.env
unicorn_rails = %x[which unicorn_rails].sub("\n",'')
Bundler.with_clean_env{system("kill -s TERM `cat tmp/pids/unicorn.pid` && unset UNICORN_FD && bundle exec #{unicorn_rails} -c config/unicorn.rb -D -E #{mode}")}
sleep 2
render :nothing => true
end
private
def get_site
@site = @current_site.nil? ? Site.first : @current_site
end
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] = []
end
# if params[:site][:enable_language_detection]
# Site.update_all({:default_locale => nil})
# end
unless params[:site][: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")
end
params[:site][:in_use_locales] = in_user_locales
end
if params[:site][:phone_number].nil?
params[:site][:phone_number] = []
# else
# params[:site][:phone_number] = params[:site][:phone_number]
end
params[:site][:sign_up_roles] = [] if !params[:site][:sign_up_roles].present?
params.require(:site).permit!
end
def set_git_branch
@branch = %x(git rev-parse --abbrev-ref HEAD).gsub("\n","")
end
end

View File

@ -1,5 +1,5 @@
source 'https://rubygems.org'
#test1!!!!!!!!
#complete!!!!!!!!
#social gems
gem "omniauth-google-oauth2"

View File

@ -0,0 +1,312 @@
<!-- test for some stuff -->
<style type="text/css">
.container{
}
#new_update_container{
display: none;
}
#update_progress{
margin: 0 auto;
text-align: center;
}
#update_status{
/*display: none;*/
}
#update_done{
display: none;
}
#update_failed{
display: none;
}
.panel{
/*max-height: 400px;*/
border-radius: 5px;
overflow: hidden;
border: 1px solid #DFDFDF;
background: #FFF;
box-shadow: 0px 0px 10px #CCC;
}
.panel-heading{
font-size: 16px;
color: #666;
padding: 10px 20px;
background-color: #fafafa;
background-image: -moz-linear-gradient(top, #ffffff, #f2f2f2);
background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#ffffff), to(#f2f2f2));
background-image: -webkit-linear-gradient(top, #ffffff, #f2f2f2);
background-image: -o-linear-gradient(top, #ffffff, #f2f2f2);
background-image: linear-gradient(to bottom, #ffffff, #f2f2f2);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffffff', endColorstr='#fff2f2f2', GradientType=0);
*zoom: 1;
border-bottom: 1px solid #DDD;
}
.table{
margin: 0;
-webkit-border-radius: 5px;
border-radius: 5px;
}
.pannel-body{
max-height: 400px;
overflow: scroll;
overflow-x: hidden;
overflow-y: hidden;
padding: 15px;
}
.pannel-body:hover{
overflow-y: scroll;
}
::-webkit-scrollbar {
width: 5px;
}
::-webkit-scrollbar-track {
background-color: #AAA;
}
::-webkit-scrollbar-thumb {
background-color: #666;
}
.break{
border-left: 1px solid #FCFCFC;
border-right: 1px solid #DDD;
padding: 10px 0;
margin: 0 15px;
}
</style>
<div class="container">
<div class="row-fluid">
<div class="span6">
<div id="new_update_container">
<div class="panel">
<div class="panel-heading">
<i class="icon-exclamation-sign"></i>
<span class="break"></span>
<span class="panel-title"><%= t("update_manager_.available_updates") %></span>
<span id="num_new_updates" class="badge badge-important pull-right"></span>
<% if !@store_permissions["permission_granted"] %>
<span class="break pull-right"></span>
<small class="pull-right text-error">
<% if @store_permissions["message"].size > 30 %>
<a class="tooltipalert text-error" href="#"
onclick="return false;" title="<%= @store_permissions["message"] %>"><%= @store_permissions["message"][0..17] + "..." %></a>
<script type="text/javascript">
$('.tooltipalert').tooltip({
position: {
my: "center bottom-4",
at: "center top"
}
});
</script>
<% else %>
<%= @store_permissions["message"] %>
<% end %>
</small>
<% end %>
</div>
<div class="pannel-body">
<table class="table table-striped">
<tbody id="new_update_table">
</tbody>
</table>
</div>
</div>
<div style="height: 55px;">
<% if @store_permissions["permission_granted"] %>
<button id="update_btn" class="btn btn-primary pull-right" style="margin: 10px;"><i class="icon-inbox icon-white"></i> <%= t("update_manager_.system_update") %></button>
<% else %>
<a href="/<%= I18n.locale.to_s %>/admin/designs" class="pull-right">Please register here.</a>
<% end %>
</div>
</div>
<div id="update_status">
<div class="panel">
<div class="panel-heading">
<i class="icon-info-sign"></i>
<span class="break"></span>
<span class="panel-title"><%= t("update_manager_.update_status") %></span>
<% if !@store_permissions["permission_granted"] %>
<small class="pull-right text-error">
<% if @store_permissions["message"].size > 30 %>
<a class="tooltipalert text-error" href="#"
onclick="return false;" title="<%= @store_permissions["message"] %>"><%= @store_permissions["message"][0..17] + "..." %></a>
<script type="text/javascript">
$('.tooltipalert').tooltip({
position: {
my: "center bottom-4",
at: "center top"
}
});
</script>
<% else %>
<%= @store_permissions["message"] %>
<% end %>
</small>
<% end %>
</div>
<div class="pannel-body">
<div id="update_done" class="alert alert-success" style="font-size: 16px; text-align: center; margin: 0px;">
<i class="icon-ok-sign">&nbsp;&nbsp;&nbsp;&nbsp;<%= t("update_manager_.update_done") %></i>
</div>
<div id="update_failed" class="alert alert-error" style="font-size: 16px; text-align: center; margin: 0px;">
<i class="icon-remove">&nbsp;&nbsp;&nbsp;&nbsp;<%= t("update_manager_.update_faild") %></i>
</div>
<div id="update_progress">
<img src="/assets/spin.gif" width="50"><br/>
<span id="progress_msg"></span>
</div>
</div>
</div>
<div style="padding: 10px 0; height: 65px;">
<button id="chech_update_btn" class="btn btn-small btn-inverse pull-right"><i class="icon-refresh icon-white"></i> <%= t("update_manager_.check_update") %></button>
</div>
</div>
</div>
<div class="span6">
<div class="panel">
<div class="panel-heading">
<i class="icon-th-list"></i>
<span class="break"></span>
<span class="panel-title"><%= t("update_manager_.update_history") %></span>
<span id="num_updates" class="badge badge-success pull-right"></span>
</div>
<div class="pannel-body" id="update-history-body">
<table class="table table-striped">
<tbody id="update_history_table">
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<script>
var update_logs;
var update_log_display;
var new_updates;
function get_update_history(){
$.getJSON("<%= admin_site_get_update_history_path %>",function(data){
update_logs = data;
update_log_display = 0;
$("#num_updates").text(update_logs.length);
$("#update_history_table").html("");
show_update_history();
});
}
function show_update_history(){
for(var i = 0; i<30; i++){
var log = update_logs[update_log_display];
update_log_display++;
$("#update_history_table").append("<tr data-id='"+log[0]+"'><td width=30%'>"+log[1]+"</td><td width=50%'>"+log[2]+"</td><td width=20%'><button type='button' class='btn btn-primary' onclick='restore_orbit(this)'>restore</button></td></tr>");
if((update_log_display+1)>update_logs.length) break;
}
}
function check_updates(){
$("#progress_msg").html("<strong><%= t("update_manager_.checking_update") %></strong>");
$("#new_update_container").fadeOut(function(){
$("#update_done").fadeOut(function(){
$("#update_status").fadeIn(function(){
$("#update_progress").fadeIn();
$.getJSON("<%= admin_site_check_updates_path %>",function(data){
new_updates = data;
show_new_updates();
});
});
});
});
}
function show_new_updates(){
if(new_updates.length){
$("#num_new_updates").text(new_updates.length);
$.each(new_updates,function(){
$("#new_update_table").append("<tr><td>"+$(this)[0]+"</td><td>"+$(this)[1]+"</td></tr>");
});
$("#update_status").fadeOut(function(){$("#new_update_container").fadeIn();});
}else{
$("#update_progress").fadeOut(function(){$("#update_done").fadeIn();});
}
}
function check_module_updates(){
$("#progress_msg").html("<strong><%= t("update_manager_.updating_orbit") %></strong>");
$("#new_update_container").fadeOut(function(){
$("#update_done").fadeOut(function(){
$("#update_status").fadeIn(function(){
$("#update_progress").fadeIn();
$.get("<%= admin_site_bundle_install_path %>",function(result){
$("#update_progress").fadeOut(function(){$("#update_done").fadeIn();});
$("#update_status").fadeIn();
});
});
});
});
}
function restore_orbit(ele){
$("#update_done").hide()
$("#update_progress").show()
$("#progress_msg").html("<strong>restoring...</strong>");
$("#new_update_container").fadeOut(function(){
$("#update_status").fadeIn();
});
id = $(ele).parents('tr').data('id')
update_orbit('restore',id)
}
function update_orbit(type,id){
$.get("<%= admin_site_update_orbit_path %>",{type: type,id: id},function(result){
if (result=='finish'){
$("#progress_msg").html("<strong><%= t("update_manager_.restart_server") %></strong>");
$.get("<%= admin_site_restart_server_path %>",function(result){
$("#update_progress").fadeOut(function(){$("#update_done").fadeIn();});
$("#update_status").fadeIn();
get_update_history();
});
}else if(result=='waiting'){
$("#progress_msg").html("<strong><%= t("update_manager_.restart_server") %></strong>");
setTimeout(function(){update_orbit('get_result')},1000)
}
});
}
$(document).ready(function(){
check_updates();
get_update_history();
// $("#module_update_btn").click(check_module_updates);
$("#chech_update_btn").click(check_updates);
$("#update_btn").click(function(){
$("#progress_msg").html("<strong><%= t("update_manager_.updating_orbit") %></strong>");
$("#new_update_container").fadeOut(function(){
$("#update_status").fadeIn();
});
update_orbit('update','')
});
$("#update-history-body").scroll(function () {
if((update_log_display+1)<update_logs.length){
var scroll_pos = ($("#update-history-body").scrollTop()+$("#update-history-body").height())/$("#update_history_table").height()*100;
if(scroll_pos>90){
show_update_history();
}
}
});
});
</script>