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