From 613081a3f5a5f66cf521bffb7209ae47a8b75748 Mon Sep 17 00:00:00 2001 From: Bohung Date: Wed, 11 Aug 2021 18:25:03 +0800 Subject: [PATCH] Fix bug. --- .../admin/site_panel_controller.rb | 2 +- .../site_panel/get_certs_for_site.html.erb | 8 ++- lib/tasks/change_site_cert.rake | 2 +- lib/tasks/detect_sites.rake | 5 +- lib/tasks/exec_command.rake | 54 +++++++++++-------- lib/tasks/install_certbot.rake | 9 +++- 6 files changed, 52 insertions(+), 28 deletions(-) diff --git a/app/controllers/admin/site_panel_controller.rb b/app/controllers/admin/site_panel_controller.rb index a727e22..63c2ff0 100644 --- a/app/controllers/admin/site_panel_controller.rb +++ b/app/controllers/admin/site_panel_controller.rb @@ -38,7 +38,7 @@ class Admin::SitePanelController < OrbitAdminController else domain_name_search_text = "" end - @site_certs = SiteCert.all.where(:is_valid=>true,:domain_names=>/\A#{domain_name_search_text}/) + @site_certs = SiteCert.all.where(:is_valid=>true,:domain_names=>/\A#{domain_name_search_text}/,:start_date.lte=>DateTime.now,:end_date.gte=>DateTime.now) # @site_certs = SiteCert.all if site_construct @enable_cert_id = site_construct.site_cert_id diff --git a/app/views/admin/site_panel/get_certs_for_site.html.erb b/app/views/admin/site_panel/get_certs_for_site.html.erb index b3deb19..1911985 100644 --- a/app/views/admin/site_panel/get_certs_for_site.html.erb +++ b/app/views/admin/site_panel/get_certs_for_site.html.erb @@ -43,10 +43,16 @@ <%=t('client_management.domain_name')%> + + <%=radio_button_tag("site_cert","certbot",false)%> + Certbot + + + <% @site_certs.each do |site_cert| %> <%=radio_button_tag("site_cert",site_cert.id,site_cert.id == @enable_cert_id)%> - <%=site_cert.upload_date %> + <%=site_cert.upload_date %><%= '(certbot)' if site_cert.is_certbot%> <%=site_cert.display_start_date %> / <%=site_cert.display_end_date %> <%=site_cert.display_domain_names %> diff --git a/lib/tasks/change_site_cert.rake b/lib/tasks/change_site_cert.rake index 90e1c8f..e719179 100644 --- a/lib/tasks/change_site_cert.rake +++ b/lib/tasks/change_site_cert.rake @@ -16,7 +16,7 @@ namespace :create_site do if is_certbot domain_name = @site_construct.domain_name if domain_name.present? - certbot_path = exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'which certbot'",false,true).strip + certbot_path = exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'which certbot certbot-auto'",false,true).strip.split("\n")[0] if certbot_path.present? if @site_cert update_infos("Using certbot to change cert setting...") diff --git a/lib/tasks/detect_sites.rake b/lib/tasks/detect_sites.rake index 75c8a58..b3b60bf 100644 --- a/lib/tasks/detect_sites.rake +++ b/lib/tasks/detect_sites.rake @@ -34,7 +34,7 @@ namespace :create_site do next end Net::SSH.start(@site_server.ip , @site_server.account , password: @site_server.password) do |ssh| - certbot_path = exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'which certbot'",false,true).strip + certbot_path = exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'which certbot certbot-auto'",false,true).strip.split("\n")[0] @site_server.has_certbot = certbot_path.present? @site_server.save if @site_server.has_certbot @@ -51,9 +51,10 @@ namespace :create_site do nginx_include_dir = exec_command_by_user(ssh,'grep include /etc/nginx/nginx.conf | grep -v "\#\|include /etc/nginx/mime.types\|include /etc/nginx/conf.d/\*.conf\|/etc/nginx/sites-enabled/\*"') nginx_include_dir = nginx_include_dir.gsub(/include|;|\n/,'').strip domain_name = @site_server.domain_name#'serv.rulingcom.com' - server_names = exec_command_by_user(ssh,"grep 'server_name' -r #{nginx_include_dir}") + server_names = exec_command_by_user(ssh,"grep -H 'server_name' -r #{nginx_include_dir}") server_names_array = server_names.scan(/(.*):[ \t]*server_name[ \t]+(.*);/) server_names_array = server_names_array.group_by{|v| v[0]} + server_names_array.each do |nginx_file, server_name_with_file| server_names_for_site = server_name_with_file.map{|v| v[1].split(/[ |\t]+/)}.flatten.uniq - ["localhost","127.0.0.1"] server_name_list = [] diff --git a/lib/tasks/exec_command.rake b/lib/tasks/exec_command.rake index 3326a98..b065fef 100644 --- a/lib/tasks/exec_command.rake +++ b/lib/tasks/exec_command.rake @@ -119,21 +119,29 @@ namespace :exec_commands do end return output 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.to_s else - @site_construct.infos = @site_construct.infos.push(info.to_s) + if update_array + @site_construct.infos += info + else + @site_construct.infos.push(info.to_s) + end end @site_construct.save! return @site_construct.infos end - def update_thread_infos_for_exec(info,update_last=false) + 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 else - @thread.status["infos"] = @thread.status["infos"].push(info.to_s) + if update_array + @thread.status["infos"] += info + else + @thread.status["infos"].push(info.to_s) + end end @thread.save! return @thread.status["infos"] @@ -154,26 +162,28 @@ namespace :exec_commands do channel.exec(command) do |ch, success| abort "could not execute command: #{command}" unless success channel.on_data do |ch, data| - print "#{data}" - if data.include? "\n" || outputs.empty? - outputs.push(data.to_s) - if update - update_thread_infos_for_exec(data) if @flag - update_infos_for_exec(data) - end - else - if outputs.count == 0 - outputs.push(data.to_s) - else - outputs[-1] += (data.to_s rescue "") - end - if update - update_thread_infos_for_exec(data,true) if @flag - update_infos_for_exec(data,true) - end - end if data.to_s.include?("sudo password:") || data.to_s.include?("Password:") channel.send_data "#{@password}\n" + else + print "#{data}" + if data.include?("\n") || outputs.empty? + output_lines = data.to_s.split("\n").select{|l| l.present?} + outputs += output_lines + if update + update_thread_infos_for_exec(output_lines,false,true) if @flag + update_infos_for_exec(output_lines,false,true) + end + else + if outputs.count == 0 + outputs.push(data.to_s) + else + outputs[-1] += (data.to_s rescue "") + end + if update + update_thread_infos_for_exec(data,true) if @flag + update_infos_for_exec(data,true) + end + end end end channel.on_close do |ch| diff --git a/lib/tasks/install_certbot.rake b/lib/tasks/install_certbot.rake index b21948b..d208464 100644 --- a/lib/tasks/install_certbot.rake +++ b/lib/tasks/install_certbot.rake @@ -37,9 +37,16 @@ namespace :create_site do certbot_path = exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'which certbot'",false,true).strip snap_path = exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'which snap'",false,true).strip if certbot_path.blank? + update_thread_infos("checking kernel version") + kernel_version = exec_ssh_command_by_sudo_and_see_output(ssh,"sudo -p 'sudo password:' uname -r",false,true).strip.to_f rescue 0.0 + update_thread_infos("kernel_version: #{kernel_version}") + if kernel_version < 4.4 + raise "Kernel version need upgrade to >= 4.4(snap need kernel >= 4.4)" + end if snap_path.blank? + update_thread_infos("execing apt update...") + exec_ssh_command_by_sudo_and_see_output(ssh,"sudo -p 'sudo password:' apt-get -y -o DPkg::options::='--force-confdef' -o DPkg::options::='--force-confold' update",true,false) update_thread_infos("Installing snap...") - exec_ssh_command_by_sudo_and_see_output(ssh,"sudo -p 'sudo password:' apt update",true,false) exec_ssh_command_by_sudo_and_see_output(ssh,"sudo -p 'sudo password:' apt install snapd -y",true,false) snap_path = exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'which snap'",false,true).strip end