From ad3118ec454bc38ba3b5e4b970fa18da418dc39f Mon Sep 17 00:00:00 2001 From: chiu Date: Thu, 18 Feb 2021 09:46:55 +0800 Subject: [PATCH] fix error --- lib/tasks/change_site_server_name.rake | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/tasks/change_site_server_name.rake b/lib/tasks/change_site_server_name.rake index 4abc175..254fa37 100644 --- a/lib/tasks/change_site_server_name.rake +++ b/lib/tasks/change_site_server_name.rake @@ -9,8 +9,8 @@ namespace :create_site do if !site_server.nil? @password = site_server.password Net::SSH.start(site_server.ip , site_server.account , password: site_server.password) do |ssh| - nginx_file_content = exec_ssh_command_by_sudo(ssh,"bash -c \"cat #{site_construct.nginx_file}\"") - nginx_file_content = nginx_file_content.gsub(/^[ \t]*server_name[ \t]+.*;/," server_name #{args.server_name};").gsub('$','\$').gsub(/^\[sudo\].*\: /,'') + nginx_file_content = exec_command_by_user(ssh,"cat #{site_construct.nginx_file}") + nginx_file_content = nginx_file_content.gsub(/^[ \t]*server_name[ \t]+.*;/," server_name #{args.server_name};").gsub('$','\$') puts nginx_file_content cmd = "sh -c \"echo '#{nginx_file_content}' > #{site_construct.nginx_file}\"" exec_ssh_command_by_sudo(ssh,cmd) @@ -22,6 +22,10 @@ namespace :create_site do puts [e,e.backtrace] end end + def exec_command_by_user(session,command) + output = session.exec!(command) + return output + end def exec_ssh_command_by_sudo(session,command) output = session.exec!("echo '#{@password}' | sudo -S #{command}") if output.include?("sudo:") && output.include?("command not found")