add create site feature

This commit is contained in:
邱博亞 2021-01-19 12:48:03 +08:00
parent ce83a61b25
commit 2322db910c
19 changed files with 1141 additions and 192 deletions

View File

@ -35,6 +35,32 @@ class Admin::SitePanelController < OrbitAdminController
elsif params[:type] == 'delete'
SiteServer.find(params[:id]).destroy
redirect_to admin_site_panel_server_manager_path
elsif params[:type] == 'detect_sites'
Thread.new do
system("rake create_site:detect_sites")
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.map{|s| s.server_name rescue ""}.select{|n| !n.blank?} rescue [])
else
@site_server = SiteServer.find(params[:id])
end
@ -90,21 +116,40 @@ class Admin::SitePanelController < OrbitAdminController
end
elsif params[:type] == 'close'
Thread.new do
system("rake create_site:exec_commands[#{params[:id]},,close_site]")
system("rake exec_commands:exec_commands[#{params[:id]},,close_site]")
end
elsif params[:type] == 'open'
Thread.new do
system("rake create_site:exec_commands[#{params[:id]},,open_site]")
system("rake exec_commands:exec_commands[#{params[:id]},,open_site]")
end
elsif params[:type] == 'detail'
@site = SiteConstruct.find(params[:id])
render 'see_detail_for_created_site' and return
elsif params[:type] == 'delete_from_list'
SiteConstruct.find(params[:id]).destroy
redirect_to :back and return
else
Thread.new do
system("rake create_site:exec_commands[#{params[:id]},'#{params[:commands]}',open_site]")
if params[:id].blank? && !params[:server_names].blank?
system("rake exec_commands:exec_commands[,'#{params[:commands]}',exec_all,'#{params[:server_names].join('////')}']")
else
system("rake exec_commands:exec_commands[#{params[:id]},'#{params[:commands]}',exec_commands]")
end
end
end
render :json =>{"success"=>true}
end
def sites_list
@sites = SiteConstruct.all.desc(:id).page(params[:page].to_i).per(10)
@module_app = ModuleApp.where(:title=>@app_title).first
@categories = @module_app.categories
@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]).page(params[:page].to_i).per(10)
if request.xhr?
render :partial => "sites_list_table"
end
end
def site_infos
site_construct = SiteConstruct.where(:id=>params[:id]).first

View File

@ -8,6 +8,7 @@ class SiteConstruct
field :server_type
field :site_name
field :domain_name
field :nginx_file
field :db_name
field :port, type: String, :default => "80"
field :path
@ -17,6 +18,7 @@ class SiteConstruct
field :constructed, type: Boolean, :default => false
field :status, type: String, :default => ""
field :infos, type: Array, :default => []
field :hidden, type: Boolean, :default => false
after_initialize do |record|
if record.status.nil?
record.status = ""
@ -26,5 +28,13 @@ class SiteConstruct
record.infos = []
record.save
end
if record.nginx_file.nil?
record.nginx_file = "/etc/nginx/orbit_sites/"+record.site_name.to_s
record.save
end
if record.path.nil?
dir_path = ((record.site_type == "School" && !record.school_name.blank?) ? "school_sites/#{record.school_name}" : "orbit_sites")
record.path = "/home/rulingcom/#{dir_path}"
end
end
end

View File

