From 2a0c6938295fcaafc1642e2f35ce67f7aaf596c5 Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Fri, 10 Apr 2020 14:46:06 +0400
Subject: [PATCH] Added service listing and nullable zones. Description: - run
 make list to show all running services and their statuses.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 Makefile                            | 20 +++++++++++------
 listing.sh                          | 33 +++++++++++++++++++++++++++++
 nginx/subdomain_template_http.conf  |  6 +++---
 nginx/subdomain_template_https.conf |  6 +++---
 4 files changed, 53 insertions(+), 12 deletions(-)
 create mode 100755 listing.sh

diff --git a/Makefile b/Makefile
index dd8d429..c16c1ef 100644
--- a/Makefile
+++ b/Makefile
@@ -59,6 +59,11 @@ reload:
 shell:
 	@docker exec -it ${cont_name} sh
 
+## Show all hosted domains and their targets.
+list:
+	@chmod 777 ./listing.sh
+	@sh ./listing.sh
+
 # params:
 #- target - proxy destanation adress. Also you can specify container name in memes_network;
 #- addr - new address mapping in format ${subdomain}.${domain}.${zone} e.g. sub.domain.com.
@@ -80,21 +85,24 @@ endif
 	SUBDOMAIN_VAR=$$(echo "${addr}" | cut -d '.' -f1) ;\
 	DOMAIN_VAR=$$(echo "${addr}" | cut -d '.' -f2);\
 	ZONE_VAR=$$(echo "${addr}" | cut -d '.' -f3);\
-	ZONE_VAR="$${ZONE_VAR:-com}";\
-	echo "Creating $${SUBDOMAIN_VAR}.$${DOMAIN_VAR} file"; \
+	if [[ -n "$${ZONE_VAR}" ]];then \
+		ZONE_VAR=".$${ZONE_VAR}"; \
+	fi; \
+	CONF_FILE="./nginx/sites-enabled/$${SUBDOMAIN_VAR}.$${DOMAIN_VAR}$${ZONE_VAR}.conf";\
+	echo "Creating config file '$${CONF_FILE}'"; \
 	if [[ "${proto}" == "https" ]];then \
-		cp ./nginx/subdomain_template_https.conf "./nginx/sites-enabled/$${SUBDOMAIN_VAR}.$${DOMAIN_VAR}.conf";\
+		cp ./nginx/subdomain_template_https.conf "$${CONF_FILE}";\
 	else \
-		cp ./nginx/subdomain_template_http.conf "./nginx/sites-enabled/$${SUBDOMAIN_VAR}.$${DOMAIN_VAR}.conf";\
+		cp ./nginx/subdomain_template_http.conf "$${CONF_FILE}";\
 	fi; \
 	sed -i "s/\$${sub_domain}/$${SUBDOMAIN_VAR}/g;"\
 	"s/\$${main_domain}/$${DOMAIN_VAR}/g;"\
 	"s/\$${domain_zone}/$${ZONE_VAR}/g;"\
-	"s/\$${target_cont}/${target}/" "./nginx/sites-enabled/$${SUBDOMAIN_VAR}.$${DOMAIN_VAR}.conf"; \
+	"s/\$${target_cont}/${target}/" "$${CONF_FILE}"; \
 	docker exec -it ${cont_name} nginx -s reload; if [ $$? -ne 0 ]; then\
 		echo -e '${RED}Wrong config.${RESET}\n'\
 		'Please make sure that ${YELLOW}target${RESET} and ${YELLOW}addr${RESET} parameters was provided and correct.' ;\
-		rm ./nginx/sites-enabled/$${SUBDOMAIN_VAR}.$${DOMAIN_VAR}.conf ;\
+		rm "$${CONF_FILE}" ;\
 		docker exec -it ${cont_name} nginx -s reload;\
 	fi
 
diff --git a/listing.sh b/listing.sh
new file mode 100755
index 0000000..c2add5e
--- /dev/null
+++ b/listing.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+RESET="$(tput -Txterm sgr0)"
+RED="$(tput -Txterm setaf 1)"
+GREEN="$(tput -Txterm setaf 2)"
+YELLOW="$(tput -Txterm setaf 3)"
+
+sites_dir="./nginx/sites-enabled/"
+
+get_param(){
+  grep "$1" "${sites_dir}${filename}" | xargs | cut -d ' ' -f "$2" | sed 's/;//'
+}
+
+
+for filename in $(ls "$sites_dir" | sort -d); do
+  listen_to="$(get_param listen 2)"
+  PROTO="https"
+  if [ 80 = "$listen_to" ]; then
+    PROTO="http"
+  fi
+  ADDR="$(get_param server_name 2)"
+  if [ "$ADDR" = "_" ];then
+    continue;
+  fi
+  STATUS="${GREEN}[  UP  ]"
+  URL="$PROTO://${ADDR}"
+  STATUS_CODE="$(curl -o /dev/null -s -w "%{http_code}\n" "$URL")"
+  if [ "$STATUS_CODE" -ne "200" ];then
+    STATUS="${RED}[ DOWN ]"  
+  fi
+  PROXY="$(get_param "set \$target" 3)"
+  echo "$STATUS ${URL}${RESET} => ${YELLOW}${PROXY}${RESET}";
+done
diff --git a/nginx/subdomain_template_http.conf b/nginx/subdomain_template_http.conf
index dece20c..b58b8a7 100644
--- a/nginx/subdomain_template_http.conf
+++ b/nginx/subdomain_template_http.conf
@@ -1,13 +1,13 @@
 server{
   
   listen 80;
-  server_name ${sub_domain}.${main_domain}.${domain_zone};
+  server_name ${sub_domain}.${main_domain}${domain_zone};
 
   include /var/nginx/common/server.conf;
   client_max_body_size            100m;
 
-  access_log  /var/log/nginx/${sub_domain}.${main_domain}.${domain_zone}_access.log combined;
-  error_log   /var/log/nginx/${sub_domain}.${main_domain}.${domain_zone}_error.log;
+  access_log  /var/log/nginx/${sub_domain}.${main_domain}${domain_zone}_access.log combined;
+  error_log   /var/log/nginx/${sub_domain}.${main_domain}${domain_zone}_error.log;
 
   location / {
     resolver 127.0.0.11 valid=30s;
diff --git a/nginx/subdomain_template_https.conf b/nginx/subdomain_template_https.conf
index 50dc330..185f7e0 100644
--- a/nginx/subdomain_template_https.conf
+++ b/nginx/subdomain_template_https.conf
@@ -1,7 +1,7 @@
 server{
   
   listen 443 ssl;
-  server_name ${sub_domain}.${main_domain}.${domain_zone};
+  server_name ${sub_domain}.${main_domain}${domain_zone};
 
   ssl_certificate /var/nginx/ssl/${main_domain}_certificate.pem;
   ssl_certificate_key /var/nginx/ssl/${main_domain}_private.key;
@@ -9,8 +9,8 @@ server{
   include /var/nginx/common/server.conf;
   client_max_body_size            100m;
 
-  access_log  /var/log/nginx/${sub_domain}.${main_domain}.${domain_zone}_access.log combined;
-  error_log   /var/log/nginx/${sub_domain}.${main_domain}.${domain_zone}_error.log;
+  access_log  /var/log/nginx/${sub_domain}.${main_domain}${domain_zone}_access.log combined;
+  error_log   /var/log/nginx/${sub_domain}.${main_domain}${domain_zone}_error.log;
 
   location / {
     resolver 127.0.0.11 valid=30s;
-- 
GitLab