This commit is contained in:
BoHung Chiu 2022-10-16 15:13:12 +08:00
parent cc104e83ba
commit 644aa05ee4
4 changed files with 50 additions and 19 deletions

View File

@ -120,7 +120,14 @@ class SiteConstruct
domain_name_str = ((domain_name_str.split(/\s+/) - [default_domain]) + [default_domain]).join(" ")
end
if old_server_block.present?
new_server_block = old_server_block.gsub(/(listen\s+)[^;]+/){|ff| "#{$1}#{port_text}"}
new_server_block = old_server_block.gsub('\n',"\n").gsub(/(listen\s+)[^;]+/){|ff| "#{$1}#{port_text}"}
if port_text == "80"
new_server_block = new_server_block.gsub(/^.*#\s*managed by Certbot(\n|$)/,'')
else
if self.site_cert && !(self.site_cert.is_certbot)
new_server_block = new_server_block.gsub(/#\s*managed by Certbot/,'')
end
end
new_server_block = new_server_block.gsub(/(server_name\s+)[^;]+/m){|ff| "#{$1}#{domain_name_str}"}
new_server_block = new_server_block.gsub(/\s*ssl_certificate[^;]+;/,'')
level_2_block = parse_nginx_text_to_server_blocks(old_server_block,true,2)
@ -199,11 +206,19 @@ class SiteConstruct
end
def cert_file_remote_store_path
site_cert = self.site_cert
"#{self.full_site_path}/ssl_certs/#{site_cert.id}/#{site_cert["cert_file"]}"
if site_cert.source_paths.present? && site_cert.source_paths.count == 2
site_cert.source_paths[0]
else
"#{self.full_site_path}/ssl_certs/#{site_cert.id}/#{site_cert["cert_file"]}"
end
end
def private_key_remote_store_path
site_cert = self.site_cert
"#{self.full_site_path}/ssl_certs/#{site_cert.id}/#{site_cert["private_key"]}"
if site_cert.source_paths.present? && site_cert.source_paths.count == 2
site_cert.source_paths[1]
else
"#{self.full_site_path}/ssl_certs/#{site_cert.id}/#{site_cert["private_key"]}"
end
end
def get_path
return self.path.to_s.gsub(" ","\\ ")

View File

@ -20,6 +20,9 @@
ul.no_list_style li label {
font-weight: bold;
}
#info_texts{
white-space: pre;
}
</style>
<table class="table main-list default footable-loaded">
<thead>
@ -63,7 +66,7 @@
<a class="btn btn-primary update_nginx_settings" href="#" data-key="<%=site_server.server_name%>" style="background: blueviolet;"><%=t("client_management.update_nginx_settings")%></a>
<% end %>
<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-danger" 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>
<a class="btn btn-primary detect_sites" href="#" data-key="<%=site_server.server_name%>">Detect sites</a>
</td>
@ -100,10 +103,10 @@
})
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>"+
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>");
"<div style='clear:both;'></div><div id='server_names_block'></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({
resizable: true,
@ -151,7 +154,7 @@
var infos = request.responseJSON.infos;
var status = request.responseJSON.status;
if($("#info_texts").length == 0)
return infos.join("<br>")
return infos.join("\n")
else{
if(status == "")
var status_text = "not yet create";
@ -160,7 +163,7 @@
if(!status_text){
status_text = "<span style=\"color: skyblue;\">"+status+"</span>";
}
$("#info_texts").html(status_text+"<div style='clear:both;'></div>"+infos.join("<br>"));
$("#info_texts").html(status_text+"<div style='clear:both;'></div>"+infos.join("\n"));
msg_end.scrollIntoView();
timeout_id = window.setTimeout(see_infos(key),1000);
}

View File

@ -5,6 +5,9 @@
ul.no_list_style li label {
font-weight: bold;
}
#info_texts{
white-space: pre;
}
</style>
<table class="table main-list default footable-loaded">
<thead>
@ -106,9 +109,9 @@
var infos = request.responseJSON.infos;
var status = request.responseJSON.status;
if($("#info_texts").length == 0)
return infos.join("<br>")
return infos.join("\n")
else{
$("#info_texts").html(infos.join("<br>"));
$("#info_texts").html(infos.join("\n"));
if(status == "")
var status_text = "not yet create";
else

View File

@ -105,13 +105,23 @@ namespace :create_site do
auto_update_infos("Installing Cert on #{valid_domain_names.join(" , ")}")
cert_file_content = [(@site_cert.cert_file.file.read.strip rescue ""),(@site_cert.ca_bundle.file.read.strip rescue "")].join("\n").strip
private_key_content = @site_cert.private_key.file.read
cert_file_store_path = @site_construct.cert_file_remote_store_path
exec_ssh_command_by_sudo(ssh,"mkdir -p #{File.dirname(cert_file_store_path)}")
exec_command_by_user(ssh,"x='#{cert_file_content}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{cert_file_store_path}\"")
private_key_store_path = @site_construct.private_key_remote_store_path
exec_ssh_command_by_sudo(ssh,"mkdir -p #{File.dirname(private_key_store_path)}")
exec_command_by_user(ssh,"x='#{private_key_content}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{private_key_store_path}\"")
auto_update_infos("Finish copy.")
if is_certbot && @site_cert.source_paths.present? && @site_cert.source_paths.count == 2
source_paths = @site_cert.source_paths
is_exist = exec_command_by_user(ssh, "echo '#{@password}'|sudo -S bash -c 'if [ -e \"#{source_paths[0]}\" ] && [ -e \"#{source_paths[1]}\" ] ; then echo 1; else echo 0; fi'").strip() == "1"
else
is_exist = false
end
if is_exist
auto_update_infos("Cert already exist.")
else
cert_file_store_path = @site_construct.cert_file_remote_store_path
exec_ssh_command_by_sudo(ssh,"mkdir -p #{File.dirname(cert_file_store_path)}")
exec_command_by_user(ssh,"x='#{cert_file_content}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{cert_file_store_path}\"")
private_key_store_path = @site_construct.private_key_remote_store_path
exec_ssh_command_by_sudo(ssh,"mkdir -p #{File.dirname(private_key_store_path)}")
exec_command_by_user(ssh,"x='#{private_key_content}'; echo '#{@password}' | sudo -S sh -c \"echo '$x' > #{private_key_store_path}\"")
auto_update_infos("Finish copy.")
end
auto_update_infos("Setting Cert...")
nginx_file_content = exec_command_by_user(ssh,"cat #{@site_construct.nginx_file}")
all_ports = (@site_construct.port + ["443"]).uniq