Add "only copy installed modules" feature.

This commit is contained in:
BoHung Chiu 2021-04-13 11:06:09 +08:00
parent bc631ffd59
commit fd8ee8bd0e
6 changed files with 44 additions and 31 deletions

View File

@ -105,7 +105,7 @@ class Admin::SitePanelController < OrbitAdminController
end end
else else
Thread.new do Thread.new do
system("bundle exec rake create_site:copy_site['#{ip}','#{user}','#{password}','#{site_name}','#{domain_name}','#{port}','#{db_name}','#{path}','#{site_construct_id}','#{params[:site_construct][:copy_id]}']") system("bundle exec rake create_site:copy_site['#{ip}','#{user}','#{password}','#{site_name}','#{domain_name}','#{port}','#{db_name}','#{path}','#{site_construct_id}','#{params[:site_construct][:copy_id]}',#{site_construct.only_copy_installed_module}]")
end end
end end
redirect_to "#{admin_site_panel_sites_list_path}?id=#{site_construct_id}" redirect_to "#{admin_site_panel_sites_list_path}?id=#{site_construct_id}"
@ -128,7 +128,7 @@ class Admin::SitePanelController < OrbitAdminController
site_construct_id = params[:id] site_construct_id = params[:id]
if site_construct.copy_id if site_construct.copy_id
Thread.new do Thread.new do
system("bundle exec rake create_site:copy_site['#{ip}','#{user}','#{password}','#{site_name}','#{domain_name}','#{port}','#{db_name}','#{path}','#{site_construct_id}','#{site_construct.copy_id}']") system("bundle exec rake create_site:copy_site['#{ip}','#{user}','#{password}','#{site_name}','#{domain_name}','#{port}','#{db_name}','#{path}','#{site_construct_id}','#{site_construct.copy_id}',#{site_construct.only_copy_installed_module}]")
end end
else else
Thread.new do Thread.new do

View File

@ -21,7 +21,7 @@ class SiteConstruct
field :infos, type: Array, :default => [] field :infos, type: Array, :default => []
field :hidden, type: Boolean, :default => false field :hidden, type: Boolean, :default => false
field :copy_id field :copy_id
field :only_copy_installed_module, type: Boolean, :default => false
field :cert_ver_added_text field :cert_ver_added_text
field :cert_ver_file_content field :cert_ver_file_content
field :cert_ver_location_path field :cert_ver_location_path

View File

@ -20,16 +20,23 @@
<div class="tab-content module-area"> <div class="tab-content module-area">
<!-- Basic Module --> <!-- Basic Module -->
<div class="tab-pane fade in active" id="basic"> <div class="tab-pane fade in active" id="basic">
<div class="control-group"> <% copy_source = SiteConstruct.find(params[:copy_id]) rescue nil %>
<% copy_source = SiteConstruct.find(params[:copy_id]) rescue nil %> <% if copy_source %>
<% if copy_source %> <div class="control-group">
<label class="control-label muted"><%= t("client_management.copy_source") %></label> <label class="control-label muted"><%= t("client_management.copy_source") %></label>
<div class="controls"> <div class="controls">
<a title="<%= copy_source.site_name %>" href="<%= copy_source.get_domain_name %>" style="font-weight: bold; font-size: 1.3em;"><%= copy_source.site_name %></a> <a title="<%= copy_source.site_name %>" href="<%= copy_source.get_domain_name %>" style="font-weight: bold; font-size: 1.3em;"><%= copy_source.site_name %></a>
</div> </div>
<%= f.hidden_field :copy_id, :value => params[:copy_id] %> <%= f.hidden_field :copy_id, :value => params[:copy_id] %>
<% end %> </div>
</div> <div class="control-group">
<label class="control-label muted"><%= t("client_management.only_copy_installed_module") %></label>
<div class="controls">
<%= f.check_box :only_copy_installed_module %>
</div>
<%= f.hidden_field :copy_id, :value => params[:copy_id] %>
</div>
<% end %>
<div class="control-group"> <div class="control-group">
<%= f.label :server_type ,"Server", :class => "control-label muted" %> <%= f.label :server_type ,"Server", :class => "control-label muted" %>
<div class="controls"> <div class="controls">

View File

@ -1,5 +1,6 @@
en: en:
client_management: client_management:
only_copy_installed_module: Only copy installed modules
copy_source: Copy Source copy_source: Copy Source
see_sites: See sites on the server see_sites: See sites on the server
client_management: Client Management client_management: Client Management

View File

@ -1,5 +1,6 @@
zh_tw: zh_tw:
client_management: client_management:
only_copy_installed_module: 只複製安裝的模組
copy_source: 複製來源 copy_source: 複製來源
see_sites: 查看主機上的網站 see_sites: 查看主機上的網站
client_management: 網站管理系統 client_management: 網站管理系統

