Browse Source

Refactor issue certs

bryan 3 years ago
parent
commit
b8c904bbf3
1 changed files with 43 additions and 45 deletions
  1. 43 45
      acme-cpanel.sh

+ 43 - 45
acme-cpanel.sh

@@ -5,7 +5,6 @@
 # See README.md for more details
 #
 # Copyright 2020 Bryan Roessler <bryanroessler@gmail.com>
-#
 
 unset SITES_DIR USEREMAIL DOMAIN_FILES DOMAIN_GROUPS DEPLOY_CMD_PREFIX ISSUE_CMD_PREFIX DEBUG GROUP
 
@@ -156,57 +155,56 @@ load_domains() {
 
 issue_and_deploy_certs() {
 
-    local domain_root domain domain_group
-    local -a issue_cmd=()
-    local -a deploy_cmd=()
-    
-    if [[ -v GROUP ]]; then
-        for domain_group in "${DOMAIN_GROUPS[@]}"; do
-            unset i
-            for domain in $domain_group; do # we want to split on whitespace
-                [[ "$domain" == "" ]] && continue
-                # Get the webroot from the first domain
-                if [[ ! -v i ]]; then 
-                    local i="set"
-                    domain_root=$(get_webroot "$domain")
-                    issue_cmd=("${ISSUE_CMD_PREFIX[@]}" "-w" "$domain_root")
+    local group_root domain_root domain domain_group
+
+    for domain_group in "${DOMAIN_GROUPS[@]}"; do
+        local -a issue_cmd=("${ISSUE_CMD_PREFIX[@]}")
+        local -a deploy_cmd=("${DEPLOY_CMD_PREFIX[@]}")
+        local i="set"
+        # Issue certificates
+        for domain in $domain_group; do # we want to split on whitespace
+            [[ "$domain" == "" ]] && continue
+            if [[ -v GROUP ]]; then
+                if [[ "$METHOD" == "webroot" && -v i ]]; then
+                    group_root=$(get_webroot "$domain")
+                    issue_cmd+=("-w" "$group_root")
+                    unset i
                 fi
+                # Append domains to issue command that we will call after the loop
                 issue_cmd+=("-d" "$domain" "-d" "www.$domain")
-            done
-
-            # Issue certificate for entire domain group
-            echo "Running:" "${issue_cmd[@]}"
-            if ! "${issue_cmd[@]}"; then
-                echo "Failed to issue certificate"
-            fi
-            # Deploy certificates one by one
-            for domain in $domain_group; do
-                deploy_cmd=("${DEPLOY_CMD_PREFIX[@]}" "-w" "$domain_root" "-d" "$domain")
-                echo "Running:" "${deploy_cmd[@]}"
-                "${deploy_cmd[@]}"
-            done
-        done
-    else
-        for domain_group in "${DOMAIN_GROUPS[@]}"; do
-            # Issue and deploy certificates one by one
-            for domain in $domain_group; do # we want to split on whitespace
-                issue_cmd=("${ISSUE_CMD_PREFIX[@]}" "-d" "$domain" "-d" "www.$domain")
-                [[ "$METHOD" == "webroot" ]] && domain_root=$(get_webroot "$domain") && issue_cmd=("${issue_cmd[@]}" "-w" "$domain_root")
-                deploy_cmd=("${DEPLOY_CMD_PREFIX[@]}" "-d" "$domain") # I think we only need to deploy to the domain, not subdomains
-                [[ "$METHOD" == "webroot" ]] && deploy_cmd=("${deploy_cmd[@]}" "-w" "$domain_root")
+            # Issue certificate for single domain
+            else 
+                local -a issue_cmd=("${ISSUE_CMD_PREFIX[@]}")
+                domain_root=$(get_webroot "$domain")
+                issue_cmd+=("-d" "$domain" "-d" "www.$domain")
+                [[ "$METHOD" == "webroot" ]] && issue_cmd+=("-w" "$domain_root")
                 echo "Running:" "${issue_cmd[@]}"
                 if ! "${issue_cmd[@]}"; then
-                    echo "Failed to issue certificate for $domain"
+                    echo "Failed to issue certificate for domain: $domain"
                     err=1
                 fi
-                echo "Running:" "${deploy_cmd[@]}"
-                if ! "${deploy_cmd[@]}"; then
-                    echo "Failed to deploy certificate for $domain"
-                    err=1
-                fi
-            done
+            fi
         done
-    fi
+
+        # Issue certificate for group of domains
+        if [[ -v GROUP ]]; then 
+            echo "Running:" "${issue_cmd[@]}"
+            if ! "${issue_cmd[@]}"; then
+                echo "Failed to issue certificate for domain group: $domain_group"
+                err=1
+            fi
+        fi
+
+        # Deploy certificates one domain at a time
+        for domain in $domain_group; do
+            deploy_cmd=("${DEPLOY_CMD_PREFIX[@]}" "-d" "$domain") # I think we only need to deploy to the domain, not subdomains (e.g. www.)
+            echo "Running:" "${deploy_cmd[@]}"
+            if ! "${deploy_cmd[@]}"; then
+                echo "Failed to deploy certificate for $domain"
+                err=1
+            fi
+        done
+    done
 }