@ -116,6 +116,7 @@
})
$('#site_construct_server_type').click(function(){
var domain_name = domain_name_relations[$(this).val()];
$("#domain_name").val($("#site_name").val().replace("_","-") + "."+domain_name);
$('#domain_name').siblings('.hint').html('schoolname-deptname.'+domain_name+' eg: nctu-eed.'+domain_name);
})
$("#site_name").on("blur",function(){
@ -134,7 +135,16 @@
}
})
$("#site_construct_site_type").val("School");
$("form#new_site_construct").on("submit",function(){
$("form.main-forms").submit(function(){
$("#path").removeAttr("disabled");
var type = $("#site_construct_site_type").val();
type = (type == "School" ? "school_sites" : "orbit_sites" );
var school = $("#school_name").val();
if(school != ""){
$("#path").val("/home/rulingcom/" + type + "/" + school);
}else{
$("#path").val("/home/rulingcom/" + type);
}
console.log($("#path").val());
})
</script>

View File

@ -1,6 +1,7 @@
<table class="table main-list default footable-loaded">
<thead>
<tr>
<th>Sites amount</th>
<th>Server name</th>
<th>IP</th>
<th>Action</th>
@ -9,11 +10,19 @@
<tbody>
<% @site_servers.each do |site_server|%>
<tr>
<td>
<p>
Active: <%=SiteConstruct.where(:server_type => site_server.server_name,:status=>"finish").count %><br>
Closed: <%=SiteConstruct.where(:server_type => site_server.server_name,:status=>"closed").count %><br>
Not yet installed: <%=SiteConstruct.where(:server_type => site_server.server_name,:status=>"").count %><br>
</p>
</td>
<td><%=site_server.server_name%></td>
<td><%=site_server.ip%></td>
<td>
<a class="btn btn-primary" href = "<%= admin_site_panel_edit_server_info_path+"?id=#{site_server.id.to_s}" %>"><%=t(:edit)%></a>
<a class="btn btn-primary" href = "#" onclick="if(window.confirm('Do you really want to delete <%=site_server.server_name%>?')) { window.location.href = '<%= admin_site_panel_edit_server_info_path+"?id=#{site_server.id.to_s}&type=delete" %>';}"><%=t(:remove)%></a>
<a class="btn btn-primary" href = "<%= admin_site_panel_sites_list_path+"?server_name=#{site_server.server_name}" %>"><%= t('client_management.see_sites') %></a>
</td>
</tr>
<% end %>
@ -21,5 +30,113 @@
</table>
<div class="bottomnav clearfix">
<div class="pagination pagination-centered"><%=paginate(@site_servers)%></div>
<a id="see_infos" class="btn btn-primary pull-right" href="#">See infos</a>
<a class="btn btn-primary pull-right" href="<%=admin_site_panel_edit_server_info_path+"?type=create"%>"><%=t(:add)%></a>
</div>
<a id="detect_sites" class="btn btn-primary pull-right" href="#">Detect sites</a>
<a id="exec_commands" class="btn btn-primary pull-right" href="#">Exec commands</a>
</div>
<script type="text/javascript">
var close_info = false;
var timeout_id;
var status_relation = {"starting":"<span style=\"color: skyblue;\">starting</span>","execing":"<span style=\"color: skyblue;\">execing</span>","detecting":"<span style=\"color: skyblue;\">detecting</span>","error":"<span style=\"color: red;\">error</span>","finish": "<span style=\"color: darkseagreen;\">finish</span>","closed":"<span style=\"color: red;\">closed</span>"};
function show_exec_commands_block(server_names){
if($("#exec-commands-dialog-confirm").length == 0){
$("#main-wrap").before("<div id='exec-commands-dialog-confirm' title='Commands to Exec'>"+
"<div style='clear:both;'></div><div id='server_names_block'></div><textarea style='height: 200px;width: 100%;' id='commands_area'></textarea>"+
"</div>");
};
$( "#exec-commands-dialog-confirm" ).dialog({
resizable: true,
minHeight: 300,
maxHeight: 400,
modal: true,
width: '80%',
close: function(){$( this ).dialog( "close" );},
buttons: {
"<%= t(:submit) %>": function(){$( this ).dialog( "close" );exec_commands()},
}
});
$("#server_names_block").html($.map(server_names,function(server_name){
return "<input style='float: left;' class='server_name_checkbox' type='checkbox' id='server_name_"+server_name+"'><label style='float: left;' for='server_name_"+server_name+"'>"+server_name+"</label>"
}))
}
function exec_commands(){
var commands = $('#commands_area').val().replace("\r\n","////").replace("\n","////").replace("<br>","////").replace("<br/>","////");
var server_names = [];
var $els = $(".server_name_checkbox");
for(var i = 0; i < $els.length; i++){
if($els.eq(i).prop("checked")){
console.log($els.eq(i));
server_names.push($("label[for='"+$els.eq(i).attr('id')+"']").text().trim());
}
}
$.post("<%=admin_site_panel_edit_site_path%>",{'server_names':server_names,'commands':commands}).done(function(){
show_infos_dialog("execing_commands");
});
}
function see_infos(key=""){
if(!close_info){
var request = $.post("<%=admin_site_panel_edit_server_info_path%>",{"type":'see_infos',"key":key});
request.done(function(data){
var infos = request.responseJSON.infos;
var status = request.responseJSON.status;
if($("#info_texts").length == 0)
return infos.join("<br>")
else{
if(status == "")
var status_text = "not yet create";
else
var status_text = status_relation[status];
$("#info_texts").html(status_text+"<div style='clear:both;'></div>"+infos.join("<br>"));
msg_end.scrollIntoView();
timeout_id = window.setTimeout(see_infos(),1000);
}
})
}else{
window.clearTimeout(timeout_id);
}
};
function show_infos_dialog(key=""){
close_info = true;
window.clearTimeout(timeout_id);
close_info = false;
if($("#dialog-confirm").length == 0){
$("#main-wrap").before("<div id='dialog-confirm' title='site infos'>"+
"<div style='clear:both;'></div><div id='info_texts'>"+see_infos(key)+"</div><div id='msg_end' style='height:0px; overflow:hidden'></div>"+
"</div>");
}else{
see_infos(key);
};
$( "#dialog-confirm" ).dialog({
resizable: true,
minHeight: 100,
maxHeight: 400,
width: '80%',
modal: true,
close: function(){close_info = true;},
buttons: {
"<%= t('client_management.confirm') %>": function(){$( this ).dialog( "close" );close_info = true;},
"stop update": function(){close_info = true;}
}
});
}
$(document).ready(function(){
$("#detect_sites").off('click').on('click',function(){
$.post("<%=admin_site_panel_edit_server_info_path%>",{"type":"detect_sites"}).done(function(){
show_infos_dialog();
})
});
$("#exec_commands").off('click').on('click',function(){
//show_exec_commands_block(server_names)
$.post("<%=admin_site_panel_edit_server_info_path%>",{"type":"get_server_names"}).done(function(server_names){
var server_names = Array.isArray(server_names) ? server_names : [];
show_exec_commands_block(server_names);
})
});
$("#see_infos").off('click').on('click',function(){
$.post("<%=admin_site_panel_edit_server_info_path%>",{"type":"see_infos"}).done(function(){
show_infos_dialog();
})
});
})
</script>

View File

@ -0,0 +1,192 @@
<div id="filter" class="topnav clearfix open">
<ul class="breadcrumb text-info pull-left">
<li><a href="/zh_tw/admin/dashboards">網站內容</a><span class="divider">/</span></li><li><a href="/admin/site_panel"><span class="translation_missing" title="translation missing: zh_tw.module_name.client_management">Client Management</span></a><span class="divider">/</span></li><li class="active"><span class="translation_missing" title="translation missing: zh_tw.restful_actions.server_manager">Server Manager</span></li>
</ul>
<div class="searchClear pull-left" style="clear: left;">
<form id="module-search-form">
<div class="sc-field"><i class="icon-search"></i><i class="icons-cross-3 search-clear" style="display: none;"></i><input type="text" id="filter-input" class="search-query input-medium" placeholder="搜尋" value=""></div>
</form>
<img id="search-preloader" src="/assets/preloader.gif" style="height: 40px; position: absolute; left: 220px; top: -5px; display:none;">
</div>
<ul class="nav nav-pills filter-nav pull-right">
<li class="accordion-group">
<div class="accordion-heading">
<a href="#collapse-category" data-toggle="collapse" data-parent="#filter" class="accordion-toggle"><%=t(:category)%></a>
</div>
</li>
<li class="accordion-group">
<div class="accordion-heading">
<a href="#collapse-tags" data-toggle="collapse" data-parent="#filter" class="accordion-toggle"><%=t(:tags)%></a>
</div>
</li>
</ul>
<div class="filter-group accordion-group">
<div class="accordion-body in collapse" id="collapse-category" style="height: auto;">
<div class="accordion-inner pagination-right">
<% @categories.each do |category|%>
<a class="btn btn-small " href="#" id="filter_<%=category.id%>" onclick="filter.addFilter('filters[category][]=<%=category.id%>');$(this).toggleClass('active');return false;"><%=category.title%></a>
<% end %>
</div>
<div class="filter-clear">
<a href="#" onclick="filter.clearFilter();" class="btn btn-link btn-small"><i class="icons-cycle"></i> 清除</a>
</div>
</div>
<div class="accordion-body collapse" id="collapse-tags">
<div class="accordion-inner pagination-right">
</div>
<div class="filter-clear">
<a href="#" onclick="filter.clearFilter();" class="btn btn-link btn-small"><i class="icons-cycle"></i> 清除</a>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var interval = 0;
var keyword;
var searchLock=false;
$("#filter-input").bind("input", function() {queueSearch();});
$("#module-search-form").submit(function(){queueSearch(); return false; });
var params = function(key){
var result = {};
var datas = window.location.search ? window.location.search.replace('?','').split('&') : []
datas.map(function(data){
tmp = data.split("=");
tmp[1] = decodeURIComponent(tmp[1]);
if(key){
if(tmp[0]==key){
if(tmp[0].indexOf('[]')>=0){
if(!Array.isArray(result)) result = [];
result.push(tmp[1]);
}else{
result = tmp[1];
}
}
}else{
if(tmp[0].indexOf('[]')>=0){
if(typeof(result[tmp[0]]) == "undefined") result[tmp[0]] = [];
result[tmp[0]].push(tmp[1]);
}else{
result[tmp[0]] = tmp[1];
}
}
});
return result;
}
var queueSearch = function(){
if($("#filter-input").val()!=keyword){
keyword = $("#filter-input").val();
interval=500;
if(!searchLock) moduleSearch();
}
}
var moduleSearch = function(){
$("#search-preloader").fadeIn();
searchLock = true;
if(interval==0){
url = document.URL;
url = url.replace('#','');
if(url.indexOf("keywords=")>=0){
url = url.replace("keywords="+encodeURIComponent(params("keywords")),"keywords="+keyword)
}else{
url = (url.indexOf("?")>=0) ? url+"&keywords="+keyword : url+"?keywords="+keyword
}
url = url.replace("page="+params("page"),"page=1");
history.pushState(null, null, url);
$.get(url,function(data){
searchLock = false;
$("#index_table").html(data);
bindPagination();
$("#search-preloader").fadeOut();
});
}else{
interval -= 100;
setTimeout(moduleSearch,100);
}
}
var bindPagination = function(){
$(".pagination a").click(function(){
filter.updateTable($(this).attr('href'),false,true);
return false;
});
}
var Filter = function(dom){
var self = this;
var makeFilters = function(){
return (window.location.search ? window.location.search.replace('?','').split('&') : []);
}
var filters = makeFilters(),
dom = $(dom),
mainUrl = window.location.pathname;
this.updateTable = function(url, goback, is_pagination){
update = true;
xurl = (url == null ? ( filters.length ? mainUrl + "?" + filters.join('&') : mainUrl ) : url);
$.ajax({
url : xurl,
type : "get",
dataType : "html"
}).done(function(data){
if(is_pagination){
history.pushState(null, null, decodeURIComponent(xurl));
}else{
if(!goback) history.replaceState(null, null, decodeURIComponent(xurl));
}
filters = makeFilters();
dom.html(data);
bindPagination();
update = false;
})
}
this.addFilter = function(filter){
filters = makeFilters();
$.each(filters,function(idx,data){
if(typeof(data)=="undefined") return true;
if(data.indexOf("page=")>-1) filters.splice(idx,1);
});
if( (index = filters.indexOf(filter) ) > -1){
mainUrl = mainUrl.replace(filter,'');
filters.splice(index,1);
}else{
filters.push(filter);
}
self.updateTable();
return false;
};
this.clearFilter = function(){
$(".filter-group a.active").removeClass("active");
filters = [];
self.updateTable();
return false;
}
window.onpopstate = function(event){
if(!update){
$("#filter-input").val( $.isEmptyObject(params('keywords')) ? "" : params('keywords') );
self.updateTable(document.location, true);
$(".filter-group .btn-small").removeClass('active');
$.each(document.location.search.split('&'),function(key,filter){
if(filter.split('=')[0].indexOf("keywords")>=0) return true;
$('#filter_'+filter.split('=')[1]).addClass('active');
});
}
update = false;
}
}
var update = false;
var filter;
$(document).ready(function(){
filter = new Filter("#index_table");
bindPagination();
});
</script>

View File

@ -0,0 +1,204 @@
<table class="table main-list default footable-loaded">
<thead>
<tr>
<th>Server name</th>
<th>Domain name</th>
<th>Port</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<% status_relation = {"creating"=>"<span style=\"color: skyblue;\">creating</span>".html_safe,"error"=>"<span style=\"color: red;\">error</span>".html_safe,"finish"=>"<span style=\"color: darkseagreen;\">finish</span>".html_safe,"closed"=>"<span style=\"color: red;\">closed</span>".html_safe}%>
<% @sites.each do |site|%>
<tr>
<td><%=site.server_type%></td>
<td><a href="<%=((site.port.to_s == "443") ? "https" : "http")%>://<%=site.domain_name%><%=((site.port.to_s != '80' && site.port.to_s != '443') ? ':'+site.port.to_s : '')%>" title="open <%=((site.port.to_s == "443") ? "https" : "http")%>://<%=site.domain_name%><%=((site.port.to_s != '80' && site.port.to_s != '443') ? ':'+site.port.to_s : '')%> to new window" target="_blank"><%=site.domain_name%></a></td>
<td><%=site.port%></td>
<td class="site_status"><%= (site.status.blank? ? "not yet create" : status_relation[site.status]) %></td>
<td>
<a href="#" title="see infos for <%=site.domain_name%>" class="btn btn-primary see_infos" data-id="<%=site.id.to_s%>">See infos</a>
<% if site.status.blank? %>
<a href="#" title="create <%=site.domain_name%>" class="btn btn-primary create_site" data-id="<%=site.id.to_s%>">Create site</a>
<% elsif site.status == "error" || site.status == "finish" %>
<a href="#" title="recreate <%=site.domain_name%>" class="btn btn-primary create_site" data-id="<%=site.id.to_s%>">Recreate site</a>
<a href="#" title="Delete <%=site.domain_name%>" class="btn btn-primary delete_site" data-id="<%=site.id.to_s%>">Delete site</a>
<a href="#" title="Close <%=site.domain_name%>" class="btn btn-primary close_site" data-id="<%=site.id.to_s%>">Close site</a>
<a href="#" title="Restart <%=site.domain_name%>" class="btn btn-primary open_site" data-id="<%=site.id.to_s%>">Restart site</a>
<% elsif site.status == "closed" %>
<a href="#" title="Start <%=site.domain_name%>" class="btn btn-primary open_site" data-id="<%=site.id.to_s%>">Start site</a>
<% end%>
<a href="#" title="Exec commands on <%=site.domain_name%>" class="btn btn-primary exec_commands" data-id="<%=site.id.to_s%>">Exec commands</a>
<a href="<%=admin_site_panel_edit_site_path+"?id=#{site.id}&type=detail"%>" title="See <%=site.domain_name%> detail" class="btn btn-primary see_detail" data-id="<%=site.id.to_s%>">Detail</a>
<a href="<%=admin_site_panel_edit_site_path+"?id=#{site.id}&type=delete_from_list"%>" title="Delete <%=site.domain_name%> from list" class="btn btn-primary see_detail" data-id="<%=site.id.to_s%>">Delete from list</a>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="bottomnav clearfix">
<div class="pagination pagination-centered"><%=paginate(@sites)%></div>
<a class="btn btn-primary pull-right" href="<%=admin_site_panel_path%>" title="Create new web"><%=t(:add)%></a>
</div>
<script type="text/javascript">
$(document).ready(function(){
var close_info = false;
var timeout_id;
var status_relation = {"creating":"<span style=\"color: skyblue;\">creating</span>","error":"<span style=\"color: red;\">error</span>","finish": "<span style=\"color: darkseagreen;\">finish</span>","closed":"<span style=\"color: red;\">closed</span>"};
function see_infos(id){
if(!close_info){
var request = $.post("<%=admin_site_panel_site_infos_path%>",{"id":id});
request.done(function(data){
var infos = request.responseJSON.infos;
var status = request.responseJSON.status;
if($("#info_texts").length == 0)
return infos.join("<br>")
else{
$("#info_texts").html(infos.join("<br>"));
if(status == "")
var status_text = "not yet create";
else
var status_text = status_relation[status];
if(status == "finish"){
var params = {}
var params_text = window.location.search.split(/[?&]/).slice(1);
params_text.forEach(function(text){
if(text.split("=")[0] != "")
params[text.split("=")[0]] = text.split("=")[1].split("#")[0].split("&")[0];
});
var new_params_text = "?";
Object.keys(params).forEach(function(key){
var value = params[key];
if(key == "id"){
if(value != id){
new_params_text += (key+"="+value+"&")
}
}else
new_params_text += (key+"="+value+"&")
});
if(new_params_text[new_params_text.length -1] == "&")
new_params_text = new_params_text.substr(0,new_params_text.length - 1)
if(params["id"] == id){
close_info = true;
window.location.href = window.location.href.replace(window.location.search,new_params_text);
}
}
$(".see_infos[data-id="+id+"]").parent().siblings(".site_status").html(status_text);
//msg_end.scrollIntoView();
timeout_id = window.setTimeout(see_infos(id),1000);
}
})
}else{
window.clearTimeout(timeout_id);
}
};
function show_infos_dialog(item){
close_info = true;
window.clearTimeout(timeout_id);
close_info = false;
var id = $(item).attr("data-id");
if($("#dialog-confirm").length == 0){
$("#main-wrap").before("<div id='dialog-confirm' title='site infos'>"+
"<div style='clear:both;'></div><div id='info_texts'>"+see_infos(id)+"</div><div id='msg_end' style='height:0px; overflow:hidden'></div>"+
"</div>");
}else{
see_infos(id);
};
$( "#dialog-confirm" ).dialog({
resizable: true,
minHeight: 300,
maxHeight: 400,
modal: true,
width: '80%',
close: function(){$( this ).dialog( "close" );close_info = true;},
buttons: {
"<%= t('client_management.confirm') %>": function(){$( this ).dialog( "close" );close_info = true;},
"stop update": function(){close_info = true;}
}
});
}
$('.see_infos').click(function(){show_infos_dialog(this);});
$('.create_site').click(function(){
close_info = true;
window.clearTimeout(timeout_id);
close_info = false;
var id = $(this).attr("data-id");
var item = this;
$.post("<%= admin_site_panel_create_site_path %>",{'id': id}).done(function(){
show_infos_dialog(item);
})
});
$('.delete_site').click(function(){
var item = this;
if(window.confirm("Do you really want to delete this site?")){
if(window.confirm("Do you really want to delete this site?")){
$.post("<%=admin_site_panel_edit_site_path%>",{'id':$(this).attr("data-id"),'type':'delete'}).done(function(){
show_infos_dialog(item);
item.remove();
});
}
}
});
$('.close_site').click(function(){
var item = this;
$.post("<%=admin_site_panel_edit_site_path%>",{'id':$(this).attr("data-id"),'type':'close'}).done(function(){
show_infos_dialog(item);
item.remove();
});
});
$('.open_site').click(function(){
var item = this;
$.post("<%=admin_site_panel_edit_site_path%>",{'id':$(this).attr("data-id"),'type':'open'}).done(function(){
show_infos_dialog(item);
item.remove();
});
});
$('.exec_commands').click(function(){
show_exec_commands_block($(this).attr('data-id'));
});
function show_exec_commands_block(id){
if($("#exec-commands-dialog-confirm").length == 0){
$("#main-wrap").before("<div id='exec-commands-dialog-confirm' title='Commands to Exec'>"+
"<div style='clear:both;'></div><textarea style='height: 200px;width: 100%;' id='commands_area' data-id='"+id+"'></textarea>"+
"</div>");
}else{
$('#commands_area').attr('data-id',id);
};
$( "#exec-commands-dialog-confirm" ).dialog({
resizable: true,
minHeight: 300,
maxHeight: 400,
modal: true,
width: '80%',
close: function(){$( this ).dialog( "close" );},
buttons: {
"<%= t(:submit) %>": function(){$( this ).dialog( "close" );exec_commands()},
}
});
}
function exec_commands(){
var commands = $('#commands_area').val().replace("\r\n","////");
var id = $('#commands_area').attr('data-id');
console.log(id);
console.log(commands);
$.post("<%=admin_site_panel_edit_site_path%>",{'id':id,'commands':commands}).done(function(){
show_infos_dialog($('.see_infos[data-id="'+id+'"]').eq(0));
});
}
<% if !params[:id].blank? %>
see_infos("<%=params[:id]%>");
$( "#dialog-confirm" ).dialog({
resizable: true,
minHeight: 300,
maxHeight: 400,
width: '80%',
modal: true,
close: function(){$( this ).dialog( "close" );close_info = true;},
buttons: {
"<%= t('client_management.confirm') %>": function(){$( this ).dialog( "close" );close_info = true;},
"stop update": function(){close_info = true;}
}
});
<% end %>
})
</script>

View File

@ -0,0 +1,45 @@
<div class="form-horizontal">
<div class="control-group">
<label class="control-label">Server</label>
<div class="controls"><%=@site.server_type%></div>
</div>
<div class="control-group">
<label class="control-label">Domain name</label>
<div class="controls"><%=@site.domain_name%></div>
</div>
<div class="control-group">
<label class="control-label">Port</label>
<div class="controls"><%=@site.port%></div>
</div>
<div class="control-group">
<label class="control-label">Site type</label>
<div class="controls"><%=@site.site_type%></div>
</div>
<% if @site.site_type == "School"%>
<div class="control-group">
<label class="control-label">School name</label>
<div class="controls"><%=@site.school_name%></div>
</div>
<% end %>
<div class="control-group">
<label class="control-label">Site name</label>
<div class="controls"><%=@site.site_name%></div>
</div>
<div class="control-group">
<label class="control-label">Database name</label>
<div class="controls"><%=@site.db_name%></div>
</div>
<div class="control-group">
<label class="control-label">Nginx file</label>
<div class="controls"><%=@site.nginx_file%></div>
</div>
<div class="control-group">
<label class="control-label">Site path</label>
<div class="controls"><%=@site.path%></div>
</div>
<div class="control-group">
<div class="controls">
<a href="<%=admin_site_panel_sites_list_path%>" class="btn btn-primary"><%=t(:back)%></a>
</div>
</div>
</div>

View File

@ -1,3 +1,5 @@
<%= javascript_include_tag "lib/jquery-ui-1.12.1/jquery-ui" %>
<%= stylesheet_link_tag "lib/jquery-ui-1.12.1/jquery-ui" %>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "lib/module-area" %>
<% end %>

View File

@ -1,145 +1,20 @@
<%= javascript_include_tag "lib/jquery-ui-1.12.1/jquery-ui" %>
<%= stylesheet_link_tag "lib/jquery-ui-1.12.1/jquery-ui" %>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "lib/module-area" %>
<% end %>
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "lib/main-forms" %>
<%= stylesheet_link_tag "lib/main-list" %>
<%= stylesheet_link_tag "lib/pageslide" %>
<% end %>
<% if !params[:id].blank? %>
<div id='dialog-confirm' title='site infos'>
<div style='clear:both;'></div>
<div id='info_texts'></div>
</div>
<% end %>
<table class="table main-list default footable-loaded">
<thead>
<tr>
<th>Server name</th>
<th>Domain name</th>
<th>Port</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<% status_relation = {"creating"=>"<span style=\"color: skyblue;\">creating</span>".html_safe,"error"=>"<span style=\"color: red;\">error</span>".html_safe,"finish"=>"<span style=\"color: darkseagreen;\">finish</span>".html_safe,"closed"=>"<span style=\"color: red;\">closed</span>".html_safe}%>
<% @sites.each do |site|%>
<tr>
<td><%=site.server_type%></td>
<td><a href="http://<%=site.domain_name%><%=(site.port.to_s != '80' ? ':'+site.port.to_s : '')%>" title="open http://<%=site.domain_name%> to new window" target="_blank"><%=site.domain_name%></a></td>
<td><%=site.port%></td>
<td class="site_status"><%= (site.status.blank? ? "not yet create" : status_relation[site.status]) %></td>
<td>
<a href="#" title="see infos for <%=site.domain_name%>" class="btn btn-primary see_infos" data-id="<%=site.id.to_s%>">See infos</a>
<% if site.status.blank? %>
<a href="#" title="create <%=site.domain_name%>" class="btn btn-primary create_site" data-id="<%=site.id.to_s%>">Create site</a>
<% elsif site.status == "error" || site.status == "finish" %>
<a href="#" title="recreate <%=site.domain_name%>" class="btn btn-primary create_site" data-id="<%=site.id.to_s%>">Recreate site</a>
<a href="#" title="Delete <%=site.domain_name%>" class="btn btn-primary delete_site" data-id="<%=site.id.to_s%>">Delete site</a>
<a href="#" title="Close <%=site.domain_name%>" class="btn btn-primary close_site" data-id="<%=site.id.to_s%>">Close site</a>
<% elsif site.status == "closed" %>
<a href="#" title="Start <%=site.domain_name%>" class="btn btn-primary open_site" data-id="<%=site.id.to_s%>">Start site</a>
<% end%>
</td>
</tr>
<% end %>
</tbody>
</table>
<div class="bottomnav clearfix">
<div class="pagination pagination-centered"><%=paginate(@sites)%></div>
<a class="btn btn-primary pull-right" href="<%=admin_site_panel_path%>" title="Create new web"><%=t(:add)%></a>
</div>
<script type="text/javascript">
$(document).ready(function(){
var close_info = false;
var timeout_id;
var status_relation = {"creating":"<span style=\"color: skyblue;\">creating</span>","error":"<span style=\"color: red;\">error</span>","finish": "<span style=\"color: darkseagreen;\">finish</span>","closed":"<span style=\"color: red;\">closed</span>"};
function see_infos(id){
if(!close_info){
var request = $.post("<%=admin_site_panel_site_infos_path%>",{"id":id});
request.done(function(data){
var infos = request.responseJSON.infos;
var status = request.responseJSON.status;
if($("#info_texts").length == 0)
return infos.join("<br>")
else{
$("#info_texts").html(infos.join("<br>"));
if(status == "")
var status_text = "not yet create";
else
var status_text = status_relation[status];
$(".see_infos[data-id="+id+"]").parent().siblings(".site_status").html(status_text);
timeout_id = window.setTimeout(see_infos(id),1000);
}
})
}else{
window.clearTimeout(timeout_id);
}
};
function show_infos_dialog(item){
close_info = true;
window.clearTimeout(timeout_id);
close_info = false;
var id = $(item).attr("data-id");
if($("#dialog-confirm").length == 0){
$("#main-wrap").before("<div id='dialog-confirm' title='site infos'>"+
"<div style='clear:both;'></div><div id='info_texts'>"+see_infos(id)+"</div>"+
"</div>");
}else{
see_infos(id);
};
$( "#dialog-confirm" ).dialog({
resizable: true,
minHeight: 100,
maxHeight: 400,
modal: true,
buttons: {
"<%= t('client_management.confirm') %>": function(){$( this ).dialog( "close" );close_info = true;}
}
});
}
$('.see_infos').click(function(){show_infos_dialog(this);});
$('.create_site').click(function(){
close_info = true;
window.clearTimeout(timeout_id);
close_info = false;
var id = $(this).attr("data-id");
var item = this;
$.post("<%= admin_site_panel_create_site_path %>",{'id': id}).done(function(){
show_infos_dialog(item);
})
});
$('.delete_site').click(function(){
var item = this;
if(window.confirm("Do you really want to delete this site?")){
if(window.confirm("Do you really want to delete this site?")){
$.post("<%=admin_site_panel_edit_site_path%>",{'id':$(this).attr("data-id"),'type':'delete'}).done(function(){
show_infos_dialog(item);
item.remove();
});
}
}
});
$('.close_site').click(function(){
var item = this;
$.post("<%=admin_site_panel_edit_site_path%>",{'id':$(this).attr("data-id"),'type':'close'}).done(function(){
show_infos_dialog(item);
item.remove();
});
});
$('.open_site').click(function(){
var item = this;
$.post("<%=admin_site_panel_edit_site_path%>",{'id':$(this).attr("data-id"),'type':'open'}).done(function(){
show_infos_dialog(item);
item.remove();
});
});
<% if !params[:id].blank? %>
see_infos("<%=params[:id]%>");
$( "#dialog-confirm" ).dialog({
resizable: true,
minHeight: 100,
maxHeight: 400,
modal: true,
buttons: {
"<%= t('client_management.confirm') %>": function(){$( this ).dialog( "close" );close_info = true;}
}
});
<% end %>
})
</script>
<%= render :partial => "sites_list_filter" %>
<span id="index_table">
<%=render :partial=>'sites_list_table',:locals=>{:@sites=>@sites}%>
</span>

View File

@ -1,5 +1,6 @@
en:
client_management:
see_sites: See sites on the server
client_management: Client Management
my_sites: My Sites
help_and_faq: Help and FAQ

View File

@ -1,5 +1,6 @@
zh_tw:
client_management:
see_sites: 查看主機上的網站
client_management: Client Management
my_sites: My Sites
help_and_faq: Help and FAQ

View File

@ -36,10 +36,13 @@ Rails.application.routes.draw do
get "contracts"
end
end
get "site_panel/edit_site" => "site_panel#edit_site"
post "site_panel/edit_site" => "site_panel#edit_site"
get "site_panel/site_infos" => "site_panel#site_infos"
post "site_panel/site_infos" => "site_panel#site_infos"
post "site_panel/create_site" => "site_panel#create_site"
post "site_panel/create" => "site_panel#create"
patch "site_panel/create" => "site_panel#create"
get "site_panel" => "site_panel#index"
get "site_panel/sites_list" => "site_panel#sites_list"
get "site_panel/server_manager" => "site_panel#server_manager"

View File

@ -22,6 +22,14 @@ namespace :create_site do
begin
@site_construct.update(:status=>"creating")
@site_construct.update!(:infos=>[])
begin
Net::SSH.start(args.ip , args.user , password: args.password) do |ssh|
end
rescue Net::SSH::HostKeyMismatch
system("ssh-keygen -f \"$HOME/.ssh/known_hosts\" -R #{args.ip}")
rescue Errno::ENOTTY
system("ssh-add \"$HOME/.ssh/id_rsa\"")
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'+
@ -43,23 +51,23 @@ namespace :create_site do
'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}\"")
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}\"")
update_infos("restarting nginx")
exec_ssh_command_by_sudo(ssh,"service nginx restart")
exec_ssh_command_by_sudo_for_create(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}")
exec_ssh_command_by_sudo_for_create(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")
exec_ssh_command_by_sudo_for_create(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_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(ssh,"echo '#{db_setting_text.gsub("orbit_4_5",args.db_name)}' > #{args.path}/#{args.site_name}/config/mongoid.yml")
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)
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
@ -74,12 +82,17 @@ namespace :create_site do
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'")
exec_ssh_command_by_sudo_for_create(ssh,"bash -l -c 'cd #{args.path}/#{args.site_name}\n#{bundle_dir} install'",true)
exec_ssh_command_by_sudo_for_create(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'")
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\n#{bundle_dir} 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")
end
update_infos("finish creating #{args.site_name}")
exec_ssh_command_by_sudo_for_create(ssh,"chmod 777 #{args.path}/#{args.site_name} -R")
@site_construct.update(:status =>"finish")
puts "finish creating #{args.site_name} on #{args.ip}"
end
@ -96,6 +109,57 @@ namespace :create_site do
end
return output
end
def update_thread_infos_for_exec(info,update_last=false)
if update_last
@thread.status["infos"][-1] += info
else
@thread.status["infos"] = @thread.status["infos"].push(info)
end
@thread.save!
return @thread.status["infos"]
end
def exec_ssh_command_for_create(session,command,update=false)
outputs = []
session.open_channel do |channel|
channel.request_pty do |channel, success|
channel.exec(command) do |ch, success|
abort "could not execute command: #{command}" unless success
channel.on_data do |ch, data|
outputs.push(data)
print "#{data}"
if update
if data.include? "\n"
update_infos_for_exec(data)
else
update_infos_for_exec(data,true)
end
end
if data.to_s.include?("sudo password:") || data.to_s.include?("Password:")
channel.send_data "#{@password}\n"
end
end
end
end
end
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)
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)
end
return outputs.join("\n")
end
def update_infos_for_exec(info,update_last=false)
if update_last && !@site_construct.infos.empty?
@site_construct.infos[-1] += info
else
@site_construct.infos = @site_construct.infos.push(info)
end
@site_construct.save!
return @site_construct.infos
end
def update_infos(info)
puts info
@site_construct.infos = @site_construct.infos.push(info)

