From a7bdee0b5be17b3491191bb6342996efed724ba6 Mon Sep 17 00:00:00 2001 From: chiu Date: Thu, 18 Feb 2021 00:59:11 +0800 Subject: [PATCH] add multiple domain name feature --- .../admin/site_panel_controller.rb | 4 + app/models/site_construct.rb | 3 + app/views/admin/site_panel/_form.html.erb | 94 ++++++++++--- .../site_panel/_sites_list_table.html.erb | 11 +- .../see_detail_for_created_site.html.erb | 125 ++++++++++++------ .../admin/site_panel/server_manager.html.erb | 5 + .../admin/site_panel/sites_list.html.erb | 5 + lib/tasks/change_site_server_name.rake | 32 +++++ lib/tasks/detect_sites.rake | 101 +++++++------- 9 files changed, 275 insertions(+), 105 deletions(-) create mode 100644 lib/tasks/change_site_server_name.rake diff --git a/app/controllers/admin/site_panel_controller.rb b/app/controllers/admin/site_panel_controller.rb index 275f471..961237d 100644 --- a/app/controllers/admin/site_panel_controller.rb +++ b/app/controllers/admin/site_panel_controller.rb @@ -131,6 +131,10 @@ class Admin::SitePanelController < OrbitAdminController elsif params[:type] == 'detail' @site = SiteConstruct.find(params[:id]) render 'see_detail_for_created_site' and return + elsif params[:type] == 'change_server_name' + cmd = "rake create_site:change_site_server_name[#{params[:id]},'#{params[:site_construct][:domain_name]}']" + system(cmd) + redirect_to :back and return elsif params[:type] == 'delete_from_list' SiteConstruct.find(params[:id]).destroy redirect_to :back and return diff --git a/app/models/site_construct.rb b/app/models/site_construct.rb index 5c9344c..ead8476 100644 --- a/app/models/site_construct.rb +++ b/app/models/site_construct.rb @@ -51,4 +51,7 @@ class SiteConstruct end return (scheme + self.domain_name + extra_port) end + def site_server + SiteServer.where(server_name: self.server_type).first + end end \ No newline at end of file diff --git a/app/views/admin/site_panel/_form.html.erb b/app/views/admin/site_panel/_form.html.erb index 01f91d4..ec96d98 100644 --- a/app/views/admin/site_panel/_form.html.erb +++ b/app/views/admin/site_panel/_form.html.erb @@ -39,10 +39,13 @@
<%= f.label :root_domain ,"Root Domain", :class => "control-label muted" %> -
- <%= select_tag :root_domain, - options_for_select(SiteServer.first.domain_names.collect{ |u| [u, u] }), - :id => "root_domain" %> +
+
+ <%= select_tag :root_domain, + options_for_select(SiteServer.first.domain_names.collect{ |u| [u, u] }), + :class => "root_domain" %> +
+ <%= button_tag 'add root domain',:type => 'button', :id => 'add_root_domain',class: 'btn btn-info',:style => 'margin-top: 1em;' %>
@@ -72,7 +75,12 @@
<%= f.label :domain_name ,"Domain Name", :class => "control-label muted" %>
- <%= f.text_field :domain_name, :id => "domain_name",:disabled => 'disabled' %> +
+ + <%= SiteServer.first.domain_names[0] %> + <%= hidden_field_tag nil,nil, :class => "domain_name",:id=>nil %> +
+ <%= f.hidden_field :domain_name %>
@@ -107,12 +115,29 @@ \ No newline at end of file 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 9588ddf..75d950a 100644 --- a/app/views/admin/site_panel/_sites_list_table.html.erb +++ b/app/views/admin/site_panel/_sites_list_table.html.erb @@ -13,7 +13,16 @@ <% @sites.each do |site|%> <%=site.server_type%> - ://<%=site.domain_name%><%=((site.port.to_s != '80' && site.port.to_s != '443') ? ':'+site.port.to_s : '')%>" title="open <%=((site.port.to_s == "443") ? "https" : "http")%>://<%=site.domain_name%><%=((site.port.to_s != '80' && site.port.to_s != '443') ? ':'+site.port.to_s : '')%> to new window" target="_blank"><%=site.domain_name%> + + <% site.domain_name.split(" ").each_with_index do |domain_name,i| %> + <% domain_link = ((site.port.to_s == "443") ? "https" : "http") + '://' + domain_name + ((site.port.to_s != '80' && site.port.to_s != '443') ? ':'+site.port.to_s : '') %> + + <% if i!=0 %> +
+ <% end %> + <%= link_to domain_name, domain_link, :title => "open #{domain_link} to new window", :target=>"_blank" %> + <% end %> + <%=site.port%> <%= (site.status.blank? ? "not yet create" : status_relation[site.status]) %> diff --git a/app/views/admin/site_panel/see_detail_for_created_site.html.erb b/app/views/admin/site_panel/see_detail_for_created_site.html.erb index 981851c..a7e116c 100644 --- a/app/views/admin/site_panel/see_detail_for_created_site.html.erb +++ b/app/views/admin/site_panel/see_detail_for_created_site.html.erb @@ -1,45 +1,88 @@
-
- -
<%=@site.server_type%>
-
-
- -
<%=@site.domain_name%>
-
-
- -
<%=@site.port%>
-
-
- -
<%=@site.site_type%>
-
- <% if @site.site_type == "School"%> +
">
- -
<%=@site.school_name%>
+ +
<%=@site.server_type%>
- <% end %> -
- -
<%=@site.site_name%>
-
-
- -
<%=@site.db_name%>
-
-
- -
<%=@site.nginx_file%>
-
-
- -
<%=@site.path%>
-
-
-
- <%=t(:back)%> +
+ +
+ <% @site.domain_name.split(' ').each_with_index do |domain_name,i| %> +
+ <%= text_field_tag nil,domain_name,:class=>'domain_name',onchange: 'domain_name_change()' %> + <% if i != 0 %> + + <% end %> +
+ <% end %> + <%= button_tag 'add domain',:type => 'button', :id => 'add_root_domain',class: 'btn btn-info',:style => 'margin-top: 1em;' %> + <%= hidden_field_tag 'site_construct[domain_name]',@site.domain_name,id: 'site_construct_domain_name' %> +
-
-
\ No newline at end of file +
+ +
<%=@site.port%>
+
+
+ +
<%=@site.site_type%>
+
+ <% if @site.site_type == "School"%> +
+ +
<%=@site.school_name%>
+
+ <% end %> +
+ +
<%=@site.site_name%>
+
+
+ +
<%=@site.db_name%>
+
+
+ +
<%=@site.nginx_file%>
+
+
+ +
<%=@site.path%>
+
+
+
+ <%= hidden_field_tag 'id',@site.id %> + <%= hidden_field_tag 'type','change_server_name' %> + <%=t(:back)%> + +
+
+
+
+ \ No newline at end of file diff --git a/app/views/admin/site_panel/server_manager.html.erb b/app/views/admin/site_panel/server_manager.html.erb index a99d201..d2193d3 100644 --- a/app/views/admin/site_panel/server_manager.html.erb +++ b/app/views/admin/site_panel/server_manager.html.erb @@ -7,6 +7,11 @@ <%= stylesheet_link_tag "lib/main-forms" %> <%= stylesheet_link_tag "lib/main-list" %> <%= stylesheet_link_tag "lib/pageslide" %> + <% end %> <%= render_filter @filter_fields, "index_table" %> diff --git a/app/views/admin/site_panel/sites_list.html.erb b/app/views/admin/site_panel/sites_list.html.erb index ddf7309..2afd32a 100644 --- a/app/views/admin/site_panel/sites_list.html.erb +++ b/app/views/admin/site_panel/sites_list.html.erb @@ -7,6 +7,11 @@ <%= stylesheet_link_tag "lib/main-forms" %> <%= stylesheet_link_tag "lib/main-list" %> <%= stylesheet_link_tag "lib/pageslide" %> + <% end %> <% if !params[:id].blank? %>
diff --git a/lib/tasks/change_site_server_name.rake b/lib/tasks/change_site_server_name.rake new file mode 100644 index 0000000..4abc175 --- /dev/null +++ b/lib/tasks/change_site_server_name.rake @@ -0,0 +1,32 @@ +require 'net/ssh' +require 'pathname' +namespace :create_site do + desc "Change Site Server Name" + task :change_site_server_name,[:id,:server_name] => :environment do |task,args| + begin + site_construct = SiteConstruct.find(args.id) + site_server = site_construct.site_server + 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\].*\: /,'') + puts nginx_file_content + cmd = "sh -c \"echo '#{nginx_file_content}' > #{site_construct.nginx_file}\"" + exec_ssh_command_by_sudo(ssh,cmd) + exec_ssh_command_by_sudo(ssh,"service nginx restart") + end + site_construct.update_attributes(domain_name: args.server_name) + end + rescue => e + puts [e,e.backtrace] + end + 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") + output = session.exec!(command) + end + return output + end +end \ No newline at end of file diff --git a/lib/tasks/detect_sites.rake b/lib/tasks/detect_sites.rake index 218dc30..69a8375 100644 --- a/lib/tasks/detect_sites.rake +++ b/lib/tasks/detect_sites.rake @@ -31,63 +31,72 @@ namespace :create_site do next end Net::SSH.start(@site_server.ip , @site_server.account , password: @site_server.password) do |ssh| - nginx_include_dirs = exec_ssh_command_by_sudo(ssh,'echo `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_dirs = nginx_include_dirs.split("\n").map{|t| t.gsub("include",'').gsub(';','').strip} + nginx_include_dir = exec_ssh_command_by_sudo(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' - nginx_include_dirs.each do |nginx_include_dir| - server_names = exec_ssh_command_by_sudo(ssh,"grep 'server_name' -r #{nginx_include_dir}") - server_names_array = server_names.split("\n") - server_names_array = server_names_array.select{|t| !t.include?('No such file or directory')}.flat_map{|t| t.strip.split('server_name').last.split(";").first.strip.split} - server_names_array.each do |server_name| - if !server_name.include?(domain_name) - next if !`nslookup "#{server_name}"`.include?(@site_server.ip) + server_names = exec_ssh_command_by_sudo(ssh,"grep '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 = [] + server_names_for_site.each do |server_name_for_site| + if !server_name_for_site.include?(domain_name) + next if !`nslookup "#{server_name_for_site}"`.include?(@site_server.ip) end - nginx_file = exec_ssh_command_by_sudo(ssh,"grep '#{server_name}' -r #{nginx_include_dir}").split("\n").select{|s| !s.include?("No such file or directory")}.first.split('server_name').first.strip.split(":").first - site_path = Pathname.new(exec_ssh_command_by_sudo(ssh,"echo `grep root #{nginx_file} | grep -v -e '#.*root'`").to_s.split("\n").first.to_s.strip.split("root").last.to_s.gsub(";","").strip).dirname.to_s - if site_path.present? && exec_ssh_command_by_sudo(ssh,"ls #{site_path}").split.length != 0 && exec_ssh_command_by_sudo(ssh,"ls #{site_path}/Gemfile").include?("No such file or directory") - SiteConstruct.where(:server_type => @site_server.server_name , :domain_name=>server_name).destroy - next - end - path = Pathname.new(site_path).dirname.to_s - site_name = Pathname.new(site_path).basename.to_s - server_type = @site_server.server_name - port = exec_ssh_command_by_sudo(ssh,"echo `grep 'listen' #{nginx_file} | grep -v -e '#.*listen'`").split("\n").first.strip.split("listen").last.strip.split(";").first.split.select{|p| p.strip == p.strip.to_i.to_s}.first.strip rescue "80" - db_name = exec_ssh_command_by_sudo(ssh,"echo `cat #{site_path}/config/mongoid.yml | grep 'database'`").split("database:").last.strip - db_name = site_name.gsub("-","_") if db_name.include?("No such file or directory") - unicorn_sock_path = exec_ssh_command_by_sudo(ssh,"echo `grep 'server unix' #{nginx_file} | grep -v -e '#.*server unix'`").strip.split("server unix").last.strip.split(":").last.strip rescue (site_path+"/tmp/unicorn.sock") - if Pathname.new(path).basename.to_s == "orbit_sites" || Pathname.new(path).basename.to_s == "housing_sites" || Pathname.new(path).basename.to_s == "dev_sites" || Pathname.new(path).dirname.to_s == "/home" - school_name = nil - site_type = "Gravity" + server_name_list << server_name_for_site + end + server_name = server_name_list.join(' ') + site_path = Pathname.new(exec_ssh_command_by_sudo(ssh,"echo `grep root #{nginx_file} | grep -v -e '#.*root'`").to_s.split("\n").first.to_s.strip.split("root").last.to_s.gsub(";","").strip).dirname.to_s + if site_path.present? && exec_ssh_command_by_sudo(ssh,"ls #{site_path}").split.length != 0 && exec_ssh_command_by_sudo(ssh,"ls #{site_path}/Gemfile").include?("No such file or directory") + SiteConstruct.where(:server_type => @site_server.server_name , :domain_name=>server_name).destroy + next + end + path = Pathname.new(site_path).dirname.to_s + site_name = Pathname.new(site_path).basename.to_s + server_type = @site_server.server_name + port = exec_ssh_command_by_sudo(ssh,"echo `grep 'listen' #{nginx_file} | grep -v -e '#.*listen'`").split("\n").first.strip.split("listen").last.strip.split(";").first.split.select{|p| p.strip == p.strip.to_i.to_s}.first.strip rescue "80" + db_name = exec_ssh_command_by_sudo(ssh,"echo `cat #{site_path}/config/mongoid.yml | grep 'database'`").split("database:").last.strip + db_name = site_name.gsub("-","_") if db_name.include?("No such file or directory") + unicorn_sock_path = exec_ssh_command_by_sudo(ssh,"echo `grep 'server unix' #{nginx_file} | grep -v -e '#.*server unix'`").strip.split("server unix").last.strip.split(":").last.strip rescue (site_path+"/tmp/unicorn.sock") + if Pathname.new(path).basename.to_s == "orbit_sites" || Pathname.new(path).basename.to_s == "housing_sites" || Pathname.new(path).basename.to_s == "dev_sites" || Pathname.new(path).dirname.to_s == "/home" + school_name = nil + site_type = "Gravity" + else + school_name = Pathname.new(path).basename.to_s + site_type = "School" + end + pid_infos = exec_ssh_command_by_sudo(ssh,"fuser #{unicorn_sock_path}").to_s.strip.gsub("\n","") + if pid_infos.length != 0 && !pid_infos.include?("not exist") && !pid_infos.include?("No such file or directory") + status = "finish" + else + bundle_show_info = exec_ssh_command_by_sudo(ssh,"bash -l -c 'cd #{site_path};bundle show'") + if bundle_show_info.include?("is not yet checked out") || bundle_show_info.include?("No such file or directory") + status = "" else - school_name = Pathname.new(path).basename.to_s - site_type = "School" - end - pid_infos = exec_ssh_command_by_sudo(ssh,"fuser #{unicorn_sock_path}").to_s.strip.gsub("\n","") - if pid_infos.length != 0 && !pid_infos.include?("not exist") && !pid_infos.include?("No such file or directory") - status = "finish" - else - bundle_show_info = exec_ssh_command_by_sudo(ssh,"bash -l -c 'cd #{site_path};bundle show'") - if bundle_show_info.include?("is not yet checked out") || bundle_show_info.include?("No such file or directory") - status = "" - else - status = "closed" - end - end - site_construct = SiteConstruct.where(:server_type => server_type , :domain_name=>server_name).first - update_thread_infos("Detect #{server_name}".html_safe) - if site_construct.nil? - site_construct = SiteConstruct.create(:server_type=>server_type,:site_name=>site_name,:domain_name=>server_name,:nginx_file=>nginx_file,:db_name=>db_name,:port=>port,:path=>path,:site_type=>site_type,:school_name=>school_name,:user_id=>User.first.id,:status=>status) - else - site_construct.update(:server_type=>server_type,:site_name=>site_name,:domain_name=>server_name,:nginx_file=>nginx_file,:db_name=>db_name,:port=>port,:path=>path,:site_type=>site_type,:school_name=>school_name,:user_id=>User.first.id,:status=>status) + status = "closed" end end + site_constructs = SiteConstruct.where(:server_type => server_type , :domain_name.in => [server_name,*server_name_list]).all + site_construct = site_constructs[0] + Array(site_constructs[1..-1]).each do |s| + s.destroy + end + update_thread_infos("Detect #{server_name}".html_safe) + if site_construct.nil? + site_construct = SiteConstruct.create(:server_type=>server_type,:site_name=>site_name,:domain_name=>server_name,:nginx_file=>nginx_file,:db_name=>db_name,:port=>port,:path=>path,:site_type=>site_type,:school_name=>school_name,:user_id=>User.first.id,:status=>status) + else + site_construct.update(:server_type=>server_type,:site_name=>site_name,:domain_name=>server_name,:nginx_file=>nginx_file,:db_name=>db_name,:port=>port,:path=>path,:site_type=>site_type,:school_name=>school_name,:user_id=>User.first.id,:status=>status) + end end end end end + SiteConstruct.where(:domain_name.in => ['','localhost','127.0.0.1']).destroy @thread.update(:status=>@thread.status.merge({"status"=>"finish"})) rescue => e - @thread.update(:status=>{"infos"=>@thread.status["infos"].push(e.to_s),"status"=>"error"}) + puts [e,e.backtrace] + @thread.update(:status=>{"infos"=>@thread.status["infos"].push(e.to_s),"status"=>"error"}) end end end