Add built in commands options.

This commit is contained in:
bohung committed 2021-12-30 21:46:50 +08:00
1 parent e6ca9c468d
commit 47eef3d78a
6 files changed
+108 -23

No files matched your search

+44 -19
View File
@@ -19,6 +19,18 @@ namespace :exec_commands do
@thread = nil
end
if @thread.nil?
@command_trans = {}
@command_relations = {"upgrade_site"=>'git fetch origin && rails runner "a=Admin::SitesController.new;a.git_reset(%w(origin)[0],%w(update)[0]);while(1) do (Multithread.where(key: %w(update_manager)[0],status: %w(finish)[0]).count!=0 ? break : nil) end;sleep(5)"',
"bundle_update"=>"bundle update && kill -s USR2 `cat tmp/pids/unicorn.pid`",
"restart_site"=>"kill -s TERM `fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` && bundle exec unicorn_rails -c config/unicorn.rb -D -E {{rails_env}}"}
@command_relations.each do |k,v|
@command_trans[k] = I18n.t("client_management.#{k}")
end
rails_envs = ["production", "development"]
rails_envs.each do |env|
@command_relations["start_site_in_env,#{env}"] = "kill -s TERM `fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` && bundle exec unicorn_rails -c config/unicorn.rb -D -E #{env}"
@command_trans["start_site_in_env,#{env}"] = I18n.t("client_management.start_site_in_env",{:env=>env})
end
begin
if @thread.nil? && args.type == "exec_all"
@thread = Multithread.create(:key=>'execing_commands',:status=>{"infos"=>[],"status"=>"execing"})
@@ -78,23 +90,32 @@ namespace :exec_commands do
end
update_infos_for_exec("finish starting #{@site_construct.site_name}")
@site_construct.update(:status =>"finish")
elsif args.type == 'exec_all'
sites = SiteConstruct.where(:server_type=>site_server.server_name).to_a
else
if args.type == 'exec_all'
sites = SiteConstruct.where(:server_type=>site_server.server_name).to_a
else
sites = [@site_construct]
end
commands = args.commands.split("////").select{|c| c != ""} rescue [args.commands]
sites.each do |site_construct|
@site_construct = site_construct
@site_construct.update!(:infos=>[])
exec_ssh_command_by_sudo(ssh,"chmod 777 #{@site_construct.path}/#{@site_construct.site_name} -R")
rails_env = @site_construct.rails_env.blank? ? "development" : @site_construct.rails_env
commands.each do |command|
exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'")
@command_i18n = command
update_flag = 1
@command_relations.each do |k,v|
if command.include?("{{#{k}}}")
update_flag = 2 #Not logging commands result
end
command = command.gsub("{{#{k}}}",v)
@command_i18n = @command_i18n.gsub("{{#{k}}}",@command_trans[k])
end
command = command.gsub("{{rails_env}}",rails_env)
exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'", update_flag)
end
end
else
exec_ssh_command_by_sudo(ssh,"chmod 777 #{@site_construct.path}/#{@site_construct.site_name} -R")
commands = args.commands.split("////").select{|c| c != ""} rescue [args.commands]
commands.each do |command|
exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'")
end
end
end
end
@@ -148,15 +169,19 @@ namespace :exec_commands do
@thread.save!
return @thread.status["infos"]
end
def exec_ssh_command_and_see_output(session,command,update=true,output_string=false)
def exec_ssh_command_and_see_output(session,command,update_flag=1,output_string=false)
outputs = []
@flag = (@type == "exec_all")
command_i18n = command
update = (update_flag == true || update_flag == 1 || update_flag == 2)
update_outputs = (update_flag == true || update_flag == 1)
if update
command_i18n = @command_i18n if @command_i18n
if @site_construct
update_thread_infos_for_exec("execing #{command} on on <a href='#{((@site_construct.get_port == "443") ? "https" : "http")}://#{@site_construct.domain_name}#{((@site_construct.get_port=="80" || @site_construct.get_port=="443" || @site_construct.get_port.blank?) ? "" : (':'+@site_construct.get_port))}'>#{@site_construct.domain_name}</a>") if @flag
update_infos_for_exec("execing #{command}")
update_thread_infos_for_exec(["execing #{command_i18n} on on <a href='#{((@site_construct.get_port == "443") ? "https" : "http")}://#{@site_construct.domain_name}#{((@site_construct.get_port=="80" || @site_construct.get_port=="443" || @site_construct.get_port.blank?) ? "" : (':'+@site_construct.get_port))}'>#{@site_construct.domain_name}</a>",""],false,true) if @flag
update_infos_for_exec(["execing #{command_i18n}",""],false,true)
elsif @thread
update_thread_infos_for_exec("execing #{command}...")
update_thread_infos_for_exec(["execing #{command_i18n}...",""],false,true)
end
end
session.open_channel do |channel|
@@ -182,7 +207,7 @@ namespace :exec_commands do
new_arr << ""
end
outputs += new_arr
if update
if update_outputs
if first_output
update_thread_infos_for_exec(first_output,true) if @flag
update_infos_for_exec(first_output,true)
@@ -196,7 +221,7 @@ namespace :exec_commands do
else
outputs[-1] += (data_str rescue "")
end
if update
if update_outputs
update_thread_infos_for_exec(data_str,true) if @flag
update_infos_for_exec(data_str,true)
end
@@ -206,10 +231,10 @@ namespace :exec_commands do
channel.on_close do |ch|
if update
if @site_construct
update_thread_infos_for_exec("finish execing #{command} on <a href='#{((@site_construct.get_port == "443") ? "https" : "http")}://#{@site_construct.domain_name}#{((@site_construct.get_port=="80" || @site_construct.get_port=="443" || @site_construct.get_port.blank?) ? "" : (':'+@site_construct.get_port))}'>#{@site_construct.domain_name}</a>") if @flag
update_infos_for_exec("finish execing #{command}")
update_thread_infos_for_exec("finish execing #{command_i18n} on <a href='#{((@site_construct.get_port == "443") ? "https" : "http")}://#{@site_construct.domain_name}#{((@site_construct.get_port=="80" || @site_construct.get_port=="443" || @site_construct.get_port.blank?) ? "" : (':'+@site_construct.get_port))}'>#{@site_construct.domain_name}</a>") if @flag
update_infos_for_exec("finish execing #{command_i18n}")
elsif @thread
update_thread_infos_for_exec("finish execing #{command}")
update_thread_infos_for_exec("finish execing #{command_i18n}")
end
end
end
@@ -223,7 +248,7 @@ namespace :exec_commands do
return outputs
end
end
def exec_ssh_command_by_sudo_and_see_output(session,command,update=true,output_string=false)
def exec_ssh_command_by_sudo_and_see_output(session,command,update=1,output_string=false)
outputs = exec_ssh_command_and_see_output(session,command,update)
if outputs.join("\n").include?("Permission denied") || outputs.join("\n").include?("Operation not permitted")
outputs = exec_ssh_command_and_see_output(session,"sudo -p 'sudo password:' #{command}",update)