From 4f6f07280ad9c0ed076423e46badfe51aee09b4d Mon Sep 17 00:00:00 2001 From: BoHung Chiu Date: Wed, 19 Jul 2023 15:39:53 +0800 Subject: [PATCH] Fix bug. --- .../admin/site_panel_controller.rb | 2 +- .../site_panel/_sites_list_table.html.erb | 2 +- lib/tasks/copy_site.rake | 96 ++++++++++++++----- lib/tasks/create_site.rake | 76 ++++++++++++--- lib/tasks/exec_command.rake | 34 ++++++- 5 files changed, 168 insertions(+), 42 deletions(-) diff --git a/app/controllers/admin/site_panel_controller.rb b/app/controllers/admin/site_panel_controller.rb index 38a5ce8..ee902ce 100644 --- a/app/controllers/admin/site_panel_controller.rb +++ b/app/controllers/admin/site_panel_controller.rb @@ -358,7 +358,7 @@ class Admin::SitePanelController < OrbitAdminController when 'delete_from_list' SiteConstruct.find(params[:id]).destroy if Is_Rails5 - redirect_back(fallback_location: { action: "sites_list"}) + redirect_back(fallback_location: { action: "sites_list"}) and return else redirect_to :back and return end 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 154e601..3f0e6c6 100644 --- a/app/views/admin/site_panel/_sites_list_table.html.erb +++ b/app/views/admin/site_panel/_sites_list_table.html.erb @@ -286,7 +286,7 @@ }else{ $("#info_texts").html("Do you really want to "+$(this).attr('title')+"."); if($('#confirm_input').length == 0){ - $('#info_texts').after("\" style=\"width: 17em;\">"); + $('#info_texts').after("\" style=\"width: 17em;\">"); }else{ $('#confirm_input').css('display','block'); $('#confirm_input').val(''); diff --git a/lib/tasks/copy_site.rake b/lib/tasks/copy_site.rake index 7c92e44..44b8815 100644 --- a/lib/tasks/copy_site.rake +++ b/lib/tasks/copy_site.rake @@ -48,7 +48,8 @@ namespace :create_site do extra_exclude_path = ",public/uploads/" end server = SiteServer.where(:server_name => template_site.server_type).first - ls_out = exec_ssh_command_for_copy(ssh, "ls #{@site_construct.full_site_path}/app").join("\n") + update_infos("Copying site's files for #{args.site_name}!") + ls_out = exec_ssh_command_for_copy(ssh, "ls #{@site_construct.full_site_path}/app", false).join("\n") if ls_out.include?("No such") || org_creating if @site_construct.server_type == template_site.server_type exec_ssh_command_for_copy(ssh,"rsync -ar --info=progress2 --no-inc-recursive --delete --exclude={tmp/cache/,tmp/unicorn.sock,tmp/pids/*#{extra_exclude_path}} #{template_site.path}/#{template_site.get_site_name}/ #{args.path}/#{@site_construct.get_site_name}/",true) @@ -138,33 +139,68 @@ namespace :create_site do end return output.encode!("UTF-8", :invalid => :replace, :undef => :replace, :replace => '') end - def update_thread_infos_for_exec(info,update_last=false) - if update_last - @thread.status["infos"][-1] += info - else - @thread.status["infos"] = @thread.status["infos"].push(info) - end - @thread.save! - return @thread.status["infos"] - end - def exec_ssh_command_for_copy(session,command,update=false) + def exec_ssh_command_for_copy(session,command,update_outputs=false) outputs = [] session.open_channel do |channel| channel.request_pty do |channel, success| channel.exec("LANG=en.UTF8 #{command}") do |ch, success| abort "could not execute command: #{command}" unless success channel.on_data do |ch, data| - outputs.push(data) - print "#{data}" - if update - if data.include? "\n" - update_infos_for_exec(data) - else - update_infos_for_exec(data,true) - end - end - if data.to_s.include?("sudo password:") || data.to_s.include?("Password:") + data_str = data.to_s + data_str.encode!("UTF-8", :invalid => :replace, :undef => :replace, :replace => '') + if data_str.include?("sudo password:") || data_str.include?("Password:") channel.send_data "#{@password}\n" + else + print data_str unless @no_stdout + data_str.gsub!("\r\n", "\n") + rm_idx = data_str.index("\r") + if rm_idx.nil? + @remove_last_line = false + else + @remove_last_line = (outputs.count > 0 && data_str[0...rm_idx].exclude?("\n")) + end + data_str.gsub!(/.*\r/, '') + next if data_str.length == 0 + if data_str.include?("\n") || outputs.empty? + output_lines = data_str.split("\n") + first_output = output_lines[0] + if first_output + if @remove_last_line + outputs = outputs[1..-1] + outputs = [] if outputs.nil? + end + if outputs.count != 0 + outputs[-1] += first_output + else + outputs << first_output + end + end + new_arr = output_lines[1..-1] + new_arr = [] if new_arr.nil? + if data_str[-1] == "\n" + new_arr << "" + end + outputs += new_arr + if update_outputs + if first_output + update_infos_for_exec(first_output,true) + end + update_infos_for_exec(new_arr,false,true) + end + else + if @remove_last_line + outputs = outputs[1..-1] + outputs = [] if outputs.nil? + end + if outputs.count == 0 + outputs.push(data_str) + else + outputs[-1] += (data_str rescue "") + end + if update_outputs + update_infos_for_exec(data_str,true) + end + end end end end @@ -182,11 +218,23 @@ namespace :create_site do end return tmp end - def update_infos_for_exec(info,update_last=false) + def update_infos_for_exec(info,update_last=false,update_array=false) + return if @site_construct.nil? if update_last && !@site_construct.infos.empty? - @site_construct.infos[-1] += info + if @remove_last_line + @site_construct.infos[-1] = info.to_s + else + @site_construct.infos[-1] += info.to_s + end else - @site_construct.infos = @site_construct.infos.push(info) + if @remove_last_line + @site_construct.infos = @site_construct.infos[0...-1] + end + if update_array + @site_construct.infos += info + else + @site_construct.infos.push(info.to_s) + end end @site_construct.save! return @site_construct.infos diff --git a/lib/tasks/create_site.rake b/lib/tasks/create_site.rake index 1ea5602..8314eda 100644 --- a/lib/tasks/create_site.rake +++ b/lib/tasks/create_site.rake @@ -108,24 +108,68 @@ namespace :create_site do @thread.save! return @thread.status["infos"] end - def exec_ssh_command_for_create(session,command,update=false) + def exec_ssh_command_for_create(session,command,update_outputs=false) outputs = [] session.open_channel do |channel| channel.request_pty do |channel, success| channel.exec("LANG=en.UTF8 #{command}") do |ch, success| abort "could not execute command: #{command}" unless success channel.on_data do |ch, data| - outputs.push(data) - print "#{data}" - if update - if data.include? "\n" - update_infos_for_exec(data) - else - update_infos_for_exec(data,true) - end - end - if data.to_s.include?("sudo password:") || data.to_s.include?("Password:") + data_str = data.to_s + data_str.encode!("UTF-8", :invalid => :replace, :undef => :replace, :replace => '') + if data_str.include?("sudo password:") || data_str.include?("Password:") channel.send_data "#{@password}\n" + else + print data_str unless @no_stdout + data_str.gsub!("\r\n", "\n") + rm_idx = data_str.index("\r") + if rm_idx.nil? + @remove_last_line = false + else + @remove_last_line = (outputs.count > 0 && data_str[0...rm_idx].exclude?("\n")) + end + data_str.gsub!(/.*\r/, '') + next if data_str.length == 0 + if data_str.include?("\n") || outputs.empty? + output_lines = data_str.split("\n") + first_output = output_lines[0] + if first_output + if @remove_last_line + outputs = outputs[1..-1] + outputs = [] if outputs.nil? + end + if outputs.count != 0 + outputs[-1] += first_output + else + outputs << first_output + end + end + new_arr = output_lines[1..-1] + new_arr = [] if new_arr.nil? + if data_str[-1] == "\n" + new_arr << "" + end + outputs += new_arr + if update_outputs + if first_output + update_infos_for_exec(first_output,true) + end + update_infos_for_exec(new_arr,false,true) + end + else + if @remove_last_line + outputs = outputs[1..-1] + outputs = [] if outputs.nil? + end + if outputs.count == 0 + outputs.push(data_str) + else + outputs[-1] += (data_str rescue "") + end + if update_outputs + update_infos_for_exec(data_str,true) + end + end end end end @@ -145,10 +189,16 @@ namespace :create_site do end def update_infos_for_exec(info,update_last=false,update_array=false) return if @site_construct.nil? - info.encode!("UTF-8", :invalid => :replace, :undef => :replace, :replace => '') if update_last && !@site_construct.infos.empty? - @site_construct.infos[-1] += info.to_s + if @remove_last_line + @site_construct.infos[-1] = info.to_s + else + @site_construct.infos[-1] += info.to_s + end else + if @remove_last_line + @site_construct.infos = @site_construct.infos[0...-1] + end if update_array @site_construct.infos += info else diff --git a/lib/tasks/exec_command.rake b/lib/tasks/exec_command.rake index 84021bc..ad928f1 100644 --- a/lib/tasks/exec_command.rake +++ b/lib/tasks/exec_command.rake @@ -196,8 +196,15 @@ namespace :exec_commands do def update_infos_for_exec(info,update_last=false,update_array=false) return if @site_construct.nil? if update_last && !@site_construct.infos.empty? - @site_construct.infos[-1] += info.to_s + if @remove_last_line + @site_construct.infos[-1] = info.to_s + else + @site_construct.infos[-1] += info.to_s + end else + if @remove_last_line + @site_construct.infos = @site_construct.infos[0...-1] + end if update_array @site_construct.infos += info else @@ -209,8 +216,15 @@ namespace :exec_commands do end def update_thread_infos_for_exec(info,update_last=false,update_array=false) if update_last && !@thread.status["infos"].empty? - @thread.status["infos"][-1] += info.to_s + if @remove_last_line + @thread.status["infos"][-1] = info.to_s + else + @thread.status["infos"][-1] += info.to_s + end else + if @remove_last_line + @thread.status["infos"] = @thread.status["infos"][0...-1] + end if update_array @thread.status["infos"] += info else @@ -247,12 +261,22 @@ namespace :exec_commands do else print data_str unless @no_stdout data_str.gsub!("\r\n", "\n") - data_str.sub!(/\r$/, '') + rm_idx = data_str.index("\r") + if rm_idx.nil? + @remove_last_line = false + else + @remove_last_line = (outputs.count > 0 && data_str[0...rm_idx].exclude?("\n")) + end + data_str.gsub!(/.*\r/, '') next if data_str.length == 0 if data_str.include?("\n") || outputs.empty? output_lines = data_str.split("\n") first_output = output_lines[0] if first_output + if @remove_last_line + outputs = outputs[1..-1] + outputs = [] if outputs.nil? + end if outputs.count != 0 outputs[-1] += first_output else @@ -274,6 +298,10 @@ namespace :exec_commands do update_infos_for_exec(new_arr,false,true) end else + if @remove_last_line + outputs = outputs[1..-1] + outputs = [] if outputs.nil? + end if outputs.count == 0 outputs.push(data_str) else