Refactor script with functions
This commit is contained in:
parent
0bd74cbf63
commit
1c63e6d4e9
84
renew_cert.sh
Normal file → Executable file
84
renew_cert.sh
Normal file → Executable file
@ -4,8 +4,8 @@ path="$( cd "$(dirname "$0")" ; pwd -P )"
|
|||||||
|
|
||||||
#############
|
#############
|
||||||
# variables
|
# variables
|
||||||
acme_dir=/srv/certs/acme-challenge/.well-known/acme-challenge
|
acme_dir=/docker/certs/acme-challenge/.well-known/acme-challenge
|
||||||
opt_dir=/srv/certs/.opt
|
opt_dir=/docker/certs/.opt
|
||||||
account_key=$opt_dir/account.key
|
account_key=$opt_dir/account.key
|
||||||
acme_tiny=$opt_dir/acme_tiny.py
|
acme_tiny=$opt_dir/acme_tiny.py
|
||||||
openssl_conf=$opt_dir/openssl.conf
|
openssl_conf=$opt_dir/openssl.conf
|
||||||
@ -14,35 +14,36 @@ openssl_conf=$opt_dir/openssl.conf
|
|||||||
# script
|
# script
|
||||||
|
|
||||||
|
|
||||||
# stop script if receive SIGINT (ctrl-c)
|
main() {
|
||||||
trap "exit" INT
|
# stop script if receive SIGINT (ctrl-c)
|
||||||
|
trap "exit" INT
|
||||||
|
|
||||||
#print starting date
|
#print starting date
|
||||||
echo "Cert renew script: $(date)"
|
echo "Cert renew script: $(date)"
|
||||||
|
|
||||||
# check if needed files are provided
|
# check if needed files are provided
|
||||||
if [ $# -eq 0 ]; then
|
if [ $# -eq 0 ]; then
|
||||||
echo "No arguments provided."
|
echo "No arguments provided."
|
||||||
echo "Usage: $0 [folder]"
|
echo "Usage: $0 [folder]"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -d "$opt_dir" ]; then
|
if [ ! -d "$opt_dir" ]; then
|
||||||
echo "opt dir doesn't exists!"
|
echo "opt dir doesn't exists!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -d "$acme_dir" ]; then
|
if [ ! -d "$acme_dir" ]; then
|
||||||
echo "acme directory ($acme_dir) doesn't exists!"
|
echo "acme directory ($acme_dir) doesn't exists!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -s "$account_key" ]; then
|
if [ ! -s "$account_key" ]; then
|
||||||
echo "Account Key doesn't exists!"
|
echo "Account Key doesn't exists!"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -s "$acme_tiny" ]; then
|
if [ ! -s "$acme_tiny" ]; then
|
||||||
echo -n "Python script acme_tiny.py is missing. Downloading... "
|
echo -n "Python script acme_tiny.py is missing. Downloading... "
|
||||||
wget -qO "$acme_tiny" https://raw.githubusercontent.com/diafygi/acme-tiny/4.1.0/acme_tiny.py
|
wget -qO "$acme_tiny" https://raw.githubusercontent.com/diafygi/acme-tiny/4.1.0/acme_tiny.py
|
||||||
if [ $? != 0 ]; then
|
if [ $? != 0 ]; then
|
||||||
@ -51,28 +52,40 @@ if [ ! -s "$acme_tiny" ]; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo -ne "Finished\n"
|
echo -ne "Finished\n"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
acme_dir=${acme_dir%/}
|
acme_dir=${acme_dir%/}
|
||||||
opt_dir=${opt_dir%/}
|
opt_dir=${opt_dir%/}
|
||||||
|
|
||||||
# check if python is installed
|
# check if python is installed
|
||||||
command -v python >/dev/null 2>&1 || { echo >&2 "I require python but it's not installed. Aborting."; exit 1; }
|
command -v python >/dev/null 2>&1 || { echo >&2 "Python is required but it's not installed. Aborting."; exit 1; }
|
||||||
|
|
||||||
counter=0
|
counter=0
|
||||||
|
|
||||||
for arg in "$@"
|
for arg in "$@"
|
||||||
do
|
do
|
||||||
|
process_renewal
|
||||||
|
|
||||||
|
if [ $? == 0 ]; then
|
||||||
|
echo "Certificate successfully created!"
|
||||||
|
counter=$((counter+1))
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "$counter new certificates created!"
|
||||||
|
}
|
||||||
|
|
||||||
|
process_renewal() (
|
||||||
arg="$path/${arg%/}"
|
arg="$path/${arg%/}"
|
||||||
|
|
||||||
if [ ! -d "$arg" ]; then
|
if [ ! -d "$arg" ]; then
|
||||||
echo "Folder $arg doesn't exists!"
|
echo "Folder $arg doesn't exists!"
|
||||||
continue
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -s "$arg/domain.conf" ]; then
|
if [ ! -s "$arg/domain.conf" ]; then
|
||||||
echo "Configuration file doen't exists!"
|
echo "Configuration file doen't exists!"
|
||||||
continue
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# load configuration variables
|
# load configuration variables
|
||||||
@ -82,12 +95,13 @@ do
|
|||||||
# check domain.conf variables
|
# check domain.conf variables
|
||||||
if [ -z "$NAME" ]; then
|
if [ -z "$NAME" ]; then
|
||||||
echo "No name given for domain \"$arg\"".
|
echo "No name given for domain \"$arg\"".
|
||||||
continue
|
return 1
|
||||||
fi
|
fi
|
||||||
|
echo "Processing certificate \"$NAME\"..."
|
||||||
|
|
||||||
if [ ${#DOMAINS[@]} -eq 0 ]; then
|
if [ ${#DOMAINS[@]} -eq 0 ]; then
|
||||||
echo "No domains given for \"$NAME\"."
|
echo "No domains given for \"$NAME\"."
|
||||||
continue
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
@ -119,7 +133,7 @@ do
|
|||||||
|
|
||||||
if [ $? != 0 ]; then
|
if [ $? != 0 ]; then
|
||||||
echo "Creating csr/key files FAILED for \"$NAME\"!"
|
echo "Creating csr/key files FAILED for \"$NAME\"!"
|
||||||
continue
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# get certificate
|
# get certificate
|
||||||
@ -128,7 +142,7 @@ do
|
|||||||
if [ $? != 0 ]; then
|
if [ $? != 0 ]; then
|
||||||
rm -rf $arg/tmp.pem
|
rm -rf $arg/tmp.pem
|
||||||
echo "Getting certificate for \"$NAME\" FAILED!"
|
echo "Getting certificate for \"$NAME\" FAILED!"
|
||||||
continue
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -s "$arg/tmp.pem" ]; then
|
if [ -s "$arg/tmp.pem" ]; then
|
||||||
@ -136,12 +150,10 @@ do
|
|||||||
else
|
else
|
||||||
echo "New certificate for \"$NAME\" doesn't exists or is empty"
|
echo "New certificate for \"$NAME\" doesn't exists or is empty"
|
||||||
rm -rf "$arg/tmp.pem"
|
rm -rf "$arg/tmp.pem"
|
||||||
continue
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Certificate for \"$NAME\" successfully created!"
|
return 0
|
||||||
counter=$((counter+1))
|
)
|
||||||
|
|
||||||
done
|
main "$@"; exit
|
||||||
|
|
||||||
echo "$counter new certificates created!"
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user