Răsfoiți Sursa

Improved remote API

Serghey Rodin 10 ani în urmă
părinte
comite
7b0824015f
2 a modificat fișierele cu 64 adăugiri și 86 ștergeri
  1. 53 84
      func/remote.sh
  2. 11 2
      web/api/index.php

+ 53 - 84
func/remote.sh

@@ -1,11 +1,4 @@
 send_api_cmd() {
-    if [ -z $PORT ]; then
-        PORT=8083
-    fi
-    if [ -z $USER ]; then
-        USER=admin
-    fi
-
     answer=$(curl -s -k \
         --data-urlencode "user=$USER" \
         --data-urlencode "password=$PASSWORD" \
@@ -20,21 +13,22 @@ send_api_cmd() {
         --data-urlencode "arg7=$8" \
         --data-urlencode "arg8=$9" \
         https://$HOST:$PORT/api/)
+    return $answer
+}
 
-    if [ "$answer" != '0' ]; then
-        return 1
-    else
-        return 0
-    fi
+send_api_file() {
+    answer=$(curl -s -k \
+        --data-urlencode "user=$USER" \
+        --data-urlencode "password=$PASSWORD" \
+        --data-urlencode "returncode=yes" \
+        --data-urlencode "cmd=v-make-tmp-file" \
+        --data-urlencode "arg1=$(cat $1)" \
+        --data-urlencode "arg2=$2" \
+        https://$HOST:$PORT/api/)
+    return $answer
 }
 
 send_ssh_cmd() {
-    if [ -z $PORT ]; then
-        PORT=22
-    fi
-    if [ -z $USER ]; then
-        USER=admin
-    fi
     if [ -z "$IDENTITY_FILE" ] && [ "$USER" = 'root' ]; then
         IDENTITY_FILE="/root/.ssh/id_rsa"
     fi
@@ -55,13 +49,7 @@ send_ssh_cmd() {
     fi
 }
 
-scp_cmd() {
-    if [ -z $PORT ]; then
-        PORT=22
-    fi
-    if [ -z $USER ]; then
-        USER=admin
-    fi
+send_scp_file() {
     if [ -z "$IDENTITY_FILE" ]; then
         IDENTITY_FILE="/home/admin/.ssh/id_rsa"
     fi
@@ -77,75 +65,33 @@ is_dnshost_new() {
     if [ -e "$VESTA/conf/dns-cluster.conf" ]; then
         check_host=$(grep "HOST='$host'" $VESTA/conf/dns-cluster.conf)
         if [ ! -z "$check_host" ]; then
-            echo "Error: dns host $host exists"
-            log_event "$E_EXISTS" "$EVENT"
-            exit $E_EXISTS
+            check_result $E_EXISTS "remote dns host $host exists"
         fi
     fi
 }
 
 is_dnshost_alive() {
-    HOST=$host
-    PORT=$port
-    USER=$user
-    PASSWORD=$password
-
-    # Switch on connection type
-    case $type in
-        ssh) send_cmd="send_ssh_cmd" ;;
-        *)  send_cmd="send_api_cmd" ;;
-    esac
+    cluster_cmd v-list-sys-config
+    check_result $? "$type connection to $HOST failed" $E_CONNECT
 
-    # Check host connection
-    $send_cmd v-list-sys-config
-    if [ $? -ne 0 ]; then
-        echo "Error: $type connection to $HOST failed"
-        log_event "$E_CONNECT" "$EVENT"
-        exit $E_CONNECT
-    fi
-
-    # Check recipient dns user
-    if [ -z "$DNS_USER" ]; then
-        DNS_USER='dns-cluster'
-    fi
-    if [ ! -z "$verbose" ]; then
-        echo "DNS_USER: $DNS_USER"
-    fi
-    $send_cmd v-list-user $DNS_USER
-    if [ $? -ne 0 ]; then
-        echo "Error: dns user $DNS_USER doesn't exist"
-        log_event "$E_NOTEXIST" "$EVENT"
-        exit $E_NOTEXIST
-    fi
+    cluster_cmd v-list-user $DNS_USER
+    check_result $? "$DNS_USER doesn't exist" $E_CONNECT
 }
 
 remote_dns_health_check() {
-    # Define tmp mail vars
-    subj="DNS sync failed"
-    email=$(grep CONTACT $VESTA/data/users/admin/user.conf | cut -f 2 -d \')
-    send_mail="$VESTA/web/inc/mail-wrapper.php"
-    tmpfile=$(mktemp)
+    OLD_IFS="$IFS"
+    IFS=$'\n'
 
     # Starting health-check
     for str in $(grep "SUSPENDED='no'" $VESTA/conf/dns-cluster.conf); do
-
-        # Get host values
         eval $str
 
-        # Check connection type
-        if [ -z "TYPE" ]; then
-            TYPE='api'
-        fi
-
-        # Switch on connection type
-        case $TYPE in
-            ssh) send_cmd="send_ssh_cmd" ;;
-            *)  send_cmd="send_api_cmd" ;;
-        esac
-
-        # Check host connection
-        $send_cmd v-list-sys-config
+        # Checking host connection
+        cluster_cmd v-list-user $DNS_USER
         if [ $? -ne 0 ]; then
+
+            # Creating error report
+            tmpfile=$(mktemp)
             echo "$(basename $0) $*" > $tmpfile
             echo -e "Error: $TYPE connection to $HOST failed.\n" >> $tmpfile
             echo -n "Remote dns host has been suspended." >> $tmpfile
@@ -154,14 +100,37 @@ remote_dns_health_check() {
             echo "v-unsuspend-remote-dns-host $HOST" >> $tmpfile
             echo "v-sync-dns-cluster $HOST" >> $tmpfile
             echo -e "\n\n--\nVesta Control Panel\n$(hostname)" >> $tmpfile
-            cat $tmpfile |  $send_mail -s "$subj" $email
 
+            if [ "$1" = 'no_email' ]; then
+                cat $tmpfile
+            else
+                subj="DNS sync failed"
+                email=$($BIN/v-get-user-value admin CONTACT)
+                cat $tmpfile |$send_mail -s "$subj" $email
+            fi
+
+            # Deleting tmp file
+            rm -f $tmpfile
             log_event "$E_CONNECT" "$EVENT"
-            dconf="../../../conf/dns-cluster"
+
+            # Suspending remote host
+            dconf="../../conf/dns-cluster"
             update_object_value "$dconf" 'HOST' "$HOST" '$SUSPENDED' 'yes'
         fi
-
-        # Remove tmp file
-        rm -f $tmpfile
     done
+    IFS="$OLD_IFS"
+}
+
+cluster_cmd() {
+    case $TYPE in
+        ssh)    send_ssh_cmd $* ;;
+        api)    send_api_cmd $* ;;
+    esac
+}
+
+cluster_file() {
+    case $TYPE in
+        ssh)    send_scp_file $* ;;
+        api)    send_api_file $* ;;
+    esac
 }

+ 11 - 2
web/api/index.php

@@ -65,8 +65,17 @@ if (isset($_POST['user']) || isset($_POST['hash'])) {
     if(!empty($arg9)){
          $cmdquery = $cmdquery.$arg9; }
 
-    // Run query
-    exec ($cmdquery, $output, $return_var);
+    // Check command
+    if ($cmd == "'v-make-tmp-file'") {
+        // Used in DNS Cluster
+        $fp = fopen($_POST['arg2'], 'w');
+        fwrite($fp, $_POST['arg1']."\n");
+        fclose($fp);
+        $return_var = 0;
+    } else {
+        // Run normal cmd query
+        exec ($cmdquery, $output, $return_var);
+    }
 
     if ((!empty($_POST['returncode'])) && ($_POST['returncode'] == 'yes')) {
         echo $return_var;