remote.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Check if script already running or not
  2. is_procces_running() {
  3. SCRIPT=$(basename $0)
  4. for pid in $(pidof -x $SCRIPT); do
  5. if [ $pid != $$ ]; then
  6. check_result $E_INUSE "$SCRIPT is already running"
  7. fi
  8. done
  9. }
  10. send_api_cmd() {
  11. answer=$(curl -s -k \
  12. --data-urlencode "user=$USER" \
  13. --data-urlencode "password=$PASSWORD" \
  14. --data-urlencode "returncode=yes" \
  15. --data-urlencode "cmd=$1" \
  16. --data-urlencode "arg1=$2" \
  17. --data-urlencode "arg2=$3" \
  18. --data-urlencode "arg3=$4" \
  19. --data-urlencode "arg4=$5" \
  20. --data-urlencode "arg5=$6" \
  21. --data-urlencode "arg6=$7" \
  22. --data-urlencode "arg7=$8" \
  23. --data-urlencode "arg8=$9" \
  24. https://$HOST:$PORT/api/)
  25. return $answer
  26. }
  27. send_api_file() {
  28. answer=$(curl -s -k \
  29. --data-urlencode "user=$USER" \
  30. --data-urlencode "password=$PASSWORD" \
  31. --data-urlencode "returncode=yes" \
  32. --data-urlencode "cmd=v-make-tmp-file" \
  33. --data-urlencode "arg1=$(cat $1)" \
  34. --data-urlencode "arg2=$2" \
  35. https://$HOST:$PORT/api/)
  36. return $answer
  37. }
  38. send_ssh_cmd() {
  39. if [ -z "$IDENTITY_FILE" ] && [ "$USER" = 'root' ]; then
  40. IDENTITY_FILE="/root/.ssh/id_rsa"
  41. fi
  42. if [ -z "$IDENTITY_FILE" ]; then
  43. IDENTITY_FILE="/home/$USER/.ssh/id_rsa"
  44. fi
  45. if [ "$USER" = 'root' ]; then
  46. args="$VESTA/bin/$1 \"$2\" \"$3\" \"$4\" \"$5\""
  47. else
  48. args="sudo $VESTA/bin/$1 \"$2\" \"$3\" \"$4\" \"$5\""
  49. fi
  50. ssh -i $IDENTITY_FILE $USER@$HOST -p $PORT "$args" > /dev/null 2>&1
  51. if [ "$?" -ne '0' ]; then
  52. return 1
  53. else
  54. return 0
  55. fi
  56. }
  57. send_scp_file() {
  58. if [ -z "$IDENTITY_FILE" ]; then
  59. IDENTITY_FILE="/home/admin/.ssh/id_rsa"
  60. fi
  61. scp -P $PORT -i $IDENTITY_FILE $1 $USER@$HOST:$2 > /dev/null 2>&1
  62. if [ "$?" -ne '0' ]; then
  63. return 1
  64. else
  65. return 0
  66. fi
  67. }
  68. is_dnshost_new() {
  69. if [ -e "$VESTA/conf/dns-cluster.conf" ]; then
  70. check_host=$(grep "HOST='$host'" $VESTA/conf/dns-cluster.conf)
  71. if [ ! -z "$check_host" ]; then
  72. check_result $E_EXISTS "remote dns host $host exists"
  73. fi
  74. fi
  75. }
  76. is_dnshost_alive() {
  77. cluster_cmd v-list-sys-config
  78. check_result $? "$type connection to $HOST failed" $E_CONNECT
  79. cluster_cmd v-list-user $DNS_USER
  80. check_result $? "$DNS_USER doesn't exist" $E_CONNECT
  81. }
  82. remote_dns_health_check() {
  83. OLD_IFS="$IFS"
  84. IFS=$'\n'
  85. # Starting health-check
  86. for str in $(grep "SUSPENDED='no'" $VESTA/conf/dns-cluster.conf); do
  87. eval $str
  88. # Checking host connection
  89. cluster_cmd v-list-user $DNS_USER
  90. if [ $? -ne 0 ]; then
  91. # Creating error report
  92. tmpfile=$(mktemp)
  93. echo "$(basename $0) $*" > $tmpfile
  94. echo -e "Error: $TYPE connection to $HOST failed.\n" >> $tmpfile
  95. echo -n "Remote dns host has been suspended." >> $tmpfile
  96. echo -n "After resolving issue run " >> $tmpfile
  97. echo -e "following commands:\n" >> $tmpfile
  98. echo "v-unsuspend-remote-dns-host $HOST" >> $tmpfile
  99. echo "v-sync-dns-cluster $HOST" >> $tmpfile
  100. echo -e "\n\n--\nVesta Control Panel\n$(hostname)" >> $tmpfile
  101. if [ "$1" = 'no_email' ]; then
  102. cat $tmpfile
  103. else
  104. subj="DNS sync failed"
  105. email=$($BIN/v-get-user-value admin CONTACT)
  106. cat $tmpfile |$SENDMAIL -s "$subj" $email
  107. fi
  108. # Deleting tmp file
  109. rm -f $tmpfile
  110. log_event "$E_CONNECT" "$ARGUMENTS"
  111. # Suspending remote host
  112. dconf="../../conf/dns-cluster"
  113. update_object_value "$dconf" 'HOST' "$HOST" '$SUSPENDED' 'yes'
  114. fi
  115. done
  116. IFS="$OLD_IFS"
  117. }
  118. cluster_cmd() {
  119. case $TYPE in
  120. ssh) send_ssh_cmd $* ;;
  121. api) send_api_cmd $* ;;
  122. esac
  123. }
  124. cluster_file() {
  125. case $TYPE in
  126. ssh) send_scp_file $* ;;
  127. api) send_api_file $* ;;
  128. esac
  129. }