陈佳浩
陈佳浩
发布于 2024-06-26 / 92 阅读
0
0

以前写的脚本,懒得改了

#!/bin/bash
#
#********************************************************************
#Author:				chenjiahao
#QQ:					1938191576
#Date:					2022-12-12
#FileName:				initialize.sh
#URL:					https://www.placjh.com
#Description:			The deploy script
#Copyright (C):			2022 All rights reserved
#********************************************************************

color () {
	RES_COL=60
	MOVE_TO_COL="echo -en \\033[${RES_COL}G"
	SETCOLOR_SUCCESS="echo -en \\033[1;32m"
	SETCOLOR_FAILURE="echo -en \\033[1;31m"
	SETCOLOR_WARNING="echo -en \\033[1;33m"
	SETCOLOR_NORMAL="echo -en \E[0m"
	echo -n "$1" && $MOVE_TO_COL
	echo -n "["
	if [ $2 = "success" -o $2 = "0" ] ;then
		${SETCOLOR_SUCCESS}
		echo -n $"  OK  "
	elif [ $2 = "failure" -o $2 = "1"  ] ;then 
		${SETCOLOR_FAILURE}
		echo -n $"FAILED"
	else
		${SETCOLOR_WARNING}
		echo -n $"WARNING"
	fi
	${SETCOLOR_NORMAL}
	echo -n "]"
	echo 
}

initialize() {
		Variable_setting() {
		#变量设置
			PREINSTALL_HOSTNAME=test.placjh.com
			ETH=eth0
			PREINSTALL_IPADDR=192.168.1.200
			PREINSTALL_NETMASK=24
			PREINSTALL_GATEWAY=192.168.1.1
			PREINSTALL_DNS1=192.168.1.17
			PREINSTALL_MIRRORS=my_repo

			. /etc/os-release
		}

		installing_software() {
		#安装软件
			if [ $ID == "centos" -o $ID == "rocky" ];then
				yum -y install lrzsz tar wget vim zip unzip bash-completion &>/dev/null
			elif [ $ID == "ubuntu" ];then
				apt update &>/dev/null
				apt -y install lrzsz tar curl -LO vim zip unzip bash-completion &>/dev/null
			else
				echo "不支持的OS"
				exit
			fi
		}

		close_selinux() {
		#关闭selinux
			sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config &>/dev/null
			setenforce 0 &>/dev/null
		}

	close_firewalld(){
		#关闭防火墙
		if [ $ID == "centos" ];then
			systemctl disable --now firewalld &>/dev/null
		elif [ $ID == "ubuntu" ];then
			systemctl disable --now ufw &>/dev/null
		else
			echo "不支持的OS"
			exit
		fi
	}

	Changing_the_Time_Zone() {
	#修改时区,安装chrony
		sudo timedatectl set-timezone Asia/Shanghai &>/dev/null
		if [ $ID == "centos" -o $ID == "rocky" ];then
			yum -y install chronyd &>/dev/null
		elif [ $ID == "ubuntu" ];then
			apt -y install chrony &>/dev/null
		else
			echo "不支持的OS"
			exit
		fi
		sed -i '/^server/s/^/#/' /etc/chrony.conf
		sed -i '/#server 3.centos.pool.ntp.org iburst/a server 192.168.1.251 iburst' /etc/chrony.conf
	}

	Modifying_a_Host_Name() {
	#修改主机名称
		read -p "请输入主机名,默认为$PREINSTALL_HOSTNAME" -t 15 MY_HOSTNAME
		hostnamectl set-hostname ${MY_HOSTNAME:-$PREINSTALL_HOSTNAME} &>/dev/null
	}

	Configuring_the_Source() {
	#配置源
		read -p "你要使用清华源还是阿里源[aliyun/tsinghua/my_repo],默认$PREINSTALL_MIRRORS,:" -t 15 MY_MIRRORS
		case ${MY_MIRRORS:-$PREINSTALL_MIRRORS} in
		tsinghua)
			if [ $ID == "centos" ];then
				sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' -i.bak /etc/yum.repos.d/CentOS-*.repo &>/dev/null
				wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-$VERSION_ID.repo &>/dev/null
			elif [ $ID == "ubuntu" ];then
				sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list &>/dev/null
				sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.tuna.tsinghua.edu.cn@g" /etc/apt/sources.list &>/dev/null
			else
				echo "不支持的OS!"
				exit
			fi
			;;
		aliyun)
			if [ $ID == "centos" ];then
				sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.aliyun.com|g' -i.bak /etc/yum.repos.d/CentOS-*.repo &>/dev/null
				wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-$VERSION_ID.repo &>/dev/null
			elif [ $ID == "ubuntu" ];then
				sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.aliyun.com@g" /etc/apt/sources.list &>/dev/null
				sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.aliyun.com@g" /etc/apt/sources.list &>/dev/null
			else
				echo "不支持的OS!"
				exit
			fi
			;;
		my_repo)
			mkdir /etc/yum.repos.d/backup
			mv /etc/yum.repos.d/CentOS* /etc/yum.repos.d/backup/
			cat > /etc/yum.repos.d/my.repo <<-EOF
			[epel]
			name=Extra Packages for Enterprise Linux 7 - $basearch
			baseurl=http://192.168.1.21/repository/yum/7/epel/
			enabled=1
			gpgcheck=0

			[base]
			name=Extra Packages for Enterprise Linux 7 - $basearch
			baseurl=http://192.168.1.21/repository/yum/7/base/
			enabled=1
			gpgcheck=0

			[updates]
			name=Extra Packages for Enterprise Linux 7 - $basearch
			baseurl=http://192.168.1.21/repository/yum/7/updates/
			enabled=1
			gpgcheck=0

			[extras]
			name=Extra Packages for Enterprise Linux 7 - $basearch
			baseurl=http://192.168.1.21/repository/yum/7/extras/
			enabled=1
			gpgcheck=0

			[docker-ce-stable]
			name=Extra Packages for Enterprise Linux 7 - $basearch
			baseurl=http://192.168.1.21/repository/yum/7/docker-ce-stable/
			enabled=1
			gpgcheck=0
			EOF
			;;
		*)
			echo "错误!请输入[aliyun/tsinghua/my_repo]"
			exit
		esac
	}

	Changing_a_NIC_Name() {
	#修改网卡名字为$ETH
		grep 'net.ifnames=0' /etc/default/grub &>/dev/null
		if ! [ $? -eq 0 ];then
			sudo sed -i '/^GRUB_CMDLINE_LINUX=/s/"$/net.ifnames=0"/' /etc/default/grub &>/dev/null
		fi
		ip a|grep eth0
		if ! [ $? -eq 0 ];then
			if [ $ID == "centos" ];then
				grub2-mkconfig -o /boot/grub2/grub.cfg &>/dev/null
			elif [ $ID == "ubuntu" ];then
				grub-mkconfig -o /boot/grub/grub.cfg &>/dev/null
			fi
		fi
	}

	Configuring_a_Static_IP_Address() {
	#配置静态IP
		read -p "请输入要修改的网卡名称,默认为$ETH,:" -t 15 MY_ETH
		read -p "请输入IP,默认为$PREINSTALL_IPADDR,:" -t 15 MY_IPADDR
		read -p "请输入掩码,默认为$PREINSTALL_NETMASK,:" -t 15 MY_NETMASK
		read -p "请输入网关,默认为$PREINSTALL_GATEWAY,:" -t 15 MY_GATEWAY
		read -p "请输入DNS,默认为$PREINSTALL_DNS1,:" -t 15 MY_DNS1
		if [ $ID == "centos" ];then
			cat > /etc/sysconfig/network-scripts/ifcfg-${MY_ETH:-$ETH} <<-EOF
			NAME=${MY_ETH:-$ETH}
			DEVICE=${MY_ETH:-$ETH}
			ONBOOT=yes
			IPADDR=${MY_IPADDR:-$PREINSTALL_IPADDR}
			PREFIX=${MY_NETMASK:-$PREINSTALL_NETMASK}
			GATEWAY=${MY_GATEWAY:-$PREINSTALL_GATEWAY}
			DNS1=${MY_DNS1:-$PREINSTALL_DNS1}
			EOF
			nmcli c reload && nmcli c up ${MY_ETH:-$ETH} &>/dev/null
		elif [ $ID == "ubuntu" ];then
			cat >> /etc/netplan/${MY_ETH:-$ETH}-installer-config.yaml <<-EOF
			network:
			  version: 2
			  ethernets:
			    ${MY_ETH:-$ETH}:
			      addresses: [${MY_IPADDR:-$PREINSTALL_IPADDR}/${MY_NETMASK:-$PREINSTALL_NETMASK}]
			      gateway4: ${MY_GATEWAY:-$PREINSTALL_GATEWAY}
			      nameservers:
			        addresses: [${MY_DNS1:-$PREINSTALL_DNS1}]
			EOF
			netplan apply &>/dev/null
		else
			echo "不支持的OS!"
			exit
		fi
	}
	bond () {
		BOND_NAME=bond0
		BOND_IP=10.0.0.123
		BOND_NETMASK=255.255.255.0
		BOND_GATEWAY=10.0.0.1
		BOND_DNS=223.5.5.5
		BOND_SLAVE1=ens33
		BOND_SLAVE2=ens34

		read -p "请输入你的bond名字,默认为$BOND_NAME:" -t 15 BOND_NAME0
		read -p "请输入你的bond网卡IP,默认为$BOND_IP:" -t 15 BOND_IP0
		read -p "请输入你的bond网卡掩码,默认为$BOND_NETMASK:" -t 15 BOND_NETMASK0
		read -p "请输入你的bond网卡网关,默认为$BOND_GATEWAY:" -t 15 BOND_GATEWAY0
		read -p "请输入你的bond网卡DNS,默认为$BOND_DNS:" -t 15 BOND_DNS0
		read -p "请输入你的工作网卡名1,默认为$BOND_SLAVE1:" -t 15 BOND_SLAVE10
		read -p "请输入你的工作网卡名2,默认为$BOND_SLAVE2:" -t 15 BOND_SLAVE20

		cat > /etc/sysconfig/network-scripts/ifcfg-${BOND_NAME0:-$BOND_NAME} <<-EOF
		DEVICE=${BOND_NAME0:-$BOND_NAME}
		TYPE=Ethernet
		ONBOOT=yes
		NM_CONTROLLED=no
		IPV6INIT=no
		NAME=${BOND_NAME0:-$BOND_NAME}
		BONDING_OPTS="miimon=100 mode=1"
		IPADDR=${BOND_IP0:-$BOND_IP}
		NETMASK=${BOND_NETMASK0:-$BOND_NETMASK}
		GATEWAY=${BOND_GATEWAY0:-$BOND_GATEWAY}
		DNS1=${BOND_DNS0:-$BOND_DNS0}
		EOF

		cat > /etc/sysconfig/network-scripts/ifcfg-${BOND_SLAVE10:-$BOND_SLAVE1} <<-EOF
		DEVICE=${BOND_SLAVE10:-$BOND_SLAVE1}
		TYPE=Ethernet
		ONBOOT=yes
		SLAVE=yes
		MASTER=${BOND_NAME0:-$BOND_NAME}
		NM_CONTROLLED=no
		EOF

		cat > /etc/sysconfig/network-scripts/ifcfg-${BOND_SLAVE20:-$BOND_SLAVE2} <<-EOF
		DEVICE=${BOND_SLAVE20:-$BOND_SLAVE2}
		TYPE=Ethernet
		ONBOOT=yes
		SLAVE=yes
		MASTER=${BOND_NAME0:-$BOND_NAME}
		NM_CONTROLLED=no
		EOF
		systemctl restart network
		cat /proc/net/bonding/${BOND_NAME0:-$BOND_NAME}
	}

	start_menu() {
		menu="
		安装基础软件
		关闭SELinux
		关闭防火墙
		修改时区,安装chrony
		修改主机名称
		配置源
		修改网卡名字
		配置静态IP
		执行以上8项
		配置bond(主备模式)
		重启
		返回上级
		"
		PS3="请输入要进行的操作:"
		select menu in $menu;do
		case $REPLY in
		1)
			echo "开始安装软件"
			installing_software
			;;
		2)
			echo "关闭SELinux"
			close_selinux
			;;
		3)
			echo "关闭防火墙"
			close_firewalld
			;;
		4)
			echo "修改时区,安装chrony"
			Changing_the_Time_Zone
			;;
		5)
			echo "修改主机名称"
			Modifying_a_Host_Name
			;;
		6)
			echo "配置源"
			Configuring_the_Source
			;;
		7)
			echo "修改网卡名字"
			Changing_a_NIC_Name
			;;
		8)
			echo "配置静态IP"
			Configuring_a_Static_IP_Address
			;;
		9)
			echo "开始初始化"
			installing_software
			close_selinux
			close_firewalld
			Changing_the_Time_Zone
			Modifying_a_Host_Name
			Configuring_the_Source
			Changing_a_NIC_Name
			Configuring_a_Static_IP_Address
			;;
		10)
			echo "配置bond(主备模式)"
			bond
			;;
		11)
			echo "重启"
			Restart_the_server
			;;
		12)
			func_menu
		esac
		done
	}

	Variable_setting
	start_menu
}

