This commit is contained in:
bohung committed 2022-11-14 12:46:26 +08:00
1 parent c4be16f884
commit e94b273e90
8 files changed
+269 -173

No files matched your search

+31 -13
View File
@@ -4,22 +4,29 @@ require 'json'
require 'base64'
namespace :exec_commands do
desc "Exec commands Script"
task :exec_commands,[:base64_args,:site_construct_id,:commands,:type,:server_name,:rails_env] => :environment do |task,args|
task :exec_commands,[:base64_args,:site_construct_id,:commands,:type,:server_name,:rails_env,:commands_i18n] => :environment do |task,args|
if args.base64_args.present?
tmp = JSON.parse(Base64.decode64(args.base64_args)) rescue {}
args = Rake::TaskArguments.new(tmp.keys, tmp.values)
end
@type = args.type
if !args.site_construct_id.blank?
if args.site_construct_id.present?
@site_construct = SiteConstruct.find(args.site_construct_id)
site_server = SiteServer.where(:server_name=>@site_construct.server_type).first
site_servers = [site_server]
@site_construct.update(:status=>"execing",:infos=>[""])
else
@site_construct = nil
site_servers = SiteServer.where(:server_name.in=>args.server_name.split("////")).to_a
end
if args.type == "exec_all"
Multithread.where(:key=>'execing_commands').each{|thread| thread.destroy if (thread.status["status"] == "error" || thread.status["status"] == "finish")}
Multithread.where(:key=>'execing_commands').each do |thread|
if thread.status["status"] == "error" || thread.status["status"] == "finish"
thread.destroy
elsif thread.updated_at < (Time.now - 5.minute)
thread.destroy
end
end
@thread = Multithread.where(:key=>'execing_commands').first
else
@thread = nil
@@ -38,8 +45,12 @@ namespace :exec_commands do
@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"})
if args.type == "exec_all"
if @thread.nil?
@thread = Multithread.create(:key=>'execing_commands',:status=>{"infos"=>[],"status"=>"execing"})
else
@thread.update(:status=>{"infos"=>[],"status"=>"execing"})
end
end
site_servers.each do |site_server|
ip = site_server.ip
@@ -102,21 +113,26 @@ namespace :exec_commands do
else
sites = [@site_construct]
end
if args.commands.include?("{{create_users}}")
commands = args.commands
if commands.class == String
commands = commands.split("////").select{|c| c != ""}
elsif commands.nil?
commands = []
end
if commands.include?("{{create_users}}")
commands = ['sudo -p "sudo password:" chmod 777 {{full_site_path}} -R',
'sudo -p "sudo password:" chmod 777 {{full_site_path}}/public -R',
'sudo -p "sudo password:" chmod 777 {{full_site_path}}/app/templates -R',
'sudo -p "sudo password:" useradd --home-dir {{full_site_path}}/app/templates {{site_name}} --no-create-home --badnames',
'sudo -p "sudo password:" bash -l -c \'echo -e "{{site_name}}\n{{site_name}}"|passwd {{site_name}}\'']
else
commands = args.commands.split("////").select{|c| c != ""} rescue [args.commands]
end
@commands_i18n = args.commands_i18n # command 客製化翻譯
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|
commands.each_with_index do |command, idx|
@command_i18n = command
update_flag = 1
@command_relations.each do |k,v|
@@ -126,8 +142,9 @@ namespace :exec_commands do
command = command.gsub("{{#{k}}}",v)
@command_i18n = @command_i18n.gsub("{{#{k}}}",@command_trans[k])
end
if command.include?('{{db_name}}')
@command_i18n = "create super manager"
if @commands_i18n
tmp = @commands_i18n[idx]
@command_i18n = tmp if tmp
end
command = command.gsub("{{rails_env}}",rails_env)
command = command.gsub("{{full_site_path}}",@site_construct.full_site_path)
@@ -136,8 +153,7 @@ namespace :exec_commands do
@command_i18n = @command_i18n.gsub("{{rails_env}}",rails_env)
@command_i18n = @command_i18n.gsub("{{full_site_path}}",@site_construct.full_site_path)
@command_i18n = @command_i18n.gsub("{{site_name}}",@site_construct.site_name)
command = command.gsub(/'{1,3}/,"'\"'\"'")
puts ['command',command,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'"]
command = command.gsub("'","'\"'\"'")
exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'", update_flag)
@command_i18n = nil
end
@@ -147,6 +163,8 @@ namespace :exec_commands do
end
if !@thread.nil?
@thread.update(:status=>@thread.status.merge({"status"=>"finish"}))
elsif @site_construct
@site_construct.update(:status =>"finish")
end
rescue => e
if !@thread.nil?