This commit is contained in:
BoHung Chiu 2021-02-21 11:59:36 +08:00
parent cf2d6f29d6
commit 563716fc43
5 changed files with 62 additions and 53 deletions

View File

@ -38,6 +38,9 @@ class SiteConstruct
record.path = "/home/rulingcom/#{dir_path}"
end
end
def get_site_name
return self.site_name.gsub(" ","\\ ")
end
def get_domain_name
scheme = ""
extra_port = ""

View File

@ -67,13 +67,13 @@
<div class="control-group">
<%= f.label :site_name ,"Site Name", :class => "control-label muted" %>
<div class="controls">
<%= f.text_field :site_name, :id => "site_name", :placeholder => "xxx_xxx" %>
<%= f.text_field :site_name, :id => "site_name", :placeholder => "xxx_xxx", :oninput=> "replace_domain_input(this)" %>
<div class="hint">schoolname_deptname eg: nctu_eed</div>
</div>
</div>
<div class="control-group">
<%= f.label :domain_name ,"Domain Name", :class => "control-label muted" %>
<%= f.label :domain_name ,"Domain Name", :class => "control-label muted", :oninput=> "replace_domain_input(this)" %>
<div class="controls">
<div class="domain_group">
<input type="text" onchange="change_domain_input(this)" oninput="replace_domain_input(this)">
@ -213,6 +213,12 @@
$("#path").val("/home/rulingcom/" + type);
}
})
$("#school_name").on("input",function(){
var school = $(this).val();
if(school != ""){
$("#path").val("/home/rulingcom/school_sites/" + school);
}
})
function replace_domain_input(ele){
var correct_val = $(ele).val().match(/([0-9]||[a-z]||[-_.])*/gi).join("");
$(ele).val(correct_val.replace("_","-"));

View File

@ -10,7 +10,7 @@ namespace :create_site do
@site_construct.server_type = (SiteServer.where(:ip=>args.ip).first.server_name rescue args.ip)
@site_construct.site_name = args.site_name
@site_construct.domain_name = args.domain_name
@site_construct.db_name = args.db_name
@site_construct.db_name = args.db_name.gsub(" ","")
@site_construct.port = args.port
@site_construct.path = args.path
@site_construct.school_name = args.site_name.split(/[-_]/)
@ -32,12 +32,12 @@ namespace :create_site do
end
Net::SSH.start(args.ip , args.user , password: args.password) do |ssh|
update_infos("setting nginx for #{args.site_name}")
nginx_setting_texts = ('upstream '+args.site_name+'_sock {\n'+
' server unix:'+args.path+'/'+args.site_name+'/tmp/unicorn.sock;\n'+
nginx_setting_texts = ('upstream '+@site_construct.get_site_name+'_sock {\n'+
' server unix:'+args.path+'/'+@site_construct.get_site_name+'/tmp/unicorn.sock;\n'+
'}\n'+
'server {\n'+
' listen '+args.port+';\n\n'+
' root '+args.path+'/'+args.site_name+'/public;\n\n'+
' root '+args.path+'/'+@site_construct.get_site_name+'/public;\n\n'+
'server_name '+args.domain_name+';\n\n'+
' client_max_body_size 500m;\n\n'+
' location / {\n'+
@ -48,11 +48,11 @@ namespace :create_site do
' proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\n'+
' proxy_set_header Host \$http_host;\n'+
' proxy_connect_timeout 360;\n'+
'proxy_pass http://'+args.site_name+'_sock;\n'+
'proxy_pass http://'+@site_construct.get_site_name+'_sock;\n'+
' }\n'+
'}')
exec_ssh_command_by_sudo_for_create(ssh,"touch /etc/nginx/orbit_sites/#{args.site_name}")
exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{nginx_setting_texts}' > /etc/nginx/orbit_sites/#{args.site_name}\"")
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}\"")
update_infos("restarting nginx")
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' service nginx restart")
update_infos("finish restarting nginx")
@ -61,51 +61,51 @@ namespace :create_site do
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")
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.site_name}/ #{args.path}/#{args.site_name}/",true)
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)
else
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")
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.site_name}/ #{args.path}/#{args.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/*} #{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(session,"ssh-keygen -f \"$HOME/.ssh/known_hosts\" -R #{server.ip}")
exec_ssh_command_by_sudo_for_create(session,"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.site_name}/ #{args.path}/#{args.site_name}/",true)
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)
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}/#{args.site_name} -R")
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chmod 777 #{args.path}/#{args.site_name} -R")
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")
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(template_site.db_name,args.db_name)}' > #{args.path}/#{args.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)
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}/#{args.site_name}/dump_xxxx")
exec_ssh_command_by_sudo_for_create(ssh,"mongorestore --drop -d #{args.db_name} #{args.path}/#{args.site_name}/dump_xxxx/#{template_site.db_name}")
exec_ssh_command_by_sudo_for_create(ssh,"rm -rf #{args.path}/#{args.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,"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.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}/#{args.site_name}/dump_xxxx/#{template_site.db_name}")
exec_ssh_command_by_sudo_for_create(ssh,"rm -rf #{args.path}/#{args.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.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'")
end
update_infos("Finish copying database!")
update_infos("execing bundle install...")
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{args.site_name}\nbundle install'",true)
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{args.site_name}\nbundle 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'")
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}/#{args.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_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'")
if outputs.include? "not writable"
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chown #{args.user}:#{args.user} #{args.path}/#{args.site_name} -R")
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chmod 777 #{args.path}/#{args.site_name} -R")
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")
end
update_infos("finish creating #{args.site_name}")
exec_ssh_command_by_sudo_for_create(ssh,"chmod 777 #{args.path}/#{args.site_name} -R")
exec_ssh_command_by_sudo_for_create(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

View File

@ -9,7 +9,7 @@ namespace :create_site do
@site_construct.server_type = (SiteServer.where(:ip=>args.ip).first.server_name rescue args.ip)
@site_construct.site_name = args.site_name
@site_construct.domain_name = args.domain_name
@site_construct.db_name = args.db_name
@site_construct.db_name = args.db_name.gsub(" ","")
@site_construct.port = args.port
@site_construct.path = args.path
@site_construct.school_name = args.site_name.split(/[-_]/)
@ -31,12 +31,12 @@ namespace :create_site do
end
Net::SSH.start(args.ip , args.user , password: args.password) do |ssh|
update_infos("setting nginx for #{args.site_name}")
nginx_setting_texts = ('upstream '+args.site_name+'_sock {\n'+
' server unix:'+args.path+'/'+args.site_name+'/tmp/unicorn.sock;\n'+
nginx_setting_texts = ('upstream '+@site_construct.get_site_name+'_sock {\n'+
' server unix:'+args.path+'/'+@site_construct.get_site_name+'/tmp/unicorn.sock;\n'+
'}\n'+
'server {\n'+
' listen '+args.port+';\n\n'+
' root '+args.path+'/'+args.site_name+'/public;\n\n'+
' root '+args.path+'/'+@site_construct.get_site_name+'/public;\n\n'+
'server_name '+args.domain_name+';\n\n'+
' client_max_body_size 500m;\n\n'+
' location / {\n'+
@ -47,25 +47,25 @@ namespace :create_site do
' proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;\n'+
' proxy_set_header Host \$http_host;\n'+
' proxy_connect_timeout 360;\n'+
'proxy_pass http://'+args.site_name+'_sock;\n'+
'proxy_pass http://'+@site_construct.get_site_name+'_sock;\n'+
' }\n'+
'}')
exec_ssh_command_by_sudo_for_create(ssh,"touch /etc/nginx/orbit_sites/#{args.site_name}")
exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{nginx_setting_texts}' > /etc/nginx/orbit_sites/#{args.site_name}\"")
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}\"")
update_infos("restarting nginx")
exec_ssh_command_by_sudo_for_create(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}")
update_infos("cloning orbit4-5 from #{args.git_url} to #{args.path}/#{args.site_name}")
exec_ssh_command_by_sudo_for_create(ssh,"git clone #{args.git_url} #{args.path}/#{args.site_name}")
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{args.site_name} && git submodule add #{args.git_template_url} app/templates/default-theme'")
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{args.site_name} && git submodule add #{args.git_extension_url} modules'")
ssh.exec!("bash -l -c 'cd #{args.path}/#{args.site_name} && ln -s modules/* .'")
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chown #{args.user}:#{args.user} #{args.path}/#{args.site_name} -R")
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chmod 777 #{args.path}/#{args.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("orbit_4_5",args.db_name)}' > #{args.path}/#{args.site_name}/config/mongoid.yml\"",true)
exec_ssh_command_by_sudo_for_create(ssh,"git clone #{args.git_url} #{args.path}/#{@site_construct.get_site_name}")
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{@site_construct.get_site_name} && git submodule add #{args.git_template_url} app/templates/default-theme'")
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{@site_construct.get_site_name} && git submodule add #{args.git_extension_url} modules'")
ssh.exec!("bash -l -c 'cd #{args.path}/#{@site_construct.get_site_name} && ln -s modules/* .'")
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.gsub(" ","\\ ")} -R")
db_setting_text = ssh.exec!("cat #{args.path}/#{@site_construct.get_site_name}/config/mongoid.yml")
update_infos("setting dbname to #{@site_construct.db_name}")
exec_ssh_command_by_sudo_for_create(ssh,"sh -c \"echo '#{db_setting_text.gsub("orbit_4_5",@site_construct.db_name)}' > #{args.path}/#{@site_construct.get_site_name}/config/mongoid.yml\"",true)
gemdir = ssh.exec!("/home/rulingcom/.rvm/bin/rvm gemdir").to_s
if gemdir.include?("No such file or directory")
ls = ssh.exec!("echo `ls /home`").to_s.split
@ -80,17 +80,17 @@ namespace :create_site do
else
bundle_dir = gemdir.gsub("\n","") + '/bin/bundle'
update_infos("execing bundle install...")
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{args.site_name}\nbundle install'",true)
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{args.site_name}\nbundle 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'")
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}/#{args.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_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'")
if outputs.include? "not writable"
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chown #{args.user}:#{args.user} #{args.path}/#{args.site_name} -R")
exec_ssh_command_by_sudo_for_create(ssh,"sudo -p 'sudo password:' chmod 777 #{args.path}/#{args.site_name} -R")
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")
end
update_infos("finish creating #{args.site_name}")
exec_ssh_command_by_sudo_for_create(ssh,"chmod 777 #{args.path}/#{args.site_name} -R")
exec_ssh_command_by_sudo_for_create(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

View File

@ -21,14 +21,14 @@ namespace :create_site do
Net::SSH.start(ip , user , password: password) do |ssh|
@site_construct.update!(:infos=>[])
update_infos("deleting database for #{@site_construct.site_name}")
db_name = exec_ssh_command_by_sudo(ssh,"echo `cat #{@site_construct.path}/#{@site_construct.site_name}/config/mongoid.yml | grep 'database'`").split("database:").last.strip
db_name = exec_ssh_command_by_sudo(ssh,"echo `cat #{@site_construct.path}/#{@site_construct.get_site_name}/config/mongoid.yml | grep 'database'`").split("database:").last.strip
exec_ssh_command_by_sudo(ssh,"bash -l -c 'echo \"db.dropDatabase()\" | mongo --shell \'#{db_name}\''")
update_infos("finish deleting database for #{@site_construct.site_name}")
update_infos("deleting orbit4-5 at #{@site_construct.path}/#{@site_construct.site_name}")
exec_ssh_command_by_sudo(ssh,"rm -rf #{@site_construct.path}/#{@site_construct.site_name}")
exec_ssh_command_by_sudo(ssh,"rm -rf #{@site_construct.path}/#{@site_construct.get_site_name}")
update_infos("finish deleting #{@site_construct.site_name}")
update_infos("deleting nginx for #{@site_construct.site_name}")
exec_ssh_command_by_sudo(ssh,"rm -f #{@site_construct.nginx_file}")
exec_ssh_command_by_sudo(ssh,"rm -f #{@site_construct.nginx_file.gsub(" ","\\ ")}")
update_infos("finish deleting nginx setting for #{@site_construct.site_name}")
update_infos("restarting nginx")
exec_ssh_command_by_sudo(ssh,"service nginx restart")