From f19ce221ae27ff6da16eb9529d417a6f63ff7441 Mon Sep 17 00:00:00 2001 From: bohung Date: Thu, 8 Sep 2022 14:48:32 +0800 Subject: [PATCH] Fix bug. --- .../admin/site_panel_controller.rb | 16 +++++- app/models/site_construct.rb | 49 ++++++++++--------- app/models/site_server.rb | 2 + lib/tasks/add_cert_ver_for_site.rake | 2 +- lib/tasks/change_site_cert.rake | 2 +- lib/tasks/change_site_server_name.rake | 2 +- lib/tasks/copy_site.rake | 2 +- lib/tasks/create_site.rake | 2 +- lib/tasks/exec_command.rake | 1 + 9 files changed, 47 insertions(+), 31 deletions(-) diff --git a/app/controllers/admin/site_panel_controller.rb b/app/controllers/admin/site_panel_controller.rb index 92430c2..df43b64 100644 --- a/app/controllers/admin/site_panel_controller.rb +++ b/app/controllers/admin/site_panel_controller.rb @@ -1,4 +1,11 @@ class Admin::SitePanelController < OrbitAdminController + require "rake" + begin + Rails.application.load_tasks + rescue => e + puts "Load tasks failed." + puts e.to_s + end before_action :check_server_ability, only: [:index,:server_manager] def initialize super @@ -318,9 +325,9 @@ class Admin::SitePanelController < OrbitAdminController else Thread.new do if params[:id].blank? && !params[:server_names].blank? - system("bundle exec rake exec_commands:exec_commands[,'#{params[:commands]}',exec_all,'#{params[:server_names].join('////')}']") + Rake::Task['exec_commands:exec_commands'].execute(Rake::TaskArguments.new([:commands, :type, :server_name], [params[:commands], 'exec_all', params[:server_names].join('////')])) else - system("bundle exec rake exec_commands:exec_commands[#{params[:id]},'#{params[:commands]}',exec_commands]") + Rake::Task['exec_commands:exec_commands'].execute(Rake::TaskArguments.new([:site_construct_id, :commands, :type], [params[:id], params[:commands], 'exec_commands'])) end end end @@ -356,6 +363,11 @@ class Admin::SitePanelController < OrbitAdminController render :json => {:status=>site_construct.status,:infos=>site_construct.infos} end end + def backup_setting + @site_server = SiteServer.find(params[:id]) + end + def update_backup_setting + end private def site_cert_params site_cert_params = params.require(:site_cert).permit! rescue {} diff --git a/app/models/site_construct.rb b/app/models/site_construct.rb index a9fe5db..eb89c1e 100644 --- a/app/models/site_construct.rb +++ b/app/models/site_construct.rb @@ -61,9 +61,9 @@ class SiteConstruct end end def generate_nginx_text(old_nginx_text="") - sock_text = 'upstream '+self.get_site_name+'_sock {\n'+ - ' server unix:'+self.full_site_path+'/tmp/unicorn.sock;\n'+ - '}\n' + sock_text = "upstream #{self.get_site_name}_sock {\n"+ + " server unix:#{self.full_site_path}/tmp/unicorn.sock;\n"+ + "}\n" all_ports = self.port.uniq server_blocks = [] if old_nginx_text.present? @@ -71,7 +71,7 @@ class SiteConstruct server_blocks = all_blocks.select{|s| s.match(/\A[\s\r\n]*server\s*{/)} upstream_block = all_blocks.select{|s| s.match(/\A[\s\r\n]*upstream/)}.first rescue nil if upstream_block.present? - sock_text = upstream_block + '\n' + sock_text = upstream_block + "\n" end end nginx_text = sock_text + all_ports.map.with_index{|port,i| @@ -80,7 +80,7 @@ class SiteConstruct else generate_server_block(port,server_blocks[0]) end - }.join('\n') + }.join("\n").gsub("\n", '\n') end def match_exact_index(text,match_character,level=1) text.enum_for(:scan,/(?:[^#{match_character}])#{match_character}{#{level}}(?!#{match_character})/m).map { offset_index=::Regexp.last_match.to_s.index(match_character);::Regexp.last_match.offset(0).first + offset_index} @@ -160,31 +160,32 @@ class SiteConstruct if redirect_default_text.present? new_server_block = new_server_block.sub(/\s*root/){|ff| redirect_default_text + ff} end - new_server_block = new_server_block.gsub(/[ \t\s]+\n/,"\n\n").gsub(/\n{3,}/,'\n\n') + new_server_block = new_server_block.gsub(/[\s]+\n/,"\n\n").gsub(/\n{3,}/,"\n\n").gsub("\n", '\n') else - 'server {\n'+ - ' listen '+port_text+';\n\n'+ + new_server_block = "server {\n"+ + " listen #{port_text};\n\n"+ (port == "443" ? " ssl_certificate #{self.cert_file_remote_store_path};\n\n"+ " ssl_certificate_key #{self.private_key_remote_store_path};\n\n"+ ((self.redirect_to_https && !self.site_cert.nil?) ? " if ($host ~ (#{self.site_cert.domain_names.map{|s| '^'+s.gsub('.','\.').gsub('*','[^.]*').gsub(',','')}.join('|')}) ) {\n"+ " return 301 https://$host$request_uri;\n"+ - "}\n" : '') : '')+ + " }\n" : '') : '')+ redirect_default_text + - ' root '+self.full_site_path+'/public;\n\n'+ - ' server_name '+domain_name_str+';\n\n'+ - ' client_max_body_size 500m;\n\n'+ - ' location / {\n'+ - ' try_files \$uri \$uri/index.html \$uri.html @app;\n'+ - ' }\n\n'+ - ' location @app {\n'+ - ' proxy_redirect off;\n'+ - ' proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\n'+ - ' proxy_set_header Host \$http_host;\n'+ - (port == "443" ? ' proxy_set_header X-Forwarded-Proto https;\n' : '')+ - ' proxy_connect_timeout 360;\n'+ - ' proxy_pass http://'+self.get_site_name+'_sock;\n'+ - ' }\n'+ - '}' + " root #{self.full_site_path}/public;\n\n"+ + " server_name #{domain_name_str};\n\n"+ + " client_max_body_size 500m;\n\n"+ + " location / {\n"+ + " try_files $uri $uri/index.html $uri.html @app;\n"+ + " }\n\n"+ + " location @app {\n"+ + " proxy_redirect off;\n"+ + " proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n"+ + " proxy_set_header Host $http_host;\n"+ + (port == "443" ? " proxy_set_header X-Forwarded-Proto https;\n" : "")+ + " proxy_connect_timeout 360;\n"+ + " proxy_pass http://#{self.get_site_name}_sock;\n"+ + " }\n"+ + "}" + new_server_block.gsub("\n", '\n') end end def display_port diff --git a/app/models/site_server.rb b/app/models/site_server.rb index e50d353..c94b53c 100644 --- a/app/models/site_server.rb +++ b/app/models/site_server.rb @@ -15,6 +15,8 @@ class SiteServer field :active , type: Boolean ,default: true field :has_certbot, type: Boolean , default: false field :need_update_site_ids, type: Array, default: [] + has_many :site_server_file_backups, :autosave => true, :dependent => :destroy + has_many :site_server_db_backups, :autosave => true, :dependent => :destroy def domain_names if self.domain_name != '' [self.domain_name] diff --git a/lib/tasks/add_cert_ver_for_site.rake b/lib/tasks/add_cert_ver_for_site.rake index 1e5d3cc..09b9e76 100644 --- a/lib/tasks/add_cert_ver_for_site.rake +++ b/lib/tasks/add_cert_ver_for_site.rake @@ -28,7 +28,7 @@ namespace :create_site do cmd = "x='#{cert_ver_file_content}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{file_path}\"; unset x" exec_command_by_user(ssh,cmd) - cmd = "x='#{nginx_config}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{site_construct.nginx_file}\"; unset x" + cmd = "x='#{nginx_config.gsub(/'{1,3}/,"\"\'\"")}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{site_construct.nginx_file}\"; unset x" exec_command_by_user(ssh,cmd) exec_ssh_command_by_sudo(ssh,"service nginx restart") end diff --git a/lib/tasks/change_site_cert.rake b/lib/tasks/change_site_cert.rake index 4255e7e..bfb6312 100644 --- a/lib/tasks/change_site_cert.rake +++ b/lib/tasks/change_site_cert.rake @@ -117,7 +117,7 @@ namespace :create_site do all_ports = (@site_construct.port + ["443"]).uniq @site_construct.update(:port=> all_ports,:status=>'finish' ) nginx_file_content = @site_construct.generate_nginx_text(nginx_file_content) - cmd = "x='#{nginx_file_content}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{@site_construct.nginx_file}\"" + cmd = "x='#{nginx_file_content.gsub(/'{1,3}/,"\"\'\"")}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{@site_construct.nginx_file}\"" exec_command_by_user(ssh,cmd) end end diff --git a/lib/tasks/change_site_server_name.rake b/lib/tasks/change_site_server_name.rake index 98b96ac..8067491 100644 --- a/lib/tasks/change_site_server_name.rake +++ b/lib/tasks/change_site_server_name.rake @@ -32,7 +32,7 @@ namespace :create_site do nginx_file_content = exec_command_by_user(ssh,"cat #{site_construct.nginx_file}") nginx_file_content = site_construct.generate_nginx_text(nginx_file_content) auto_update_infos("Writing...") - cmd = "x='#{nginx_file_content}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{site_construct.nginx_file}\"" + cmd = "x='#{nginx_file_content.gsub("\\$","$").gsub(/'{1,3}/,"\"\'\"")}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{site_construct.nginx_file}\"" exec_command_by_user(ssh,cmd) exec_ssh_command_by_sudo(ssh,"service nginx restart") auto_update_infos("Finish!") diff --git a/lib/tasks/copy_site.rake b/lib/tasks/copy_site.rake index 508578c..cc1026d 100644 --- a/lib/tasks/copy_site.rake +++ b/lib/tasks/copy_site.rake @@ -35,7 +35,7 @@ namespace :create_site do update_infos("setting nginx for #{args.site_name}") nginx_setting_texts = @site_construct.generate_nginx_text exec_ssh_command_by_sudo_for_create(ssh,"touch /etc/nginx/orbit_sites/#{@site_construct.get_site_name}") - exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{nginx_setting_texts}' > /etc/nginx/orbit_sites/#{@site_construct.get_site_name}\"") + exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{nginx_setting_texts.gsub('$','\\$').gsub(/'{1,3}/,"\"\'\"")}' > /etc/nginx/orbit_sites/#{@site_construct.get_site_name}\"") update_infos("restarting nginx") exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' service nginx restart") update_infos("finish restarting nginx") diff --git a/lib/tasks/create_site.rake b/lib/tasks/create_site.rake index f1e78b1..6abb6ab 100644 --- a/lib/tasks/create_site.rake +++ b/lib/tasks/create_site.rake @@ -33,7 +33,7 @@ namespace :create_site do update_infos("setting nginx for #{args.site_name}") nginx_setting_texts = @site_construct.generate_nginx_text exec_ssh_command_by_sudo_for_create(ssh,"touch /etc/nginx/orbit_sites/#{@site_construct.get_site_name}") - exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{nginx_setting_texts}' > /etc/nginx/orbit_sites/#{@site_construct.get_site_name}\"") + exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{nginx_setting_texts.gsub('$','\\$').gsub(/'{1,3}/,"\"\'\"")}' > /etc/nginx/orbit_sites/#{@site_construct.get_site_name}\"") update_infos("restarting nginx") exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' service nginx restart") update_infos("finish restarting nginx") diff --git a/lib/tasks/exec_command.rake b/lib/tasks/exec_command.rake index b95c9e0..c636f89 100644 --- a/lib/tasks/exec_command.rake +++ b/lib/tasks/exec_command.rake @@ -126,6 +126,7 @@ namespace :exec_commands do @command_i18n = @command_i18n.gsub("{{rails_env}}",rails_env) @command_i18n = @command_i18n.gsub("{{full_site_path}}",@site_construct.full_site_path) @command_i18n = @command_i18n.gsub("{{site_name}}",@site_construct.site_name) + command = command.gsub(/'{1,3}/,"\"\'\"") exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'", update_flag) @command_i18n = nil end