43
lib/tasks/delete_dbs.rake Normal file
View File

@ -0,0 +1,43 @@
require 'net/ssh'
require 'pathname'
namespace :create_site do
desc "Delete databases Script"
task :delete_dbs,[:server_name,:dbs] => :environment do |task,args|
site_server = SiteServer.where(:server_name=>args.server_name).first
ip = site_server.ip
user = site_server.account
password = site_server.password
@password = password
dbs = args.dbs.split
begin
Net::SSH.start(ip , user , password: password) do |ssh|
end
rescue Net::SSH::HostKeyMismatch
system("ssh-keygen -f \"$HOME/.ssh/known_hosts\" -R #{ip}")
rescue Errno::ENOTTY
system("ssh-add \"$HOME/.ssh/id_rsa\"")
end
Net::SSH.start(ip , user , password: password) do |ssh|
dbs.each do |db|
puts "deleting database #{db}"
exec_ssh_command_by_sudo(ssh,"bash -l -c 'echo \"db.dropDatabase()\" | mongo --shell \'#{db}\''")
puts "finish deleting database #{db}"
end
end
puts "finished deleting databases rake"
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)
puts info
@site_construct.infos = @site_construct.infos.push(info)
@site_construct.save!
return @site_construct.infos
end
end

View File

@ -1,7 +1,7 @@
require 'net/ssh'
require 'pathname'
namespace :create_site do
desc "Create Site Script"
desc "Delete Site Script"
task :delete_site,[:site_construct_id] => :environment do |task,args|
@site_construct = SiteConstruct.find(args.site_construct_id)
site_server = SiteServer.where(:server_name=>@site_construct.server_type).first
@ -10,17 +10,25 @@ namespace :create_site do
password = site_server.password
@password = password
begin
begin
Net::SSH.start(ip , user , password: password) do |ssh|
end
rescue Net::SSH::HostKeyMismatch
system("ssh-keygen -f \"$HOME/.ssh/known_hosts\" -R #{ip}")
rescue Errno::ENOTTY
system("ssh-add \"$HOME/.ssh/id_rsa\"")
end
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
exec_ssh_command_by_sudo(ssh,"bash -l -c 'echo \'db.dropDatabase()\' | mongo --shell \'#{db_name}\''")
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}")
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 /etc/nginx/orbit_sites/#{@site_construct.site_name}")
exec_ssh_command_by_sudo(ssh,"rm -f #{@site_construct.nginx_file}")
update_infos("finish deleting nginx setting for #{@site_construct.site_name}")
update_infos("restarting nginx")
exec_ssh_command_by_sudo(ssh,"service nginx restart")

