This commit is contained in:
BoHung Chiu 2022-09-08 14:48:32 +08:00
parent 3f4821d0db
commit f19ce221ae
9 changed files with 47 additions and 31 deletions

View File

@ -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 {}

View File

@ -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

View File

@ -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]

View File

@ -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

View File

@ -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

View File

@ -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!")

View File

@ -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")

View File

@ -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")

View File

@ -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