remote.sh 4.0 KB

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