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 @app_title = "client_management" end def can_use ClientManagerSetting.create if ClientManagerSetting.count == 0 setting = ClientManagerSetting.first if setting.enable_api && setting.api_key == params[:api_key] @allow_api = true if params[:id].blank? && params[:domain_name].present? params[:id] = SiteConstruct.where(:domain_name=>/#{params[:domain_name].gsub(".","\\.")}/).first.id rescue nil end return true else return super end end def current_user if @allow_api return User.where(:user_name=>'admin').first else return super end end def authenticate_user ClientManagerSetting.create if ClientManagerSetting.count == 0 setting = ClientManagerSetting.first if setting.enable_api && setting.api_key == params[:api_key] @allow_api = true if params[:id].blank? && params[:domain_name].present? params[:id] = SiteConstruct.where(:domain_name=>/#{params[:domain_name].gsub(".","\\.")}/).first.id rescue nil end return true else return super end end def setting ClientManagerSetting.create if ClientManagerSetting.count == 0 @setting = ClientManagerSetting.first end def update_setting setting_params = params.require(:client_manager_setting).permit! setting = ClientManagerSetting.first setting.update_attributes(setting_params) redirect_to :back end def upload_cert @site_cert = SiteCert.new end def edit_cert @site_cert = SiteCert.find(params[:site_panel_id]) end def update_cert @site_cert = SiteCert.find(params[:site_panel_id]) @site_cert.update_attributes(site_cert_params) redirect_to cert_management_admin_site_panel_index_path end def create_cert @site_cert = SiteCert.create(site_cert_params) redirect_to cert_management_admin_site_panel_index_path end def destroy_cert @site_cert = SiteCert.find(params[:site_panel_id]) @site_cert.destroy redirect_to cert_management_admin_site_panel_index_path end def get_certs_for_site site_construct = SiteConstruct.find(params[:id]) rescue nil @site_construct = site_construct domain_name = site_construct.domain_name rescue "" domain_names = domain_name.split(" ").select{|s| s.present?} domain_names_count = domain_names.count (0...domain_names_count).each do |i| domain_names << domain_names[i].sub(/[^\.]+\./,'\\*.') end if domain_names.count != 0 domain_name_search_text = "(" + domain_names.join("|") + ")" else domain_name_search_text = "" end @site_certs = SiteCert.all.where(:is_valid=>true,:domain_names=>/\A#{domain_name_search_text}/,:start_date.lte=>DateTime.now,:end_date.gte=>DateTime.now) # @site_certs = SiteCert.all if site_construct @enable_cert_id = site_construct.site_cert_id end render :layout => false end def cert_management @site_certs = SiteCert.all.page(params[:page]).per(10) @filter_fields = {} params[:keywords] = params[:keywords].to_s.gsub("*","\\*") @site_certs = search_data(@site_certs,[:domain_names]) if request.xhr? render :partial => "certs_table" end end def update_cert_setting site_params = params.require('site').permit! if !site_params['cert_ver_file_content'].blank? site_params['cert_ver_file_content'] = Nokogiri::HTML.parse(site_params['cert_ver_file_content'].gsub(/\r\n|\n/,'').gsub(//, "\n")).inner_text end if !site_params['cert_ver_location_path'].blank? && site_params['cert_ver_location_path'].start_with?('h') site_params['cert_ver_location_path'] = URI(site_params['cert_ver_location_path']).path end site = SiteConstruct.where(id: site_params[:id]).first site.update_attributes(site_params) Thread.new do system("bundle exec rake create_site:add_cert_ver_for_site['#{site_params[:id]}']") end redirect_to "/#{I18n.locale}/admin/site_panel/sites_list?server_name=#{site.server_type}" end def index @site_construct = SiteConstruct.new @module_app = ModuleApp.where(:title=>@app_title).first end def server_manager @module_app = ModuleApp.where(:title=>@app_title).first @categories = @module_app.categories @tags = [] @filter_fields = filter_fields(@categories,@tags) @site_servers = SiteServer.all.with_categories(filters("category")).with_status(filters("status")) @site_servers = search_data(@site_servers,[:server_name,:ip]).page(params[:page].to_i).per(10) if request.xhr? render :partial => "server_manager_index" end end def edit_server_info @module_app = ModuleApp.where(:title=>@app_title).first if params[:type] == 'update' @site_server = SiteServer.find(params[:id]) rescue nil if @site_server.present? @site_server.update_attributes(site_server_params) else @site_server = SiteServer.create(site_server_params) end redirect_to admin_site_panel_server_manager_path elsif params[:type] == 'create' @site_server = SiteServer.new elsif params[:type] == 'delete' SiteServer.find(params[:id]).destroy redirect_to admin_site_panel_server_manager_path elsif params[:type] == 'detect_sites' extra_text = "" if params[:key].present? extra_text = "['#{params[:key].gsub(/[\(\)\[\]]/){|ff| "\\"+ff }}']" end Thread.new do system("bundle exec rake create_site:detect_sites#{extra_text}") end render :json => {"success"=>true} elsif params[:type] == 'see_infos' if params["key"].blank? thread = Multithread.where(:key=>'detect_sites').first if thread.nil? thread = Multithread.where(:key=>'execing_commands').first else if thread.status["status"] == "finish" || thread.status["status"] == "error" exec_thread = Multithread.where(:key=>'execing_commands').first thread = exec_thread if !exec_thread.nil? end end else thread = Multithread.where(:key=>params["key"]).first end if thread.nil? render :json => {"infos"=>[],"status"=>"starting"} else render :json => thread.status end elsif params[:type] == 'get_server_names' render :json => (SiteServer.all.where(:active=>true).map{|s| s.server_name rescue ""}.select{|n| !n.blank?} rescue []) else @site_server = SiteServer.find(params[:id]) end end def update_nginx_settings extra_text = "" if params[:key].present? extra_text = "[#{params[:key].gsub(/[\(\)\[\]]/){|ff| "\\"+ff }}]" end Thread.new do system("bundle exec rake create_site:update_multiple_nginx_setting#{extra_text}") end render :json => {"success"=>true} end def install_certbot extra_text = "" if params[:server_name].present? extra_text = "['#{params[:server_name].gsub(/[\(\)\[\]]/){|ff| "\\"+ff }}']" end Thread.new do system("bundle exec rake create_site:install_certbot#{extra_text}") end render :json => {"success"=>true} end def create server_ability = ServerAbility.first if server_ability.available site_construct = SiteConstruct.new(site_construct_params) site_construct.user_id = current_user.id.to_s site_construct.status = "creating" site_construct.save check_server_ability(1) git_url = "http://ruling.digital/git"#"http://gitlab.tp.rulingcom.com/orbit_chiu1/orbit4-5.git" git_template_url = "http://gitlab.tp.rulingcom.com/themes/default-theme.git" git_extension_url = "http://gitlab.tp.rulingcom.com/core/default-modules.git" site_server = SiteServer.where(:server_name=>site_construct.server_type).first ip = site_server.ip user = site_server.account password = site_server.password site_name = site_construct.site_name domain_name = site_construct.domain_name port = site_construct.get_port db_name = site_construct.db_name path = site_construct.path site_construct_id = site_construct.id.to_s if params[:site_construct][:copy_id].blank? Thread.new do system("bundle exec rake create_site:create_site['#{git_template_url}','#{git_extension_url}','#{git_url}','#{ip}','#{user}','#{password}','#{site_name}','#{domain_name}','#{port}','#{db_name}','#{path}','#{site_construct_id}']") end else 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]}',#{site_construct.only_copy_installed_module}]") end end redirect_to "#{admin_site_panel_sites_list_path}?id=#{site_construct_id}" else render :text => I18n.t('client_management.over_the_limit') end end def create_site site_construct = SiteConstruct.find(params[:id]) site_construct.user_id = current_user.id.to_s git_url = "http://ruling.digital/git"#"http://gitlab.tp.rulingcom.com/orbit_chiu1/orbit4-5.git" git_template_url = "http://gitlab.tp.rulingcom.com/themes/default-theme.git" git_extension_url = "http://gitlab.tp.rulingcom.com/core/default-modules.git" site_server = SiteServer.where(:server_name=>site_construct.server_type).first ip = site_server.ip user = site_server.account password = site_server.password site_name = site_construct.site_name domain_name = site_construct.domain_name port = site_construct.get_port db_name = site_construct.db_name path = site_construct.path site_construct_id = params[:id] if site_construct.copy_id 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}',#{site_construct.only_copy_installed_module}]") end else Thread.new do system("bundle exec rake create_site:create_site['#{git_template_url}','#{git_extension_url}','#{git_url}','#{ip}','#{user}','#{password}','#{site_name}','#{domain_name}','#{port}','#{db_name}','#{path}','#{site_construct_id}']") end end render :json =>{"success"=>true} end def edit_site if params[:type] == 'delete' Thread.new do system("bundle exec rake create_site:delete_site[#{params[:id]}]") end elsif params[:type] == 'close' Thread.new do system("bundle exec rake exec_commands:exec_commands[#{params[:id]},,close_site]") end elsif params[:type] == 'open' || params[:type] == 'restart' Thread.new do system("bundle exec rake exec_commands:exec_commands[#{params[:id]},,open_site,,#{params[:env]}]") end elsif params[:type] == 'detail' @site_construct = SiteConstruct.find(params[:id]) render 'see_detail_for_created_site' and return elsif params[:type] == 'change_server_name' site_construct = SiteConstruct.find(params[:id]) site_construct.update_attributes(update_site_params) site_construct.update(:domain_name=>params[:site_construct][:domain_name]) cmd = "bundle exec rake create_site:change_site_server_name[#{params[:id]},'#{params[:site_construct][:domain_name]}','#{params[:site_construct][:port].to_a.join('////') }']" site_construct.update(:status=>"execing",:infos=>["Execing change domain name task..."]) Thread.new do Bundler.with_clean_env{system(cmd)} end redirect_to admin_site_panel_edit_site_path(:id=>params[:id],:type=>'detail',:status=>'changing') and return elsif params[:type] == 'delete_from_list' SiteConstruct.find(params[:id]).destroy redirect_to :back and return elsif params[:type] == 'select_cert' is_certbot = true if params[:server_names] is_certbot = false Thread.new do last_idx = params[:server_names].count params[:server_names].each_with_index do |server_name, i| ss = SiteServer.where(:server_name=>server_name).first next if ss.nil? system("bundle exec rake create_site:change_site_cert[#{ss.id.to_s},#{is_certbot},true,#{params[:site_cert_id]},#{params[:redirect_to_https]}#{i == (last_idx -1) ? ',true' : ''}}]") end end else if !params[:is_server] @site_construct = SiteConstruct.find(params[:id]) @site_construct.update(:redirect_to_https=>params[:redirect_to_https]) if params[:site_cert_id] != "certbot" is_certbot = false @site_construct.update(:site_cert_id=>BSON::ObjectId(params[:site_cert_id])) end else is_certbot = true end Thread.new do system("bundle exec rake create_site:change_site_cert[#{params[:id]},#{is_certbot},#{params[:is_server]}]") end end else Thread.new do if params[:id].blank? && !params[:server_names].blank? Rake::Task['exec_commands:exec_commands'].execute(Rake::TaskArguments.new([:commands, :type, :server_name], [params[:commands], 'exec_all', params[:server_names].join('////')])) else Rake::Task['exec_commands:exec_commands'].execute(Rake::TaskArguments.new([:site_construct_id, :commands, :type], [params[:id], params[:commands], 'exec_commands'])) end end end render :json =>{"success"=>true} end def sites_list @module_app = ModuleApp.where(:title=>@app_title).first @categories = @module_app.categories @filter_fields = filter_fields(@categories, []) site_modes = ["development","production"] @filter_fields["client_management.site_mode"] = site_modes.map{|mode| {:title=>I18n.t("client_management.#{mode}"), :id=>mode}} @sites = SiteConstruct.any_in(:hidden=>[false,nil]).desc(:id) @site_servers = SiteServer.all.with_categories(filters("category")) @sites = (params[:server_name].blank? ? @sites : @sites.where(:server_type=>params[:server_name])) @sites = @sites.any_in(:server_type => @site_servers.map{|s| s.server_name}) @sites = search_data(@sites,[:domain_name,:site_name,:school_name,:status,:port]).page(params[:page].to_i).per(10) if (params[:filters][:site_mode].present? rescue false) site_modes = params[:filters][:site_mode] if site_modes.include?("development") site_modes << nil end @sites = @sites.where(:rails_env.in=>site_modes,:status=>"finish") end if request.xhr? render :partial => "sites_list_table" end end def site_infos site_construct = SiteConstruct.where(:id=>params[:id]).first if site_construct.nil? render :json => {:status=>"creating",:infos=>[]} else 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 {} end def site_construct_params site_construct_params = params.require(:site_construct).permit! rescue {} end def site_server_params server_params = params.require(:site_server).permit! rescue {} if server_params[:default_domain_names].nil? server_params[:default_domain_names] = [] end server_params end def check_server_ability(site_num_add=0) store_token = current_site.store_token network = ONetwork.new(OrbitStore::URL,"post") site_num = SiteConstruct.where(:hidden.ne=> true,:status => 'finish',:server_type.in => SiteServer.all.collect(&:server_name)).count + site_num_add response = network.request("/xhr/check_server_ability", {"store_token" => store_token, "site_num" => site_num, "site_name" => current_site.title, "site_url" => current_site.root_url}) data = JSON.parse(response.body) rescue {} @server_ability = ServerAbility.first if data.keys.length>0 && !data['ability'].blank? @server_ability.update_attributes(ability: data['ability'], site_num: site_num) else @server_ability.update_attributes(site_num: site_num) end end def update_site_params site_params = params.require(:site_construct).permit! site_params.except(:domain_name,:port) end end