108
lib/tasks/detect_sites.rake Normal file
View File

@ -0,0 +1,108 @@
require 'net/ssh'
require 'pathname'
namespace :create_site do
desc "Detect sites"
task :detect_sites,[:detect_name] => :environment do |task,args|
#Multithread.where(:key=>'detect_sites').destroy
Multithread.where(:key=>'detect_sites').each{|thread| thread.destroy if (thread.status["status"] == "error" || thread.status["status"] == "finish")}
@thread = Multithread.where(:key=>'detect_sites').first
if @thread.nil?
begin
@thread = Multithread.create(:key=>'detect_sites',:status=>{"infos"=>[],"status"=>"detecting"})
if( args.detect_name.nil? rescue true)
site_servers = SiteServer.all.to_a
else
site_servers = SiteServer.where(:server_name=>args.detect_name).to_a
end
site_servers.each do |site_server|
@site_server = site_server
update_thread_infos("<span style='color: skyblue;'>"+@site_server.server_name+"</span>")
@password = @site_server.password
begin
begin
Net::SSH.start(@site_server.ip , @site_server.account , password: @site_server.password) do |ssh|
end
rescue Net::SSH::HostKeyMismatch
system("ssh-keygen -f \"$HOME/.ssh/known_hosts\" -R #{@site_server.ip}")
rescue Errno::ENOTTY
system("ssh-add \"$HOME/.ssh/id_rsa\"")
rescue => e
update_thread_infos(e.to_s)
next
end
Net::SSH.start(@site_server.ip , @site_server.account , password: @site_server.password) do |ssh|
nginx_include_dirs = exec_ssh_command_by_sudo(ssh,'echo `grep include /etc/nginx/nginx.conf | grep -v "\#\|include /etc/nginx/mime.types\|include /etc/nginx/conf.d/\*.conf\|/etc/nginx/sites-enabled/\*"`')
nginx_include_dirs = nginx_include_dirs.split("\n").map{|t| t.gsub("include",'').gsub(';','').strip}
domain_name = @site_server.domain_name#'serv.rulingcom.com'
nginx_include_dirs.each do |nginx_include_dir|
server_names = exec_ssh_command_by_sudo(ssh,"grep 'server_name' -r #{nginx_include_dir}")
server_names_array = server_names.split("\n")
server_names_array = server_names_array.select{|t| !t.include?('No such file or directory')}.flat_map{|t| t.strip.split('server_name').last.split(";").first.strip.split}
server_names_array.each do |server_name|
if !server_name.include?(domain_name)
next if !`nslookup "#{server_name}"`.include?(@site_server.ip)
end
nginx_file = exec_ssh_command_by_sudo(ssh,"grep '#{server_name}' -r #{nginx_include_dir}").split("\n").select{|s| !s.include?("No such file or directory")}.first.split('server_name').first.strip.split(":").first
site_path = Pathname.new(exec_ssh_command_by_sudo(ssh,"echo `grep root #{nginx_file} | grep -v -e '#.*root'`").to_s.split("\n").first.to_s.strip.split("root").last.to_s.gsub(";","").strip).dirname.to_s
if site_path.present? && exec_ssh_command_by_sudo(ssh,"ls #{site_path}").split.length != 0 && exec_ssh_command_by_sudo(ssh,"ls #{site_path}/Gemfile").include?("No such file or directory")
SiteConstruct.where(:server_type => @site_server.server_name , :domain_name=>server_name).destroy
next
end
path = Pathname.new(site_path).dirname.to_s
site_name = Pathname.new(site_path).basename.to_s
server_type = @site_server.server_name
port = exec_ssh_command_by_sudo(ssh,"echo `grep 'listen' #{nginx_file} | grep -v -e '#.*listen'`").split("\n").first.strip.split("listen").last.strip.split(";").first.split.select{|p| p.strip == p.strip.to_i.to_s}.first.strip rescue "80"
db_name = exec_ssh_command_by_sudo(ssh,"echo `cat #{site_path}/config/mongoid.yml | grep 'database'`").split("database:").last.strip
db_name = site_name.gsub("-","_") if db_name.include?("No such file or directory")
unicorn_sock_path = exec_ssh_command_by_sudo(ssh,"echo `grep 'server unix' #{nginx_file} | grep -v -e '#.*server unix'`").strip.split("server unix").last.strip.split(":").last.strip rescue (site_path+"/tmp/unicorn.sock")
if Pathname.new(path).basename.to_s == "orbit_sites" || Pathname.new(path).basename.to_s == "housing_sites" || Pathname.new(path).basename.to_s == "dev_sites" || Pathname.new(path).dirname.to_s == "/home"
school_name = nil
site_type = "Gravity"
else
school_name = Pathname.new(path).basename.to_s
site_type = "School"
end
pid_infos = exec_ssh_command_by_sudo(ssh,"fuser #{unicorn_sock_path}").to_s.strip.gsub("\n","")
if pid_infos.length != 0 && !pid_infos.include?("not exist") && !pid_infos.include?("No such file or directory")
status = "finish"
else
bundle_show_info = exec_ssh_command_by_sudo(ssh,"bash -l -c 'cd #{site_path};bundle show'")
if bundle_show_info.include?("is not yet checked out") || bundle_show_info.include?("No such file or directory")
status = ""
else
status = "closed"
end
end
site_construct = SiteConstruct.where(:server_type => server_type , :domain_name=>server_name).first
update_thread_infos("Detect <a href='#{((port == "443") ? "https" : "http")}://#{server_name}#{((port=="80" || port=="443") ? "" : (':'+port))}'>#{server_name}</a>".html_safe)
if site_construct.nil?
site_construct = SiteConstruct.create(:server_type=>server_type,:site_name=>site_name,:domain_name=>server_name,:nginx_file=>nginx_file,:db_name=>db_name,:port=>port,:path=>path,:site_type=>site_type,:school_name=>school_name,:user_id=>User.first.id,:status=>status)
else
site_construct.update(:server_type=>server_type,:site_name=>site_name,:domain_name=>server_name,:nginx_file=>nginx_file,:db_name=>db_name,:port=>port,:path=>path,:site_type=>site_type,:school_name=>school_name,:user_id=>User.first.id,:status=>status)
end
end
end
end
end
end
@thread.update(:status=>@thread.status.merge({"status"=>"finish"}))
rescue => e
@thread.update(:status=>{"infos"=>@thread.status["infos"].push(e.to_s),"status"=>"error"})
end
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_thread_infos(info)
puts info
@thread.status["infos"] = @thread.status["infos"].push(info)
@thread.save!
return @thread.status["infos"]
end
end

