This commit is contained in:
chiu 2020-02-20 13:00:39 +08:00
parent 202d2ba643
commit a70a22d436
1 changed files with 43 additions and 23 deletions

View File

@ -134,7 +134,7 @@ class Admin::SitesController < OrbitAdminController
end
def get_update_history
@update_log = %x{git log --pretty=format:"%ad','%s" --date=short}.split("\n").map{|log| log.gsub("'","").split(",")}.to_json
@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
@ -143,41 +143,61 @@ class Admin::SitesController < OrbitAdminController
@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 store_permissions["permission_granted"]
result = ""
need_stash = %x(git diff).blank?
%x(git stash) unless need_stash
%x(git fetch origin)
pull_result = %x(git pull -r --ff-only 2>&1 origin #{@branch})
%x(git stash pop) unless need_stash
if pull_result.include? "fatal: Not possible to fast-forward, aborting."
result = "failed"
if params['type'] == 'update'
if store_permissions["permission_granted"]
git_reset('origin','update')
render :text => 'waiting'
else
result = "success"
# Bundler.with_clean_env { `cd #{Rails.root} && bundle update` }
render :json => store_permissions.to_json
end
render :text => result
else
render :json => store_permissions.to_json
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 { `cd #{Rails.root} && BUNDLE_GEMFILE=built_in_extensions.rb bundle update && bundle` }
Bundler.with_clean_env { `cd #{Rails.root} && BUNDLE_GEMFILE=downloaded_extensions.rb bundle update && bundle` }
Bundler.with_clean_env { system("cd #{Rails.root} && bundle update") }
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 5
sleep 2
render :nothing => true
end
def restart_server
%x(kill -s USR2 `cat tmp/pids/unicorn.pid`)
sleep 5
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