remote.sh 4.0 KB

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