Leon4gr45 commited on
Commit
e386518
·
verified ·
1 Parent(s): ab91c49

Upload fresh start.sh

Browse files
Files changed (1) hide show
  1. start.sh +186 -0
start.sh ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ set -e # Exit immediately if a command exits with a non-zero status.
3
+
4
+ echo "--- Script Start ---"
5
+ echo "Running as user: $(whoami) (UID: $(id -u), GID: $(id -g))"
6
+ # ... (rest of initial echos and directory setup remains the same) ...
7
+
8
+ # --- Persistent Data Setup ---
9
+ N8N_DATA_DIR="/data/.n8n"
10
+ POSTGRES_DATA_DIR="/data/postgres" # Should be owned by n8nuser (UID 1000)
11
+ QDRANT_DATA_DIR="/data/qdrant_storage" # Should be owned by n8nuser
12
+ OLLAMA_MODELS_DIR="/data/.ollama" # Should be owned by n8nuser
13
+
14
+ echo "Ensuring application data directories exist..."
15
+ mkdir -p "$N8N_DATA_DIR" "$POSTGRES_DATA_DIR" "$QDRANT_DATA_DIR" "$OLLAMA_MODELS_DIR"
16
+ echo "Permissions of data directories in /data (should be owned by $(whoami)):"
17
+ ls -ld "$N8N_DATA_DIR" "$POSTGRES_DATA_DIR" "$QDRANT_DATA_DIR" "$OLLAMA_MODELS_DIR"
18
+
19
+ ln -sfn "$N8N_DATA_DIR" /home/n8nuser/.n8n
20
+ export OLLAMA_MODELS="$OLLAMA_MODELS_DIR"
21
+
22
+ # --- PostgreSQL Setup & Start (as n8nuser) ---
23
+ echo "--- PostgreSQL Setup (running as $(whoami)) ---"
24
+ echo "PGDATA is set to: $POSTGRES_DATA_DIR"
25
+ POSTGRES_PORT="${DB_POSTGRESDB_PORT:-5432}" # Define port for consistency
26
+
27
+ # Ensure n8nuser has rwx permissions on its own PGDATA.
28
+ chmod u+rwx "$POSTGRES_DATA_DIR"
29
+
30
+ if [ ! -d "$POSTGRES_DATA_DIR/base" ]; then
31
+ echo "Initializing PostgreSQL database in $POSTGRES_DATA_DIR as user $(whoami)..."
32
+ /usr/lib/postgresql/14/bin/initdb -D "$POSTGRES_DATA_DIR" --username=$(whoami) --no-locale --encoding=UTF8
33
+ if [ $? -ne 0 ]; then echo "ERROR: PostgreSQL initdb failed."; ls -ld "$POSTGRES_DATA_DIR"; exit 1; fi
34
+
35
+ echo "PostgreSQL Initialized. Ownership/permissions of $POSTGRES_DATA_DIR after initdb:"
36
+ ls -ld "$POSTGRES_DATA_DIR"
37
+
38
+ echo "Temporarily starting PostgreSQL to create database role and database..."
39
+ # For pg_ctl, it's good practice to point to the PGDATA explicitly.
40
+ # pg_ctl will find postgresql.conf within PGDATA.
41
+ /usr/lib/postgresql/14/bin/pg_ctl -D "$POSTGRES_DATA_DIR" -o "-p $POSTGRES_PORT -c listen_addresses='localhost'" -w start
42
+
43
+ PG_ROLE_NAME="${DB_POSTGRESDB_USER:-n8n}"
44
+ PG_PASSWORD="${DB_POSTGRESDB_PASSWORD}"
45
+ PG_DB_NAME="${DB_POSTGRESDB_DATABASE:-n8n}"
46
+
47
+ if [ -z "$PG_PASSWORD" ]; then echo "ERROR: DB_POSTGRESDB_PASSWORD is not set."; exit 1; fi
48
+
49
+ echo "Creating PostgreSQL role '$PG_ROLE_NAME' and database '$PG_DB_NAME'..."
50
+ psql -v ON_ERROR_STOP=1 --username=$(whoami) --port="$POSTGRES_PORT" --host=localhost postgres <<-EOSQL
51
+ CREATE ROLE "$PG_ROLE_NAME" WITH LOGIN PASSWORD '$PG_PASSWORD';
52
+ CREATE DATABASE "$PG_DB_NAME" OWNER "$PG_ROLE_NAME";
53
+ EOSQL
54
+ echo "PostgreSQL role and database created."
55
+ /usr/lib/postgresql/14/bin/pg_ctl -D "$POSTGRES_DATA_DIR" -m fast -w stop
56
+ echo "Temporary PostgreSQL server stopped."
57
+ else
58
+ echo "PostgreSQL database found in $POSTGRES_DATA_DIR."
59
+ fi
60
+
61
+ echo "Starting PostgreSQL server for application use (as $(whoami))..."
62
+ # The postgres command will use the postgresql.conf found within $POSTGRES_DATA_DIR
63
+ # We don't need to specify -c config_file if it's inside PGDATA.
64
+ # Just ensure the port is correctly passed if not default.
65
+ /usr/lib/postgresql/14/bin/postgres -D "$POSTGRES_DATA_DIR" -p "$POSTGRES_PORT" &
66
+ PG_PID=$!
67
+ echo "Waiting for PostgreSQL to start (PID: $PG_PID)..."
68
+ max_wait=30
69
+ count=0
70
+ until pg_isready -h localhost -p "$POSTGRES_PORT" -U $(whoami) -q || [ $count -eq $max_wait ]; do
71
+ echo -n "."
72
+ sleep 1
73
+ count=$((count+1))
74
+ done
75
+ if [ $count -eq $max_wait ]; then
76
+ echo "ERROR: PostgreSQL timed out waiting to start."
77
+ PG_LOG_DIR_GUESS="$POSTGRES_DATA_DIR/log"
78
+ if [ -d "$PG_LOG_DIR_GUESS" ]; then
79
+ echo "PostgreSQL logs from $PG_LOG_DIR_GUESS:"
80
+ # List log files before cat'ing, and cat only the latest ones if possible or just one
81
+ ls -lt "$PG_LOG_DIR_GUESS"
82
+ cat "$PG_LOG_DIR_GUESS"/postgresql-*.log 2>/dev/null || echo "No standard log files found or readable."
83
+ else
84
+ echo "$PG_LOG_DIR_GUESS not found."
85
+ fi
86
+ exit 1
87
+ fi
88
+ echo "PostgreSQL started successfully on port $POSTGRES_PORT."
89
+
90
+
91
+ # --- Qdrant Setup & Start (as n8nuser) ---
92
+ # (This section remains the same)
93
+ echo "--- Qdrant Setup (running as $(whoami)) ---"
94
+ QDRANT_CONFIG_DIR="/qdrant/config"
95
+ QDRANT_STORAGE_PATH_IN_CONFIG="/qdrant/storage"
96
+ ln -sfn "$QDRANT_DATA_DIR" "$QDRANT_STORAGE_PATH_IN_CONFIG"
97
+
98
+ echo "Starting Qdrant..."
99
+ /usr/local/bin/qdrant --config-path "$QDRANT_CONFIG_DIR/config.yaml" &
100
+ QDRANT_PID=$!
101
+ echo "Waiting for Qdrant to start (PID: $QDRANT_PID)..."
102
+ until curl -sf http://localhost:6333/readyz > /dev/null; do
103
+ echo -n "."
104
+ sleep 1
105
+ if ! ps -p $QDRANT_PID > /dev/null; then echo "Qdrant process died."; exit 1; fi
106
+ done
107
+ echo "Qdrant started."
108
+
109
+ # --- Ollama Start (as n8nuser) ---
110
+ # (This section remains the same)
111
+ echo "--- Ollama Setup (running as $(whoami)) ---"
112
+ echo "Ollama models will be stored in: $OLLAMA_MODELS_DIR"
113
+
114
+ echo "Starting Ollama server..."
115
+ ollama serve &
116
+ OLLAMA_PID=$!
117
+ echo "Waiting for Ollama to start (PID: $OLLAMA_PID)..."
118
+ until curl -sf http://localhost:11434/api/tags > /dev/null 2>&1 ; do
119
+ echo -n "."
120
+ sleep 1
121
+ if ! ps -p $OLLAMA_PID > /dev/null; then echo "Ollama process died."; exit 1; fi
122
+ done
123
+ echo "Ollama started."
124
+
125
+ DEFAULT_MODEL="${DEFAULT_OLLAMA_MODEL:-llama3.1:8b}"
126
+ echo "Checking for default Ollama model: $DEFAULT_MODEL"
127
+ if ! ollama show "$DEFAULT_MODEL" > /dev/null 2>&1; then
128
+ echo "Pulling $DEFAULT_MODEL... This may take a while."
129
+ ollama pull "$DEFAULT_MODEL"
130
+ if [ $? -ne 0 ]; then echo "Warning: Failed to pull $DEFAULT_MODEL"; else echo "$DEFAULT_MODEL pulled."; fi
131
+ else
132
+ echo "$DEFAULT_MODEL already present."
133
+ fi
134
+
135
+ # --- n8n Start (as n8nuser) ---
136
+ echo "--- n8n Setup (running as $(whoami)) ---"
137
+
138
+ if [ -z "$N8N_ENCRYPTION_KEY" ]; then
139
+ echo "ERROR: N8N_ENCRYPTION_KEY is not set. Please set it as a Space secret."
140
+ exit 1
141
+ fi
142
+ if [ -z "$DB_POSTGRESDB_PASSWORD" ]; then
143
+ echo "ERROR: DB_POSTGRESDB_PASSWORD is not set. Please set it as a Space secret."
144
+ exit 1
145
+ fi
146
+
147
+ export N8N_USER_FOLDER="/home/n8nuser/.n8n"
148
+ export GENAI_MODELS_N8N_DEFAULT_MODEL="$DEFAULT_MODEL"
149
+ export DB_POSTGRESDB_PORT="$POSTGRES_PORT"
150
+
151
+ # --- MOST PERMISSIVE EMBEDDING CONFIGURATION FOR DIAGNOSIS ---
152
+ echo "WARNING: USING HIGHLY PERMISSIVE (INSECURE) EMBEDDING SETTINGS FOR DIAGNOSIS!"
153
+ export N8N_SEC_EMBEDDER_HEADER_MODE="allowAll"
154
+ # The mode "allowAll" should theoretically make n8n send headers that allow embedding from anywhere.
155
+ # This is a simpler way to test than just relying on the regex if there's any doubt.
156
+ # If "allowAll" doesn't work, then N8N_SEC_EMBEDDER_HEADER_ALLOW_FROM_REGEX=".*" is the next most forceful.
157
+ # We'll also set the regex to ".*" just in case "allowAll" isn't picked up or is deprecated.
158
+ export N8N_SEC_EMBEDDER_HEADER_ALLOW_FROM_REGEX=".*"
159
+ # --- End Embedding Configuration ---
160
+
161
+ export N8N_SEC_ALLOW_ALL_ORIGINS_FOR_WEBHOOKS="true"
162
+ export N8N_RUNNERS_ENABLED="true" # As per n8n deprecation warning
163
+
164
+ # Ensure URLs are set, as this can sometimes affect header generation or self-referencing
165
+ if [ -n "$SPACE_HOST" ]; then
166
+ # Assuming SPACE_HOST is like 'username-spacename.hf.space'
167
+ DERIVED_URL="https://$SPACE_HOST/"
168
+ export N8N_EDITOR_BASE_URL="$DERIVED_URL"
169
+ export WEBHOOK_URL="$DERIVED_URL"
170
+ echo "N8N URLs derived from SPACE_HOST: $N8N_EDITOR_BASE_URL"
171
+ elif [ -n "$HF_SPACE_PUBLIC_URL" ]; then # Another possible HF env var
172
+ export N8N_EDITOR_BASE_URL="$HF_SPACE_PUBLIC_URL"
173
+ export WEBHOOK_URL="$HF_SPACE_PUBLIC_URL"
174
+ echo "N8N URLs set from HF_SPACE_PUBLIC_URL: $N8N_EDITOR_BASE_URL"
175
+ else
176
+ FALLBACK_URL="http://localhost:${N8N_PORT:-5678}/"
177
+ export N8N_EDITOR_BASE_URL="$FALLBACK_URL"
178
+ export WEBHOOK_URL="$FALLBACK_URL"
179
+ echo "Warning: Specific Hugging Face URL environment variables not found. Using fallback: $FALLBACK_URL"
180
+ fi
181
+
182
+ # Optional: Increase n8n log level for more startup details
183
+ # export N8N_LOG_LEVEL="debug"
184
+
185
+ echo "Starting n8n with highly permissive (INSECURE) embedding rules for diagnosis..."
186
+ exec n8n start