View File

@ -1,43 +1,108 @@
require 'net/ssh'
require 'pathname'
namespace :create_site do
desc "Create Site Script"
task :exec_commands,[:site_construct_id,:commands,:type] => :environment do |task,args|
@site_construct = SiteConstruct.find(args.site_construct_id)
site_server = SiteServer.where(:server_name=>@site_construct.server_type).first
ip = site_server.ip
user = site_server.account
password = site_server.password
@password = password
#begin
Net::SSH.start(ip , user , password: password) do |ssh|
@site_construct.update!(:infos=>[])
if args.type == 'close_site'
exec_ssh_command_by_sudo(ssh,"bash -l -c 'kill -s TERM `fuser #{@site_construct.path}/#{@site_construct.site_name}/tmp/unicorn.sock`'")
update_infos("finish closing #{@site_construct.site_name}")
@site_construct.update(:status =>"closed")
elsif args.type == 'open_site'
exec_ssh_command_by_sudo(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};kill -s TERM `fuser tmp/unicorn.sock`;bundle exec unicorn_rails -c config/unicorn.rb -D -E development'")
update_infos("finish starting #{@site_construct.site_name}")
@site_construct.update(:status =>"finish")
else
commands = YAML.load(args.commands) rescue args.commands
if(!commands.blank? rescue true)
if commands.class == Array
commands.each do |command|
update_infos("execing #{command}")
exec_ssh_command_by_sudo(ssh,"bash -l -c '#{command}'")
namespace :exec_commands do
desc "Exec commands Script"
task :exec_commands,[:site_construct_id,:commands,:type,:server_name] => :environment do |task,args|
@type = args.type
if !args.site_construct_id.blank?
@site_construct = SiteConstruct.find(args.site_construct_id)
site_server = SiteServer.where(:server_name=>@site_construct.server_type).first
site_servers = [site_server]
else
@site_construct = nil
site_servers = SiteServer.where(:server_name.in=>args.server_name.split("////")).to_a
end
if args.type == "exec_all"
Multithread.where(:key=>'execing_commands').each{|thread| thread.destroy if (thread.status["status"] == "error" || thread.status["status"] == "finish")}
@thread = Multithread.where(:key=>'execing_commands').first
else
@thread = nil
end
if @thread.nil?
begin
if @thread.nil? && args.type == "exec_all"
@thread = Multithread.create(:key=>'execing_commands',:status=>{"infos"=>[],"status"=>"execing"})
end
site_servers.each do |site_server|
ip = site_server.ip
user = site_server.account
password = site_server.password
@password = password
begin
Net::SSH.start(ip , user , password: password) do |ssh|
end
rescue Net::SSH::HostKeyMismatch
system("ssh-keygen -f \"$HOME/.ssh/known_hosts\" -R #{ip}")
rescue Errno::ENOTTY
system("ssh-add \"$HOME/.ssh/id_rsa\"")
end
Net::SSH.start(ip , user , password: password) do |ssh|
@site_construct.update!(:infos=>[]) rescue nil
if args.type == 'close_site'
exec_ssh_command_by_sudo_and_see_output(ssh,"chmod 777 #{@site_construct.path}/#{@site_construct.site_name} -R",false)
exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'kill -s TERM `fuser #{@site_construct.path}/#{@site_construct.site_name}/tmp/unicorn.sock`'",false)
update_infos_for_exec("finish closing #{@site_construct.site_name}")
@site_construct.update(:status =>"closed")
elsif args.type == 'open_site'
default_rails_env = 'development'
enable_rails_env = ['development','production']
rails_env = default_rails_env
log_files = exec_ssh_command_by_sudo_and_see_output(ssh,"ls -t '#{@site_construct.path}/#{@site_construct.site_name}/log'",false).flat_map{|output| output.split(/(\r\n|\t|\n|\s+)/)}.select{|output| output.present? && /(\r\n|\t|\n|\s+)/.match(output).nil?} rescue []
log_files.each do |log_file|
if( enable_rails_env.include?(log_file.sub('.log','')) rescue false)
rails_env = log_file.sub('.log','')
break
end
end
update_infos_for_exec("starting site to #{rails_env}")
outputs = exec_ssh_command_by_sudo_and_see_output(ssh,"sudo -p 'sudo password:' chmod 777 #{@site_construct.path}/#{@site_construct.site_name} -R",false)
output = exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.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 #{rails_env}\n'",false)
if output.include? "bundle install"
update_infos_for_exec("not yet bundle install")
outputs = exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};bundle install'")
if outputs.join("\n").include? "Username for"
update_infos_for_exec("Cannot finish bundle install.")
update_infos_for_exec("This site include an private git project.")
@site_construct.update(:status =>"error")
break
end
end
update_infos_for_exec("finish starting #{@site_construct.site_name}")
@site_construct.update(:status =>"finish")
elsif args.type == 'exec_all'
sites = SiteConstruct.where(:server_type=>args.server_name).to_a
commands = args.commands.split("////").select{|c| c != ""} rescue [args.commands]
sites.each do |site_construct|
@site_construct = site_construct
@site_construct.update!(:infos=>[])
exec_ssh_command_by_sudo(ssh,"chmod 777 #{@site_construct.path}/#{@site_construct.site_name} -R")
commands.each do |command|
exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'")
end
end
else
update_infos("execing #{commands}")
exec_ssh_command_by_sudo(ssh,"bash -l -c '#{commands}'")
exec_ssh_command_by_sudo(ssh,"chmod 777 #{@site_construct.path}/#{@site_construct.site_name} -R")
commands = args.commands.split("////").select{|c| c != ""} rescue [args.commands]
commands.each do |command|
exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'")
end
end
end
end
if !@thread.nil?
@thread.update(:status=>@thread.status.merge({"status"=>"finish"}))
end
rescue => e
if !@thread.nil?
@thread.update(:status=>{"infos"=>@thread.status["infos"].push(e.message),"status"=>"error"})
@thread.update(:status=>{"infos"=>@thread.status["infos"].push(e.backtrace.join("\n")),"status"=>"error"})
end
if !@site_construct.nil?
@site_construct.update(:status =>"error",:infos=>@site_construct.infos.push("#{e.message}"))
@site_construct.update(:status =>"error",:infos=>@site_construct.infos.push("#{e.backtrace.join("\n")}"))
end
end
#rescue =>e
# @site_construct.update(:status =>"error",:infos=>@site_construct.infos.push("#{e}"))
# end
end
end
def exec_ssh_command_by_sudo(session,command)
output = session.exec!("echo '#{@password}' | sudo -S #{command}")
@ -47,10 +112,71 @@ namespace :create_site do
end
return output
end
def update_infos(info)
puts info
@site_construct.infos = @site_construct.infos.push(info)
def update_infos_for_exec(info,update_last=false)
if update_last && !@site_construct.infos.empty?
@site_construct.infos[-1] += info.to_s
else
@site_construct.infos = @site_construct.infos.push(info.to_s)
end
@site_construct.save!
return @site_construct.infos
end
def update_thread_infos_for_exec(info,update_last=false)
if update_last && !@thread.status["infos"].empty?
@thread.status["infos"][-1] += info.to_s
else
@thread.status["infos"] = @thread.status["infos"].push(info.to_s)
end
@thread.save!
return @thread.status["infos"]
end
def exec_ssh_command_and_see_output(session,command,update=true)
outputs = []
@flag = (@type == "exec_all")
if update
update_thread_infos_for_exec("execing #{command} on on <a href='#{((@site_construct.port == "443") ? "https" : "http")}://#{@site_construct.domain_name}#{((@site_construct.port=="80" || @site_construct.port=="443") ? "" : (':'+@site_construct.port))}'>#{@site_construct.domain_name}</a>") if @flag
update_infos_for_exec("execing #{command}")
end
session.open_channel do |channel|
channel.request_pty do |channel, success|
channel.exec(command) do |ch, success|
abort "could not execute command: #{command}" unless success
channel.on_data do |ch, data|
print "#{data}"
if data.include? "\n" || outputs.empty?
outputs.push(data.to_s)
if update
update_thread_infos_for_exec(data) if @flag
update_infos_for_exec(data)
end
else
outputs[-1] += data.to_s rescue ""
if update
update_thread_infos_for_exec(data,true) if @flag
update_infos_for_exec(data,true)
end
end
if data.to_s.include?("sudo password:") || data.to_s.include?("Password:")
channel.send_data "#{@password}\n"
end
end
channel.on_close do |ch|
if update
update_thread_infos_for_exec("finish execing #{command} on <a href='#{((@site_construct.port == "443") ? "https" : "http")}://#{@site_construct.domain_name}#{((@site_construct.port=="80" || @site_construct.port=="443") ? "" : (':'+@site_construct.port))}'>#{@site_construct.domain_name}</a>") if @flag
update_infos_for_exec("finish execing #{command}")
end
end
end
end
end
session.loop
return outputs
end
def exec_ssh_command_by_sudo_and_see_output(session,command,update=true)
outputs = exec_ssh_command_and_see_output(session,command,update)
if outputs.join("\n").include?("Permission denied") || outputs.join("\n").include?("Operation not permitted")
outputs = exec_ssh_command_and_see_output(session,"sudo -p 'sudo password:' #{command}",update)
end
return outputs
end
end

