Add built in commands options.

This commit is contained in:
BoHung Chiu 2021-12-30 21:46:50 +08:00
parent e6ca9c468d
commit 47eef3d78a
6 changed files with 108 additions and 23 deletions

View File

@ -0,0 +1,12 @@
<%
@command_trans = {}
@command_relations = {"upgrade_site"=>'git fetch origin && rails runner "a=Admin::SitesController.new;a.git_reset(%w(origin)[0]\,%w(update)[0]);while(1) do (Multithread.where(key: %w(update_manager)[0]\,status: %w(finish)[0]).count!=0 ? break : nil) end;sleep(5)"',"bundle_update"=>"bundle update && kill -s USR2 `cat tmp/pids/unicorn.pid`","restart_site"=>"kill -s TERM `fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` && bundle exec unicorn_rails -c config/unicorn.rb -D -E {{rails_env}}"}
@command_relations.each do |k,v|
@command_trans[k] = I18n.t("client_management.#{k}")
end
rails_envs = ["production", "development"]
rails_envs.each do |env|
@command_relations["start_site_in_env\\,#{env}"] = "kill -s TERM `fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` && bundle exec unicorn_rails -c config/unicorn.rb -D -E #{env}"
@command_trans["start_site_in_env\\,#{env}"] = I18n.t("client_management.start_site_in_env",{:env=>env})
end
%>

View File

