-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathn8n.sh
More file actions
259 lines (234 loc) · 6.98 KB
/
n8n.sh
File metadata and controls
259 lines (234 loc) · 6.98 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/bin/bash
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[1;34m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
RESET='\033[0m'
# Functions
error_exit() {
echo -e "\n${RED}❌ Error: $1${RESET}" >&2
exit 1
}
info() {
echo -e "${CYAN}ℹ️ $1${RESET}"
}
success() {
echo -e "${GREEN}✅ $1${RESET}"
}
line() {
echo -e "${BLUE}───────────────────────────────────────────${RESET}"
}
# Show menu
show_menu() {
clear
echo -e "${CYAN}By --> Peyman * Github.com/Ptechgithub * ${RESET}"
echo ""
echo -e "${YELLOW}╔══════════════════════════════════════════╗${RESET}"
echo -e "${YELLOW}║${CYAN} n8n Installer Menu ${YELLOW}║${RESET}"
echo -e "${YELLOW}╠══════════════════════════════════════════╣"
echo -e "${YELLOW}║ 1️⃣ Install n8n ║${RESET}"
echo -e "${YELLOW}║ 2️⃣ Uninstall n8n ║${RESET}"
echo -e "${YELLOW}║ 3️⃣ Exit ║${RESET}"
echo -e "${YELLOW}╚══════════════════════════════════════════╝${RESET}"
echo
read -p "📌 Select an option [1-3]: " OPTION
}
# Install n8n
install_n8n() {
if [ -d "n8n-docker" ]; then
line
success "n8n appears to be already installed. Skipping installation."
return
fi
# Check root
if [ "$EUID" -ne 0 ]; then
error_exit "Please run this script as root ⚠️"
fi
# Install Docker if missing
if ! command -v docker &>/dev/null; then
info "Docker not found. Installing Docker 🐳..."
curl -fsSL https://get.docker.com -o get-docker.sh || error_exit "Failed to download Docker install script."
sh get-docker.sh || error_exit "Docker installation failed."
rm get-docker.sh
else
line
success "Docker is already installed 🐳"
fi
# Install Docker
if ! command -v docker-compose &>/dev/null; then
info "Docker Compose not found. Installing Docker Compose 🔧..."
LATEST_COMPOSE=$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep 'tag_name' | cut -d\" -f4)
curl -L "https://github.com/docker/compose/releases/download/${LATEST_COMPOSE}/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose || error_exit "Failed to download Docker Compose."
chmod +x /usr/local/bin/docker-compose
else
success "Docker Compose is already installed 🔧"
fi
# Inputs
line
echo " 🌍 n8n Auto Deployment Setup"
echo ""
read -p "🌐 Enter your domain name (leave blank to use server IP): " DOMAIN
if [ -n "$DOMAIN" ]; then
read -p "📧 Enter your email address for SSL: " EMAIL
[ -z "$EMAIL" ] && error_exit "Email is required for SSL"
fi
read -p "👤 n8n username (default: admin): " N8N_USER
N8N_USER=${N8N_USER:-admin}
read -s -p "🔒 n8n password (default: securepassword): " N8N_PASS
echo
N8N_PASS=${N8N_PASS:-securepassword}
line
# Setup vars
if [ -n "$DOMAIN" ]; then
N8N_HOST="$DOMAIN"
N8N_PORT=443
N8N_PROTOCOL="https"
WEBHOOK_URL="https://$DOMAIN/"
N8N_SECURE_COOKIE=true
USE_DOMAIN=true
else
SERVER_IP=$(hostname -I | awk '{print $1}')
N8N_HOST="$SERVER_IP"
N8N_PORT=5678
N8N_PROTOCOL="http"
WEBHOOK_URL="http://$SERVER_IP:5678/"
N8N_SECURE_COOKIE=false
USE_DOMAIN=false
fi
PROJECT_DIR="n8n-docker"
mkdir -p "$PROJECT_DIR"
cd "$PROJECT_DIR" || error_exit "Failed to enter directory"
# Generate docker-compose.yml
if [ "$USE_DOMAIN" = true ]; then
mkdir -p letsencrypt
touch letsencrypt/acme.json
chmod 600 letsencrypt/acme.json
cat >docker-compose.yml <<EOF
version: '3.8'
services:
traefik:
image: traefik:latest
restart: always
command:
- "--providers.docker=true"
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "--certificatesresolvers.myresolver.acme.tlschallenge=true"
- "--certificatesresolvers.myresolver.acme.email=${EMAIL}"
- "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
ports:
- "80:80"
- "443:443"
volumes:
- "./letsencrypt:/letsencrypt"
- "/var/run/docker.sock:/var/run/docker.sock:ro"
networks:
- web
n8n:
image: docker.n8n.io/n8nio/n8n
restart: unless-stopped
environment:
N8N_HOST: "${N8N_HOST}"
N8N_PORT: "5678"
N8N_PROTOCOL: "https"
N8N_BASIC_AUTH_USER: "${N8N_USER}"
N8N_BASIC_AUTH_PASSWORD: "${N8N_PASS}"
N8N_BASIC_AUTH_ACTIVE: "true"
N8N_SECURE_COOKIE: "true"
N8N_USER_MANAGEMENT_DISABLED: "false"
WEBHOOK_URL: "https://${N8N_HOST}/"
labels:
- "traefik.enable=true"
- "traefik.http.routers.n8n.rule=Host(\`${N8N_HOST}\`)"
- "traefik.http.routers.n8n.entrypoints=websecure"
- "traefik.http.routers.n8n.tls.certresolver=myresolver"
volumes:
- n8n_data:/home/node/.n8n
networks:
- web
volumes:
n8n_data:
networks:
web:
EOF
else
cat >docker-compose.yml <<EOF
version: '3.8'
services:
n8n:
image: docker.n8n.io/n8nio/n8n
restart: always
ports:
- "5678:5678"
environment:
N8N_HOST: "${N8N_HOST}"
N8N_PORT: "5678"
N8N_PROTOCOL: "http"
N8N_BASIC_AUTH_USER: "${N8N_USER}"
N8N_BASIC_AUTH_PASSWORD: "${N8N_PASS}"
N8N_BASIC_AUTH_ACTIVE: "true"
N8N_SECURE_COOKIE: "false"
N8N_USER_MANAGEMENT_DISABLED: "false"
WEBHOOK_URL: "http://${N8N_HOST}:5678/"
volumes:
- n8n_data:/home/node/.n8n
volumes:
n8n_data:
EOF
fi
docker-compose up -d || error_exit "Docker Compose failed"
line
success "n8n is running at: $WEBHOOK_URL"
line
}
# Uninstall n8n
uninstall_n8n() {
line
echo -e "⚠️ This will stop and remove n8n containers and data!"
read -p "Are you sure? (yes/no): " confirm
if [[ "$confirm" != "yes" ]]; then
line
echo "❌ Uninstall cancelled."
return
fi
# Check Docker presence
if command -v docker &>/dev/null; then
if docker ps -a --format '{{.Names}}' | grep -q '^n8n$'; then
info "Stopping and removing n8n container..."
docker stop n8n &>/dev/null
docker rm n8n &>/dev/null
fi
else
info "Docker is not installed. Skipping container cleanup."
fi
if [ -d "n8n-docker" ]; then
cd n8n-docker
if command -v docker-compose &>/dev/null; then
docker-compose down -v || error_exit "Failed to stop services"
else
info "docker-compose not found. Skipping docker-compose cleanup."
fi
cd ..
rm -rf n8n-docker
line
success "n8n has been uninstalled and removed from your system."
else
line
info "No installation directory found, but attempted to clean containers."
success "n8n cleanup complete (if it was installed)."
fi
}
# Main
show_menu
case "$OPTION" in
1) install_n8n ;;
2) uninstall_n8n ;;
3)
echo "👋 Goodbye!"
exit 0
;;
*) echo "❗ Invalid option. Please select 1, 2, or 3." ;;
esac