client_management/lib/tasks/create_site.rake

103 lines
5.3 KiB
Ruby

require 'net/ssh'
require 'pathname'
namespace :create_site do
desc "Create Site Script"
task :create_site,[:git_user,:git_password,:git_url,:ip,:user,:password,:site_name,:domain_name,:port,:db_name,:path,:site_construct_id] => :environment do |task,args|
@password = args.password
puts args
if args.site_construct_id.blank?
@site_construct = SiteConstruct.new
@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.port = args.port
@site_construct.path = args.path
@site_construct.school_name = args.site_name.split(/[-_]/)
@site_construct.user_id = User.first.id.to_s
@site_construct.save
else
@site_construct = SiteConstruct.find(args.site_construct_id)
end
begin
@site_construct.update(:status=>"creating")
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'+
'}\n'+
'server {\n'+
' listen '+args.port+';\n\n'+
' root '+args.path+'/'+args.site_name+'/public;\n\n'+
'server_name '+args.domain_name+';\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'+
' proxy_connect_timeout 360;\n'+
'proxy_pass http://'+args.site_name+'_sock;\n'+
' }\n'+
'}')
exec_ssh_command_by_sudo(ssh,"touch /etc/nginx/orbit_sites/#{args.site_name}")
exec_ssh_command_by_sudo(ssh,"sh -c \"echo '#{nginx_setting_texts}' > /etc/nginx/orbit_sites/#{args.site_name}\"")
update_infos("restarting nginx")
exec_ssh_command_by_sudo(ssh,"service nginx restart")
update_infos("finish restarting nginx")
dir = Pathname.new(args.path)
while dir.to_s != '/' do
exec_ssh_command_by_sudo(ssh,"mkdir #{dir}")
dir = dir.dirname
end
update_infos("cloning orbit4-5 from #{args.git_url} to #{args.path}/#{args.site_name}")
exec_ssh_command_by_sudo(ssh,"git clone #{args.git_url.gsub("http://","http://#{args.git_user+':'+args.git_password+'@'}").gsub("https://","https://#{args.git_user+':'+args.git_password+'@'}")} #{args.path}/#{args.site_name}")
exec_ssh_command_by_sudo(ssh,"chown #{args.user}:#{args.user} -R #{args.path}/#{args.site_name}")
exec_ssh_command_by_sudo(ssh,"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(ssh,"echo '#{db_setting_text.gsub("orbit_4_5",args.db_name)}' > #{args.path}/#{args.site_name}/config/mongoid.yml")
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
ls.each do |d|
gemdir = ssh.exec!("/home/#{d}/.rvm/bin/rvm gemdir").to_s
break if !gemdir.to_s.include?("No such file or directory")
end
end
if gemdir.include?("No such file or directory")
update_infos("please connect your admin to install rvm on #{args.ip}")
@site_construct.update(:status =>"error")
else
bundle_dir = gemdir.gsub("\n","") + '/bin/bundle'
update_infos("execing bundle install...")
exec_ssh_command_by_sudo(ssh,"bash -l -c 'cd #{args.path}/#{args.site_name}\n#{bundle_dir} install'")
exec_ssh_command_by_sudo(ssh,"bash -l -c 'cd #{args.path}/#{args.site_name}\n#{bundle_dir} install'")
update_infos("finish execing bundle install")
update_infos("starting #{args.site_name} web server to development")
exec_ssh_command_by_sudo(ssh,"bash -l -c 'cd #{args.path}/#{args.site_name}\n#{bundle_dir} exec unicorn_rails -c config/unicorn.rb -D -E development\n'")
update_infos("finish creating #{args.site_name}")
@site_construct.update(:status =>"finish")
puts "finish creating #{args.site_name} on #{args.ip}"
end
end
rescue =>e
@site_construct.update(:status =>"error",:infos=>@site_construct.infos.push("#{e}"))
end
end
def exec_ssh_command_by_sudo(session,command)
output = session.exec!("echo '#{@password}' | sudo -S #{command}")
# output = session.exec!("echo '#{@password}' | sudo -S -s #{command}")
if output.include?("sudo:") && output.include?("command not found")
output = session.exec!(command)
end
return output
end
def update_infos(info)
@site_construct.infos = @site_construct.infos.push(info)
@site_construct.save!
return @site_construct.infos
end
end