-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstartup.sh
173 lines (128 loc) · 4.08 KB
/
startup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
# Exit script if any command fails
set -e
# Function to handle errors
error_handler() {
echo "Error: An error occurred at line $1"
exit 1
}
# Trap errors
trap 'error_handler $LINENO' ERR
# Check if Node.js is installed
if ! command -v node &> /dev/null
then
# Log message
echo "Installing Node.js..."
# Check if nvm is installed
if ! command -v nvm &> /dev/null
then
# Log message
echo "Installing nvm..."
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
fi
# Load nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
# Install the latest stable version of Node.js using nvm
nvm install stable
fi
# Update package index files
sudo apt-get update
# Check if git is installed
if ! command -v git &> /dev/null
then
# Log message
echo "Installing git..."
# Install git
sudo apt-get install -y git
fi
# Check if Nginx is installed
if ! command -v nginx &> /dev/null
then
# Log message
echo "Installing Nginx..."
# Install Nginx
sudo apt-get install -y nginx --fix-missing
fi
echo "Creating SSL certificate..."
# Configure Nginx to use SSL
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/ssl/private/nginx-selfsigned.key -out /etc/ssl/certs/nginx-selfsigned.crt -subj "/C=US/ST=State/L=City/O=OrgName/OU=IT Department/CN=$(hostname).com"
sudo mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old
echo "Creating Nginx configuration file..."
# Create the default Nginx configuration file if it doesn't exist
if [ ! -f /etc/nginx/sites-available/default ]; then
sudo touch /etc/nginx/sites-available/default
fi
# Give the default Nginx configuration file proper permissions
sudo chmod 777 /etc/nginx/sites-available/default
echo "Configuring Nginx..."
# Configure Nginx
sudo cat > /etc/nginx/sites-available/default <<EOF
server {
listen 80;
listen [::]:80;
server_name _;
return 301 https://\$host\$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name _;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
location /api/ {
proxy_pass http://localhost:3001/;
proxy_http_version 1.1;
proxy_set_header Upgrade \$http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host \$host;
proxy_cache_bypass \$http_upgrade;
}
location / {
root /var/www/html;
}
}
EOF
sudo chmod o-w /etc/nginx/sites-available/default
sudo systemctl restart nginx
# Create the local script file
# touch local_script.sh
# Make the script executable
# chmod +x local_script.sh
# echo "@reboot ./local_script.sh" | crontab -
# Log message
echo "Cloning Node.js server repository..."
# Clone the Node.js server repository
git clone https://github.com/peaqnetwork/peaq-rpi-server.git
# Navigate to the server directory
cd peaq-rpi-server
# Log message
echo "Installing dependencies..."
# Install dependencies
npm i
# Log message
echo "Installing PM2..."
# Install PM2
npm install pm2@latest -g
echo "Installing PM2 Logrotate..."
# Install PM2 Logrotate
pm2 install pm2-logrotate
# Log message
echo "Starting server using PM2..."
# Create the build
npm run build
# Start the server using PM2
pm2 start build/src/server.js --watch --name "peaq-server"
# Start the update script using PM2 which will be restarted every 20 seconds (update the time interval to 1 min)
pm2 start bash --exp-backoff-restart-delay=30000 --name "startup-script" -- -c "curl -H 'Cache-Control: no-cache, no-store' -s https://raw.githubusercontent.com/peaqnetwork/peaq-rpi-server/dev/update.sh | bash"
# Save the PM2 process list
pm2 save
# Extract the Startup Script command from the output of 'pm2 startup'
startup_script=$(pm2 startup | awk '/sudo/ {$1=""; print "sudo " $0}')
# Execute the Startup Script command
eval "$startup_script"
# Save the PM2 process list
pm2 save
# Log success message
echo "Script completed successfully!"