47
lib/tasks/test_ssh.rake Normal file
View File

@ -0,0 +1,47 @@
require 'net/ssh'
require 'net/ssh/proxy/http'
require 'pathname'
namespace :create_site do
desc "Test ssh"
task :test_ssh,[] => :environment do |task,args|
@site_server = SiteServer.where(:server_name=>'dev').first
Net::SSH.start(@site_server.ip , @site_server.account , password: @site_server.password) do |ssh|
exec_ssh_command_by_sudo_and_see_output_for_test(ssh,"bash -l -c 'cd orbit_sites/design3;bundle update'")
end
end
def exec_ssh_command_and_see_output_for_test(session,command)
outputs = []
puts "execing #{command}"
session.open_channel do |channel|
channel.request_pty do |channel, success|
channel.exec(command) do |ch, success|
abort "could not execute command: #{command}" unless success
channel.on_data do |ch, data|
outputs.push(data)
print "#{data}"
if data.to_s.include? "sudo password:"
channel.send_data "#{@site_server.password}\n"
end
end
channel.on_close do |ch|
puts "finish execing #{command}"
end
end
end
end
session.loop
return outputs
end
def exec_ssh_command_by_sudo_and_see_output_for_test(session,command)
outputs = exec_ssh_command_and_see_output_for_test(session,command)
if outputs.join("\n").include?("Permission denied")
exec_ssh_command_and_see_output_for_test(session,"sudo -p 'sudo password:' #{command}")
end
end
def update_thread_infos(info)
puts info
@thread.status["infos"] = @thread.status["infos"].push(info)
@thread.save!
return @thread.status["infos"]
end
end

