1 Star 0 Fork 12

smile/playground

forked from 数舟/playground 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
playground.sh 24.75 KB
一键复制 编辑 原始数据 按行查看 历史
数舟 提交于 2024-06-05 10:34 . fix scala
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
#! /bin/bash
# TODO
# playground search -> 查找可以安装的软件列表
# playground install -> 安装对应组件时,设置安装在某几个节点,并将配置信息保存到数据库
# playground install online -> 在线获取软件
# 每个脚本均可单独执行,使用环境变量判断环境是否初始化、playground是否安装;
# 如果playground已经安装,则将脚本移动到/opt/app目录,如果未安装则直接执行
## TODO. init之前做一个环境check,如果环境有问题,则exit退出并报错
## Check主要两个问题,1、ssh免密登录 2、各节点能否使用yum安装软件(默认需要外网)
# 软件安装目录
# 更新前为:/home/hadoop/automaticDeploy
PLAY_HOME=/opt/playground
# playground安装
function install()
{
# 创建脚本保存目录
mkdir -p $PLAY_HOME
# 将所有脚本拷贝到对应目录
cp -r ./* $PLAY_HOME
# 本地脚本赋权
chmod -R u+x $PLAY_HOME/hadoop
chmod -R u+x $PLAY_HOME/systems
chmod -R u+x $PLAY_HOME/product
chmod -R u+x $PLAY_HOME/playground.sh
chmod -R u+x $PLAY_HOME/configs.sh
chmod -R u+x $PLAY_HOME/manager.sh
chmod -R u+x $PLAY_HOME/configSoft.sh
# 建立软连接到/bin目录
ln -s $PLAY_HOME/playground.sh /bin/playground
ln -s $PLAY_HOME/manager.sh /bin/pl_manager
# 将环境变量持久化到/etc/profile中
profile=/etc/profile
sed -i "/^export PLAY_HOME/d" $profile
echo "export PLAY_HOME=$PLAY_HOME" >> $profile
# 更新/etc/profile文件
source /etc/profile && source /etc/profile
# 安装成功标志位
echo "1" > $PLAY_HOME/SUCCESS
# 输出安装成功信息
echo "Installation completed successfully!"
}
# playground卸载
function uninstall()
{
# 删除脚本目录
rm -r $PLAY_HOME
# 清理环境变量
profile=/etc/profile
sed -i "/^export PLAY_HOME/d" $profile
# 更新/etc/profile文件
source /etc/profile && source /etc/profile
echo "success!"
}
# 更新所有playground
function update_all()
{
# 本地脚本赋权
# TODO.脚本更新分发软件包
chmod -R u+x $PLAY_HOME/hadoop
chmod -R u+x $PLAY_HOME/systems
chmod -R u+x $PLAY_HOME/product
urrent_host=`hostname`
#1.遍历主机列表
while read line;
do
hostname=`echo $line | cut -d " " -f2`
echo "目前正在设置$hostname节点的系统环境"
#为其它远程主机复制文件
if [[ $hostname != $current_host ]]
then
# 脚本分发
scp -r $PLAY_HOME $hostname:/opt/
ssh -n $hostname "chmod -R u+x $PLAY_HOME/hadoop"
ssh -n $hostname "chmod -R u+x $PLAY_HOME/systems"
ssh -n $hostname "chmod -R u+x $PLAY_HOME/product"
fi
done < $PLAY_HOME/host_ip.txt
}
# 更新某个脚本或配置文件
function update_file()
{
change_file=$1
to_dir=$2
current_host=`hostname`
# 遍历主机列表
while read line;
do
hostname=`echo $line | cut -d " " -f2`
echo "目前正在更新$hostname节点的系统环境"
# 为其它远程主机复制文件
if [[ $hostname != $current_host ]]
then
# 脚本分发
scp -r $change_file $hostname:$to_dir
ssh -n $hostname "chmod -R u+x $PLAY_HOME/hadoop"
ssh -n $hostname "chmod -R u+x $PLAY_HOME/systems"
ssh -n $hostname "chmod -R u+x $PLAY_HOME/product"
fi
done < $PLAY_HOME/host_ip.txt
}
# 添加软件,$1为软件名或路径
function add_soft()
{
# shopt -s nullglob
# shopt -u nullglob
# if [ $# -gt 1 ]; then
# # echo "listing args with $@" #在"$@"中遍历参数
for arg in "$@"
do
if [ -e $arg ];
then
mv $arg $PLAY_HOME/frames
echo "$arg软件包添加成功!"
# 判断其它节点是否已经安装PL_GROUND
plyIsExists=`find $PLAY_HOME -name I_SUCCESS`
if [[ ${#plyIsExists} -ne 0 ]];then
# 将组件分发到其它节点
update_file $PLAY_HOME/frames/$arg $PLAY_HOME/frames/
else
echo "dont need scp"
fi
else
if [ $arg != 'add' ]
then echo "请检查$arg文件路径是否正确!"
fi
fi
done
# else
# mv $2 $PLAY_HOME/soft
# fi
}
# 添加lib依赖,$1为软件名或路径
function add_lib()
{
for arg in "$@"
do
if [ -e $arg ];
then
mv $arg $PLAY_HOME/frames/lib
echo "$arg软件包添加成功!"
# 判断其它节点是否已经安装PL_GROUND
plyIsExists=`find $PLAY_HOME -name I_SUCCESS`
if [[ ${#plyIsExists} -ne 0 ]];then
# 将组件分发到其它节点
update_file $PLAY_HOME/frames/lib/$arg $PLAY_HOME/frames/lib
else
echo "dont need scp"
fi
else
if [ $arg != 'lib' ]
then echo "请检查$arg文件路径是否正确!"
fi
fi
done
}
# 搜索集群已安装软件和待安装软件
# TODO.修改add soft函数,将安装包添加到配置文件中进行标记
# TODO.软件安装成功后,添加标志位表示
function search()
{
echo "TODO"
}
# 检查安装包信息是否存在
check_package_info() {
local package_name=$1
found=false
if grep -q "$package_name" "$PLAY_HOME/frames.txt"; then
echo "已配置 $package_name 安装信息:"
echo "======================================="
package_info=$(egrep "$package_name" "$PLAY_HOME/frames.txt")
package=$(echo $package_info | awk '{print $1}')
is_installed=$(echo $package_info | awk '{print $2}')
nodes=$(echo $package_info | awk '{print $3}')
master=$(echo "$package_info" | awk '{print $4}')
if [ "$is_installed" == "true" ]; then
echo "安装包名: $package"
echo "需要安装的所有节点: $nodes"
if [ -n "$master" ]; then
echo "主节点(如果有): $master"
fi
echo "======================================="
# 检查是否有同名的安装包
if [ -f "$PLAY_HOME/frames/$package" ]; then
echo "在$PLAY_HOME/frames目录下找到了 $package 安装包。"
else
echo "在$PLAY_HOME/frames目录下没有找到 $package 安装包。"
echo "请使用playground add $package 命令添加安装包。"
echo "======================================="
echo "如需修改版本,请使用playground config $package_name 命令进行配置。"
return 1
fi
fi
echo "======================================="
# 提示用户是否需要修改
read -p "是否需要修改 $package_name 安装信息?(y/n):" modify
if [ "$modify" == "y" ]; then
echo "请使用playground config $package_name 命令进行配置。"
return 1
fi
found=true
else
echo "frames.txt文件中没有找到 $package_name 的安装配置。"
echo "请使用playground config $package_name 命令进行配置。"
return 1
fi
}
function check_mysql_info()
{
local package_name=mysql
if grep -q "$package_name" "$PLAY_HOME/frames.txt"; then
echo "已配置 $package_name 安装信息:"
echo "======================================="
package_info=$(egrep "$package_name" "$PLAY_HOME/frames.txt")
package=$(echo $package_info | awk '{print $1}')
is_installed=$(echo $package_info | awk '{print $2}')
nodes=$(echo $package_info | awk '{print $3}')
master=$(echo "$package_info" | awk '{print $4}')
if [ "$is_installed" == "true" ]; then
echo "安装包名: $package"
echo "需要安装的所有节点: $nodes"
if [ -n "$master" ]; then
echo "主节点(如果有): $master"
fi
echo "======================================="
if [ -d "$PLAY_HOME/frames/$package" ]; then
echo "在$PLAY_HOME/frames目录下找到了 $package 安装包。"
else
echo "在$PLAY_HOME/frames目录下没有找到 $package 安装包。"
echo "请使用playground add $package 命令添加安装包。"
echo "======================================="
echo "如需修改版本,请使用playground config $package_name 命令进行配置。"
echo "======================================="
$PLAY_HOME/configSoft.sh download mysql
return 1
fi
fi
echo "======================================="
# 提示用户是否需要修改
read -p "是否需要修改 $package_name 安装信息?(y/n):" modify
if [ "$modify" == "y" ]; then
echo "请使用playground config $package_name 命令进行配置。"
return 1
fi
else
echo "frames.txt文件中没有找到 $package_name 的安装配置。"
echo "请使用playground config $package_name 命令进行配置。"
return 1
fi
}
function check_tez_info()
{
# 提示用户是否配置hive on tez
read -p "是否配置hive on tez?[y/n]: " confirm
if [ "$confirm" == "y" ]; then
# 调用check_package_info tez进行配置
check_package_info tez
if [ $? -eq 1 ]; then
exit 1
fi
#判断lzo依赖是否存在
LIB_DIR="$PLAY_HOME/frames/lib"
DRIVER_NAME="hadoop-lzo"
lzo_file=$(find "$LIB_DIR" -name "${DRIVER_NAME}-*.jar" | head -n 1)
if [ -n "$lzo_file" ]; then
echo "======================================="
echo "在$PLAY_HOME/frames/lib目录下找到了$(basename $lzo_file)"
else
echo "======================================="
echo "$LIB_DIR 目录下没有lzo驱动包"
echo "请使用playground lib [驱动包] 命令添加lzo包。"
echo "例如playground lib hadoop-lzo-0.4.20.jar"
exit 1
fi
elif [ "$confirm" == "n" ]; then
# 检查$PLAY_HOME/frames.txt文件中是否包含指定配置信息
if grep -q "apache-tez-.*-bin.tar.gz true node03" "$PLAY_HOME/frames.txt"; then
read -p "当前配置文件中的设置需要安装tez,是否取消配置?[y/n]: " install_confirm
if [ "$install_confirm" == "y" ]; then
# 将配置文件中的true改为false
sed -i 's/apache-tez-.*-bin.tar.gz true node03/apache-tez-.*-bin.tar.gz false node03/' "$PLAY_HOME/frames.txt"
echo "已将配置文件中的设置修改为不需要安装tez"
elif [ "$install_confirm" == "n" ]; then
# 调用check_package_info tez进行配置
check_package_info tez
else
echo "无效输入"
return 1
fi
else
echo "取消配置hive on tez"
fi
else
echo "无效输入"
return 1
fi
}
function check_hive_info()
{
local package_name=hive
# 检测hive依赖
if [[ "$package_name" == "hive" ]]; then
#拷贝Mysql连接驱动
mysqlDrive=`egrep "^mysql-drive" $PLAY_HOME/configs.txt | cut -d " " -f2`
#判断驱动是否存在
driveIsExists=`find $PLAY_HOME/frames/lib -name $mysqlDrive`
if [[ ${#driveIsExists} -ne 0 ]];then
echo "在$PLAY_HOME/frames目录下找到了 $mysqlDrive 驱动包。"
else
echo "$PLAY_HOME/frames/lib目录下没有Mysql驱动包"
echo "请使用playground lib [驱动包] 命令添加安装包。"
echo "======================================="
echo "mysql驱动包默认为. mysql-connector-java-5.1.26-bin.jar"
echo "如需更改mysql驱动包,请修改$PLAY_HOME/configs.txt文件"
return 1
fi
fi
}
# 查看集群IP情况
function getClusterIp()
{
# 输出已配置的网络信息
echo "已配置的网络信息:"
while read line; do
# 提取文件中的ip
ip=$(echo $line | cut -d " " -f1)
# 提取文件中的hostname
hostname=$(echo $line | cut -d " " -f2)
# 提取文件中的用户名
user_name=$(echo $line | cut -d " " -f3)
# 提取文件中的密码
pass_word=$(echo $line | cut -d " " -f4)
echo "IP: $ip, Hostname: $hostname, Username: $user_name, Password: $pass_word"
done < "$PLAY_HOME/host_ip.txt"
}
# 添加集群IP
function addClusterHost()
{
read -p "enter nodes member: " cluster_count
for ((i=1;i<=$cluster_count;i++))
do
# 提示用户输入新的网络信息
read -p "请输入IP地址: " ip
read -p "请输入Hostname: " hostname
read -p "请输入用户名: " username
read -p "请输入密码: " passwd
echo "======================================="
#查询$ip是否存在于/etc/hosts里面
egrep "$hostname" $PLAY_HOME/host_ip.txt >& /dev/null
if [ $? -eq 0 ]
then
#$?是上一个程序执行是否成功的标志,如果执行成功则$?为0,否则不为0,存在则先把就的ip设置删除掉
sed -i "/$hostname/d" $PLAY_HOME/host_ip.txt
sed -i "/^$ip/d" $PLAY_HOME/host_ip.txt
fi
#把ip、hostname添加到$PLAY_HOME/hosts配置文件中
echo "$ip $hostname $username $passwd" >> $PLAY_HOME/host_ip.txt
done
# 输出已配置的网络信息
getClusterIp
}
# 删除指定IP
function deleteClusterHost()
{
read -p "enter hostname to delete: " to_delete
if [ $? -eq 0 ]
then
#$?是上一个程序执行是否成功的标志,如果执行成功则$?为0,否则不为0,存在则先把就的ip设置删除掉
sed -i "/$to_delete/d" $PLAY_HOME/host_ip.txt
else
echo "hostname not found!"
fi
}
# 更新集群IP
updateClusterHost() {
# 检查host_ip.txt文件是否为空
if [ ! -s "$PLAY_HOME/host_ip.txt" ]; then
echo "host_ip.txt文件为空,请直接添加网络信息。"
addClusterHost
return
fi
# 输出已配置的网络信息
getClusterIp
# 提示用户选择更新哪个网络信息
read -p "请输入要更新的网络信息的Hostname(输入all更新所有网络信息): " update_hostname
if [ "$update_hostname" == "all" ]; then
# 清空host_ip.txt文件
> "$PLAY_HOME/host_ip.txt"
echo "host_ip.txt文件已清空,请重新添加网络信息。"
addClusterHost
return
fi
# 在文件中找到要更新的行并删除
if ! sed -i "/$update_hostname/d" "$PLAY_HOME/host_ip.txt"; then
echo "没有找到对应的网络配置,请重新输入。"
updateClusterHost
return
fi
# 提示用户输入新的网络信息
read -p "请输入IP地址: " new_ip
read -p "请输入Hostname: " new_hostname
read -p "请输入用户名: " new_user_name
read -p "请输入密码: " new_pass_word
# 添加新的网络信息到文件中
echo "$new_ip $new_hostname $new_user_name $new_pass_word" >> "$PLAY_HOME/host_ip.txt"
echo "网络信息更新成功。"
# 询问用户是否继续更新
read -p "是否继续更新网络信息?(输入y继续,输入其他任意键退出): " continue_update
if [ "$continue_update" == "y" ]; then
updateClusterHost
else
return
fi
}
# $1为需要批量执行的脚本
# 远程批量执行脚本:打开伪终端
function clusterInstall()
{
#1.遍历主机列表
while read line;
do
hostname=`echo $line | cut -d " " -f2`
echo "目前正在配置$hostname节点的系统组件"
#2.在各节点执行脚本
ssh -t -t -n $hostname $1 $2
done < $PLAY_HOME/host_ip.txt
}
function playground()
{
if [ $# -eq 0 ]; then
echo "初始化集群,请输入参数init"
echo "添加软件包,请输入参数add [path]"
echo "组件安装,请输入参数install [options]"
echo "其中options是要安装的组件名,如hadoop、hive、hbase等"
exit 1
fi
# 安装playground
if [[ $1 == "install" && -z $2 ]]; then
# 安装脚本
install
fi
if [[ $1 == "uninstall" && -z $2 ]]; then
# 卸载脚本
uninstall
fi
if [ $1 == "add" ]; then
# todo.设置检查参数个数,来批量添加,目前使用
# .\/\*可以批量导入
# 添加软件
add_soft $*
fi
if [ $1 == "lib" ]; then
# todo.设置检查参数个数,来批量添加,目前使用
# .\/\*可以批量导入
# 添加依赖
add_lib $*
fi
# 初始化集群
if [ $1 == "init" ]; then
# 脚本赋权
chmod -R u+x $PLAY_HOME/hadoop
chmod -R u+x $PLAY_HOME/systems
chmod -R u+x $PLAY_HOME/product
# 集群初始化脚本执行
$PLAY_HOME/systems/clusterOperate.sh
fi
# 更改集群信息
if [ $1 == "nodes" ]; then
case $2 in
get)
getClusterIp
;;
add)
addClusterHost
;;
update)
updateClusterHost
;;
delete)
deleteClusterHost
;;
sync)
# 更新host_ip配置文件
update_file $PLAY_HOME/host_ip.txt $PLAY_HOME
;;
esac
fi
# 配置基础环境
if [ $1 == "config" ]; then
case $2 in
yum)
$PLAY_HOME/systems/configureYum.sh config
;;
jdk)
$PLAY_HOME/configSoft.sh config jdk
;;
scala)
$PLAY_HOME/configSoft.sh config scala
;;
hadoop)
$PLAY_HOME/configSoft.sh config hadoop
;;
mysql)
$PLAY_HOME/configSoft.sh config mysql
;;
hive)
$PLAY_HOME/configSoft.sh config hive
;;
tez)
$PLAY_HOME/configSoft.sh config tez
;;
flink)
$PLAY_HOME/configSoft.sh config flink
;;
zookeeper)
$PLAY_HOME/configSoft.sh config zookeeper
;;
kafka)
$PLAY_HOME/configSoft.sh config kafka
;;
hbase)
$PLAY_HOME/configSoft.sh config hbase
;;
ds)
$PLAY_HOME/configSoft.sh config ds
;;
spark)
$PLAY_HOME/configSoft.sh config spark
;;
sqoop)
$PLAY_HOME/configSoft.sh config sqoop
;;
esac
fi
# 软件包安装,先判断安装包是否存在,然后执行安装逻辑
install_package() {
package_name=$1
shell_file=$2
check_package_info $package_name
if [ $? -eq 1 ]; then
exit 1
else
clusterInstall $shell_file
fi
}
# mysql 软件包安装,先判断安装包是否存在,然后执行安装逻辑
install_mysql_package() {
shell_file=$1
check_mysql_info
if [ $? -eq 1 ]; then
exit 1
else
clusterInstall $shell_file
fi
}
# hive 软件包安装,先判断安装包是否存在,然后执行安装逻辑
install_hive_package() {
shell_file=$1
check_package_info hive
if [ $? -eq 1 ]; then
exit 1
fi
check_hive_info
if [ $? -eq 1 ]; then
exit 1
fi
check_tez_info
if [ $? -eq 1 ]; then
exit 1
else
clusterInstall $shell_file
fi
}
# 执行安装脚本
if [ $1 == "install" ]; then
case $2 in
yum)
sh $PLAY_HOME/systems/configureYum.sh install
;;
scala)
install_package scala $PLAY_HOME/systems/configureScala.sh
;;
maven)
clusterInstall $PLAY_HOME/systems/configureMaven.sh
;;
nodejs)
clusterInstall $PLAY_HOME/systems/configureNode.sh
;;
hadoop)
install_package hadoop $PLAY_HOME/hadoop/installHadoop.sh
;;
mysql)
install_mysql_package $PLAY_HOME/hadoop/installMysql.sh
;;
hive)
install_hive_package $PLAY_HOME/hadoop/installHive.sh
;;
zookeeper)
install_package zookeeper $PLAY_HOME/hadoop/installZookeeper.sh
;;
hbase)
install_package hbase $PLAY_HOME/hadoop/installHBase.sh
;;
kafka)
install_package kafka $PLAY_HOME/hadoop/installKafka.sh
;;
spark)
install_package spark $PLAY_HOME/hadoop/installSpark.sh
;;
flink)
install_package flink $PLAY_HOME/hadoop/installFlink.sh
;;
sqoop)
install_package sqoop $PLAY_HOME/hadoop/installSqoop.sh
;;
flume)
clusterInstall $PLAY_HOME/hadoop/installFlume.sh
;;
presto)
clusterInstall $PLAY_HOME/hadoop/installPresto.sh
;;
azkaban)
clusterInstall $PLAY_HOME/hadoop/installAzkaban.sh
;;
hue)
clusterInstall $PLAY_HOME/hadoop/installHue.sh
;;
yanagishima)
clusterInstall $PLAY_HOME/hadoop/installYanagishima.sh
;;
es)
clusterInstall $PLAY_HOME/hadoop/installElasticSearch.sh install
;;
ds)
install_package ds $PLAY_HOME/hadoop/installDolphinScheduler.sh
;;
zeppelin)
clusterInstall $PLAY_HOME/hadoop/installZeppelin.sh
;;
kylin)
clusterInstall $PLAY_HOME/hadoop/installKylin.sh
;;
docker)
clusterInstall $PLAY_HOME/product/installDocker.sh
;;
k8s)
$PLAY_HOME/product/installK8s.sh
;;
esac
fi
# 执行卸载
if [ $1 == "uninstall" ]; then
case $2 in
es)
clusterInstall $PLAY_HOME/hadoop/installElasticSearch.sh uninstall
;;
ds)
clusterInstall $PLAY_HOME/hadoop/installDolphinScheduler.sh uninstall
;;
esac
fi
# 更新集群安装脚本
if [ $1 == "update" ]; then
case $2 in
all)
echo "update all files"
update_all
;;
config)
echo "update frames.txt file"
update_file $PLAY_HOME/frames.txt $PLAY_HOME
echo "update configs.txt file"
update_file $PLAY_HOME/configs.txt $PLAY_HOME
echo "update host_ip.txt file"
update_file $PLAY_HOME/host_ip.txt $PLAY_HOME
;;
*)
echo "update single file"
read -p "update script to hadoop , systems or product dir? : " to_dir
cp $2 $PLAY_HOME/$to_dir
update_file $2 $PLAY_HOME/$to_dir
;;
esac
fi
}
playground $1 $2
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/enliven/playground.git
git@gitee.com:enliven/playground.git
enliven
playground
playground
master

搜索帮助