@ -13,6 +13,14 @@
color: red;
}
</style>
<style type="text/css">
ul.no_list_style li{
list-style: none;
}
ul.no_list_style li label {
font-weight: bold;
}
</style>
<table class="table main-list default footable-loaded">
<thead>
<tr>
@ -77,8 +85,15 @@
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){
var built_in_commands = "<div><ul class='no_list_style'>"
<% render :partial => "get_commands_list" %>
var command_trans = <%= @command_trans.to_json.html_safe %>;
Object.keys(command_trans).forEach(function(k){
built_in_commands += "<li><label><input class='built_in_commands' type='checkbox' data-key='{{"+ k +"}}'>" + command_trans[k] + "</label></li>"
})
built_in_commands += "</ul></div>"
$("#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 style='clear:both;'></div><div id='server_names_block'></div>"+built_in_commands+"<textarea style='height: 200px;width: 100%;' id='commands_area'></textarea>"+
"</div>");
};
$( "#exec-commands-dialog-confirm" ).dialog({
@ -100,7 +115,13 @@
}))
}
function exec_commands(){
var commands = $('#commands_area').val().replace("\r\n","////").replace("\n","////").replace("<br>","////").replace("<br/>","////");
var commands = "";
var built_in_commands_selected = [];
$("input.built_in_commands:checked").each(function(i,v){
built_in_commands_selected.push($(v).data("key"));
})
commands += built_in_commands_selected.join("////");
commands += $('#commands_area').val().replace(/(\r\n|\n)/g,"////");
var server_names = [];
var $els = $(".server_name_checkbox");
for(var i = 0; i < $els.length; i++){

View File

@ -1,3 +1,11 @@
<style type="text/css">
ul.no_list_style li{
list-style: none;
}
ul.no_list_style li label {
font-weight: bold;
}
</style>
<table class="table main-list default footable-loaded">
<thead>
<tr>
@ -293,8 +301,15 @@
});
function show_exec_commands_block(id){
if($("#exec-commands-dialog-confirm").length == 0){
var built_in_commands = "<div><ul class='no_list_style'>"
<% render :partial => "get_commands_list" %>
var command_trans = <%= @command_trans.to_json.html_safe %>;
Object.keys(command_trans).forEach(function(k){
built_in_commands += "<li><label><input class='built_in_commands' type='checkbox' data-key='{{"+ k +"}}'>" + command_trans[k] + "</label></li>"
})
built_in_commands += "</ul></div>"
$("#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 style='clear:both;'></div>"+built_in_commands+"<textarea style='height: 200px;width: 100%;' id='commands_area' data-id='"+id+"'></textarea>"+
"</div>");
}else{
$('#commands_area').attr('data-id',id);
@ -312,7 +327,13 @@
});
}
function exec_commands(){
var commands = $('#commands_area').val().replace(/(\r\n|\n)/,"////");
var commands = "";
var built_in_commands_selected = [];
$("input.built_in_commands:checked").each(function(i,v){
built_in_commands_selected.push($(v).data("key"));
})
commands += built_in_commands_selected.join("////");
commands += $('#commands_area').val().replace(/(\r\n|\n)/g,"////");
var id = $('#commands_area').attr('data-id');
console.log(id);
console.log(commands);

View File

@ -7,6 +7,9 @@ en:
upload_cert: Upload Cert
cert_management: Cert Management
client_management:
start_site_in_env: "Start site in %{env}"
upgrade_site: "Upgrade site"
bundle_update: "Bundle update"
batch_install: Batch Install
install_certbot_certificates_in_batches: Install certbot certificates in batches
update_nginx_settings: "Update nginx settings"

View File

@ -7,6 +7,9 @@ zh_tw:
upload_cert: 上傳憑證
cert_management: 憑證管理
client_management:
start_site_in_env: "開啟網站為%{env}"
upgrade_site: 更新網站
bundle_update: "Bundle update"
batch_install: 批次安裝
install_certbot_certificates_in_batches: 批次安裝certbot憑證
update_nginx_settings: "修改nginx設定"

View File

@ -19,6 +19,18 @@ namespace :exec_commands do
@thread = nil
end
if @thread.nil?
@command_trans = {}
@command_relations = {"upgrade_site"=>'git fetch origin && rails runner "a=Admin::SitesController.new;a.git_reset(%w(origin)[0],%w(update)[0]);while(1) do (Multithread.where(key: %w(update_manager)[0],status: %w(finish)[0]).count!=0 ? break : nil) end;sleep(5)"',
"bundle_update"=>"bundle update && kill -s USR2 `cat tmp/pids/unicorn.pid`",
"restart_site"=>"kill -s TERM `fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` && bundle exec unicorn_rails -c config/unicorn.rb -D -E {{rails_env}}"}
@command_relations.each do |k,v|
@command_trans[k] = I18n.t("client_management.#{k}")
end
rails_envs = ["production", "development"]
rails_envs.each do |env|
@command_relations["start_site_in_env,#{env}"] = "kill -s TERM `fuser tmp/pids/unicorn.sock tmp/sockets/unicorn.sock tmp/unicorn.sock` && bundle exec unicorn_rails -c config/unicorn.rb -D -E #{env}"
@command_trans["start_site_in_env,#{env}"] = I18n.t("client_management.start_site_in_env",{:env=>env})
end
begin
if @thread.nil? && args.type == "exec_all"
@thread = Multithread.create(:key=>'execing_commands',:status=>{"infos"=>[],"status"=>"execing"})
@ -78,23 +90,32 @@ namespace :exec_commands do
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=>site_server.server_name).to_a
else
if args.type == 'exec_all'
sites = SiteConstruct.where(:server_type=>site_server.server_name).to_a
else
sites = [@site_construct]
end
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")
rails_env = @site_construct.rails_env.blank? ? "development" : @site_construct.rails_env
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}'")
@command_i18n = command
update_flag = 1
@command_relations.each do |k,v|
if command.include?("{{#{k}}}")
update_flag = 2 #Not logging commands result
end
command = command.gsub("{{#{k}}}",v)
@command_i18n = @command_i18n.gsub("{{#{k}}}",@command_trans[k])
end
command = command.gsub("{{rails_env}}",rails_env)
exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'", update_flag)
end
end
else
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
@ -148,15 +169,19 @@ namespace :exec_commands do
@thread.save!
return @thread.status["infos"]
end
def exec_ssh_command_and_see_output(session,command,update=true,output_string=false)
def exec_ssh_command_and_see_output(session,command,update_flag=1,output_string=false)
outputs = []
@flag = (@type == "exec_all")
command_i18n = command
update = (update_flag == true || update_flag == 1 || update_flag == 2)
update_outputs = (update_flag == true || update_flag == 1)
if update
command_i18n = @command_i18n if @command_i18n
if @site_construct
update_thread_infos_for_exec("execing #{command} on on <a href='#{((@site_construct.get_port == "443") ? "https" : "http")}://#{@site_construct.domain_name}#{((@site_construct.get_port=="80" || @site_construct.get_port=="443" || @site_construct.get_port.blank?) ? "" : (':'+@site_construct.get_port))}'>#{@site_construct.domain_name}</a>") if @flag
update_infos_for_exec("execing #{command}")
update_thread_infos_for_exec(["execing #{command_i18n} on on <a href='#{((@site_construct.get_port == "443") ? "https" : "http")}://#{@site_construct.domain_name}#{((@site_construct.get_port=="80" || @site_construct.get_port=="443" || @site_construct.get_port.blank?) ? "" : (':'+@site_construct.get_port))}'>#{@site_construct.domain_name}</a>",""],false,true) if @flag
update_infos_for_exec(["execing #{command_i18n}",""],false,true)
elsif @thread
update_thread_infos_for_exec("execing #{command}...")
update_thread_infos_for_exec(["execing #{command_i18n}...",""],false,true)
end
end
session.open_channel do |channel|
@ -182,7 +207,7 @@ namespace :exec_commands do
new_arr << ""
end
outputs += new_arr
if update
if update_outputs
if first_output
update_thread_infos_for_exec(first_output,true) if @flag
update_infos_for_exec(first_output,true)
@ -196,7 +221,7 @@ namespace :exec_commands do
else
outputs[-1] += (data_str rescue "")
end
if update
if update_outputs
update_thread_infos_for_exec(data_str,true) if @flag
update_infos_for_exec(data_str,true)
end
@ -206,10 +231,10 @@ namespace :exec_commands do
channel.on_close do |ch|
if update
if @site_construct
update_thread_infos_for_exec("finish execing #{command} on <a href='#{((@site_construct.get_port == "443") ? "https" : "http")}://#{@site_construct.domain_name}#{((@site_construct.get_port=="80" || @site_construct.get_port=="443" || @site_construct.get_port.blank?) ? "" : (':'+@site_construct.get_port))}'>#{@site_construct.domain_name}</a>") if @flag
update_infos_for_exec("finish execing #{command}")
update_thread_infos_for_exec("finish execing #{command_i18n} on <a href='#{((@site_construct.get_port == "443") ? "https" : "http")}://#{@site_construct.domain_name}#{((@site_construct.get_port=="80" || @site_construct.get_port=="443" || @site_construct.get_port.blank?) ? "" : (':'+@site_construct.get_port))}'>#{@site_construct.domain_name}</a>") if @flag
update_infos_for_exec("finish execing #{command_i18n}")
elsif @thread
update_thread_infos_for_exec("finish execing #{command}")
update_thread_infos_for_exec("finish execing #{command_i18n}")
end
end
end
@ -223,7 +248,7 @@ namespace :exec_commands do
return outputs
end
end
def exec_ssh_command_by_sudo_and_see_output(session,command,update=true,output_string=false)
def exec_ssh_command_by_sudo_and_see_output(session,command,update=1,output_string=false)
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)