Add delete site and close site and open site feature.

Add exec command feature
This commit is contained in:
BoHung Chiu 2020-04-28 21:26:49 +08:00
parent 3d8167d5e4
commit ce83a61b25
7 changed files with 180 additions and 32 deletions

View File

@ -79,13 +79,32 @@ class Admin::SitePanelController < OrbitAdminController
path = site_construct.path
site_construct_id = params[:id]
Thread.new do
puts site_construct_id
system("rake create_site:create_site['#{git_user}','#{git_password}','#{git_url}','#{ip}','#{user}','#{password}','#{site_name}','#{domain_name}','#{port}','#{db_name}','#{path}','#{site_construct_id}']")
end
render :json =>{"success"=>true}
end
def edit_site
if params[:type] == 'delete'
Thread.new do
system("rake create_site:delete_site[#{params[:id]}]")
end
elsif params[:type] == 'close'
Thread.new do
system("rake create_site:exec_commands[#{params[:id]},,close_site]")
end
elsif params[:type] == 'open'
Thread.new do
system("rake create_site:exec_commands[#{params[:id]},,open_site]")
end
else
Thread.new do
system("rake create_site:exec_commands[#{params[:id]},'#{params[:commands]}',open_site]")
end
end
render :json =>{"success"=>true}
end
def sites_list
@sites = SiteConstruct.all.desc(:id)
@sites = SiteConstruct.all.desc(:id).page(params[:page].to_i).per(10)
end
def site_infos
site_construct = SiteConstruct.where(:id=>params[:id]).first

View File

@ -17,4 +17,14 @@ class SiteConstruct
field :constructed, type: Boolean, :default => false
field :status, type: String, :default => ""
field :infos, type: Array, :default => []
after_initialize do |record|
if record.status.nil?
record.status = ""
record.save
end
if record.infos.nil?
record.infos = []
record.save
end
end
end

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" %>
<% if !params[:id].blank? %>
<div id='dialog-confirm' title='site infos'>
<div style='clear:both;'></div>
@ -15,11 +17,11 @@
</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}%>
<% 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%>" title="open http://<%=site.domain_name%> to new window" target="_blank"><%=site.domain_name%></a></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>
@ -28,6 +30,10 @@
<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>
@ -35,13 +41,14 @@
</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>"};
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});
@ -57,25 +64,18 @@
else
var status_text = status_relation[status];
$(".see_infos[data-id="+id+"]").parent().siblings(".site_status").html(status_text);
if(status == "creating" || status == "error" ){
console.log(request.responseJSON);
timeout_id = window.setTimeout(see_infos(id),1000);
}else{
close_info = true;
window.clearTimeout(timeout_id);
console.log(request.responseJSON);
}
timeout_id = window.setTimeout(see_infos(id),1000);
}
})
}else{
window.clearTimeout(timeout_id);
}
};
$('.see_infos').click(function(){
function show_infos_dialog(item){
close_info = true;
window.clearTimeout(timeout_id);
close_info = false;
var id = $(this).attr("data-id");
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>"+
@ -92,31 +92,43 @@
"<%= 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(){
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;}
}
});
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({

View File

@ -36,6 +36,7 @@ Rails.application.routes.draw do
get "contracts"
end
end
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"

View File

@ -21,6 +21,7 @@ namespace :create_site do
end
begin
@site_construct.update(:status=>"creating")
@site_construct.update!(:infos=>[])
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'+
@ -96,6 +97,7 @@ namespace :create_site do
return output
end
def update_infos(info)
puts info
@site_construct.infos = @site_construct.infos.push(info)
@site_construct.save!
return @site_construct.infos

View File

@ -0,0 +1,48 @@
require 'net/ssh'
require 'pathname'
namespace :create_site do
desc "Create 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
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=>[])
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}\''")
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}")
update_infos("finish deleting nginx setting for #{@site_construct.site_name}")
update_infos("restarting nginx")
exec_ssh_command_by_sudo(ssh,"service nginx restart")
update_infos("finish restarting nginx")
@site_construct.update(:status =>"")
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)
puts info
@site_construct.infos = @site_construct.infos.push(info)
@site_construct.save!
return @site_construct.infos
end
end

View File

@ -0,0 +1,56 @@
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}'")
end
else
update_infos("execing #{commands}")
exec_ssh_command_by_sudo(ssh,"bash -l -c '#{commands}'")
end
end
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)
puts info
@site_construct.infos = @site_construct.infos.push(info)
@site_construct.save!
return @site_construct.infos
end
end