remote.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. send_api_cmd() {
  2. if [ -z $PORT ]; then
  3. PORT=8083
  4. fi
  5. if [ -z $USER ]; then
  6. USER=admin
  7. fi
  8. answer=$(curl -s -k \
  9. --data-urlencode "user=$USER" \
  10. --data-urlencode "password=$PASSWORD" \
  11. --data-urlencode "returncode=yes" \
  12. --data-urlencode "cmd=$1" \
  13. --data-urlencode "arg1=$2" \
  14. --data-urlencode "arg2=$3" \
  15. --data-urlencode "arg3=$4" \
  16. --data-urlencode "arg4=$5" \
  17. --data-urlencode "arg5=$6" \
  18. --data-urlencode "arg6=$7" \
  19. --data-urlencode "arg7=$8" \
  20. --data-urlencode "arg8=$9" \
  21. https://$HOST:$PORT/api/)
  22. if [ "$answer" != '0' ]; then
  23. return 1
  24. else
  25. return 0
  26. fi
  27. }
  28. send_ssh_cmd() {
  29. if [ -z $PORT ]; then
  30. PORT=22
  31. fi
  32. if [ -z $USER ]; then
  33. USER=admin
  34. fi
  35. if [ -z "$IDENTITY_FILE" ] && [ "$USER" = 'root' ]; then
  36. IDENTITY_FILE="/root/.ssh/id_rsa"
  37. fi
  38. if [ -z "$IDENTITY_FILE" ]; then
  39. IDENTITY_FILE="/home/$USER/.ssh/id_rsa"
  40. fi
  41. if [ "$USER" = 'root' ]; then
  42. args="$VESTA/bin/$1 \"$2\" \"$3\" \"$4\" \"$5\""
  43. else
  44. args="sudo $VESTA/bin/$1 \"$2\" \"$3\" \"$4\" \"$5\""
  45. fi
  46. ssh -i $IDENTITY_FILE $USER@$HOST -p $PORT "$args" > /dev/null 2>&1
  47. if [ "$?" -ne '0' ]; then
  48. return 1
  49. else
  50. return 0
  51. fi
  52. }
  53. scp_cmd() {
  54. if [ -z $PORT ]; then
  55. PORT=22
  56. fi
  57. if [ -z $USER ]; then
  58. USER=admin
  59. fi
  60. if [ -z "$IDENTITY_FILE" ]; then
  61. IDENTITY_FILE="/home/admin/.ssh/id_rsa"
  62. fi
  63. scp -P $PORT -i $IDENTITY_FILE $1 $USER@$HOST:$2 > /dev/null 2>&1
  64. if [ "$?" -ne '0' ]; then
  65. return 1
  66. else
  67. return 0
  68. fi
  69. }
  70. is_dnshost_new() {
  71. if [ -e "$VESTA/conf/dns-cluster.conf" ]; then
  72. check_host=$(grep "HOST='$host'" $VESTA/conf/dns-cluster.conf)
  73. if [ ! -z "$check_host" ]; then
  74. echo "Error: dns host $host exists"
  75. log_event "$E_EXISTS" "$EVENT"
  76. exit $E_EXISTS
  77. fi
  78. fi
  79. }
  80. is_dnshost_alive() {
  81. HOST=$host
  82. PORT=$port
  83. USER=$user
  84. PASSWORD=$password
  85. # Switch on connection type
  86. case $type in
  87. ssh) send_cmd="send_ssh_cmd" ;;
  88. *) send_cmd="send_api_cmd" ;;
  89. esac
  90. # Check host connection
  91. $send_cmd v-list-sys-config
  92. if [ $? -ne 0 ]; then
  93. echo "Error: $type connection to $HOST failed"
  94. log_event "$E_CONNECT" "$EVENT"
  95. exit $E_CONNECT
  96. fi
  97. # Check recipient dns user
  98. if [ -z "$DNS_USER" ]; then
  99. DNS_USER='dns-cluster'
  100. fi
  101. if [ ! -z "$verbose" ]; then
  102. echo "DNS_USER: $DNS_USER"
  103. fi
  104. $send_cmd v-list-user $DNS_USER
  105. if [ $? -ne 0 ]; then
  106. echo "Error: dns user $DNS_USER doesn't exist"
  107. log_event "$E_NOTEXIST" "$EVENT"
  108. exit $E_NOTEXIST
  109. fi
  110. }
  111. remote_dns_health_check() {
  112. # Define tmp mail vars
  113. subj="DNS sync failed"
  114. email=$(grep CONTACT $VESTA/data/users/admin/user.conf | cut -f 2 -d \')
  115. send_mail="$VESTA/web/inc/mail-wrapper.php"
  116. tmpfile=$(mktemp)
  117. # Starting health-check
  118. for str in $(grep "SUSPENDED='no'" $VESTA/conf/dns-cluster.conf); do
  119. # Get host values
  120. eval $str
  121. # Check connection type
  122. if [ -z "TYPE" ]; then
  123. TYPE='api'
  124. fi
  125. # Switch on connection type
  126. case $TYPE in
  127. ssh) send_cmd="send_ssh_cmd" ;;
  128. *) send_cmd="send_api_cmd" ;;
  129. esac
  130. # Check host connection
  131. $send_cmd v-list-sys-config
  132. if [ $? -ne 0 ]; then
  133. echo "$(basename $0) $*" > $tmpfile
  134. echo -e "Error: $TYPE connection to $HOST failed.\n" >> $tmpfile
  135. echo -n "Remote dns host has been suspended." >> $tmpfile
  136. echo -n "After resolving issue run " >> $tmpfile
  137. echo -e "following commands:\n" >> $tmpfile
  138. echo "v-unsuspend-remote-dns-host $HOST" >> $tmpfile
  139. echo "v-sync-dns-cluster $HOST" >> $tmpfile
  140. echo -e "\n\n--\nVesta Control Panel\n$(hostname)" >> $tmpfile
  141. cat $tmpfile | $send_mail -s "$subj" $email
  142. log_event "$E_CONNECT" "$EVENT"
  143. dconf="../../../conf/dns-cluster"
  144. update_object_value "$dconf" 'HOST' "$HOST" '$SUSPENDED' 'yes'
  145. fi
  146. # Remove tmp file
  147. rm -f $tmpfile
  148. done
  149. }