48
lib/tasks/test_vpn.rake Normal file
View File

@ -0,0 +1,48 @@
require 'net/ssh'
require 'net/ssh/proxy/http'
require 'pathname'
namespace :create_site do
desc "Detect sites"
task :test_vpn,[:ip,:port,:username,:password] => :environment do |task,args|
# Multithread.where(:key=>'detect_sites').each{|thread| thread.destroy if (thread.status["status"] == "error" || thread.status["status"] == "finish")}
# @thread = Multithread.where(:key=>'detect_sites').first
#if @thread.nil?
# begin
# @thread = Multithread.create(:key=>'detect_sites',:status=>{"infos"=>[],"status"=>"detecting"})
# SiteServer.all.to_a.each do |site_server|
# @site_server = site_server
# update_thread_infos("<span style='color: skyblue;'>"+@site_server.server_name+"</span>")
# @password = @site_server.password
#begin
@site_server = SiteServer.where(:server_name=>"rulingcom.com").first
proxy = Net::SSH::Proxy::HTTP.new(args.ip, args.port.to_i,
:user => args.username, :password => args.password)
Net::SSH.start(@site_server.ip , @site_server.account , password: @site_server.password, proxy: proxy) do |ssh|
exec_ssh_command_by_sudo("echo 'abc'")
end
#rescue Errno::ETIMEDOUT , Net::SSH::ConnectionTimeout => e
# update_thread_infos(e.to_s)
#end
# end
#@thread.update(:status=>@thread.status.merge({"status"=>"finish"}))
#rescue => e
# puts e
# @thread.update(:status=>{"infos"=>@thread.status["infos"].push(e.to_s),"status"=>"error"})
#end
#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_thread_infos(info)
puts info
@thread.status["infos"] = @thread.status["infos"].push(info)
@thread.save!
return @thread.status["infos"]
end
end