All pastes #1238084 Raw Edit

Unnamed

public text v1 · immutable
#1238084 ·published 2008-10-27 20:25 UTC
rendered paste body
#$Id: fcgi_funcs 38 2008-10-27 16:25:47Z valera $
# functions for parsing configuration file
get_hosts() {
	result=""
	y=$(awk -F = '/^host_[0-9][0-9]?=/ {print $2}' ${CONFIG})
	for x in ${y}; do
		result=${result}" "$x
	done
	echo $result
}

get_identifier() {
	host=$1
	cmd="awk -F = '/^host_[0-9][0-9]?=$host[ ]?\$/ {print \$1}' ${CONFIG}"
	x=$(eval $cmd)
	if [ "X"${x} = "X" ]; then
		echo "Error: cant find specifier"
		exit 1
	fi
	echo $x
}

get_dir() {
	host=$1
	x=$(get_identifier $host)
	cmd="echo \$${x}_path"
	dir=$(eval $cmd)
	if [ "X"${dir} = "X" ]; then
		echo "Error: cant find dir for host $host"
		exit 1
	fi
	echo $dir
}

get_proto() {
	host=$1
	x=$(get_identifier $host)
	cmd="echo \$${x}_proto"
	dir=$(eval $cmd)
	if [ "X"${dir} = "X" ]; then
		echo "Error: cant find proto for host $host"
		exit 1
	fi
	echo $dir
}

get_user() {
	host=$1
	x=$(get_identifier $host)
	cmd="echo \$${x}_user"
	user=$(eval $cmd)
	if [ "X"${user} = "X" ]; then
		echo "Error: for host $host chroot user not specified"
		exit 1
	fi
	echo $user
}

get_port() {
	host=$1
	x=$(get_identifier $host)
	cmd="echo \$${x}_port"
	user=$(eval $cmd)
	if [ "X"${user} = "X" ]; then
		echo "Error: for host $host cant find bind port"
		exit 1
	fi
	echo $user
}

get_load() {
	host=$1
	x=$(get_identifier $host)
	cmd="echo \$${x}_load"
	load=$(eval $cmd)
	if [ "X"${load} = "X" ]; then
		echo "Error: for host $host cant find load"
		exit 1
	fi
	echo $load
}

get_method() {
	host=$1
	x=$(get_load $host)
	if [ "X"${x} = "X" ]; then
		echo "Error: for host $host cant find method (thread or fork)"
		exit 1
	elif [ ${x} = "L" ]; then
		echo "threaded"
	elif [ ${x} = "M" ]; then	
		echo "threaded"
	elif [ ${x} = "H" ]; then
		echo "prefork"
	fi		
}

get_min() {
	host=$1
	x=$(get_load $host)
	if [ "X"${x} = "X" ]; then
		echo "Error: for host $host cant find load"
		exit 1
	elif [ ${x} = "L" ]; then
		echo "20"
	elif [ ${x} = "M" ]; then	
		echo "40"
	elif [ ${x} = "H" ]; then
		echo "20"
	fi		
}

get_max() {
	host=$1
	x=$(get_load $host)
	if [ "X"${x} = "X" ]; then
		echo "Error: for host $host cant find load"
		exit 1;
	elif [ ${x} = "L" ]; then
		echo "20"
	elif [ ${x} = "M" ]; then	
		echo "40"
	elif [ ${x} = "H" ]; then
		echo "20"
	fi		
}
# functions for various operations
host_status() {
	host=$1
	dir=$(get_dir $host) 2>&-
	pid=$(cd $dir && cat ${host}.pid 2>&-)
	if [ "X"${pid} = "X" ]; then
		echo "Status: $host is down"
		exit 0;
	fi
	check=$(ps aux|awk '{print $2}'| grep $pid)
	if [ "X"${check} = "X" ]; then
		echo "Status: $host is down"
	else
		echo "Status: $host is up and running pid $pid"
	fi
}

host_start() {
	host=$1
	dir=$(get_dir $host)
	user=$(get_user $host)
	port=$(get_port $host)
	method=$(get_method $host)
	min=$(get_min $host)
	max=$(get_max $host)
	proto=$(get_proto $host)
# check	 to prevent double starts
	pid=$(cd $dir && cat ${host}.pid 2>&-) 2>&-
# we are running of freebsd too
	SU=$(which su)
	if [ "X""$pid" != "X" ] ; then
		check=$(ps ax | grep python |grep $pid)
		if [ "X""$check" != "X" ]; then
			echo "Error: $host is already running pid - $pid"
			exit 1;
		fi
	fi
	
# check	 that such directory exists
	if [ ! -d ${dir} ]; then
		echo "Error: cant start $host, $dir is not accessible"
		exit 1;
	fi
	if [ ${AS_ADMIN} = "Yes" ]; then
		cmd="$SU $user -c \"cd ${dir} ; ./manage.py runfcgi daemonize=true protocol=$proto  maxspare=$max minspare=$min method=$method host=$BIND_TO port=$port  outlog=${dir}/$OUTLOG errlog=${dir}/$OUTLOG pidfile=${dir}/${host}.pid \"" 
	else
		cmd="cd ${dir} ; ./manage.py runfcgi daemonize=true protocol=$proto  maxspare=$max minspare=$min method=$method host=$BIND_TO port=$port  outlog=${dir}/$OUTLOG errlog=${dir}/$OUTLOG  pidfile=${dir}/${host}.pid" 
	fi
	eval $cmd 
	result=$?
	if [ $result = 0 ] ; then 
		echo "Site $host started"
	else
		echo "Error: cant start $host"
		exit 1;
	fi
}

host_stop() {
	host=$1
	dir=$(get_dir $host)
	user=$(get_user $host)
	port=$(get_port $host)
	pid=$(cd $dir && cat ${host}.pid 2>&-)
# we are running of freebsd too
	SU=$(which su)
	if [ X"$pid" = "X"  ]; then
		echo "Error: cant find pid $host"
	else 	
		if [ ${AS_ADMIN} = "Yes" ]; then
			cmd="$SU $user -c \"kill -KILL $pid \"" 
			eval $cmd	
		else
			kill -KILL $pid 
		fi
		if [ $? != 0 ]; then
			echo "Error: cant stop host $host"
		else
			if [ ${AS_ADMIN} = "Yes" ]; then
				cmd="$SU  $user  -c \"cd $dir; rm ${host}.pid \"" 
				eval $cmd
			else
				(cd $dir; rm ${host}.pid) 
			fi
			echo "Site $host stopped"
		fi
	fi
}

host_restart() {
	host=$1
	host_stop $host
	host_start $host
}

# functions for bulk operations
bulk_stop() {
	HOSTS=$(get_hosts) 
	for app in $HOSTS ; do
		fcgi_manage $app stop
	done

}

bulk_start() {
	HOSTS=$(get_hosts) 
	for app in $HOSTS ; do
		fcgi_manage $app start
	done
}
bulk_status() {
	HOSTS=$(get_hosts) 
	for app in $HOSTS ; do
		fcgi_manage $app status
	done
}

bulk_restart() {
	HOSTS=$(get_hosts) 
	for app in $HOSTS ; do
		fcgi_manage $app restart
	done
}