Restart_the_server() {
#重启
	reboot
}

Exit_script() {
#退出脚本
	echo "Bye~"
	exit
}
Installation_application() {
	install_docker(){
		UBUNTU_VERSION=5:20.10.17~3-0~ubuntu-focal
		CENTOS_VERSION=20.10.17-3.el7
		SPEED_UP='{
			"registry-mirrors": ["https://yct1mrfl.mirror.aliyuncs.com"]
		}'

		. /etc/os-release

		prepare(){
			if [ $ID = "ubuntu" ];then
				apt update &>/dev/null
				apt -y install curl -LO apt-transport-https ca-certificates curl software-properties-common &>/dev/null
				curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - &>/dev/null
				add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" &>/dev/null
				apt -y update &>/dev/null
			elif [ $ID = "centos" ];then
				if [ $VERSION_ID = "7" ];then
					yum -y install wget &>/dev/null
#					curl -LO -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo &>/dev/null
					yum clean all &>/dev/null
				elif [ $VERSION_ID = "8" ];then
					yum -y install wget &>/dev/null
					cat >> /etc/yum.repos.d/docker.repo <<-EOF
					[docker]
					name=docker
					gpgcheck=0
					baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/8/x86_64/stable/
					EOF
				else
					color 系统版本不支持!1
				fi
			else
				color OS不支持! 1
				exit
			fi
		}

		install(){
			if [ $ID = "ubuntu" ];then
				echo "列出所有docker-ce的包:"
				echo -e "\E[32;1m"
				apt-cache madison docker-ce
				echo -e "\E[0m"
				echo "请在下面输入要安装的版本号,默认为$UBUNTU_VERSION"
				read -p "请输入要安装的版本号:" -t 5 VERSION
				echo -e "\n开始安装。。。"
				apt -y install docker-ce=${VERSION:-$UBUNTU_VERSION} docker-ce-cli=${VERSION:-$UBUNTU_VERSION}  &>/dev/null
			elif [ $ID = "centos" ];then
				echo "列出所有docker-ce的包:"
				echo -e "\E[32;1m"
				yum list docker-ce --showduplicates
				echo -e "\E[0m"
				echo "请在下面输入要安装的版本号,默认为$CENTOS_VERSION"
				read -p "请输入要安装的版本号:" -t 5 VERSION
				echo -e "\n开始安装。。。"
				yum -y install docker-ce-${VERSION:-$CENTOS_VERSION} docker-ce-cli-${VERSION:-$CENTOS_VERSION} &>/dev/null
				systemctl enable --now docker
			else
				color OS不支持! 1
				exit
			fi
			if [ $? -eq 0 ];then
				color 安装完成 0
			else
				color 安装失败 1
			fi
		}

		speed_up(){
			if ! [ -f /etc/docker/daemon.json ];then
				[ -d /etc/docker ] || mkdir -p /etc/docker
				cat > /etc/docker/daemon.json <<-EOF
				$SPEED_UP
				EOF
				systemctl daemon-reload
				systemctl restart docker
				color 已添加镜像加速 0
			fi
		}

		prepare
		install
		speed_up
	}
	install_harbor() {
		VERSION=v2.8.0
		DIR=/apps
		PASSWORD=123456

		find_docker_compose(){
			docker --version &>/dev/null
			if [ ! $? -eq 0 ];then
				echo "请先安装docker"
				exit
			fi
			docker-compose version &>/dev/null
			if [ ! $? -eq 0 ];then
				echo "没有安装docker-compose,开始自动为你下载"
				curl -L https://github.com/docker/compose/releases/download/v2.11.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose &>/dev/null
				if [ ! $? -eq 0 ];then
					echo "下载docker-compose失败,请重试"
					rm -rf /usr/local/bin/docker-compose
					exit
				fi
				chmod +x /usr/local/bin/docker-compose &>/dev/null
			fi
		}
		download(){
			if [ ! -f harbor-offline-installer-$VERSION.tgz ];then
				curl -LO https://github.com/goharbor/harbor/releases/download/$VERSION/harbor-offline-installer-$VERSION.tgz &>/dev/null
				if [ ! $? -eq 0 ];then
					echo "下载失败,请重试"
					rm -rf harbor-offline-installer-$VERSION.tgz
					exit
				fi
			fi
			[ -d $DIR ] || mkdir $DIR
			tar xf harbor-offline-installer-$VERSION.tgz -C $DIR/
		}

		install(){
			if [ ! -f $DIR/harbor/harbor.yml ];then
				if [ -f $DIR/harbor/harbor.yml.tmpl ];then
					mv $DIR/harbor/harbor.yml.tmpl $DIR/harbor/harbor.yml
					sed -i "s/hostname: reg.mydomain.com/hostname: `hostname -I|awk '{print $1}'`/" $DIR/harbor/harbor.yml
					sed -i '/^https:/ s/^/#/' $DIR/harbor/harbor.yml
					sed -i '/port: 443/ s/^/#/p' $DIR/harbor/harbor.yml
					sed -i '/certificate:/ s/^/#/p' $DIR/harbor/harbor.yml
					sed -i '/private_key/ s/^/#/p' $DIR/harbor/harbor.yml
					sed -i "/harbor_admin_password:/ s/Harbor12345/$PASSWORD/" $DIR/harbor/harbor.yml
				elif [ -f $DIR/harbor/harbor.cfg ];then
					sed -i "s/hostname: reg.mydomain.com/hostname: `hostname -I|awk '{print $1}'`/" $DIR/harbor/harbor.cfg
					sed -i '/^https:/ s/^/#/' $DIR/harbor/harbor.cfg
					sed -i '/port: 443/ s/^/#/p' $DIR/harbor/harbor.cfg
					sed -i '/certificate:/ s/^/#/p' $DIR/harbor/harbor.cfg
					sed -i '/private_key/ s/^/#/p' $DIR/harbor/harbor.cfg
					sed -i "/harbor_admin_password:/ s/Harbor12345/$PASSWORD/" $DIR/harbor/harbor.cfg
				else
					echo "没有找到配置文件,部署失败"
					exit
				fi
			fi
			. /etc/os-release
			if [ $ID = "ubuntu" ];then
				apt -y install python3 &>/dev/null
			elif [ $ID = "centos" -o "rocky" ];then
				yum -y install python3 &>/dev/null
			else
				echo "请先手动安装python"
				exit
			fi
			cd $DIR/harbor
			./install.sh &>/dev/null
			if [ ! $? -eq 0 ];then
				echo "安装失败"
				rm -rf harbor-offline-installer-$VERSION.tgz
				exit
			fi
		}

		system(){
			cat > /lib/systemd/system/harbor.service <<-EOF
			[Unit]
			Description=Harbor
			After=docker.service systemd-networkd.service systemd-resolved.service
			Requires=docker.service
			Documentation=http://github.com/vmware/harbor

			[Service]
			Type=simple
			Restart=on-failure
			RestartSec=5
			ExecStart=/usr/local/bin/docker-compose -f $DIR/harbor/docker-compose.yml up
			ExecStop=/usr/local/bin/docker-compose -f $DIR/harbor/docker-compose.yml down

			[Install]
			WantedBy=multi-user.target
			EOF
			systemctl daemon-reload &>/dev/null
			systemctl enable  harbor &>/dev/null
			systemctl restart harbor &>/dev/null
			ss -ntl |grep 80 &>/dev/null
			if [ $? -eq 0 ];then
				echo -e "请访问:\E[32;1mhttp://`hostname -I |awk '{print $1}'`\E[0m"
				echo -e "用户名:\E[32;1madmin\E[0m"
				echo -e "密  码:\E[32;1m$PASSWORD\E[0m"
			else
				echo "启动失败"
			fi
		}
		find_docker_compose
		download
		install
		system
	}

	install_Elasticsearch(){
		ES_VERSION=7.17.5
		CLUSTER=ELK-Cluster
		NODE_ID=1
		DATADIR=/data
		IP='"10.0.0.101", "10.0.0.102","10.0.0.103"'
		NODENB=2
		MEMORY=2g

		. /etc/os-release
		MEM=`sed -n '/MemTotal/p' /proc/meminfo|awk -F" " '{print $2}'`
		if [ $MEM \< 3994668 ];then
			echo "你的内存不足,不可安装使用"
			exit
		fi
		echo "请在下方输入你要安装的Elasticsearch版本号,默认为${ES_VERSION1:-$ES_VERSION}"
		read -p "请输入你要安装的版本号:" -t 15 ES_VERSION1
		echo "请在下方输入节点编号,默认为$NODE_ID"
		read -p "请输入节点编号:" -t 15 NODE_ID1
		echo "请设置集群内最少要有几个节点在运行默认为$NODENB"
		read -p "请输入:" -t 15 NODENB1
		echo "请输入分配给JVM的内存限制,默认为$MEMORY"
		read -p "请输入:" -t 15 MEMORY1
		echo "请输入各节点IP,格式为"'"$IP"'"(注意首尾单引号)"
		read -p "请输入IP:" -t 30 IP1
		echo "请输入数据存放目录,默认为$DATADIR"
		read -p "请输入:" -t 15 DATADIR1
		download(){
		ES=`echo ${ES_VERSION1:-$ES_VERSION}|awk -F"." '{print $1}'`
		if ! [ -f elasticsearch-${ES_VERSION1:-$ES_VERSION}* ];then
			echo "下载安装包"
			if [ $ID = "rocky" -o $ID = "centos" ];then
				curl -LO https://mirrors.tuna.tsinghua.edu.cn/elasticstack/yum/elastic-$ES.x/${ES_VERSION1:-$ES_VERSION}/elasticsearch-${ES_VERSION1:-$ES_VERSION}-x86_64.rpm &>/dev/null
			elif [ $ID = "ubuntu" ];then
				curl -LO https://mirrors.tuna.tsinghua.edu.cn/elasticstack/apt/$ES.x/pool/main/e/elasticsearch/elasticsearch-${ES_VERSION1:-$ES_VERSION}-amd64.deb &>/dev/null
			else
				color OS不支持 1
				exit
			fi
			if ! [ $? -eq 0 ];then
				color 软件包下载失败 1
				exit
			fi
		fi
		if [ $ID = "rocky" -o $ID = "centos" ];then
			yum -y install elasticsearch-${ES_VERSION1:-$ES_VERSION}-x86_64.rpm &>/dev/null
		elif [ $ID = "ubuntu" ];then
			dpkg -i elasticsearch-${ES_VERSION1:-$ES_VERSION}-amd64.deb &>/dev/null
		else
			color OS不支持 1
			exit
		fi
		}

		modify(){
			echo "更改配置文件"
			grep vm.max_map_count /etc/sysctl.conf &>/dev/null
			if ! [ $? -eq 0 ];then
				echo "vm.max_map_count = 262144" >> /etc/sysctl.conf
				sysctl -p &>/dev/null
			fi
			grep 'nofile          1000000' /etc/security/limits.conf &>/dev/null
			if ! [ $? -eq 0 ];then
			cat >> /etc/security/limits.conf <<-EOF
				*               soft   core           unlimited
				*               hard   core           unlimited
				*               soft   nproc           1000000
				*               hard   nproc           1000000
				*               soft   nofile          1000000
				*               hard   nofile          1000000
				*               soft   memlock         32000
				*               hard   memlock         32000
				*               soft   msgqueue        8192000
				*               hard   msgqueue        8192000
				EOF
			fi
			grep cluster.name: $CLUSTER /etc/elasticsearch/elasticsearch.yml &>/dev/null
			if ! [ $? -eq 0 ];then
				sed -i "s/#cluster.name: my-application/cluster.name: $CLUSTER/" /etc/elasticsearch/elasticsearch.yml
				sed -i "s/#node.name: node-1/node.name: node-${NODE_ID1:-$NODE_ID}/" /etc/elasticsearch/elasticsearch.yml
				sed -i "s#path.data: /var/lib/elasticsearch#path.data: ${DATADIR1:-$DATADIR}/es-data#" /etc/elasticsearch/elasticsearch.yml
				sed -i "s#path.logs: /var/log/elasticsearch#path.logs: ${DATADIR1:-$DATADIR}/es-logs#" /etc/elasticsearch/elasticsearch.yml
				sed -i 's/#bootstrap.memory_lock: true/bootstrap.memory_lock: true/' /etc/elasticsearch/elasticsearch.yml
				sed -i 's/#network.host: 192.168.0.1/network.host: 0.0.0.0/' /etc/elasticsearch/elasticsearch.yml
				sed -i "/#discovery.seed/a discovery.seed_hosts: [${IP:-$IP1}]" /etc/elasticsearch/elasticsearch.yml
				sed -i "/#cluster.initial/a cluster.initial_master_nodes: [${IP:-$IP1}]" /etc/elasticsearch/elasticsearch.yml
				sed -i 's/#action.destructive_requires_name: true/action.destructive_requires_name: true/' /etc/elasticsearch/elasticsearch.yml
				sed -i "/#cluster.initial/a gateway.recover_after_nodes: ${NODENB1:-$NODENB}" /etc/elasticsearch/elasticsearch.yml
				mkdir -p /etc/systemd/system/elasticsearch.service.d/
				echo -e "[Service]\nLimitMEMLOCK=infinity" >> /etc/systemd/system/elasticsearch.service.d/override.conf
				sed -i "s/## -Xms4g/-Xms${MEMORY1:-$MEMORY}/" /etc/elasticsearch/jvm.options
				sed -i "s/## -Xmx4g/-Xmx${MEMORY1:-$MEMORY}/" /etc/elasticsearch/jvm.options
				sed -i 's/LimitNOFILE=65535/LimitNOFILE=1000000/' /usr/lib/systemd/system/elasticsearch.service
				sed -i 's/LimitNPROC=4096/LimitNPROC=65535/' /usr/lib/systemd/system/elasticsearch.service
				mkdir -p ${DATADIR1:-$DATADIR}/es-{data,logs}
				chown -R elasticsearch.elasticsearch ${DATADIR1:-$DATADIR}/es-{data,logs}
			fi
			color 更改完毕 0
		}

		run(){
			echo "启动"
			systemctl daemon-reload &>/dev/null
			systemctl enable --now elasticsearch.service &>/dev/null
			systemctl is-active elasticsearch.service &>/dev/null
			systemctl status elasticsearch.service &>/dev/null
			if [ $? -eq 0 ];then
				color 启动成功 0
			else
				color 启动失败 1
			fi
		}

		download
		modify
		run
	}

	install_beats() {
		BEATS=filebeat
		BEAT_VERSION=7.17.5

		. /etc/os-release
		echo "请在下面输入要安装的beat类型,默认为$BEATS"
		read -p "请输入要安装的beat类型:" -t 15 BEATS1
		echo "请在下面输入要安装的${BEATS1:-$BEATS}版本号,默认为$BEAT_VERSION"
		read -p "请输入要安装的${BEATS1:-$BEATS}版本号:" -t 15 BEAT_VERSION1
		if [ ! -f ${BEATS1:-$BEATS}-${BEAT_VERSION1:-$BEAT_VERSION}* ];then
			if [ $ID = "ubuntu" ];then
				curl -LO https://artifacts.elastic.co/downloads/beats/${BEATS1:-$BEATS}/${BEATS1:-$BEATS}-${BEAT_VERSION1:-$BEAT_VERSION}-amd64.deb &>/dev/null
			elif [ $ID = "centos" -o $ID = "rocky" ];then
				curl -LO https://artifacts.elastic.co/downloads/beats/${BEATS1:-$BEATS}/${BEATS1:-$BEATS}-${BEAT_VERSION1:-$BEAT_VERSION}-x86_64.rpm &>/dev/bull
			else
				echo "不支持的OS"
				exit
			fi
			if [ $? -eq 0 ];then
				echo "下载完毕,开始安装"
			else
				echo "下载失败"
				rm -rf ${BEATS1:-$BEATS}-${BEAT_VERSION1:-$BEAT_VERSION}-*
				exit
			fi
		fi

		if [ $ID = "ubuntu" ];then
			apt -y install ./${BEATS1:-$BEATS}-${BEAT_VERSION1:-$BEAT_VERSION}-amd64.deb &>/dev/null
		elif [ $ID = "centos" -o  $ID = "rocky" ];then
			yum -y install ${BEATS1:-$BEATS}-${BEAT_VERSION1:-$BEAT_VERSION}-x86_64.rpm &>/dev/null
		fi

		if [ $? -eq 0 ];then
			echo "安装完毕,启动"
		else
			echo "安装失败"
			exit
		fi
		systemctl enable --now ${BEATS1:-$BEATS}
	}

	install_kibana() {
		KIBANA_VERSION=7.17.5

		. /etc/os-release
		echo "请在下面输入要安装的kibana版本,默认为$KIBANA_VERSION"
		read -p "请输入要安装的beat类型:" -t 15 KIBANA_VERSION1
		if [ ! -f kibana-${KIBANA_VERSION1:-$KIBANA_VERSION}* ];then
			if [ $ID = "ubuntu" ];then
				curl -LO https://artifacts.elastic.co/downloads/kibana/kibana-${KIBANA_VERSION1:-$KIBANA_VERSION}-amd64.deb &>/dev/null
			elif [ $ID = "centos" -o $ID = "rocky" ];then
				curl -LO https://artifacts.elastic.co/downloads/kibana/kibana-${KIBANA_VERSION1:-$KIBANA_VERSION}-x86_64.rpm &>/dev/bull
			else
				echo "不支持的OS"
				exit
			fi
			if [ $? -eq 0 ];then
				echo "下载完毕,开始安装"
			else
				echo "下载失败"
				rm -rf kibana-${KIBANA_VERSION1:-$KIBANA_VERSION}-*
				exit
			fi
		fi

		if [ $ID = "ubuntu" ];then
			apt -y install ./kibana-${KIBANA_VERSION1:-$KIBANA_VERSION}-amd64.deb &>/dev/null
		elif [ $ID = "centos" -o $ID = "rocky" ];then
			yum -y install kibana-${KIBANA_VERSION1:-$KIBANA_VERSION}-x86_64.rpm &>/dev/null
		fi

		if [ $? -eq 0 ];then
			echo "安装完毕,启动"
		else
			echo "安装失败"
			exit
		fi
		systemctl enable --now kibana
	}

	install_logstash() {
		LOGSTASH_VERSION=7.17.5
		NODE_NAME=logstash.node1
		WORKER=2
		BULK=1000
		DELAY=50
		DATADIR=/var/lib/logstash
		LOGDIR=/var/log/logstash
		MAXIMUM_MEMORY=1g
		MEMORY_STACK_SIZE=1g
		RUN_AS=logstash

		. /etc/os-release
		echo "请在下面输入要安装的logstash版本号,默认为$LOGSTASH_VERSION"
		read -p "请输入你要安装的版本号:" -t 15 LOGSTASH_VERSION1
		echo "请在下面输入节点名称,默认为$NODE_NAME"
		read -p "请输入节点名称:" -t 15 NODE_NAME1
		download (){
			if [ $ID = "ubuntu" ];then
				if [ ! -d /usr/share/logstash ];then
					apt update &>/dev/null
					echo "检查JAVA,如果没有自动安装openjdk-11-jdk"
					java -version &>/dev/null || apt -y install openjdk-11-jdk &>/dev/null
					if [ ! -f logstash-${LOGSTASH_VERSION1:-$LOGSTASH_VERSION}-amd64.deb ];then
						echo "下载deb包"
						curl -LO https://artifacts.elastic.co/downloads/logstash/logstash-${LOGSTASH_VERSION1:-$LOGSTASH_VERSION}-amd64.deb &>/dev/null
						if [ ! $? -eq 0 ];then
							color 下载失败 1
							rm -rf logstash-${LOGSTASH_VERSION1:-$LOGSTASH_VERSION}-amd64.deb &>/dev/null
							exit
						fi
					fi
					echo "安装"
					apt -y install ./logstash-${LOGSTASH_VERSION1:-$LOGSTASH_VERSION}-amd64.deb &>/dev/null
				fi
			elif [ $ID = "centos" -o $ID = "rocky" ];then
				echo "检查JAVA,如果没有自动安装java-11-openjdk.x86_64"
				java -version &>/dev/null || yum -y install java-11-openjdk.x86_64 &>/dev/null
				if [ ! -f logstash-${LOGSTASH_VERSION1:-$LOGSTASH_VERSION}-x86_64.rpm ];then
					echo "下载rpm包"
					curl -LO https://artifacts.elastic.co/downloads/logstash/logstash-${LOGSTASH_VERSION1:-$LOGSTASH_VERSION}-x86_64.rpm &>/dev/null
					if [ ! $? -eq 0 ];then
						color 下载失败 1
						rm -rf logstash-${LOGSTASH_VERSION1:-$LOGSTASH_VERSION}-x86_64.rpm &>/dev/null
						exit
					fi
				fi
				echo "安装"
				yum -y install logstash-${LOGSTASH_VERSION1:-$LOGSTASH_VERSION}-x86_64.rpm &>/dev/null
			else
				color 不支持的OS 
				exit
			fi
		}
		disposition (){
			echo "修改配置"
			[-f /usr/local/bin/logstash ]  &>/dev/null || ln -s /usr/share/logstash/bin/logstash /usr/local/bin/logstash  &>/dev/null
			grep ${NODE_NAME1:-$NODE_NAME} /etc/logstash/logstash.yml &>/dev/null
			if [ ! $? -eq 0 ];then 
				sed -i "/node.name: test/a node.name: ${NODE_NAME1:-$NODE_NAME}" /etc/logstash/logstash.yml \
				&& sed -i "/pipeline.workers: 2/a pipeline.workers: $WORKER" /etc/logstash/logstash.yml \
				&& sed -i "/pipeline.batch.size: 125/a pipeline.batch.size: $BULK" /etc/logstash/logstash.yml \
				&& sed -i "/pipeline.batch.delay: 50/a pipeline.batch.delay: $DELAY" /etc/logstash/logstash.yml \
				&& sed -i "s#path.data: /var/lib/logstash#path.data: $DATADIR#" /etc/logstash/logstash.yml \
				&& sed -i "s#path.logs: /var/log/logstash#path.log: $LOGDIR#" /etc/logstash/logstash.yml \
				&& sed -i "s/^-Xms1g/-Xms$MAXIMUM_MEMORY/" /etc/logstash/jvm.options \
				&& sed -i "s/^-Xmx1g/-Xmx$MEMORY_STACK_SIZE/" /etc/logstash/jvm.options \
				&& sed -i "s/User=logstash/User=$RUN_AS/" /etc/systemd/system/logstash.service \
				&& sed -i "s/Group=logstash/Group=$RUN_AS/" /etc/systemd/system/logstash.service
			fi
		}
		run (){
			echo "启动"
			systemctl enable --now logstash &>/dev/null
			if [ $? -eq 0 ];then
				color 启动成功 0
			else
				color 启动失败 1
			fi
		}

		download
		disposition
		run
	}

	install_git(){
		GIT=2.37.1
		DIR=/apps/git
		echo "请输入要安装的git版本号,默认为$GIT"
		read -p "请输入:" -t 15 GIT1
		echo "请输入安装目录,默认为$DIR"
		read -p "请输入:" -t 15 DIR1
		download() {
			if ! [ -f v$GIT.tar.gz ];then
				color 莫得git源码包 1
				echo -e "\E[32;1m5秒后自动下载\E[0m"
				sleep 5
				echo -e "\E[32;1m开始下载\E[0m"
				curl -LO https://github.com/git/git/archive/refs/tags/v${GIT1:-$GIT}.tar.gz &>/dev/null
				if [ ! $? -eq 0 ];then
					echo "下载失败,请重试!"
					rm -rf v${GIT1:-$GIT}.tar.gz
					exit
				fi
			fi
			tar xf v${GIT1:-$GIT}.tar.gz
		}

		prepare(){
			echo -e "\E[32;1m下载依赖包\E[0m"
			yum -y install gcc make openssl-devel curl-devel expat-devel &>/dev/null
			color 完成 0
		}
		compile(){
			echo -e "\E[32;1m开始编译并安装\E[0m"
			[ -d ${DIR1:-$DIR} ] || mkdir -p ${DIR1:-$DIR} &>/dev/null
			cd git-${GIT1:-$GIT}
			make -j `grep -c processor /proc/cpuinfo` prefix=${DIR1:-$DIR} all &>/dev/null
			make prefix=/apps/git install &>/dev/null
			echo 'PATH=/apps/git/bin/:$PATH' > /etc/profile.d/git.sh
			. /etc/profile.d/git.sh
			color 完成 0
			git version
		}
		download
		prepare
		compile
	}

	install_gitlab() {
		GITLAB_VERSION=15.6.1
		HOST=gitlab.chen.com
		PASSWORD=12345678

		. /etc/os-release
		echo "请输入你要安装的gitlab版本号,默认$GITLAB_VERSION"
		read -p "请输入:" -t 15 GITLAB_VERSION1
		download() {
			if ! [ -f gitlab-ce-${GITLAB_VERSION1:-$GITLAB_VERSION}* ];then
				color "没有对应的gitlab包" 1
				color "5秒后自动下载" 0
				sleep 5
				echo -e "\E[32;1m开始下载\E[0m"
				if [ $ID == "ubuntu" ];then
					curl -LO https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/bionic/main/g/gitlab-ce/gitlab-ce-${GITLAB_VERSION1:-$GITLAB_VERSION}-ce.0_amd64.deb &>/dev/null
				elif [ $ID == "centos" ];then
					if [ $VERSION_ID == 7 ];then
						yum -y install libarchive
						curl -LO https://mirrors.bfsu.edu.cn/gitlab-ce/yum/el7/gitlab-ce-${GITLAB_VERSION1:-$GITLAB_VERSION}-ce.0.el7.x86_64.rpm &>/dev/null
					else
						echo "不支持的OS"
						exit
					fi
				else
					echo "不支持的OS"
					exit
				fi
				if [ ! $? -eq 0 ];then
					echo "下载失败,请重试!"
					rm -rf gitlab-ce-${GITLAB_VERSION1:-$GITLAB_VERSION}*
					exit
				fi
			fi
		}

		install(){
			if [ $ID == "ubuntu" ];then
				apt -y install ./gitlab-ce-${GITLAB_VERSION1:-$GITLAB_VERSION}-ce.0_amd64.deb &>/dev/null
			elif [ $ID == "centos" ];then
				yum -y install gitlab-ce-${GITLAB_VERSION1:-$GITLAB_VERSION}-ce.0.el7.x86_64.rpm &>/dev/null
			fi
			if [ $? -eq 0 ];then
				color "安装完毕" 0 
			else
				color "安装失败" 1
				exit
			fi
		}

		configuration(){
			grep -e  ^gitlab_rails /etc/gitlab/gitlab.rb &> /dev/null
			if ! [ $? -eq 0 ];then
				color "5秒后自动更改配置" 0
				sleep 5
				sed -i "s@external_url 'http://Rocky'@#external_url 'http://Rocky'@" /etc/gitlab/gitlab.rb
				cat >> /etc/gitlab/gitlab.rb <<-EOF
					external_url 'http://$HOST'
					gitlab_rails['smtp_enable'] = true
					gitlab_rails['smtp_address'] = "smtp.163.com"
					gitlab_rails['smtp_port'] = 465
					gitlab_rails['smtp_user_name'] = "placjh@163.com"
					gitlab_rails['smtp_password'] = "QKVJMQWCJYXZJMIL"
					gitlab_rails['smtp_domain'] = "163.com"
					gitlab_rails['smtp_authentication'] = "login"
					gitlab_rails['smtp_enable_starttls_auto'] = true
					gitlab_rails['smtp_tls'] = true
					gitlab_rails['gitlab_email_from'] = "placjh@163.com"
					gitlab_rails['initial_root_password'] = "$PASSWORD"
					gitlab_rails['backup_path'] = "/data/gitlab/backups"
				EOF
			fi
			gitlab-ctl reconfigure &>/dev/null
		}
		echo_logo(){
			echo -e '\n       *.                  *.\n      ***                 ***\n     *****               *****\n    .******             *******\n    ********            ********\n   ,,,,,,,,,***********,,,,,,,,,\n  ,,,,,,,,,,,*********,,,,,,,,,,,\n  .,,,,,,,,,,,*******,,,,,,,,,,,,\n      ,,,,,,,,,*****,,,,,,,,,.\n         ,,,,,,,****,,,,,,\n            .,,,***,,,,\n                ,*,.\n\n\n     _______ __  __          __\n    / ____(_) /_/ /   ____ _/ /_\n   / / __/ / __/ /   / __ `/ __ \ \n  / /_/ / / /_/ /___/ /_/ / /_/ /\n  \____/_/\__/_____/\__,_/_.___/\n'
		}
		run(){
			gitlab-ctl status >/dev/null
			if [ $? -eq 0 ];then
				echo_logo
				color "Gitlab安装完成!" 0
				echo -e "请访问链接:\E[32;1mhttp://$HOST/\E[0m"
				echo -e "用户和密码:\E[32;1mroot/$PASSWORD/\E[0m"
			else
				color "Gitlab安装失败!" 1
			fi
		}

		download
		install
		configuration
		run
	}

	install_jenkins() {
		JENKINS_VERSION=2.269

		. /etc/os-release
		echo "请在下面输入你要安装的Jenkins版本号,默认为$JENKINS_VERSION"
		read -p "请输入要安装的版本号:" -t 15 JENKINS_VERSION1
		ready_to_jdk(){
			java --version &>/dev/null
			if [ ! $? -eq 0 ];then
				echo "安装JDK"
				if [ $ID = "ubuntu" ];then
					apt update &>/dev/null
					apt -y install openjdk-11-jdk &>/dev/null
				elif [ $ID = "centos" -o $ID = "rocky" ];then
					yum -y install java-11-openjdk &>/dev/null
				fi
			fi
			if [ ! $? -eq 0 ];then
				color 安装JDK失败,请检查安装源 1
				exit
			fi
		}

		download(){
			if [ $ID = "ubuntu" ];then
				if [ ! -f jenkins_${JENKINS_VERSION1:-$JENKINS_VERSION}_all.deb ];then
					echo "下载deb包"
					curl -LO https://github.com/jenkinsci/jenkins/releases/download/jenkins-${JENKINS_VERSION1:-$JENKINS_VERSION}/jenkins_${JENKINS_VERSION1:-$JENKINS_VERSION}_all.deb &>/dev/null
					if [ ! $? -eq 0 ];then
						color 下载失败 1
						rm -rf jenkins_${JENKINS_VERSION1:-$JENKINS_VERSION}_all.deb &>/dev/null
						exit
					fi
				fi
			elif [ $ID = "centos" -o $ID = "rocky" ];then
				if [ ! -f jenkins-${JENKINS_VERSION1:-$JENKINS_VERSION}-1.1.noarch.rpm ];then
					echo "下载rpm包"
					curl -LO https://github.com/jenkinsci/jenkins/releases/download/jenkins-${JENKINS_VERSION1:-$JENKINS_VERSION}/jenkins-${JENKINS_VERSION1:-$JENKINS_VERSION}-1.1.noarch.rpm &>/dev/null
					if [ ! $? -eq 0 ];then
						color 下载失败 1
						rm -rf jenkins-${JENKINS_VERSION1:-$JENKINS_VERSION}-1.1.noarch.rpm &>/dev/null
						exit
					fi
				fi
			fi
			echo "开始安装"
			if [ $ID = "ubuntu" ];then
				apt -y install ./jenkins_${JENKINS_VERSION1:-$JENKINS_VERSION}_all.deb &>/dev/null
			elif [ $ID = "centos" -o $ID = "rocky" ];then
				yum -y install jenkins-${JENKINS_VERSION1:-$JENKINS_VERSION}-1.1.noarch.rpm &>/dev/null
			fi
		}

		run(){
			qd(){
				systemctl start jenkins &>/dev/null
				if [ ! $? -eq 0 ];then
					sleep 1
					qd
				fi
			}
			systemctl start jenkins &>/dev/null
			if [ ! $? -eq 0 ];then
				echo "某些版本可能会有启动不成功的情况,需要等待一些时间"
				qd
			fi
			/usr/lib/systemd/systemd-sysv-install enable jenkins &>/dev/null
			color 安装完毕 0
			AdminPassword(){
				if [ ! -f /var/lib/jenkins/secrets/initialAdminPassword ];then
					sleep 1
					AdminPassword
				fi
			}
			AdminPassword
			echo -e "请访问:\E[32;1mhttp://`hostname -I|awk '{print $1}'`:8080\E[0m"
			echo -e "密  码:\E[32;1m`cat /var/lib/jenkins/secrets/initialAdminPassword`\E[0m"
		}

		ready_to_jdk
		download
		run
	}

	install_prometheus() {
		VERSION=2.37.1
		DIR=/usr/local
		WEB_READ_TIMEOUT=5m
		WEB_MAX_CONNECTIONS=512
		STORAGE_TSDB_RETENTION=15d
		QUERY_TIMEOUT=2m
		QUERY_MAX_CONCURRENCY=20

		read -p "请输入你要安装的prometheus版本号,默认为$VERSION" -t 15 VERSION0
		read -p "请输入安装目录,默认为$DIR" -t 15 DIR0
		read -p "请输入请求连接的最大等待时间,默认为$WEB_READ_TIMEOUT" -t 15 WEB_READ_TIMEOUT0
		read -p "请输入最大链接数,默认为$WEB_MAX_CONNECTIONS" -t 15 WEB_MAX_CONNECTIONS0
		read -p "请设置数据存放时间,默认为$STORAGE_TSDB_RETENTION" -t 15 STORAGE_TSDB_RETENTION0
		read -p "请设置查询在中止前可能需要的最长时间,默认为$QUERY_TIMEOUT" -t 15 QUERY_TIMEOUT0
		read -p "请设置并发执行的最大查询数,默认为$QUERY_MAX_CONCURRENCY" -t 15 QUERY_MAX_CONCURRENCY0

		download(){
			if [ ! -f prometheus-${VERSION0:-$VERSION}.linux-amd64.tar.gz ];then
				echo "下载二进制资源" 
				curl -LO https://github.com/prometheus/prometheus/releases/download/v${VERSION0:-$VERSION}/prometheus-${VERSION0:-$VERSION}.linux-amd64.tar.gz &>/dev/null
				if [ $? -eq 0 ];then
					color 下载资源完毕 0
				else
					color 下载失败,请重试 1
					rm -rf prometheus-${VERSION0:-$VERSION}.linux-amd64.tar.gz &>/dev/null
					exit
				fi
			fi
		}

		deploy(){
			echo "解压并调整资源"
			[ -d ${DIR0:-$DIR} ] || mkdir ${DIR0:-$DIR}
			tar xf prometheus-${VERSION0:-$VERSION}.linux-amd64.tar.gz -C ${DIR0:-$DIR}/ &>/dev/null
			mv ${DIR0:-$DIR}/prometheus-${VERSION0:-$VERSION}.linux-amd64 ${DIR0:-$DIR}/prometheus &>/dev/null
			mkdir ${DIR0:-$DIR}/prometheus/bin ${DIR0:-$DIR}/prometheus/data ${DIR0:-$DIR}/prometheus/conf ${DIR0:-$DIR}/prometheus/logs &>/dev/null
			mv ${DIR0:-$DIR}/prometheus/prometheus ${DIR0:-$DIR}/prometheus/bin/ &>/dev/null
			mv ${DIR0:-$DIR}/prometheus/promtool ${DIR0:-$DIR}/prometheus/bin/ &>/dev/null
			cp  ${DIR0:-$DIR}/prometheus/prometheus.yml ${DIR0:-$DIR}/prometheus/conf &>/dev/null
			echo "准备启动变量"
			${DIR0:-$DIR}/prometheus/bin/prometheus --help |grep -Ev "^u|^T|^F"|sed 's/^/#/'|sed "1 i# Set the command-line arguments to pass to the server.\nARGS= --config.file=\"${DIR0:-$DIR}/prometheus/conf/prometheus.yml\" --web.read-timeout=${WEB_READ_TIMEOUT0:-$WEB_READ_TIMEOUT} --web.max-connections=${WEB_MAX_CONNECTIONS0:-$WEB_MAX_CONNECTIONS} --storage.tsdb.retention=${STORAGE_TSDB_RETENTION0:-$STORAGE_TSDB_RETENTION} --storage.tsdb.path=\"data/\" --query.timeout=${QUERY_TIMEOUT0:-$QUERY_TIMEOUT} --query.max-concurrency=${QUERY_MAX_CONCURRENCY0:-$QUERY_MAX_CONCURRENCY}" > ${DIR0:-$DIR}/prometheus/conf/prometheus
		}

		prepare(){
			id prometheus &>/dev/null ||useradd -r -s /sbin/nologin prometheus &>/dev/null && color 创建启动用户 0
			chown -R prometheus.prometheus ${DIR0:-$DIR}/prometheus &>/dev/null
			echo "生成PATH变量"
			echo "PATH=/usr/local/prometheus/bin:\$PATH" > /etc/profile.d/prometheus.sh
			echo "准备service文件"
			cat > /lib/systemd/system/prometheus.service <<-EOF
			[Unit]
			Description=Prometheus Server
			Documentation=https://prometheus.io/docs/introduction/overview/
			After=network.target

			[Service]
			Restart=on-failure
			User=prometheus
			Group=prometheus
			EnvironmentFile=${DIR0:-$DIR}/prometheus/conf/prometheus
			WorkingDirectory=${DIR0:-$DIR}/prometheus/
			ExecStart=${DIR0:-$DIR}/prometheus/bin/prometheus \$ARGS
			ExecReload=/bin/kill -HUP $MAINPID
			LimitNOFILE=65535

			[Install]
			WantedBy=multi-user.target
			EOF
		}

		run(){
			echo "启动"
			systemctl daemon-reload &>/dev/null
			systemctl enable --now prometheus &>/dev/null
			if [ $? -eq 0 ];then
				color prometheus启动成功 0
				echo -e "请访问:\E[32;1mhttp://`hostname -I |awk '{print $1}'`:9090\E[0m"
			else
				color prometheus启动失败,请查看journalctl -xe
			fi
		}

		download
		deploy
		prepare
		run
	}

	install_node_exporter() {
		VERSION=1.4.0
		DIR=/usr/local

		read -p "请输入你要安装的node_exporter版本号,默认为$VERSION:" -t 15 VERSION0
		read -p "请输入安装位置,默认为$DIR" -t 15 DIR0

		download(){
			if [ ! -f node_exporter-${VERSION0:-$VERSION}.linux-amd64.tar.gz ];then
				echo "下载二进制文件"
				curl -LO https://github.com/prometheus/node_exporter/releases/download/v${VERSION0:-$VERSION}/node_exporter-${VERSION0:-$VERSION}.linux-amd64.tar.gz &>/dev/null
				if [ $? -eq 0 ];then
					color 下载完毕 0
				else
					color 下载失败,请重试 1
					rm -rf node_exporter-${VERSION0:-$VERSION}.linux-amd64.tar.gz &>/dev/null
					exit
				fi
			fi
		}

		deploy(){
			echo "解压缩"
			tar xf node_exporter-${VERSION0:-$VERSION}.linux-amd64.tar.gz -C ${DIR0:-$DIR}/ &>/dev/null
			mv ${DIR0:-$DIR}/node_exporter-${VERSION0:-$VERSION}.linux-amd64 ${DIR0:-$DIR}/node_exporter &>/dev/null
			echo "准备启动变量"
			${DIR0:-$DIR}/node_exporter/node_exporter --help |grep -Ev "^u|^F"|sed 's/^/#/'|sed "1 i# Set the command-line arguments to pass to the server.\n# Due to shell scaping, to pass backslashes for regexes, you need to double\n# them (\\d for \d). If running under systemd, you need to double them again\n# (\\\\d to mean \d), and escape newlines too.\nARGS=\"\"" >${DIR0:-$DIR}/node_exporter/default
		}

		prepare(){
			echo "为二进制程序创建软连接"
			ln -s ${DIR0:-$DIR}/node_exporter/node_exporter /usr/local/sbin/node_exporter &>/dev/null
			id prometheus &>/dev/null || useradd -r -s /sbin/nologin prometheus &>/dev/null
			cat > /lib/systemd/system/node-exporter.service <<-EOF
			[Unit]
			Description=Prometheus exporter for machine metrics
			Documentation=https://github.com/prometheus/node_exporter

			[Service]
			Restart=always
			User=prometheus
			EnvironmentFile=${DIR0:-$DIR}/node_exporter/default
			ExecStart=/usr/local/sbin/node_exporter \$ARGS
			ExecReload=/bin/kill -HUP $MAINPID
			TimeoutStopSec=20s
			SendSIGKILL=no

			[Install]
			WantedBy=multi-user.target
			EOF
		}

		run(){
			echo "启动"
			systemctl daemon-reload &>/dev/null
			systemctl enable --now node-exporter.service &>/dev/null
		      	if [ $? -eq 0 ];then
				color node-exporter启动成功 0
				echo -------------------------------------------------------------------
				echo -e "请访问:\E[32;1mhttp://`hostname -I |awk '{print $1}'`:9100\E[0m"
			else
				color node-exporter启动失败,请检查journalctl -xe
			fi
		}
		download
		deploy
		prepare
		run
	}

	install_pushgateway() {
		VERSION=1.4.2
		DIR=/usr/local

		read -p "请输入你要安装的pushgateway版本号,默认为$VERSION:" -t 15 VERSION0
		read -p "请输入安装位置,默认为$DIR:" -t 15 DIR0
		download(){
			if [ ! -f pushgateway-${VERSION0:-$VERSION}.linux-amd64.tar.gz ];then
				echo "下载二进制资源"
				curl -LO https://github.com/prometheus/pushgateway/releases/download/v${VERSION0:-$VERSION}/pushgateway-${VERSION0:-$VERSION}.linux-amd64.tar.gz &>/dev/null
				if [ $? -eq 0 ];then
					color 下载完毕 0
				else
					color 下载失败,请重试 1
					rm -rf pushgateway-${VERSION0:-$VERSION}.linux-amd64.tar.gz &>/dev/null
					exit
				fi
			fi
		}

		deploy(){
			[ -d ${DIR0:-$DIR} ] || mkdir -p ${DIR0:-$DIR}
			echo "解压部署"
			tar xf pushgateway-${VERSION0:-$VERSION}.linux-amd64.tar.gz -C ${DIR0:-$DIR}/ &>/dev/null
			mv ${DIR0:-$DIR}/pushgateway-${VERSION0:-$VERSION}.linux-amd64 ${DIR0:-$DIR}/pushgateway &>/dev/null
		}

		prepare(){
			ln -s ${DIR0:-$DIR}/pushgateway/pushgateway /usr/local/sbin/pushgateway &>/dev/null
			cat > /lib/systemd/system/pushgateway.service <<-EOF
			[Unit]
			Description=Pushgateway
			Documentation=https://github.com/prometheus/pushgateway/
			After=network.target

			[Service]
			Type=simple
			Restart=on-failure
			ExecStart=/usr/local/sbin/pushgateway
			ExecReload=/bin/kill -HUP $MAINPID

			[Install]
			WantedBy=multi-user.target
			EOF
		}
		run(){
			echo "启动"
			systemctl daemon-reload 
			systemctl enable --now pushgateway.service &>/dev/null
			if [ $? -eq 0 ];then
				color pushgateway启动成功 0
				echo -------------------------------------------------------------------
				echo -e "请访问:\E[32;1mhttp://`hostname -I |awk '{print $1}'`:9091\E[0m"
			else
				color pushgateway启动失败,请查看journalctl -xe
			fi
		}

		download
		deploy
		prepare
		run
	}

	install_nginx() {
		NGINX=nginx-1.18.0
		DIRE=/apps/nginx
		COMPILE_OPTION="--prefix=${DIRE0:-$DIRE} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module"

		read -p "请输入要安装的nginx版本,默认为$NGINX:" -t 15 NGINX0
		read -p "请输入要安装的位置,默认为$DIRE:" -t 15 DIRE0
		read -p "请输入编译选项,默认$COMPILE_OPTION :" -t 20 COMPILE_OPTION0
		. /etc/os-release

		dependence(){
			echo "安装依赖包"
			if [ $ID = "centos" -o $ID = "rocky" ];then
				yum -y install tar wget make gcc pcre-devel openssl-devel zlib-devel &> /dev/null 
			elif [ $ID = "ubuntu" ];then
				apt update
				apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g zlib1g-dev &>/dev/null
			else
				color 不支持的OS
			fi
		}

		user(){
			echo 创建启动账号,创建安装目录
			useradd -s /sbin/nologin nginx &>/dev/null
			mkdir /apps/nginx -p
		}

		download(){
			echo "检查源码包"
			if [ ! -f ${NGINX0:-$NGINX}.tar.gz ];then
				echo "下载资源"
				curl -LO http://nginx.org/download/${NGINX0:-$NGINX}.tar.gz &> /dev/null
				if [ ! $? -eq 0 ];then
					color 下载失败 1
					rm -rf ${NGINX0:-$NGINX}.tar.gz
					exit
				fi
			fi
			echo "解压资源"
			tar xf ${NGINX0:-$NGINX}.tar.gz &> /dev/null
			color 完毕 0
		}

		install(){
			echo "编译安装"
			cd ${NGINX0:-$NGINX}/
			./configure ${COMPILE_OPTION:-$COMPILE_OPTION0} &> /dev/null
			make &> /dev/null && make install &> /dev/null
			if [ ! $? -eq 0 ];then
				color 安装失败,请排查编译安装步骤 1
				exit
			fi
		}

		prepare(){
			echo "准备"
			ln -s /apps/nginx/sbin/nginx /usr/local/bin/ &>/dev/null
			chown -R nginx.nginx ${DIRE0:-$DIRE}
			echo "创建service文件"
			cat > /lib/systemd/system/nginx.service <<-EOF
			[Unit]
			Description=The nginx HTTP and reverse proxy server
			After=network.target remote-fs.target nss-lookup.target

			[Service]
			Type=forking
			PIDFile=${DIRE0:-$DIRE}/logs/nginx.pid
			ExecStartPre=/bin/rm -f ${DIRE0:-$DIRE}/logs/nginx.pid
			ExecStartPre=${DIRE0:-$DIRE}/sbin/nginx -t
			ExecStart=${DIRE0:-$DIRE}/sbin/nginx
			ExecReload=/bin/kill -s HUB \$MAINPID
			KillSignal=SIGQUIT
			TimeoutStopSec=5
			KillMode=process
			PrivateTmp=true
			LimitNOFILE=100000

			[Install]
			WantedBy=multi-user.target
			EOF
			color 完毕 0
		}

		start_nginx(){
			echo "启动nginx"
			systemctl daemon-reload
			systemctl enable --now nginx
			if [ $? -eq 0 ];then
				color 启动成功 0
			else
				color 启动失败,请查看journalctl -xe 2
			fi
		}
		dependence
		user
		download
		install
		prepare
		start_nginx
	}

	Container_management() {
		menu="
		安装docker-ce
		安装docker-harbor
		返回上级
		"
		PS3="请输入要进行的操作:"
		select menu in $menu;do
		case $REPLY in
		1)
			install_docker
			;;
		2)
			install_harbor
			;;
		3)
			install_menu
			;;
		*)
			echo "错误!"
		esac
		done
	}

	ELK () {
		menu="
		部署Elasticsearch
		安装beats
		安装kibana
		安装logstash
		返回上级
		"
		PS3="请输入要进行的操作:"
		select menu in $menu;do
		case $REPLY in
		1)
			install_Elasticsearch
			;;
		2)
			install_beats
			;;
		3)
			install_kibana
			;;
		4)
			install_logstash
			;;
		5)
			install_menu
			;;
		*)
			echo "错误!"
		esac
		done
	}

	code_management () {
		menu="
		安装git工具
		安装gitlab
		安装jenkins
		返回上级
		"
		PS3="请输入要进行的操作:"
		select menu in $menu;do
		case $REPLY in
		1)
			install_git
			;;
		2)
			install_gitlab
			;;
		3)
			install_jenkins
			;;
		4)
			install_menu
			;;
		*)
			echo "错误!"
		esac
		done
	}

	monitoring_platform () {
		menu="
		部署prometheus监控平台
		部署node-exporter
		部署pushgateway
		返回上级
		"
		PS3="请输入要进行的操作:"
		select menu in $menu;do
		case $REPLY in
		1)
			install_prometheus
			;;
		2)
			install_node_exporter
			;;
		3)
			install_pushgateway
			;;
		4)
			install_menu
			;;
		*)
			echo "错误!"
		esac
		done
	}

	WEB () {
		menu="
		部署nginx
		部署tomcat
		返回上级
		"
		PS3="请输入要进行的操作:"
		select menu in $menu;do
		case $REPLY in
		1)
			install_nginx
			;;
#		2)
#			install_tomcat
#			;;
		3)
			install_menu
			;;
		*)
			echo "错误!"
		esac
		done
	}

	install_menu() {
		menu="
		容器管理
		ELK
		代码管理
		监控平台
		WEB服务
		返回上级
		"
		PS3="请输入要进行的操作:"
		select menu in $menu;do
		case $REPLY in
		1)
			Container_management
			;;
		2)
			ELK
			;;
		3)
			code_management
			;;
		4)
			monitoring_platform
			;;
		5)
			WEB
			;;
		6)
			func_menu
			;;
		*)
			echo "错误!"
		esac
		done
	}
	install_menu
}

#菜单
func_menu() {
menu="
初始化
安装软件
退出脚本
"
PS3="请输入要进行的操作:"
select menu in $menu;do
case $REPLY in
1)
	initialize
	;;
2)
	Installation_application
	;;
3)
	Exit_script
	;;
*)
	echo "错误!"
esac
done
}

func_menu


评论