diff --git a/app/views/admin/site_panel/_get_commands_list.html.erb b/app/views/admin/site_panel/_get_commands_list.html.erb new file mode 100644 index 0000000..913477f --- /dev/null +++ b/app/views/admin/site_panel/_get_commands_list.html.erb @@ -0,0 +1,12 @@ +<% + @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 +%> \ No newline at end of file diff --git a/app/views/admin/site_panel/_server_manager_index.html.erb b/app/views/admin/site_panel/_server_manager_index.html.erb index e5ef243..1af4d99 100644 --- a/app/views/admin/site_panel/_server_manager_index.html.erb +++ b/app/views/admin/site_panel/_server_manager_index.html.erb @@ -13,6 +13,14 @@ color: red; } + @@ -77,8 +85,15 @@ var status_relation = {"starting":"starting","execing":"execing","detecting":"detecting","error":"error","finish": "finish","closed":"closed"}; function show_exec_commands_block(server_names){ if($("#exec-commands-dialog-confirm").length == 0){ + var built_in_commands = "
" $("#main-wrap").before("
"+ - "
"+ + "
"+built_in_commands+""+ "
"); }; $( "#exec-commands-dialog-confirm" ).dialog({ @@ -100,7 +115,13 @@ })) } function exec_commands(){ - var commands = $('#commands_area').val().replace("\r\n","////").replace("\n","////").replace("
","////").replace("
","////"); + var commands = ""; + var built_in_commands_selected = []; + $("input.built_in_commands:checked").each(function(i,v){ + built_in_commands_selected.push($(v).data("key")); + }) + commands += built_in_commands_selected.join("////"); + commands += $('#commands_area').val().replace(/(\r\n|\n)/g,"////"); var server_names = []; var $els = $(".server_name_checkbox"); for(var i = 0; i < $els.length; i++){ diff --git a/app/views/admin/site_panel/_sites_list_table.html.erb b/app/views/admin/site_panel/_sites_list_table.html.erb index 4f9b814..618da13 100644 --- a/app/views/admin/site_panel/_sites_list_table.html.erb +++ b/app/views/admin/site_panel/_sites_list_table.html.erb @@ -1,3 +1,11 @@ +
@@ -293,8 +301,15 @@ }); function show_exec_commands_block(id){ if($("#exec-commands-dialog-confirm").length == 0){ + var built_in_commands = "
" $("#main-wrap").before("
"+ - "
"+ + "
"+built_in_commands+""+ "
"); }else{ $('#commands_area').attr('data-id',id); @@ -312,7 +327,13 @@ }); } function exec_commands(){ - var commands = $('#commands_area').val().replace(/(\r\n|\n)/,"////"); + var commands = ""; + var built_in_commands_selected = []; + $("input.built_in_commands:checked").each(function(i,v){ + built_in_commands_selected.push($(v).data("key")); + }) + commands += built_in_commands_selected.join("////"); + commands += $('#commands_area').val().replace(/(\r\n|\n)/g,"////"); var id = $('#commands_area').attr('data-id'); console.log(id); console.log(commands); diff --git a/config/locales/en.yml b/config/locales/en.yml index f6856af..1fef05a 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -7,6 +7,9 @@ en: upload_cert: Upload Cert cert_management: Cert Management client_management: + start_site_in_env: "Start site in %{env}" + upgrade_site: "Upgrade site" + bundle_update: "Bundle update" batch_install: Batch Install install_certbot_certificates_in_batches: Install certbot certificates in batches update_nginx_settings: "Update nginx settings" diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 24eebaf..deece3c 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -7,6 +7,9 @@ zh_tw: upload_cert: 上傳憑證 cert_management: 憑證管理 client_management: + start_site_in_env: "開啟網站為%{env}" + upgrade_site: 更新網站 + bundle_update: "Bundle update" batch_install: 批次安裝 install_certbot_certificates_in_batches: 批次安裝certbot憑證 update_nginx_settings: "修改nginx設定" diff --git a/lib/tasks/exec_command.rake b/lib/tasks/exec_command.rake index 0d03044..0d7868d 100644 --- a/lib/tasks/exec_command.rake +++ b/lib/tasks/exec_command.rake @@ -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 #{@site_construct.domain_name}") if @flag - update_infos_for_exec("execing #{command}") + update_thread_infos_for_exec(["execing #{command_i18n} on on #{@site_construct.domain_name}",""],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 #{@site_construct.domain_name}") if @flag - update_infos_for_exec("finish execing #{command}") + update_thread_infos_for_exec("finish execing #{command_i18n} on #{@site_construct.domain_name}") 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)