This commit is contained in:
BoHung Chiu 2022-09-19 12:07:27 +08:00
parent d9547f282b
commit 1ca691dd4d
4 changed files with 52 additions and 46 deletions

View File

@ -34,60 +34,60 @@ namespace :create_site do
Net::SSH.start(args.ip , args.user , password: args.password) do |ssh|
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,"x='#{nginx_setting_texts.gsub(/'{1,3}/,"\"\'\"")}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > /etc/nginx/orbit_sites/#{@site_construct.get_site_name}\"")
exec_ssh_command_by_sudo_for_copy(ssh,"touch /etc/nginx/orbit_sites/#{@site_construct.get_site_name}")
exec_ssh_command_by_sudo_for_copy(ssh,"x='#{nginx_setting_texts.gsub(/'{1,3}/,"\"\'\"")}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > /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")
exec_ssh_command_by_sudo_for_copy(ssh,"sudo -p 'sudo password:' service nginx restart")
update_infos("finish restarting nginx")
exec_ssh_command_by_sudo_for_create(ssh,"mkdir -p #{args.path}")
exec_ssh_command_by_sudo_for_copy(ssh,"mkdir -p #{args.path}")
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_copy(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
server = SiteServer.where(:server_name => template_site.server_type).first
ls_out = exec_ssh_command_for_create(ssh, "ls #{@site_construct.full_site_path}/app").join("\n")
ls_out = exec_ssh_command_for_copy(ssh, "ls #{@site_construct.full_site_path}/app").join("\n")
if ls_out.include?("No such") || org_creating
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/*#{extra_exclude_path}} #{template_site.path}/#{template_site.get_site_name}/ #{args.path}/#{@site_construct.get_site_name}/",true)
exec_ssh_command_for_copy(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
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/*#{extra_exclude_path}} #{server.account}@#{server.ip}:#{template_site.path}/#{template_site.get_site_name}/ #{args.path}/#{@site_construct.get_site_name}/",true)
exec_ssh_command_for_copy(ssh,"sudo -p 'sudo password:' apt-get install -y sshpass")
outputs = exec_ssh_command_for_copy(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")
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_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)
exec_ssh_command_by_sudo_for_copy(ssh,"ssh-keygen -f \"$HOME/.ssh/known_hosts\" -R #{server.ip}")
exec_ssh_command_by_sudo_for_copy(ssh,"ssh-keyscan -H #{server.ip} >> ~/.ssh/known_hosts")
exec_ssh_command_for_copy(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
update_infos("Finish copying site's files for #{args.site_name}!")
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chown #{args.user}:#{args.user} #{args.path}/#{@site_construct.get_site_name} -R")
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chmod 777 #{args.path}/#{@site_construct.get_site_name} -R")
exec_ssh_command_by_sudo_for_copy(ssh,"sudo -p 'sudo password:' chown #{args.user}:#{args.user} #{args.path}/#{@site_construct.get_site_name} -R")
exec_ssh_command_by_sudo_for_copy(ssh,"sudo -p 'sudo password:' chmod 777 #{args.path}/#{@site_construct.get_site_name} -R")
db_setting_text = ssh.exec!("cat #{args.path}/#{args.site_name}/config/mongoid.yml")
update_infos("setting dbname to #{args.db_name}")
exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{db_setting_text.gsub(/database: .+/,"database: "+args.db_name)}' > #{args.path}/#{@site_construct.get_site_name}/config/mongoid.yml\"",true)
exec_ssh_command_by_sudo_for_copy(ssh,"sh -c \"echo '#{db_setting_text.gsub(/database: .+/,"database: "+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}")
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,"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_copy(ssh,"mongodump -d #{template_site.db_name} -o #{args.path}/#{@site_construct.get_site_name}/dump_xxxx")
exec_ssh_command_by_sudo_for_copy(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_copy(ssh,"rm -rf #{args.path}/#{@site_construct.get_site_name}/dump_xxxx")
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_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,"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_copy(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_copy(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_copy(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_copy(ssh,"rm -rf #{args.path}/#{@site_construct.get_site_name}/dump_xxxx")
exec_ssh_command_by_sudo_for_copy(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
update_infos("Checking extensions!")
extensions = ["downloaded_extensions.rb","built_in_extensions.rb"]
extensions.each do |extension_file|
outputs = exec_ssh_command_by_sudo_for_create(ssh,"cat #{args.path}/#{@site_construct.get_site_name}/#{extension_file}")
outputs = exec_ssh_command_by_sudo_for_copy(ssh,"cat #{args.path}/#{@site_construct.get_site_name}/#{extension_file}")
next if outputs.blank?
if !outputs.include?("No such file or directory")
read_lines = outputs.split("\n").map{|l| l.split('#').first}
@ -97,32 +97,32 @@ namespace :create_site do
absolute_path = (path[0] == "/") ? path : ("#{template_site.path}/#{template_site.get_site_name}/#{path}")
absolute_path = (absolute_path[-1] == "/") ? absolute_path : (absolute_path + "/")
if @site_construct.server_type == template_site.server_type
exec_ssh_command_for_create(ssh,"rsync -arv --delete #{absolute_path} #{args.path}/#{args.site_name}/#{Pathname.new(absolute_path).basename}/")
exec_ssh_command_for_copy(ssh,"rsync -arv --delete #{absolute_path} #{args.path}/#{args.site_name}/#{Pathname.new(absolute_path).basename}/")
else
server = SiteServer.where(:server_name => template_site.server_type).first
exec_ssh_command_for_create(ssh,"sshpass -p \"#{server.password}\" rsync -arv --delete #{server.account}@#{server.ip}:#{absolute_path} #{args.path}/#{args.site_name}/#{Pathname.new(absolute_path).basename}/")
exec_ssh_command_for_copy(ssh,"sshpass -p \"#{server.password}\" rsync -arv --delete #{server.account}@#{server.ip}:#{absolute_path} #{args.path}/#{args.site_name}/#{Pathname.new(absolute_path).basename}/")
end
outputs = outputs.gsub(path,"#{args.path}/#{args.site_name}/#{Pathname.new(absolute_path).basename}/")
end
if include_path_lines.count != 0
exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{outputs.gsub("\"","\\\"").gsub("'","\\\"")}' > #{args.path}/#{@site_construct.get_site_name}/#{extension_file}\"")
exec_ssh_command_by_sudo_for_copy(ssh,"sh -c \"echo '#{outputs.gsub("\"","\\\"").gsub("'","\\\"")}' > #{args.path}/#{@site_construct.get_site_name}/#{extension_file}\"")
end
end
end
update_infos("Finish checking extensions!")
update_infos("execing bundle install...")
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{@site_construct.get_site_name}\nbundle install'",true)
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{@site_construct.get_site_name}\nbundle install'")
exec_ssh_command_by_sudo_for_copy(ssh,"bash -l -c 'cd #{args.path}/#{@site_construct.get_site_name}\nbundle install'",true)
exec_ssh_command_by_sudo_for_copy(ssh,"bash -l -c 'cd #{args.path}/#{@site_construct.get_site_name}\nbundle install'")
update_infos("finish execing bundle install")
update_infos("starting #{args.site_name} web server to development")
outputs = exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{@site_construct.get_site_name}\nkill -s TERM `fuser tmp/unicorn.sock`\nsudo -p \"sudo password:\" kill -s TERM `sudo -p \"sudo password:\" fuser tmp/unicorn.sock`\nsudo -p \"sudo password:\" rm -f tmp/pids/unicorn.pid\nbundle exec unicorn_rails -c config/unicorn.rb -D -E development\n'")
outputs = exec_ssh_command_by_sudo_for_copy(ssh,"bash -l -c 'cd #{args.path}/#{@site_construct.get_site_name}\nkill -s TERM `fuser tmp/unicorn.sock`\nsudo -p \"sudo password:\" kill -s TERM `sudo -p \"sudo password:\" fuser tmp/unicorn.sock`\nsudo -p \"sudo password:\" rm -f tmp/pids/unicorn.pid\nbundle exec unicorn_rails -c config/unicorn.rb -D -E development\n'")
if (outputs.include? "not writable" rescue false)
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chown #{args.user}:#{args.user} #{args.path}/#{@site_construct.get_site_name} -R")
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chmod 777 #{args.path}/#{@site_construct.get_site_name} -R")
exec_ssh_command_by_sudo_for_copy(ssh,"sudo -p 'sudo password:' chown #{args.user}:#{args.user} #{args.path}/#{@site_construct.get_site_name} -R")
exec_ssh_command_by_sudo_for_copy(ssh,"sudo -p 'sudo password:' chmod 777 #{args.path}/#{@site_construct.get_site_name} -R")
end
update_infos("finish creating #{args.site_name}")
exec_ssh_command_by_sudo_for_create(ssh,"chmod 777 #{args.path}/#{@site_construct.get_site_name} -R")
exec_ssh_command_by_sudo_for_copy(ssh,"chmod 777 #{args.path}/#{@site_construct.get_site_name} -R")
@site_construct.update(:status =>"finish")
puts "finish creating #{args.site_name} on #{args.ip}"
end
@ -147,11 +147,11 @@ namespace :create_site do
@thread.save!
return @thread.status["infos"]
end
def exec_ssh_command_for_create(session,command,update=false)
def exec_ssh_command_for_copy(session,command,update=false)
outputs = []
session.open_channel do |channel|
channel.request_pty do |channel, success|
channel.exec(command) do |ch, success|
channel.exec("LANG=en.UTF8 #{command}") do |ch, success|
abort "could not execute command: #{command}" unless success
channel.on_data do |ch, data|
outputs.push(data)
@ -173,12 +173,14 @@ namespace :create_site do
session.loop
return outputs
end
def exec_ssh_command_by_sudo_for_create(session,command,update=false)
outputs = exec_ssh_command_for_create(session,command,update)
def exec_ssh_command_by_sudo_for_copy(session,command,update=false)
outputs = exec_ssh_command_for_copy(session,command,update)
tmp = outputs.join("\n")
if outputs.join("\n").include?("Permission denied") || outputs.join("\n").include?("Operation not permitted")
outputs = exec_ssh_command_for_create(session,"sudo -p 'sudo password:' #{command}",update)
outputs = exec_ssh_command_for_copy(session,"sudo -p 'sudo password:' #{command}",update)
tmp = outputs.join("\n")
end
return outputs.join("\n")
return tmp
end
def update_infos_for_exec(info,update_last=false)
if update_last && !@site_construct.infos.empty?

View File

@ -103,7 +103,7 @@ namespace :create_site do
outputs = []
session.open_channel do |channel|
channel.request_pty do |channel, success|
channel.exec(command) do |ch, success|
channel.exec("LANG=en.UTF8 #{command}") do |ch, success|
abort "could not execute command: #{command}" unless success
channel.on_data do |ch, data|
outputs.push(data)
@ -127,10 +127,12 @@ namespace :create_site do
end
def exec_ssh_command_by_sudo_for_create(session,command,update=false)
outputs = exec_ssh_command_for_create(session,command,update)
if outputs.join("\n").include?("Permission denied") || outputs.join("\n").include?("Operation not permitted")
tmp = outputs.join("\n")
if tmp.include?("Permission denied") || tmp.include?("Operation not permitted")
outputs = exec_ssh_command_for_create(session,"sudo -p 'sudo password:' #{command}",update)
tmp = outputs.join("\n")
end
return outputs.join("\n")
return tmp
end
def update_infos_for_exec(info,update_last=false,update_array=false)
return if @site_construct.nil?

View File

@ -206,7 +206,7 @@ namespace :exec_commands do
end
session.open_channel do |channel|
channel.request_pty do |channel, success|
channel.exec(command) do |ch, success|
channel.exec("LANG=en.UTF8 #{command}") do |ch, success|
abort "could not execute command: #{command}" unless success
channel.on_data do |ch, data|
if data.to_s.include?("sudo password:") || data.to_s.include?("Password:")
@ -273,11 +273,13 @@ namespace :exec_commands do
end
def exec_ssh_command_by_sudo_and_see_output(session,command,update=1,output_string=false)
outputs = exec_ssh_command_and_see_output(session,command,update)
if outputs.join("\n").include?("Permission denied") || outputs.join("\n").include?("Operation not permitted")
tmp = outputs.join("\n")
if tmp.include?("Permission denied") || tmp.include?("Operation not permitted")
outputs = exec_ssh_command_and_see_output(session,"sudo -p 'sudo password:' #{command}",update)
tmp = outputs.join("\n")
end
if output_string
return outputs.join("\n")
return tmp
else
return outputs
end

View File

@ -14,7 +14,7 @@ namespace :create_site do
puts "execing #{command}"
session.open_channel do |channel|
channel.request_pty do |channel, success|
channel.exec(command) do |ch, success|
channel.exec("LANG=en.UTF8 #{command}") do |ch, success|
abort "could not execute command: #{command}" unless success
channel.on_data do |ch, data|
outputs.push(data)