Skip to content

Instantly share code, notes, and snippets.

@borgle
Created March 15, 2017 09:06
Show Gist options
  • Save borgle/d63a2ed939ab29e88b247dd24965a494 to your computer and use it in GitHub Desktop.
Save borgle/d63a2ed939ab29e88b247dd24965a494 to your computer and use it in GitHub Desktop.
很多时候,团队内每个人都需要测试环境,如果大家都在测试环境上随意捣鼓,那么tomcat, mongodb, redis, zookeeper将是一团麻,这个脚本就是方便大家进行环境初始化和测试的
#!/bin/bash
# global variable
SVR_PORT=0
RUN_USER=$USER
BIN_DIR=$HOME/bin
# resolve links - $0 may be a softlink
ARG0="$0"
while [ -h "$ARG0" ]; do
ls=`ls -ld "$ARG0"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
ARG0="$link"
else
ARG0="`dirname $ARG0`/$link"
fi
done
DIRNAME="`dirname $ARG0`"
PROGRAM="`basename $ARG0`"
readport() {
local SVC_NAME=$1
local SVC_HOST=$2
if [ -z "$SVC_NAME" ]
then
echo "service name must be not empty."
return 0
fi
if [ -z "$SVC_HOST" ]
then
SVC_HOST="0.0.0.0"
fi
while(true)
do
echo -n "Please input $SVC_NAME port > "
read SVR_PORT
if ! [ $SVR_PORT -eq $SVR_PORT 2>/dev/null ] ; then
echo "invaild port number: $SVR_PORT"
continue
fi
if [ $SVR_PORT -gt 65534 ] ; then
echo "invaild port number: $SVR_PORT"
continue
fi
chkport=`netstat -antu | grep "$SVC_HOST:*" | grep "\b$SVR_PORT\b"`
if [ -n "$chkport" ]
then
echo "$SVC_HOST:$SVR_PORT is in use."
else
return 1
fi
done
}
tomcat()
{
JDK_HOME=/usr/lib/jvm/jre-openjdk
BIN_HOME=/opt/apache-tomcat-8.0.41
INS_HOME=$HOME/tomcat
if [ ! -d $INS_HOME ]
then
mkdir -p $INS_HOME
else
echo "The Instance alreadly exists..."
exit 0
fi
cp -r $BIN_HOME/conf $INS_HOME
conf="$INS_HOME/conf/server.xml"
readport 'tomcat'
if [ $? -eq 0 ]
then
exit 0
fi
local webssl_port=$(($SVR_PORT+20000))
local shutdown_port=$(($SVR_PORT+20001))
local ajp_port=$(($SVR_PORT+20002))
sed -i "s/8080/$SVR_PORT/g" `grep 8080 -l $conf`
sed -i "s/8443/$webssl_port/g" `grep 8443 -l $conf`
sed -i "s/8005/$shutdown_port/g" `grep 8005 -l $conf`
sed -i "s/8009/$ajp_port/g" `grep 8009 -l $conf`
mkdir -p $INS_HOME/{logs,temp,webapps/ROOT,work}
cat > $INS_HOME/tomcat.sh << EOF
#!/bin/sh
JAVA_HOME="$JDK_HOME"
JAVA_OPTS="-Xms64m -Xmx128m"
CATALINA_HOME="$BIN_HOME"
CATALINA_BASE="$INS_HOME"
export JAVA_HOME JAVA_OPTS CATALINA_HOME CATALINA_BASE
\$CATALINA_HOME/bin/catalina.sh \$1
EOF
cat > $INS_HOME/webapps/ROOT/index.jsp << EOF
<html>
<head><title>Welcome</title></head>
<body><center>
<h1>This is a new tomcat instance! created by 14444.</h1>
</br>
Now time is: <%=new java.util.Date()%>
</center>
</body></html>
EOF
chmod u+x $INS_HOME/tomcat.sh
chown $RUN_USER:$RUN_USER -R $INS_HOME
# build shortcut
mkdir -p $BIN_DIR
ln -s -f $INS_HOME/tomcat.sh $BIN_DIR
}
redis(){
BIN_HOME=/opt/redis-3.2.8/src
INS_HOME=$HOME/redis
LOG_DIR=$INS_HOME/log
CONF_DIR=$INS_HOME/conf
DATA_DIR=$INS_HOME/data
if [ ! -d $INS_HOME ]
then
mkdir -p $INS_HOME
else
echo "The Instance alreadly exists..."
exit 0
fi
mkdir -p $CONF_DIR
mkdir -p $DATA_DIR
mkdir -p $LOG_DIR
cp -r "$BIN_HOME/../redis.conf" $CONF_DIR
conf="$CONF_DIR/redis.conf"
readport 'redis'
if [ $? -eq 0 ] ; then
exit 0
fi
pidfile=$INS_HOME/redis.pid
# sed -i "s/#requirepass foobared/requirepass ism-redis-passwd/g" `grep requirepass -l $conf`
sed -i "s:^logfile .*$:logfile ${LOG_DIR}/redis.log:g" $conf
sed -i "s:/var/run/redis_6379.pid:${pidfile}:g" $conf
sed -i "s:^dir .*$:dir ${DATA_DIR}:g" $conf
sed -i "s/port 6379/port $SVR_PORT/g" $conf
cat > $INS_HOME/redis.sh <<EOF
#!/bin/sh
REDISPORT=$SVR_PORT
EXEC=$BIN_HOME/redis-server
CLIEXEC=$BIN_HOME/redis-cli
PIDFILE="$pidfile"
CONF="$conf"
case "\$1" in
start)
if [ -f \$PIDFILE ]
then
echo "\$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
\$EXEC \$CONF &
fi
;;
stop)
if [ ! -f \$PIDFILE ]
then
echo "\$PIDFILE does not exist, process is not running"
else
PID=\$(cat \$PIDFILE 2>/dev/null || echo 0)
echo "Stopping ..."
\$CLIEXEC -p \$REDISPORT shutdown
while [ -x /proc/\${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
status)
PID=\$(cat \$PIDFILE 2>/dev/null || echo 0)
if [ ! -x /proc/\${PID} ]
then
echo "Redis is not running"
else
echo "Redis is running (\$PID)"
fi
;;
restart)
\$0 stop
\$0 start
;;
*)
echo "Usage: \$(basename "\$0") {start|stop|restart|status}" >&2
;;
esac
EOF
chmod u+x $INS_HOME/redis.sh
chown $RUN_USER:$RUN_USER -R $INS_HOME
# build shortcut
mkdir -p $BIN_DIR
ln -s -f $INS_HOME/redis.sh $BIN_DIR
}
mongodb(){
BIN_HOME=/usr/bin
INS_HOME=$HOME/mongodb
LOG_DIR=$INS_HOME/log
CONF_DIR=$INS_HOME/conf
DATA_DIR=$INS_HOME/data
if [ ! -d $INS_HOME ]
then
mkdir -p $INS_HOME
else
echo "The Instance alreadly exists..."
exit 0
fi
mkdir -p $CONF_DIR
mkdir -p $DATA_DIR
mkdir -p $LOG_DIR
conf=$CONF_DIR/mongodb.conf
cp -r /etc/mongod.conf $conf
readport 'mongodb', '127.0.0.1'
if [ $? -eq 0 ]
then
exit 0
fi
pidfile=${INS_HOME}/mongod.pid
sed -i "s:/var/lib/mongo:${DATA_DIR}:g" $conf
sed -i "s:/var/log/mongodb:${LOG_DIR}:g" $conf
sed -i "s:/var/run/mongodb/mongod.pid:${pidfile}:g" $conf
sed -i "s:27017:${SVR_PORT}:g" $conf
cat > $INS_HOME/mgServer.sh << EOF
#!/bin/sh
EXEC="$BIN_HOME/mongod"
CONF="$conf"
PIDFILE="$pidfile"
usage() {
echo "Usage: \$(basename "\$0") {start|stop|restart|status}" >&2
exit 1
}
PID=\$(cat \$PIDFILE 2>/dev/null || echo 0)
case "\$1" in
start)
if [ ! -x /proc/\${PID} ]
then
echo "Starting MongoDB server..."
\$EXEC -f \$CONF
else
echo "\$PIDFILE exists, process is already running or crashed"
fi
;;
stop)
if [ ! /proc/\${PID} ]
then
echo "\$PIDFILE does not exist, process is not running"
else
echo "Stopping ..."
\$EXEC -f \$CONF --shutdown
while [ -x /proc/\${PID} ]
do
echo "Waiting for MongoDB to shutdown ..."
sleep 1
done
echo "MongoDB stopped"
fi
;;
status)
if [ ! -x /proc/\${PID} ]
then
echo "MongoDB is not running"
else
echo "MongoDB is running (\$PID)"
fi
;;
restart)
\$0 stop
\$0 start
;;
*)
usage
;;
esac
exit $?
EOF
chmod u+x $INS_HOME/mgServer.sh
chown $RUN_USER:$RUN_USER -R $INS_HOME
# build shortcut
mkdir -p $BIN_DIR
ln -s -f $INS_HOME/mgServer.sh $BIN_DIR
}
zookeeper() {
JDK_HOME=/usr/lib/jvm/jre-openjdk
BIN_HOME=/opt/zookeeper-3.4.9
INS_HOME=$HOME/zookeeper
LOG_DIR=$INS_HOME/log
CONF_DIR=$INS_HOME/conf
DATA_DIR=$INS_HOME/data
if [ ! -d $INS_HOME ]
then
mkdir -p $INS_HOME
else
echo "The Instance alreadly exists..."
exit 0
fi
mkdir -p $CONF_DIR
mkdir -p $DATA_DIR
mkdir -p $LOG_DIR
mkdir -p $INS_HOME/bin
readport 'zookeeper', '127.0.0.1'
if [ $? -eq 0 ]
then
exit 0
fi
cat > $CONF_DIR/zookeeper-env.sh <<EOF
#!/bin/sh
ZOO_LOG_DIR="$LOG_DIR"
ZOO_LOG4J_PROP="INFO,CONSOLE"
EOF
cat > $CONF_DIR/zoo.cfg <<EOF
tickTime=2000
dataDir=$DATA_DIR
clientPort=$SVR_PORT
EOF
cat > $CONF_DIR/java.env <<EOF
#!/bin/sh
JAVA_HOME="$JDK_HOME"
JAVA_OPTS="-Xms64m -Xmx128m"
export JAVA_HOME JAVA_OPTS
EOF
cat > $INS_HOME/zkServer.sh <<EOF
#!/bin/sh
ZOOCFGDIR=$CONF_DIR
. "$BIN_HOME/bin/zkServer.sh" \$@
EOF
cat > $INS_HOME/zkCli.sh <<EOF
#!/bin/sh
ZOOCFGDIR=$CONF_DIR
. "$BIN_HOME/bin/zkCli.sh" \$@
EOF
cat > $INS_HOME/zkCleanup.sh <<EOF
#!/bin/sh
ZOOCFGDIR=$CONF_DIR
. "$BIN_HOME/bin/zkCleanup.sh" \$@
EOF
chmod u+x $INS_HOME/*.sh
chown $RUN_USER:$RUN_USER -R $INS_HOME
# build shortcut
mkdir -p $BIN_DIR
ln -s -f $INS_HOME/*.sh $BIN_DIR
}
cleanup(){
INS_HOME="$HOME/$1"
case "$1" in
tomcat)
[ -z ${INS_HOME}/tomcat.sh ] && $INS_HOME/tomcat.sh stop
rm -rf $BIN_DIR/tomcat.sh
;;
redis)
[ -z ${INS_HOME}/redis.sh ] && $INS_HOME/redis.sh stop
rm -rf $BIN_DIR/redis.sh
;;
mongodb)
[ -z ${INS_HOME}/mgServer.sh ] && $INS_HOME/mgServer.sh stop
rm -rf $BIN_DIR/mgServer.sh
;;
zookeeper)
[ -z ${INS_HOME}/zkServer.sh ] && $INS_HOME/zkServer.sh stop
rm -rf $BIN_DIR/zk*.sh
;;
*)
usage
;;
esac
rm -rf $INS_HOME
echo "clean completed."
}
usage() {
echo "Usage: $PROGRAM ( commands ... )"
echo "commands:"
echo " tomcat Install Tomcat 8.0.41"
echo " redis Install redis 3.2"
echo " mongodb Install mongodb 3.4.2"
echo " zookeeper Install zookeeper 3.4.9"
echo " clean <options> UnInstall {tomcat|redis|mongodb|zookeeper}"
exit 1
}
case "$1" in
tomcat)
tomcat
;;
redis)
redis
;;
mongodb)
mongodb
;;
zookeeper)
zookeeper
;;
clean)
cleanup "$2"
;;
*)
usage
;;
esac
exit $?
#!/bin/bash
if [ -n "${SUDO}" ]; then
echo "ERROR: Please run as root"
exit 1
fi
cd /opt
rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
# get tomcat
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.0.41/bin/apache-tomcat-8.0.41.tar.gz
tar -zxf apache-tomcat-8.0.41.tar.gz
rm -rf apache-tomcat-8.0.41.tar.gz
# get zookeeper
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.9/zookeeper-3.4.9.tar.gz
tar zxf zookeeper-3.4.9.tar.gz
rm -rf zookeeper-3.4.9.tar.gz
# get mongodb
cat > /etc/yum.repos.d/mongodb.repo << EOF
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
EOF
yum install mongodb-org
# get redis
wget http://download.redis.io/releases/redis-3.2.8.tar.gz
tar zxf redis-3.2.8.tar.gz
rm -rf redis-3.2.8.tar.gz
cd redis-3.2.8 && make
# init common shell environment
cd ~/
yum install java
echo "JAVA_HOME=/usr/lib/jvm/jre-openjdk" >> /etc/profile
echo "export JAVA_HOME" >> /etc/profile
source /etc/profile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment