liunx自动部署shell脚本
场景:
前几天要去甲方那边部署系统,感觉要装的东西很多,.net环境哇,mysql,pg等等。每次装都要重新去看怎么安装,而且还容易遇到一些问题,于是这次我打算写成shell脚本,方便运行
解决:
这里主要放一下我的脚本的内容,完成了部分功能。其他功能等之后有空了在慢慢补充。
首先,看看这个文件夹的情况:
这里由于我在windows下进行编写的,为了编译方便我把sh后缀先改成txt。这个auto_cmd就是主要程序,如下:
如果在liunx上运行遇到格式的错误,可以先进入vi,然后:set fileformat=unix,然后qw保存设置。这样运行就没有格式问题了。
运行的话,就./auto_cmd.sh,这样就可以了。
#!/bin/sh # 变量 RED='\033[31m' GREEN='\033[32m' YELLOW="\033[33m" RES='\033[0m' # 文件目录 HomePath="root" # 计数器 STR_TEMP="success" OLD_STR="success" # 输入变量 input="q" cmd="1" # supervisor安装 # ini文件要存放的位置 LOC="/etc/supervisord.d/" # sh文件要存放的位置 TARGET="/root/" # pg安装 # postgresql用户的密码 PG_PASSWORD="YTkejiA2216_PG!" # pg开放的端口 PG_PORT=8000 # 通过yum安装 installYum(){ echo "=====begin install ${1}=====" yum -y install ${1} STR_TEMP=$(yum list installed | grep ${1}) # 字符变,则说明已经安装 if [ "$STR_TEMP" = "$OLD_STR" ]; then echo -e "${RED}=====${1} install failed=====${RES}" elif [ "$STR_TEMP" = "" ]; then echo -e "${RED}=====${1} install failed=====${RES}" else echo -e "${GREEN}=====${1} install success=====${RES}" fi STR_TEMP=OLD_STR } # 遍历所有文件,修改权限 changefile(){ for file in `ls $1` do dir_or_file=$1"/"$file if [ -f ${dir_or_file} ]; then chmod 777 ${dir_or_file} elif [ -d ${dir_or_file} ]; then changefile $dir_or_file else echo "${dir_or_file}" fi done } # 将指定后缀移动到指定位置 # 参数1:源位置 # 参数2:后缀名 # 参数3:目标位置 getfile(){ for file in `ls $1` do dir_or_file=$1"/"$file if [ "${file##*.}"x = "$2"x ]; then echo "====${dir_or_file}====$3====" cp ${dir_or_file} $3 fi done } # 换源 change(){ echo "change" echo -e "${YELLOW}=====暂未实现=====${RES}" } # 安装superviosr installsuperviosr(){ ARRAY_NAME=("epel-release" "supervisor") # 安装 for(( i=0;i<${#ARRAY_NAME[@]};i++)) do installYum ${ARRAY_NAME[i]}; done # 配置信息 if [ ! -d "$LOC" ]; then mkdir ${LOC} -p echo -e "${GREEN}=====配置文件夹已创建=====${RES}" else echo -e "${GREEN}=====配置文件夹已创建=====${RES}" fi # 遍历ini文件,复制到配置文件夹下 TEMP=$HomePath"/1.supervisor" TYPE="ini" SHTYPE="sh" getfile ${TEMP} ${TYPE} ${LOC} # 复制sh到root下 getfile ${TEMP} ${SHTYPE} ${TARGET} # 开机启动 systemctl enable supervisord # 启动 supervisord -c /etc/supervisord.conf systemctl start supervisord } # 安装.net installdotnet(){ sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm yum -y install dotnet-sdk-6.0 yum -y install aspnetcore-runtime-6.0 yum -y install dotnet-sdk-3.1 yum -y install aspnetcore-runtime-3.1 } # 安装mysql installmysql(){ echo "install mysql" echo -e "${YELLOW}=====暂未实现=====${RES}" } # 安装nginx installnginx(){ yum install -y nginx mv /etc/nginx/nginx.conf old_redis.conf \cp -f ../6.nginx/nginx.conf /etc/nginx systemctl enable nginx systemctl start nginx } # 安装redis installredis(){ yum -y install epel-release yum update yum -y install redis mv /etc/redis.conf old_redis.conf \cp -f ../4.redis/redis.conf /etc systemctl enable redis systemctl start redis # TODO:需要检测是否安装,是否启动 } # 安装pg installpg(){ echo "################################################" echo "# Please enter your choise: #" echo "# (0) 安装postgresql 12 #" echo "# (1) 安装pg_corn(定时器插件) #" echo "# (2) 安装timescaledb(时序数据库) #" echo "# (3) 安装Pgpool(高可用方案) #" echo "################################################" read cmd case $cmd in "0") yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm yum install -y postgresql12-server # 初始化文件夹 if [ ! -d "/var/lib/pgsql/12/data" ]; then /usr/pgsql-12/bin/postgresql-12-setup initdb chmod 700 -R /var/lib/pgsql/12/data else echo -e "${GREEN}=====已经初始化过了=====${RES}" fi # 开机自启动 systemctl enable postgresql-12 # 启动服务 systemctl start postgresql-12 # 修改密码 sudo -u postgres psql -U postgres -d postgres -c "alter user postgres with password '${PG_PASSWORD}';exit;" # 配置端口 firewall-cmd --add-port=${PG_PORT}/tcp --permanent firewall-cmd --reload # 移动配置文件 cp ${HomePath}/5.postgresql/pg_hba.conf /var/lib/pgsql/12/data/pg_hba.conf cp ${HomePath}/5.postgresql/postgresql.conf /var/lib/pgsql/12/data/postgresql.conf # 配置完成 systemctl restart postgresql-12 ;; "1") echo -e "${YELLOW}=====暂未实现=====${RES}" ;; "2") echo -e "${YELLOW}=====暂未实现=====${RES}" ;; "3") echo -e "${YELLOW}=====暂未实现=====${RES}" ;; esac } # 安装grafana installgrafana(){ echo "################################################" echo "# Please enter your choise: #" echo "# (0) 安装node export(性能数据采集) #" echo "# (1) 安装prometeus(性能监控系统) #" echo "# (2) 安装grafana(数据可视化系统) #" echo "# (3) 安装loki(日志管理系统) #" echo "# (4) 安装protail(日志采集器) #" echo "# (5) 安装sendmail(grafana发送邮件) #" echo "################################################" read cmd case $cmd in "0") mkdir -p /usr/local/node_exporter # 下载安装包 wget https://github.com/prometheus/node_exporter/releases/download/v1.1.1/node_exporter-1.1.1.linux-amd64.tar.gz tar -zxvf node_exporter-1.1.1.linux-amd64.tar.gz mv node_exporter-1.1.1.linux-amd64 /usr/local/node_exporter # 创建服务 cp -f ../8.grafana/node_exporter.service /usr/lib/systemd/system # 启动 systemctl daemon-reload systemctl start node_exporter.service systemctl enable node_exporter.service # TODO:需要检测是否安装,是否启动 ;; "1") mkdir -p /usr/local/prometheus wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz tar -xzvf prometheus-2.27.1.linux-amd64.tar.gz mv prometheus-2.27.1.linux-amd64 /usr/local/prometheus cp -f ../8.grafana/prometheus.sh /usr/local/prometheus cp -f ../8.grafana/prometheus.service /usr/lib/systemd/system systemctl daemon-reload systemctl enable prometheus.service systemctl start prometheus.service # TODO:需要检测是否安装,是否启动 ;; "2") wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.0.0-1.x86_64.rpm yum -y install grafana-enterprise-8.0.0-1.x86_64.rpm systemctl daemon-reload systemctl enable grafana-server systemctl start grafana-server # TODO:需要检测是否安装,是否启动 ;; "3") mkdir -p /usr/local/loki cp -f ../8.grafana/loki-linux-amd64 /usr/local/loki cp -f ../8.grafana/loki-local-config.yaml /usr/local/loki cp -f ../8.grafana/loki.sh /usr/local/loki cp -f ../8.grafana/loki.service /usr/lib/systemd/system # TODO:需要检测是否安装,是否启动 ;; "4") mkdir -p /usr/local/promtail cp -f ../8.grafana/promtail-linux-amd64 /usr/local/promtail cp -f ../8.grafana/promtail-local-config.yaml /usr/local/promtail cp -f ../8.grafana/promtail.sh /usr/local/promtail cp -f ../8.grafana/promtail.service /usr/lib/systemd/system # TODO:需要检测是否安装,是否启动 ;; "4") yum install -y sendmail systemctl enable sendmail systemctl start sendmail # TODO:需要手动修改/etc/grafana/grafana.ini ;; esac } # 安装系统常用指令 installcmd(){ echo "################################################" echo "# Please enter your choise: #" echo "# (0) 安装vim #" echo "# (1) 安装git #" echo "# (2) 安装lsof #" echo "# (3) 安装gcc #" echo "# (4) 安装make #" echo "# (t) 手动输入安装 #" echo "################################################" read input case $input in "0") echo -e "${YELLOW}=====暂未实现=====${RES}" ;; "1") echo -e "${YELLOW}=====暂未实现=====${RES}" ;; "2") installYum lsof ;; "3") echo -e "${YELLOW}=====暂未实现=====${RES}" ;; "4") echo -e "${YELLOW}=====暂未实现=====${RES}" ;; "t") echo -e "${YELLOW}=====暂未实现=====${RES}" ;; esac } # 菜单 menu(){ source ~/.bashrc echo "################################################" echo "# Please enter your choise: #" echo "# (0) 换源 #" echo "# (1) 安装系统常用指令 #" echo "# (2) 安装supervisor #" echo "# (3) 安装.net环境 #" echo "# (4) 安装mysql #" echo "# (5) 安装postgresql和插件 #" echo "# (6) 安装nginx #" echo "# (7) 安装redis #" echo "# (8) 安装grafana #" echo "# (q) Exit Menu #" echo "################################################" read input case $input in # 换源 "0") change ;; # 安装常用指令 "1") installcmd ;; # 安装supervior "2") installsuperviosr ;; "3") installdotnet ;; "4") installmysql ;; "5") installpg ;; "6") installnginx ;; "7") installredis ;; "8") installgrafana ;; "q") exit;; esac } # 主程序 HomePath=$(pwd) changefile ${HomePath} while menu do menu done
通过这次部署,对shell脚本有了一定的了解。但是本人能力有限,有问题欢迎互相讨论,然后完整的文件夹可以私信我。
想下次用qt实现一个跨平台的部署系统,带GUI,可以可视化配置,正好最近想学qt。
参考:
https://www.runoob.com/linux/linux-shell-array.html