#!/bin/bash
#start install data
START_TIME=`date +"%Y-%m-%d %H:%M:%S"`
#输出黄色文本
echo_yellow(){
echo -e "\e[93m$1\e[0m"
}
KNOWSTREAMING_VERSION="3.4.0"
DOWNLOAD_URL="https:/github.com/didi/KnowStreaming/releases/download/v${KNOWSTREAMING_VERSION}"
FILE="KnowStreaming-${KNOWSTREAMING_VERSION}.tar.gz"
DATA_DIR="/usr/local/KnowStreaming"
#获取MySQL 实例 & 端口
echo -n "请输入 MySQL 实例 (默认 127.0.0.1) :"
read -r MYSQL_IP
MYSQL_IP=${MYSQL_IP:-127.0.0.1}
echo_yellow "MYSQL 实例为: ${MYSQL_IP}"
echo -n "请输入 MySQL 端口号 (默认 3366) :"
read -r MYSQL_PORT
MYSQL_PORT=${MYSQL_PORT:-3366}
echo_yellow "MYSQL 实例端口设置为: ${MYSQL_PORT}"
#username & passwd
echo -n "请输入 MySQL root权限用户 :"
read -r MYSQL_ROOT
MYSQL_ROOT=${MYSQL_PORT:-N}
while [ $MYSQL_ROOT == "N" ]
do
echo -n "请输入 MySQL root权限用户 :"
read -r MYSQL_ROOT
done
echo -n "请输入 MySQL root权限用户密码 :"
read -s MYSQL_PW
MYSQL_PW=${MYSQL_PW:-N}
while [ $MYSQL_PW == "N" ]
do
echo -n "请输入 MySQL root权限用户密码 :"
read -s MYSQL_PW
done
echo""
echo -n "请输入 KnowStreaming 用户IP (默认 127.0.0.1) :"
read -r KNOWSTREAMING_LOGIN_IP
KNOWSTREAMING_LOGIN_IP=${KNOWSTREAMING_LOGIN_IP:-127.0.0.1}
echo_yellow "KnowStreaming用户登入ip设置为: ${KNOWSTREAMING_LOGIN_IP}"
echo -n "请输入 KnowStreaming 用户 :"
read -r KNOWSTREAMING_ACCOUNT
KNOWSTREAMING_ACCOUNT=${KNOWSTREAMING_ACCOUNT:-N}
while [ $KNOWSTREAMING_ACCOUNT == "N" ]
do
echo -n "请输入 KnowStreaming用户 :"
read -r KNOWSTREAMING_ACCOUNT
done
echo_yellow "KnowStreaming用户登入为: ${KNOWSTREAMING_ACCOUNT}"
echo -n "请输入 KnowStreaming 用户密码 :"
read -r KNOWSTREAMING_PW
KNOWSTREAMING_PW=${KNOWSTREAMING_PW:-N}
while [ $KNOWSTREAMING_PW == "N" ]
do
echo -n "请输入 KnowStreaming用户密码 :"
read -r KNOWSTREAMING_PW
done
#download package
if [ ! -f "/opt/${FILE}" ]; then
echo_yeallow "the begin dwonload package..."
wget ${DOWNLOAD_URL}/${FILE} -P /opt/
fi
#create KnowStreaming account
echo_yeallow "create KnowStreaming account(create login account )..."
#read -s -p "Enter Password for knowstreaming :" PASSWORD
egrep "knowstreaming" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "用户已经存在(the user duplicae!)";
else
#useradd -m -p $PASSWORD knowstreaming
useradd -r -s /bin/false knowstream
[ $? -eq 0 ] && echo_yellow "create knowstreaming account ( create login for knowstreaming end)..." || echo " the create knowstreaming account fil!"
fi
#install Knowstreaming
echo_yellow "install KnowStreaming..."
tar -zxvf /opt/${FILE} -C /usr/local
mkdir /data/KnowStreaming;
mkdir /data/KnowStreaming/logs;
mkdir /data/KnowStreaming/logs ${DATA_DIR}/bin/logs;
chown -R knowstream.knowstream /data/KnowStreaming /usr/local/KnowStreaming;
echo_yellow "KnowStreaming complete..."
#install MySQL Client
echo_yellow "install MySQL Client ..."
if [ ! -f /opt/MySQL-client-5.1.73-1.glibc23.x86_64.rpm ]; then
wget https://downloads.mysql.com/archives/get/p/23/file/MySQL-client-5.1.73-1.glibc23.x86_64.rpm -P /opt/
fi
yum install -y /opt/MySQL-client-5.1.73-1.glibc23.x86_64.rpm
echo_yellow "MySQL Client Complete..."
#install JDK
if command -v java &>/dev/null; then
echo_yellow "Java duplicate"
else
echo_yellow "install Java..."
wget -q https://download.oracle.com/java/22/latest/jdk-22_linux-x64_bin.rpm -P /opt/
yum install -y /opt/jdk-22_linux-x64_bin.rpm
echo_yellow "Java Install Complete successfull."
fi
# KnowStreaming sevice configer
cat >/usr/lib/systemd/system/knowstreaming.service <<EOF
[Unit]
Description=KnowStreaming Service
Documentation=https://knowstreaming.com
Wants=network-online.target
[Service]
Type=forking
Environment="JAVA_Home=/usr/java/jdk-22"
User=root
Group=root
ExecStart=/bin/bash /usr/local/KnowStreaming/bin/startup.sh
ExecStop=/bin/bash /usr/local/KnowStreaming/bin/shutdown.sh
ExecReload=/bin/kill --signal HUP
KillMode=control-group
KillSignal=SIGTERM
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
# modify seting configure
sed -i "s/127.0.0.1:3306/$MYSQL_IP:$MYSQL_PORT/g" ${DATA_DIR}/conf/application.yml;
sed -i "s/username: root/username: $KNOWSTREAMING_ACCOUNT/g" ${DATA_DIR}/conf/application.yml;
sed -i "s/password: mysql_pass/password: $KNOWSTREAMING_PW/g" ${DATA_DIR}/conf/application.yml;
sed -i "s/-Xmx2g/-Xmx10g/g" ${DATA_DIR}/bin/startup.sh
sed -i "s/-Xms2g/-Xmx10g/g" ${DATA_DIR}/bin/startup.sh
#Create SQL File
cat >/tmp/knowstreaming_createuser.sql <<EOF
CREATE DATABASE know_streaming;
CREATE USER '${KNOWSTREAMING_ACCOUNT}'@'${KNOWSTREAMING_LOGIN_IP}' IDENTIFIED BY '${KNOWSTREAMING_PW}' ;
GRANT ALL ON know_streaming.* to '${KNOWSTREAMING_ACCOUNT}'@'${KNOWSTREAMING_LOGIN_IP}';
EOF
mysql -h ${MYSQL_IP} -P ${MYSQL_PORT} -u ${MYSQL_ROOT} -p${MYSQL_PW} < /tmp/knowstreaming_createuser.sql 2>/dev/null
rm -rf /tmp/ knowstreaming_createuser.sql
echo_yellow "create sql file complete."
echo_yellow "install Knowstreaming perior configure database..."
mysql -h ${MYSQL_IP} -P ${MYSQL_PORT} -u ${MYSQL_ROOT} -p${MYSQL_PW} know_streaming < /usr/local/KnowStreaming/init/sql/ddl-ks-km.sql
mysql -h ${MYSQL_IP} -P ${MYSQL_PORT} -u ${MYSQL_ROOT} -p${MYSQL_PW} know_streaming < /usr/local/KnowStreaming/init/sql/ddl-logi-job.sql
mysql -h ${MYSQL_IP} -P ${MYSQL_PORT} -u ${MYSQL_ROOT} -p${MYSQL_PW} know_streaming < /usr/local/KnowStreaming/init/sql/ddl-logi-security.sql
mysql -h ${MYSQL_IP} -P ${MYSQL_PORT} -u ${MYSQL_ROOT} -p${MYSQL_PW} know_streaming < /usr/local/KnowStreaming/init/sql/dml-ks-km.sql
mysql -h ${MYSQL_IP} -P ${MYSQL_PORT} -u ${MYSQL_ROOT} -p${MYSQL_PW} know_streaming < /usr/local/KnowStreaming/init/sql/dml-logi.sql
echo_yellow "install KnowStreaming perior configure database complete."
# startup KnowStreaming service
echo_yellow "startup KnowStreaming service..."
systemctl daemon-reload
systemctl start knowstreaming.service
systemctl enable knowstreaming.service >/dev/null 2>&1
systemctl status knowstreaming.service
echo_yellow "KnowStreaming mysql service start complete."
#install complete data
END_TIME=`date +"%Y-%m-%d %H:%M:%S"`
#output information
echo""
echo""
echo "#############################################"
echo "################KnowStreaming Service install configure ##############"
echo "###############################################################"
echo_yellow "install begin date : ${START_TIME}"
echo_yellow "install complete date : ${END_TIME}"
echo_yellow "KnowStreaming service ip: ${KNOWSTREAMING_LOGIN_IP}"
echo_yellow "KnowStreaming service port(port): 8080"
echo_yellow "KnowStreaming service Account(Account): ${KNOWSTREAMING_ACCOUNT}"
echo_yellow "KnowStreaming service Password(Password): ${KNOWSTREAMING_PW}"
echo_yellow "MySQL service IP: ${MYSQL_IP}"
echo_yellow "MySQL service PORT: ${MYSQL_PORT}"
echo_yellow "MySQL service Version: ${MYSQL_VERSION}"
echo""