add super manager feature

This commit is contained in:
邱博亞 2022-10-01 21:12:14 +08:00
parent c3f291ea9e
commit cc104e83ba
7 changed files with 84 additions and 12 deletions

View File

@ -278,11 +278,11 @@ class Admin::SitePanelController < OrbitAdminController
end end
elsif params[:type] == 'close' elsif params[:type] == 'close'
Thread.new do Thread.new do
system("bundle exec rake exec_commands:exec_commands[#{params[:id]},,close_site]") Rake::Task['exec_commands:exec_commands'].execute(Rake::TaskArguments.new([:site_construct_id,:commands,:type], [params[:id], '', 'close_site']))
end end
elsif params[:type] == 'open' || params[:type] == 'restart' elsif params[:type] == 'open' || params[:type] == 'restart'
Thread.new do Thread.new do
system("bundle exec rake exec_commands:exec_commands[#{params[:id]},,open_site,,#{params[:env]}]") Rake::Task['exec_commands:exec_commands'].execute(Rake::TaskArguments.new([:site_construct_id,:commands,:type,:server_name,:rails_env], [params[:id], '', 'open_site', '', params[:env]]))
end end
elsif params[:type] == 'detail' elsif params[:type] == 'detail'
@site_construct = SiteConstruct.find(params[:id]) @site_construct = SiteConstruct.find(params[:id])
@ -333,10 +333,15 @@ class Admin::SitePanelController < OrbitAdminController
end end
else else
Thread.new do Thread.new do
cmds = params[:commands].split('////')
if cmds.include?('{{create_super_manager}}') && !params[:account].blank? && !params[:password].blank?
idx = cmds.index('{{create_super_manager}}')
cmds[idx] = create_super_manager_cmd(params[:account],params[:password])
end
if params[:id].blank? && !params[:server_names].blank? 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('////')])) Rake::Task['exec_commands:exec_commands'].execute(Rake::TaskArguments.new([:commands, :type, :server_name], [cmds.join('////'), 'exec_all', params[:server_names].join('////')]))
else else
Rake::Task['exec_commands:exec_commands'].execute(Rake::TaskArguments.new([:site_construct_id, :commands, :type], [params[:id], params[:commands], 'exec_commands'])) Rake::Task['exec_commands:exec_commands'].execute(Rake::TaskArguments.new([:site_construct_id, :commands, :type], [params[:id], cmds.join('////'), 'exec_commands']))
end end
end end
end end
@ -412,4 +417,44 @@ class Admin::SitePanelController < OrbitAdminController
site_params = params.require(:site_construct).permit! site_params = params.require(:site_construct).permit!
site_params.except(:domain_name,:port) site_params.except(:domain_name,:port)
end end
def create_super_manager_cmd(user_name, password)
user = User.new(id: nil, user_name: user_name, password: password, beta_tester: true, approved: true)
member_profile = user.build_member_profile(id: nil)
user_attrs = user.attributes
user_attrs.delete('_id')
user_attrs.delete('member_profile_id')
member_profile.send(:generate_uid)
member_attrs = member_profile.attributes
member_attrs.delete('_id')
cmd = """mongo --eval 'a=function(){
var user_query = {user_name: \"#{user_attrs['user_name']}\"};
var user = db.users.findOne(user_query);
var user_id;
var user_data = #{user_attrs.to_json};
var time = new Date();
user_data.created_at = time;
user_data.updated_at = time;
user_data.password_updated_at = time;
if (user){
user_data._id = user._id;
db.users.update(user_query, {$set: user_data});
}else{
db.users.insertOne(user_data);
}
user = db.users.findOne(user_query);
var member_query = {_id: user.member_profile_id};
var member = user.member_profile_id ? db.member_profiles.findOne(member_query) : null;
var member_data = #{member_attrs.to_json};
member_data.created_at = time;
member_data.updated_at = time;
if (member){
member_data._id = member._id;
member = db.member_profiles.update(member_query, {$set: member_data});
}else{
member = db.member_profiles.insertOne(member_data);
user_data.member_profile_id = member.insertedId;
db.users.update(user_query, {$set: user_data});
}
}()' {{db_name}}""".gsub(/\n|^ */,"")
end
end end

View File

@ -9,4 +9,5 @@
@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_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}) @command_trans["start_site_in_env\\,#{env}"] = I18n.t("client_management.start_site_in_env",{:env=>env})
end end
@command_trans['create_super_manager'] = I18n.t("client_management.create_super_manager")
%> %>

View File

@ -80,6 +80,13 @@
<a id="exec_commands" class="btn btn-primary pull-right" href="#">Exec commands</a> <a id="exec_commands" class="btn btn-primary pull-right" href="#">Exec commands</a>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
$(document).on("change", "input[data-key=\"{{create_super_manager}}\"]", function() {
if ($(this).prop('checked')){
$('#super_manager_div').show()
}else{
$('#super_manager_div').hide()
}
});
var close_info = false; var close_info = false;
var timeout_id; 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>"}; 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>"};
@ -93,8 +100,10 @@
}) })
built_in_commands += "</ul></div>" built_in_commands += "</ul></div>"
$("#main-wrap").before("<div id='exec-commands-dialog-confirm' title='Commands to Exec'>"+ $("#main-wrap").before("<div id='exec-commands-dialog-confirm' title='Commands to Exec'>"+
"<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 style='clear:both;'></div><div id='server_names_block'></div>"+
"</div>"); built_in_commands+
"<div style=\"display: none;\" id=\"super_manager_div\"><input type=\"text\" placeholder=\"account\" name=\"account\"><br><input type=\"password\" placeholder=\"password\" name=\"password\"></div>"+"<textarea style='height: 200px;width: 100%;' id='commands_area'></textarea>"+
"</div>");
}; };
$( "#exec-commands-dialog-confirm" ).dialog({ $( "#exec-commands-dialog-confirm" ).dialog({
resizable: true, resizable: true,
@ -130,7 +139,7 @@
server_names.push($("label[for='"+$els.eq(i).attr('id')+"']").text().trim()); 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(){ $.post("<%=admin_site_panel_edit_site_path%>",{'server_names':server_names,'commands':commands,'account': $('input[name="account"]').val(),'password': $('input[name="password"]').val()}).done(function(){
show_infos_dialog("execing_commands"); show_infos_dialog("execing_commands");
}); });
} }

View File

@ -88,6 +88,13 @@
<a class="btn btn-primary pull-right" href="<%=admin_site_panel_path%>" title="Create new web"><%=t(:add)%></a> <a class="btn btn-primary pull-right" href="<%=admin_site_panel_path%>" title="Create new web"><%=t(:add)%></a>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
$(document).on("change", "input[data-key=\"{{create_super_manager}}\"]", function() {
if ($(this).prop('checked')){
$('#super_manager_div').show()
}else{
$('#super_manager_div').hide()
}
});
$(document).ready(function(){ $(document).ready(function(){
var close_info = false; var close_info = false;
var timeout_id; var timeout_id;
@ -309,9 +316,12 @@
built_in_commands += "<li><label><input class='built_in_commands' type='checkbox' data-key='{{"+ k +"}}'>" + command_trans[k] + "</label></li>" 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>" built_in_commands += "</ul></div>"
$("#main-wrap").before("<div id='exec-commands-dialog-confirm' title='Commands to Exec'>"+ $("#main-wrap").before("<div id='exec-commands-dialog-confirm' title='Commands to Exec'>" +
"<div style='clear:both;'></div>"+built_in_commands+"<textarea style='height: 200px;width: 100%;' id='commands_area' data-id='"+id+"'></textarea>"+ "<div style='clear:both;'></div>"+
"</div>"); built_in_commands+
"<div style=\"display: none;\" id=\"super_manager_div\"><input type=\"text\" placeholder=\"account\" name=\"account\"><br><input type=\"password\" placeholder=\"password\" name=\"password\"></div>"+
"<textarea style='height: 200px;width: 100%;' id='commands_area' data-id='"+id+"'></textarea>"+
"</div>");
}else{ }else{
$('#commands_area').attr('data-id',id); $('#commands_area').attr('data-id',id);
}; };
@ -338,7 +348,7 @@
var id = $('#commands_area').attr('data-id'); var id = $('#commands_area').attr('data-id');
console.log(id); console.log(id);
console.log(commands); console.log(commands);
$.post("<%=admin_site_panel_edit_site_path%>",{'id':id,'commands':commands}).done(function(){ $.post("<%=admin_site_panel_edit_site_path%>",{'id':id,'commands':commands,'account': $('input[name="account"]').val(),'password': $('input[name="password"]').val()}).done(function(){
show_infos_dialog($('.see_infos[data-id="'+id+'"]').eq(0)); show_infos_dialog($('.see_infos[data-id="'+id+'"]').eq(0));
}); });
} }

View File

@ -7,6 +7,7 @@ en:
upload_cert: Upload Cert upload_cert: Upload Cert
cert_management: Cert Management cert_management: Cert Management
client_management: client_management:
create_super_manager: Create Super Manager For Site
enable_api: Enable API enable_api: Enable API
api_key: API Key api_key: API Key
setting: Setting setting: Setting

View File

@ -7,6 +7,7 @@ zh_tw:
upload_cert: 上傳憑證 upload_cert: 上傳憑證
cert_management: 憑證管理 cert_management: 憑證管理
client_management: client_management:
create_super_manager: 創建網站超級管理者
enable_api: 開啟API enable_api: 開啟API
api_key: API Key api_key: API Key
setting: 設定 setting: 設定

View File

@ -120,13 +120,18 @@ namespace :exec_commands do
command = command.gsub("{{#{k}}}",v) command = command.gsub("{{#{k}}}",v)
@command_i18n = @command_i18n.gsub("{{#{k}}}",@command_trans[k]) @command_i18n = @command_i18n.gsub("{{#{k}}}",@command_trans[k])
end end
if command.include?('{{db_name}}')
@command_i18n = "create super manager"
end
command = command.gsub("{{rails_env}}",rails_env) command = command.gsub("{{rails_env}}",rails_env)
command = command.gsub("{{full_site_path}}",@site_construct.full_site_path) command = command.gsub("{{full_site_path}}",@site_construct.full_site_path)
command = command.gsub("{{site_name}}",@site_construct.site_name) command = command.gsub("{{site_name}}",@site_construct.site_name)
command = command.gsub("{{db_name}}",@site_construct.db_name)
@command_i18n = @command_i18n.gsub("{{rails_env}}",rails_env) @command_i18n = @command_i18n.gsub("{{rails_env}}",rails_env)
@command_i18n = @command_i18n.gsub("{{full_site_path}}",@site_construct.full_site_path) @command_i18n = @command_i18n.gsub("{{full_site_path}}",@site_construct.full_site_path)
@command_i18n = @command_i18n.gsub("{{site_name}}",@site_construct.site_name) @command_i18n = @command_i18n.gsub("{{site_name}}",@site_construct.site_name)
command = command.gsub(/'{1,3}/,"\"\'\"") command = command.gsub(/'{1,3}/,"'\"'\"'")
puts ['command',command,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'"]
exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'", update_flag) exec_ssh_command_by_sudo_and_see_output(ssh,"bash -l -c 'cd #{@site_construct.path}/#{@site_construct.site_name};#{command}'", update_flag)
@command_i18n = nil @command_i18n = nil
end end