-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployr.sh
186 lines (135 loc) · 4.52 KB
/
deployr.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
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
# Variables
NGINX_CONF_PATH="/etc/nginx/nginx.conf"
DEPLOY_DIR="/.deployr"
STATIC_REPO_URL="https://github.com/adityacodes30/deployr-daemon.git"
GO_EXECUTABLE="server"
DEPLOY_SCRIPT="deployr-daemon.sh"
i# Variables
if [ "$#" -ne 4 ]; then
echo "Usage: $0 <TARGET_NEXTJS_REPO_URL> <DOMAIN> <EMAIL> <DEPLOYR_PUBKEY>"
exit 1
fi
TARGET_NEXTJS_REPO_URL="$1"
DOMAIN="$2"
EMAIL="$3"
DEPLOYR_PUBKEY="$4"
echo "Test passed - DEPLOYR_PUBKEY is set and persistent"
echo "Current value: $DEPLOYR_PUBKEY"
echo "Updating system..."
sudo yum update -y
echo "Installing Nginx, Node.js, NPM, and Certbot..."
sudo amazon-linux-extras enable nginx1
sudo yum install -y nginx nodejs npm certbot python3-certbot-nginx git
echo "Starting Nginx..."
sudo systemctl start nginx
sudo systemctl enable nginx
sudo sh -c "echo 'DEPLOYR_PUBKEY=\"$DEPLOYR_PUBKEY\"' >> /etc/environment"
# Load it for current session
export DEPLOYR_PUBKEY="$DEPLOYR_PUBKEY"
echo "Testing DEPLOYR_PUBKEY..."
# Test 1: Check if variable is set
if [ -z "$DEPLOYR_PUBKEY" ]; then
echo "Error: DEPLOYR_PUBKEY is not set"
exit 1
fi
if [ "$DEPLOYR_PUBKEY" = "$4" ]; then
echo "✅ DEPLOYR_PUBKEY is set correctly"
else
echo "Error: DEPLOYR_PUBKEY does not match the input value"
echo "Expected: $4"
echo "Got: $DEPLOYR_PUBKEY"
exit 1
fi
if grep -q "DEPLOYR_PUBKEY=\"$4\"" /etc/environment; then
echo "DEPLOYR_PUBKEY is properly set in /etc/environment"
else
echo "Error: DEPLOYR_PUBKEY not found in /etc/environment"
exit 1
fi
echo "Updating Nginx configuration to proxy to the application..."
sudo tee "$NGINX_CONF_PATH" > /dev/null <<EOF
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" '
'"\$http_user_agent" "\$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name $DOMAIN;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://localhost:3000;
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;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
location /deployr/ {
proxy_pass http://localhost:6213/;
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;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \$scheme;
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
EOF
sleep 1
echo "Restarting Nginx..."
sudo systemctl restart nginx
sleep 2
echo "Running Certbot for HTTPS configuration..."
sudo certbot --nginx --non-interactive --agree-tos -m "$EMAIL" -d "$DOMAIN"
sleep 1
echo "Installing PM2..."
sudo npm install -g pm2
echo "Cloning static deployr repository into /.deployr..."
if [ -d "$DEPLOY_DIR" ]; then
sudo rm -rf "$DEPLOY_DIR"
fi
sudo mkdir "$DEPLOY_DIR"
sudo git clone "$STATIC_REPO_URL" "$DEPLOY_DIR"
sleep 1
echo "Making Go server and deploy script executable..."
sudo chmod +x "$DEPLOY_DIR/$GO_EXECUTABLE"
sudo chmod +x "$DEPLOY_DIR/$DEPLOY_SCRIPT"
echo "Starting Go server using PM2..."
pm2 delete go-server || true
pm2 start "$DEPLOY_DIR/$GO_EXECUTABLE" --name go-server -- "$TARGET_NEXTJS_REPO_URL"
echo "Restarting Nginx..."
sudo systemctl restart nginx
pm2 save
echo "Setup completed successfully!"