1+ #! /bin/bash
2+ # ########################
3+ # 提供一键部署https服务环境的脚本
4+ # 包含api服务,socket服务
5+ # @auther: iamtsm
6+ # @version: v1.0.0
7+ # ########################
8+
9+ # Function to install Node.js 16
10+ install_node () {
11+ echo " ======>Node.js is not installed. Installing Node.js 16..."
12+ curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
13+ sudo yum install -y nodejs
14+ echo " ======>Node.js 16 installed"
15+ }
16+
17+ # Function to install pm2 globally
18+ install_pm2 () {
19+ echo " ======>pm2 is not installed. Installing pm2 globally..."
20+ sudo npm install -g pm2
21+ echo " ======>pm2 installed"
22+ }
23+
24+ # Function to install lsof
25+ install_lsof () {
26+ echo " ======>lsof is not installed. Installing lsof..."
27+ sudo yum install -y lsof
28+ echo " ======>lsof installed"
29+ }
30+
31+ # Wait for a command to become available
32+ wait_for_command () {
33+ command=" $1 "
34+ while ! command -v $command & > /dev/null; do
35+ sleep 1
36+ done
37+ }
38+
39+ # Step 1: Check if sudo is installed and install if not
40+ if ! command -v sudo & > /dev/null; then
41+ echo " ======>sudo is not installed. Installing sudo..."
42+ yum install -y sudo
43+ fi
44+
45+ # Step 2: Check if curl is installed
46+ if ! command -v curl & > /dev/null; then
47+ echo " ======>curl is not installed. Installing curl..."
48+ sudo yum install -y curl
49+ fi
50+
51+ # Step 3: Check if Node.js is installed and install Node.js 16 if not
52+ if ! command -v node & > /dev/null; then
53+ install_node
54+ else
55+ echo " ======>Node.js is already installed"
56+ fi
57+
58+ # Wait for Node.js to be installed
59+ wait_for_command node
60+
61+ # Step 4: Output Node.js and npm versions
62+ node_version=$( node -v)
63+ npm_version=$( npm -v)
64+ echo " ======>Node.js version: $node_version "
65+ echo " ======>npm version: $npm_version "
66+
67+ # Step 5: Check if pm2 is installed and install it globally if not
68+ if ! command -v pm2 & > /dev/null; then
69+ install_pm2
70+ else
71+ echo " ======>pm2 is already installed"
72+ fi
73+
74+ # Wait for pm2 to be installed
75+ wait_for_command pm2
76+
77+ # Step 6: Check if lsof is installed and install if not
78+ if ! command -v lsof & > /dev/null; then
79+ install_lsof
80+ else
81+ echo " ======>lsof is already installed"
82+ fi
83+
84+ # Step 7: Check if ports 9092 and 8444 are occupied
85+ port_9092_in_use=$( sudo lsof -i :9092 | grep LISTEN | wc -l)
86+ port_8444_in_use=$( sudo lsof -i :8444 | grep LISTEN | wc -l)
87+
88+ if [ " $port_9092_in_use " -gt 0 ] || [ " $port_8444_in_use " -gt 0 ]; then
89+ echo " ======>Port 9092 or 8444 is already in use."
90+ exit 1
91+ fi
92+
93+ # Step 8: install npm packages
94+ echo " Ready to install npm packages"
95+ cd ../../svr/
96+ rm package-lock.json
97+ npm install --registry=https://registry.npmmirror.com
98+
99+ # Step 9: Run start-https.sh script to start the service
100+ echo " Ready to run auto-start-https.sh"
101+ sleep 1
102+ /bin/bash ./../bin/ubuntu16/auto-start-https.sh
0 commit comments