Просмотр исходного кода

certutil: Add errx helper

Similar as C version of errx.

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
Jan Friesse 1 месяц назад
Родитель
Сommit
efd88f11c4
2 измененных файлов с 37 добавлено и 69 удалено
  1. 23 48
      qdevices/corosync-qdevice-net-certutil.sh
  2. 14 21
      qdevices/corosync-qnetd-certutil.sh

+ 23 - 48
qdevices/corosync-qdevice-net-certutil.sh

@@ -53,6 +53,13 @@ CERTDB_FILES=("cert9.db key4.db pkcs11.txt"
 REMOTE_SHELL_EXECUTABLE="ssh"
 REMOTE_SHELL_EXECUTABLE="ssh"
 REMOTE_COPY_EXECUTABLE="scp"
 REMOTE_COPY_EXECUTABLE="scp"
 
 
+# errx exit_code message
+errx() {
+    echo "$2" >&2
+
+    exit "$1"
+}
+
 usage() {
 usage() {
     echo "$0: [-i|-M|-m|-Q|-r] [-C scp_command] [-c certificate] [-g keysize] [-n cluster_name] [-S ssh_command]"
     echo "$0: [-i|-M|-m|-Q|-r] [-C scp_command] [-c certificate] [-g keysize] [-n cluster_name] [-S ssh_command]"
     echo
     echo
@@ -128,9 +135,7 @@ find_certdb_files() {
 init_node_ca() {
 init_node_ca() {
     cert_files=`find_certdb_files`
     cert_files=`find_certdb_files`
     if [ "$cert_files" != "" ];then
     if [ "$cert_files" != "" ];then
-        echo "Certificate database already exists. Delete it to continue" >&2
-
-        exit 1
+        errx 1 "Certificate database already exists. Delete it to continue"
     fi
     fi
 
 
     if ! [ -d "$DB_DIR" ];then
     if ! [ -d "$DB_DIR" ];then
@@ -147,9 +152,7 @@ init_node_ca() {
     certutil -N -d "$DB_DIR" -f "$PWD_FILE"
     certutil -N -d "$DB_DIR" -f "$PWD_FILE"
     cert_files=`find_certdb_files`
     cert_files=`find_certdb_files`
     if [ "$cert_files" == "" ];then
     if [ "$cert_files" == "" ];then
-        echo "Can't find certificate database files. Certificate database ($DB_DIR) cannot be created" >&2
-
-        exit 1
+        errx 1 "Can't find certificate database files. Certificate database ($DB_DIR) cannot be created"
     fi
     fi
 
 
     for fname in $cert_files;do
     for fname in $cert_files;do
@@ -168,9 +171,7 @@ init_node_ca() {
 gen_cluster_cert_req() {
 gen_cluster_cert_req() {
     cert_files=`find_certdb_files`
     cert_files=`find_certdb_files`
     if [ "$cert_files" == "" ];then
     if [ "$cert_files" == "" ];then
-        echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
-
-        exit 1
+        errx 1 "Certificate database doesn't exists. Use $0 -i to create it"
     fi
     fi
 
 
     echo "Creating new certificate request"
     echo "Creating new certificate request"
@@ -184,9 +185,7 @@ gen_cluster_cert_req() {
 import_signed_cert() {
 import_signed_cert() {
     cert_files=`find_certdb_files`
     cert_files=`find_certdb_files`
     if [ "$cert_files" == "" ];then
     if [ "$cert_files" == "" ];then
-        echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
-
-        exit 1
+        errx 1 "Certificate database doesn't exists. Use $0 -i to create it"
     fi
     fi
 
 
     echo "Importing signed cluster certificate"
     echo "Importing signed cluster certificate"
@@ -200,9 +199,7 @@ import_signed_cert() {
 import_pk12() {
 import_pk12() {
     cert_files=`find_certdb_files`
     cert_files=`find_certdb_files`
     if [ "$cert_files" == "" ];then
     if [ "$cert_files" == "" ];then
-        echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
-
-        exit 1
+        errx 1 "Certificate database doesn't exists. Use $0 -i to create it"
     fi
     fi
 
 
     echo "Importing cluster certificate and key"
     echo "Importing cluster certificate and key"
@@ -230,15 +227,11 @@ quick_start() {
     # Sanity check
     # Sanity check
     for i in "$master_node" $other_nodes;do
     for i in "$master_node" $other_nodes;do
         if $REMOTE_SHELL_EXECUTABLE root@$i "[ -d \"$DB_DIR\" ]";then
         if $REMOTE_SHELL_EXECUTABLE root@$i "[ -d \"$DB_DIR\" ]";then
-            echo "Node $i seems to be already initialized. Please delete $DB_DIR" >&2
-
-            exit 1
+            errx 1 "Node $i seems to be already initialized. Please delete $DB_DIR"
         fi
         fi
 
 
         if ! $REMOTE_SHELL_EXECUTABLE "root@$i" "$0" > /dev/null;then
         if ! $REMOTE_SHELL_EXECUTABLE "root@$i" "$0" > /dev/null;then
-            echo "Node $i doesn't have $0 installed" >&2
-
-            exit 1
+            errx 1 "Node $i doesn't have $0 installed"
         fi
         fi
     done
     done
 
 
@@ -329,14 +322,10 @@ while getopts ":hiMmQrC:c:g:n:S:" opt; do
             REMOTE_SHELL_EXECUTABLE="$OPTARG"
             REMOTE_SHELL_EXECUTABLE="$OPTARG"
             ;;
             ;;
         \?)
         \?)
-            echo "Invalid option: -$OPTARG" >&2
-
-            exit 1
+            errx 1 "Invalid option: -$OPTARG"
             ;;
             ;;
         :)
         :)
-            echo "Option -$OPTARG requires an argument." >&2
-
-            exit 1
+            errx 1 "Option -$OPTARG requires an argument."
             ;;
             ;;
    esac
    esac
 done
 done
@@ -346,36 +335,28 @@ done
 case "$OPERATION" in
 case "$OPERATION" in
     "init_node_ca")
     "init_node_ca")
         if ! [ -e "$CERTIFICATE_FILE" ];then
         if ! [ -e "$CERTIFICATE_FILE" ];then
-            echo "Can't open certificate file $CERTIFICATE_FILE" >&2
-
-            exit 2
+            errx 2 "Can't open certificate file $CERTIFICATE_FILE"
         fi
         fi
 
 
         init_node_ca
         init_node_ca
     ;;
     ;;
     "gen_cluster_cert_req")
     "gen_cluster_cert_req")
         if [ "$CLUSTER_NAME" == "" ];then
         if [ "$CLUSTER_NAME" == "" ];then
-            echo "You have to specify cluster name" >&2
-
-            exit 2
+            errx 2 "You have to specify cluster name"
         fi
         fi
 
 
         gen_cluster_cert_req
         gen_cluster_cert_req
     ;;
     ;;
     "import_signed_cert")
     "import_signed_cert")
         if ! [ -e "$CERTIFICATE_FILE" ];then
         if ! [ -e "$CERTIFICATE_FILE" ];then
-            echo "Can't open certificate file $CERTIFICATE_FILE" >&2
-
-            exit 2
+            errx 2 "Can't open certificate file $CERTIFICATE_FILE"
         fi
         fi
 
 
         import_signed_cert
         import_signed_cert
     ;;
     ;;
     "import_pk12")
     "import_pk12")
         if ! [ -e "$CERTIFICATE_FILE" ];then
         if ! [ -e "$CERTIFICATE_FILE" ];then
-            echo "Can't open certificate file $CERTIFICATE_FILE" >&2
-
-            exit 2
+            errx 2 "Can't open certificate file $CERTIFICATE_FILE"
         fi
         fi
 
 
         import_pk12
         import_pk12
@@ -384,25 +365,19 @@ case "$OPERATION" in
         shift $((OPTIND-1))
         shift $((OPTIND-1))
 
 
         if [ "$CLUSTER_NAME" == "" ];then
         if [ "$CLUSTER_NAME" == "" ];then
-            echo "You have to specify cluster name" >&2
-
-            exit 2
+            errx 2 "You have to specify cluster name"
         fi
         fi
 
 
         qnetd_addr=${1:-}
         qnetd_addr=${1:-}
         if [ "$qnetd_addr" == "" ];then
         if [ "$qnetd_addr" == "" ];then
-            echo "No QNetd server address provided." >&2
-
-            exit 2
+            errx 2 "No QNetd server address provided."
         fi
         fi
 
 
         shift 1
         shift 1
 
 
         master_node=${1:-}
         master_node=${1:-}
         if [ "$master_node" == "" ];then
         if [ "$master_node" == "" ];then
-            echo "No nodes provided." >&2
-
-            exit 2
+            errx 2 "No nodes provided."
         fi
         fi
 
 
         shift 1
         shift 1

+ 14 - 21
qdevices/corosync-qnetd-certutil.sh

@@ -49,6 +49,13 @@ CA_EXPORT_FILE="$DB_DIR/qnetd-cacert.crt"
 CERTDB_FILES=("cert9.db key4.db pkcs11.txt"
 CERTDB_FILES=("cert9.db key4.db pkcs11.txt"
               "cert8.db key3.db secmod.db")
               "cert8.db key3.db secmod.db")
 
 
+# errx exit_code message
+errx() {
+    echo "$2" >&2
+
+    exit "$1"
+}
+
 usage() {
 usage() {
     echo "$0: [-i|-s] [-c certificate] [-G] [-g keysize] [-n cluster_name]"
     echo "$0: [-i|-s] [-c certificate] [-G] [-g keysize] [-n cluster_name]"
     echo
     echo
@@ -134,9 +141,7 @@ get_certutil_key_params() {
 init_qnetd_ca() {
 init_qnetd_ca() {
     cert_files=`find_certdb_files`
     cert_files=`find_certdb_files`
     if [ "$cert_files" != "" ];then
     if [ "$cert_files" != "" ];then
-        echo "Certificate database ($DB_DIR) already exists. Delete it to initialize new db" >&2
-
-        exit 1
+        errx 1 "Certificate database ($DB_DIR) already exists. Delete it to initialize new db"
     fi
     fi
 
 
     if ! [ -d "$DB_DIR" ];then
     if ! [ -d "$DB_DIR" ];then
@@ -154,9 +159,7 @@ init_qnetd_ca() {
     certutil -N -d "$DB_DIR" -f "$PWD_FILE"
     certutil -N -d "$DB_DIR" -f "$PWD_FILE"
     cert_files=`find_certdb_files`
     cert_files=`find_certdb_files`
     if [ "$cert_files" == "" ];then
     if [ "$cert_files" == "" ];then
-        echo "Can't find certificate database files. Certificate database ($DB_DIR) cannot be created" >&2
-
-        exit 1
+        errx 1 "Can't find certificate database files. Certificate database ($DB_DIR) cannot be created"
     fi
     fi
 
 
     for fname in $cert_files;do
     for fname in $cert_files;do
@@ -187,9 +190,7 @@ init_qnetd_ca() {
 sign_cluster_cert() {
 sign_cluster_cert() {
     cert_files=`find_certdb_files`
     cert_files=`find_certdb_files`
     if [ "$cert_files" == "" ];then
     if [ "$cert_files" == "" ];then
-        echo "Certificate database doesn't exists. Use $0 -i to create it" >&2
-
-        exit 1
+        errx 1 "Certificate database doesn't exists. Use $0 -i to create it"
     fi
     fi
 
 
     echo "Signing cluster certificate"
     echo "Signing cluster certificate"
@@ -240,14 +241,10 @@ while getopts ":Ghisc:g:n:" opt; do
             CLUSTER_NAME="$OPTARG"
             CLUSTER_NAME="$OPTARG"
             ;;
             ;;
         \?)
         \?)
-            echo "Invalid option: -$OPTARG" >&2
-
-            exit 1
+            errx 1 "Invalid option: -$OPTARG"
             ;;
             ;;
         :)
         :)
-            echo "Option -$OPTARG requires an argument." >&2
-
-            exit 1
+            errx 1 "Option -$OPTARG requires an argument."
             ;;
             ;;
    esac
    esac
 done
 done
@@ -262,15 +259,11 @@ case "$OPERATION" in
     ;;
     ;;
     "sign_cluster_cert")
     "sign_cluster_cert")
         if ! [ -e "$CERTIFICATE_FILE" ];then
         if ! [ -e "$CERTIFICATE_FILE" ];then
-            echo "Can't open certificate file $CERTIFICATE_FILE" >&2
-
-            exit 2
+            errx 2 "Can't open certificate file $CERTIFICATE_FILE"
         fi
         fi
 
 
         if [ "$CLUSTER_NAME" == "" ];then
         if [ "$CLUSTER_NAME" == "" ];then
-            echo "You have to specify cluster name" >&2
-
-            exit 2
+            errx 2 "You have to specify cluster name"
         fi
         fi
 
 
         sign_cluster_cert
         sign_cluster_cert