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,12 +206,20 @@ class SiteConstruct
end
def cert_file_remote_store_path
site_cert = self.site_cert
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
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(" ","\\ ")
end

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>
@ -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,6 +105,15 @@ 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
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}\"")
@ -112,6 +121,7 @@ namespace :create_site do
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