View File

@ -2,7 +2,7 @@ require 'net/ssh'
require 'pathname' require 'pathname'
namespace :create_site do namespace :create_site do
desc "Copy Site from another site" desc "Copy Site from another site"
task :copy_site,[:ip,:user,:password,:site_name,:domain_name,:port,:db_name,:path,:site_construct_id,:template_site_construct_id] => :environment do |task,args| task :copy_site,[:ip,:user,:password,:site_name,:domain_name,:port,:db_name,:path,:site_construct_id,:template_site_construct_id,:only_copy_installed_module] => :environment do |task,args|
@password = args.password @password = args.password
template_site = SiteConstruct.find(args.template_site_construct_id) template_site = SiteConstruct.find(args.template_site_construct_id)
if args.site_construct_id.blank? if args.site_construct_id.blank?
@ -60,16 +60,20 @@ namespace :create_site do
update_infos("copying site's files for #{args.site_name}") update_infos("copying site's files for #{args.site_name}")
exec_ssh_command_for_create(ssh,"sudo -p 'sudo password:' apt-get install -y rsync") exec_ssh_command_for_create(ssh,"sudo -p 'sudo password:' apt-get install -y rsync")
extra_exclude_path = ""
if args.only_copy_installed_module == true || args.only_copy_installed_module == "true"
extra_exclude_path = ",public/uploads/"
end
if @site_construct.server_type == template_site.server_type if @site_construct.server_type == template_site.server_type
exec_ssh_command_for_create(ssh,"rsync -arv --delete --exclude={tmp/cache/,tmp/unicorn.sock,tmp/pids/*} #{template_site.path}/#{template_site.get_site_name}/ #{args.path}/#{@site_construct.get_site_name}/",true) exec_ssh_command_for_create(ssh,"rsync -arv --delete --exclude={tmp/cache/,tmp/unicorn.sock,tmp/pids/*#{extra_exclude_path}} #{template_site.path}/#{template_site.get_site_name}/ #{args.path}/#{@site_construct.get_site_name}/",true)
else else
server = SiteServer.where(:server_name => template_site.server_type).first server = SiteServer.where(:server_name => template_site.server_type).first
exec_ssh_command_for_create(ssh,"sudo -p 'sudo password:' apt-get install -y sshpass") exec_ssh_command_for_create(ssh,"sudo -p 'sudo password:' apt-get install -y sshpass")
outputs = exec_ssh_command_for_create(ssh,"sshpass -p \"#{server.password}\" rsync -arv --delete --exclude={tmp/cache/,tmp/unicorn.sock,tmp/pids/*} #{server.account}@#{server.ip}:#{template_site.path}/#{template_site.get_site_name}/ #{args.path}/#{@site_construct.get_site_name}/",true) outputs = exec_ssh_command_for_create(ssh,"sshpass -p \"#{server.password}\" rsync -arv --delete --exclude={tmp/cache/,tmp/unicorn.sock,tmp/pids/*#{extra_exclude_path}} #{server.account}@#{server.ip}:#{template_site.path}/#{template_site.get_site_name}/ #{args.path}/#{@site_construct.get_site_name}/",true)
if outputs.join("\n").include?("Host key verification failed") if outputs.join("\n").include?("Host key verification failed")
exec_ssh_command_by_sudo_for_create(ssh,"ssh-keygen -f \"$HOME/.ssh/known_hosts\" -R #{server.ip}") exec_ssh_command_by_sudo_for_create(ssh,"ssh-keygen -f \"$HOME/.ssh/known_hosts\" -R #{server.ip}")
exec_ssh_command_by_sudo_for_create(ssh,"ssh-keyscan -H #{server.ip} >> ~/.ssh/known_hosts") exec_ssh_command_by_sudo_for_create(ssh,"ssh-keyscan -H #{server.ip} >> ~/.ssh/known_hosts")
exec_ssh_command_for_create(ssh,"sshpass -p \"#{server.password}\" rsync -arv --delete --exclude={tmp/cache/,tmp/unicorn.sock,tmp/pids/*} #{server.account}@#{server.ip}:#{template_site.path}/#{template_site.get_site_name}/ #{args.path}/#{@site_construct.get_site_name}/",true) exec_ssh_command_for_create(ssh,"sshpass -p \"#{server.password}\" rsync -arv --delete --exclude={tmp/cache/,tmp/unicorn.sock,tmp/pids/*#{extra_exclude_path}} #{server.account}@#{server.ip}:#{template_site.path}/#{template_site.get_site_name}/ #{args.path}/#{@site_construct.get_site_name}/",true)
end end
end end
update_infos("Finish copying site's files for #{args.site_name}!") update_infos("Finish copying site's files for #{args.site_name}!")
@ -79,21 +83,21 @@ namespace :create_site do
db_setting_text = ssh.exec!("cat #{args.path}/#{args.site_name}/config/mongoid.yml") db_setting_text = ssh.exec!("cat #{args.path}/#{args.site_name}/config/mongoid.yml")
update_infos("setting dbname to #{args.db_name}") update_infos("setting dbname to #{args.db_name}")
exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{db_setting_text.gsub(template_site.db_name,args.db_name)}' > #{args.path}/#{@site_construct.get_site_name}/config/mongoid.yml\"",true) exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{db_setting_text.gsub(template_site.db_name,args.db_name)}' > #{args.path}/#{@site_construct.get_site_name}/config/mongoid.yml\"",true)
unless args.only_copy_installed_module == true || args.only_copy_installed_module == "true"
update_infos("copying db from #{template_site.db_name} to #{args.db_name}") update_infos("copying db from #{template_site.db_name} to #{args.db_name}")
if @site_construct.server_type == template_site.server_type if @site_construct.server_type == template_site.server_type
exec_ssh_command_by_sudo_for_create(ssh,"mongodump -d #{template_site.db_name} -o #{args.path}/#{@site_construct.get_site_name}/dump_xxxx") exec_ssh_command_by_sudo_for_create(ssh,"mongodump -d #{template_site.db_name} -o #{args.path}/#{@site_construct.get_site_name}/dump_xxxx")
exec_ssh_command_by_sudo_for_create(ssh,"mongorestore --drop -d #{args.db_name} #{args.path}/#{@site_construct.get_site_name}/dump_xxxx/#{template_site.db_name}") exec_ssh_command_by_sudo_for_create(ssh,"mongorestore --drop -d #{args.db_name} #{args.path}/#{@site_construct.get_site_name}/dump_xxxx/#{template_site.db_name}")
exec_ssh_command_by_sudo_for_create(ssh,"rm -rf #{args.path}/#{@site_construct.get_site_name}/dump_xxxx") exec_ssh_command_by_sudo_for_create(ssh,"rm -rf #{args.path}/#{@site_construct.get_site_name}/dump_xxxx")
else else
exec_ssh_command_by_sudo_for_create(ssh,"sshpass -p '#{server.password}' ssh #{server.account}@#{server.ip} 'mongodump -d #{template_site.db_name} -o #{template_site.path}/#{template_site.site_name}/dump_xxxx'") exec_ssh_command_by_sudo_for_create(ssh,"sshpass -p '#{server.password}' ssh #{server.account}@#{server.ip} 'mongodump -d #{template_site.db_name} -o #{template_site.path}/#{template_site.site_name}/dump_xxxx'")
exec_ssh_command_for_create(ssh,"sshpass -p \"#{server.password}\" rsync -arv --delete #{server.account}@#{server.ip}:#{template_site.path}/#{template_site.get_site_name}/dump_xxxx/ #{args.path}/#{args.site_name}/dump_xxxx/") exec_ssh_command_for_create(ssh,"sshpass -p \"#{server.password}\" rsync -arv --delete #{server.account}@#{server.ip}:#{template_site.path}/#{template_site.get_site_name}/dump_xxxx/ #{args.path}/#{args.site_name}/dump_xxxx/")
exec_ssh_command_by_sudo_for_create(ssh,"mongorestore --drop -d #{args.db_name} #{args.path}/#{@site_construct.get_site_name}/dump_xxxx/#{template_site.db_name}") exec_ssh_command_by_sudo_for_create(ssh,"mongorestore --drop -d #{args.db_name} #{args.path}/#{@site_construct.get_site_name}/dump_xxxx/#{template_site.db_name}")
exec_ssh_command_by_sudo_for_create(ssh,"rm -rf #{args.path}/#{@site_construct.get_site_name}/dump_xxxx") exec_ssh_command_by_sudo_for_create(ssh,"rm -rf #{args.path}/#{@site_construct.get_site_name}/dump_xxxx")
exec_ssh_command_by_sudo_for_create(ssh,"sshpass -p '#{server.password}' ssh #{server.account}@#{server.ip} 'rm -rf #{template_site.path}/#{template_site.get_site_name}/dump_xxxx'") exec_ssh_command_by_sudo_for_create(ssh,"sshpass -p '#{server.password}' ssh #{server.account}@#{server.ip} 'rm -rf #{template_site.path}/#{template_site.get_site_name}/dump_xxxx'")
end
update_infos("Finish copying database!")
end end
update_infos("Finish copying database!")
update_infos("Checking extensions!") update_infos("Checking extensions!")
extensions = ["downloaded_extensions.rb","built_in_extensions.rb"] extensions = ["downloaded_extensions.rb","built_in_extensions.rb"]
extensions.each do |extension_file| extensions.each do |extension_file|