Improved remote API

This commit is contained in:
Serghey Rodin 2015-10-21 15:49:03 +03:00
commit 7b0824015f
2 changed files with 64 additions and 86 deletions

View file

@ -1,11 +1,4 @@
send_api_cmd() { send_api_cmd() {
if [ -z $PORT ]; then
PORT=8083
fi
if [ -z $USER ]; then
USER=admin
fi
answer=$(curl -s -k \ answer=$(curl -s -k \
--data-urlencode "user=$USER" \ --data-urlencode "user=$USER" \
--data-urlencode "password=$PASSWORD" \ --data-urlencode "password=$PASSWORD" \
@ -20,21 +13,22 @@ send_api_cmd() {
--data-urlencode "arg7=$8" \ --data-urlencode "arg7=$8" \
--data-urlencode "arg8=$9" \ --data-urlencode "arg8=$9" \
https://$HOST:$PORT/api/) https://$HOST:$PORT/api/)
return $answer
}
if [ "$answer" != '0' ]; then send_api_file() {
return 1 answer=$(curl -s -k \
else --data-urlencode "user=$USER" \
return 0 --data-urlencode "password=$PASSWORD" \
fi --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() { send_ssh_cmd() {
if [ -z $PORT ]; then
PORT=22
fi
if [ -z $USER ]; then
USER=admin
fi
if [ -z "$IDENTITY_FILE" ] && [ "$USER" = 'root' ]; then if [ -z "$IDENTITY_FILE" ] && [ "$USER" = 'root' ]; then
IDENTITY_FILE="/root/.ssh/id_rsa" IDENTITY_FILE="/root/.ssh/id_rsa"
fi fi
@ -55,13 +49,7 @@ send_ssh_cmd() {
fi fi
} }
scp_cmd() { send_scp_file() {
if [ -z $PORT ]; then
PORT=22
fi
if [ -z $USER ]; then
USER=admin
fi
if [ -z "$IDENTITY_FILE" ]; then if [ -z "$IDENTITY_FILE" ]; then
IDENTITY_FILE="/home/admin/.ssh/id_rsa" IDENTITY_FILE="/home/admin/.ssh/id_rsa"
fi fi
@ -77,75 +65,33 @@ is_dnshost_new() {
if [ -e "$VESTA/conf/dns-cluster.conf" ]; then if [ -e "$VESTA/conf/dns-cluster.conf" ]; then
check_host=$(grep "HOST='$host'" $VESTA/conf/dns-cluster.conf) check_host=$(grep "HOST='$host'" $VESTA/conf/dns-cluster.conf)
if [ ! -z "$check_host" ]; then if [ ! -z "$check_host" ]; then
echo "Error: dns host $host exists" check_result $E_EXISTS "remote dns host $host exists"
log_event "$E_EXISTS" "$EVENT"
exit $E_EXISTS
fi fi
fi fi
} }
is_dnshost_alive() { is_dnshost_alive() {
HOST=$host cluster_cmd v-list-sys-config
PORT=$port check_result $? "$type connection to $HOST failed" $E_CONNECT
USER=$user
PASSWORD=$password
# Switch on connection type cluster_cmd v-list-user $DNS_USER
case $type in check_result $? "$DNS_USER doesn't exist" $E_CONNECT
ssh) send_cmd="send_ssh_cmd" ;;
*) send_cmd="send_api_cmd" ;;
esac
# Check host connection
$send_cmd v-list-sys-config
if [ $? -ne 0 ]; then
echo "Error: $type connection to $HOST failed"
log_event "$E_CONNECT" "$EVENT"
exit $E_CONNECT
fi
# Check recipient dns user
if [ -z "$DNS_USER" ]; then
DNS_USER='dns-cluster'
fi
if [ ! -z "$verbose" ]; then
echo "DNS_USER: $DNS_USER"
fi
$send_cmd v-list-user $DNS_USER
if [ $? -ne 0 ]; then
echo "Error: dns user $DNS_USER doesn't exist"
log_event "$E_NOTEXIST" "$EVENT"
exit $E_NOTEXIST
fi
} }
remote_dns_health_check() { remote_dns_health_check() {
# Define tmp mail vars OLD_IFS="$IFS"
subj="DNS sync failed" IFS=$'\n'
email=$(grep CONTACT $VESTA/data/users/admin/user.conf | cut -f 2 -d \')
send_mail="$VESTA/web/inc/mail-wrapper.php"
tmpfile=$(mktemp)
# Starting health-check # Starting health-check
for str in $(grep "SUSPENDED='no'" $VESTA/conf/dns-cluster.conf); do for str in $(grep "SUSPENDED='no'" $VESTA/conf/dns-cluster.conf); do
# Get host values
eval $str eval $str
# Check connection type # Checking host connection
if [ -z "TYPE" ]; then cluster_cmd v-list-user $DNS_USER
TYPE='api'
fi
# Switch on connection type
case $TYPE in
ssh) send_cmd="send_ssh_cmd" ;;
*) send_cmd="send_api_cmd" ;;
esac
# Check host connection
$send_cmd v-list-sys-config
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
# Creating error report
tmpfile=$(mktemp)
echo "$(basename $0) $*" > $tmpfile echo "$(basename $0) $*" > $tmpfile
echo -e "Error: $TYPE connection to $HOST failed.\n" >> $tmpfile echo -e "Error: $TYPE connection to $HOST failed.\n" >> $tmpfile
echo -n "Remote dns host has been suspended." >> $tmpfile echo -n "Remote dns host has been suspended." >> $tmpfile
@ -154,14 +100,37 @@ remote_dns_health_check() {
echo "v-unsuspend-remote-dns-host $HOST" >> $tmpfile echo "v-unsuspend-remote-dns-host $HOST" >> $tmpfile
echo "v-sync-dns-cluster $HOST" >> $tmpfile echo "v-sync-dns-cluster $HOST" >> $tmpfile
echo -e "\n\n--\nVesta Control Panel\n$(hostname)" >> $tmpfile echo -e "\n\n--\nVesta Control Panel\n$(hostname)" >> $tmpfile
cat $tmpfile | $send_mail -s "$subj" $email
if [ "$1" = 'no_email' ]; then
cat $tmpfile
else
subj="DNS sync failed"
email=$($BIN/v-get-user-value admin CONTACT)
cat $tmpfile |$send_mail -s "$subj" $email
fi
# Deleting tmp file
rm -f $tmpfile
log_event "$E_CONNECT" "$EVENT" log_event "$E_CONNECT" "$EVENT"
dconf="../../../conf/dns-cluster"
# Suspending remote host
dconf="../../conf/dns-cluster"
update_object_value "$dconf" 'HOST' "$HOST" '$SUSPENDED' 'yes' update_object_value "$dconf" 'HOST' "$HOST" '$SUSPENDED' 'yes'
fi fi
# Remove tmp file
rm -f $tmpfile
done 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
} }

View file

@ -65,8 +65,17 @@ if (isset($_POST['user']) || isset($_POST['hash'])) {
if(!empty($arg9)){ if(!empty($arg9)){
$cmdquery = $cmdquery.$arg9; } $cmdquery = $cmdquery.$arg9; }
// Run query // Check command
exec ($cmdquery, $output, $return_var); if ($cmd == "'v-make-tmp-file'") {
// Used in DNS Cluster
$fp = fopen($_POST['arg2'], 'w');
fwrite($fp, $_POST['arg1']."\n");
fclose($fp);
$return_var = 0;
} else {
// Run normal cmd query
exec ($cmdquery, $output, $return_var);
}
if ((!empty($_POST['returncode'])) && ($_POST['returncode'] == 'yes')) { if ((!empty($_POST['returncode'])) && ($_POST['returncode'] == 'yes')) {
echo $return_var; echo $return_var;