client_management/lib/tasks/add_cert_ver_for_site.rake

40 lines
2.0 KiB
Ruby
Raw Normal View History

2021-02-24 10:44:09 +00:00
require 'net/ssh'
require 'pathname'
namespace :create_site do
desc "Add cert ver for site"
task :add_cert_ver_for_site,[:id] => :environment do |task,args|
begin
site_construct = SiteConstruct.find(args.id)
site_server = site_construct.site_server
if !site_server.nil? && !site_construct.cert_ver_location_path.blank? && !site_construct.cert_ver_file_content.nil?
@password = site_server.password
2023-02-23 04:52:58 +00:00
Net::SSH.start(site_server.ip , site_server.account , { password: site_server.password, port: site_server.port}) do |ssh|
2021-02-24 10:44:09 +00:00
nginx_path = site_construct.nginx_file
cert_ver_file_content = site_construct.cert_ver_file_content
location_path = site_construct.cert_ver_location_path
nginx_config = exec_command_by_user(ssh,"cat #{site_construct.nginx_file}")
if !site_construct.cert_ver_added_text.nil?
2021-03-06 10:48:14 +00:00
nginx_config = nginx_config.sub(site_construct.cert_ver_added_text,'')
2021-02-24 10:44:09 +00:00
end
file_name = location_path.split('/')[-1]
file_path = "#{site_construct.path}/#{site_construct.site_name}/tmp/#{file_name}"
site_construct.cert_ver_added_text = " location #{location_path}{#add_by_site_module\n alias #{file_path};\n }"
2021-02-24 10:44:09 +00:00
site_construct.save
2021-04-19 01:29:53 +00:00
server_array = site_construct.parse_nginx_text_to_server_blocks(nginx_config)
2021-02-24 10:44:09 +00:00
server_array.each do |server|
tmp = server[0...-1] + site_construct.cert_ver_added_text + "\n}"
2021-02-24 10:44:09 +00:00
nginx_config = nginx_config.sub(server,tmp)
end
cmd = "x='#{cert_ver_file_content}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{file_path}\"; unset x"
exec_command_by_user(ssh,cmd)
2022-09-08 06:48:32 +00:00
cmd = "x='#{nginx_config.gsub(/'{1,3}/,"\"\'\"")}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{site_construct.nginx_file}\"; unset x"
2021-02-24 10:44:09 +00:00
exec_command_by_user(ssh,cmd)
exec_ssh_command_by_sudo(ssh,"service nginx restart")
end
end
rescue => e
puts [e,e.backtrace]
end
end
end