tx2_update_faster.sh 2.35 KB
#!/bin/bash
ori_dir=$(cd $(dirname $0);pwd)
cd $ori_dir
list_name=$1
file=$2

echo -e "\033[41;33mClean up residual of ansible tools, syslog and VAServer!\033[0m"
ansible $list_name -b -m raw -a "if [ -d /home/ubuntu/.ansible/tmp ];then rm -r /home/ubuntu/.ansible/tmp;fi;if [ -d /root/.ansible/tmp ];then rm -r /root/.ansible/tmp;fi;size=\$(du -sm /var/log|awk '{print \$1}');if [ \$size -gt 1000 ];then echo clean syslog;cat /dev/null > /var/log/syslog;cat /dev/null > /var/log/syslog.1;fi;cd /root/UserApp/services/analysis_service;if [ -d VAServer ];then rm -r VAServer;fi;pkill -9 VAServer"

echo -e "\033[41;33mCopy the analyzer to the child node!\033[0m"
chmod +x check_vsftp.sh
stop_flag=`./check_vsftp.sh 0`

ansible $list_name --list-hosts|egrep -o [0-9]+.[0-9]+.[0-9]+.[0-9]+ > hosts_a
cat /dev/null > hosts_b

if [[ ! -s hosts_a ]]
then
	echo Please check /etc/ansible/hosts!
	exit 0
fi

chmod 666 $file
first_host=`head -n 1 hosts_a`
self_host=`ip addr|grep inet|awk '{print $2}'|egrep -o [0-9]+.[0-9]+.[0-9]+.[0-9]+|grep ${first_host%.*}'\.'`
echo $self_host >> hosts_b

start=`date +%s`
while [[ -s hosts_a ]]
do
	for host in `cat hosts_b`
	do
		if [[ -s hosts_a ]]
		then
			now_first=`head -n 1 hosts_a`
			sed -i '1d' hosts_a
			flag=0
			#echo $now_first >> hosts_b
			{
				result=`ansible $now_first -b -m raw -a "\
				mkdir -p /home/ubuntu/tx2_update;\
				cd /home/ubuntu/tx2_update;\
				wget --ftp-user=ubuntu --ftp-password=ubuntu ftp://$self_host/tx2_update/check_vsftp.sh;\
				chmod +x check_vsftp.sh;./check_vsftp.sh 1 $self_host;rm check_vsftp.sh;\
				wget --ftp-user=ubuntu --ftp-password=ubuntu ftp://$host/tx2_update/$file;\
				chmod 666 $file"`
				# echo "$result"
				if [[ $result =~ 'CHANGED' ]]
				then
					printf "%-35s : %-8s\n" "$host --> $now_first" "SUCCESS!"
					echo $now_first >> hosts_b
				else
					printf "%-35s : %-8s\n" "$host --> $now_first" "FAIL!"
				fi
			} &
		fi
	done
	wait
done
end=`date +%s`
let use=end-start
echo Time-consuming: $use's'
rm hosts_a hosts_b

echo -e "\033[41;33mDelete junk files and restart the analyzer!\033[0m" 
ansible $list_name -b -m raw -a "cd /home/ubuntu/tx2_update;tar -zxf $file -C  /root/UserApp/services/analysis_service/;rm $file;systemctl stop vsftpd;rm -r /home/ubuntu/tx2_update;cat /root/UserApp/services/analysis_service/VAServer/last_version"

if [[ -z $stop_flag ]];
then
	systemctl stop vsftpd
fi