-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
266 lines (230 loc) · 7.97 KB
/
setup.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
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
260
261
262
263
264
265
266
#!/bin/bash
START_STEP=1 # Default starting step
# Check if a START_STEP is provided as an argument
if [ $# -ne 0 ]; then
START_STEP=$1
echo "Starting installation from step ${START_STEP}"
fi
# --- 1. Download Repositories (with Git Pull if existing) ---
if [ ${START_STEP} -le 1 ]; then
echo "Step 1: Downloading or updating repositories..."
if [ ! -d "merry" ]; then
git clone https://github.com/GomuGomuu/merry.git
echo " merry repository cloned."
else
cd merry
git pull
echo " merry repository updated."
cd ..
fi
if [ ! -d "olop-price-scraping" ]; then
git clone https://github.com/GomuGomuu/olop-price-scraping.git
echo " olop-price-scraping repository cloned."
else
cd olop-price-scraping
git pull
echo " olop-price-scraping repository updated."
cd ..
fi
fi
# --- 2. Install Dependencies (General) ---
if [ ${START_STEP} -le 2 ]; then
echo "Step 2: Installing dependencies (General)..."
# Python (ensure it's installed; this is just a check)
if ! command -v python3 &>/dev/null; then
echo "Error: Python 3 not found. Please install it."
exit 1
fi
# Pip (ensure it's installed; this is just a check)
if ! command -v pip &>/dev/null; then
echo "Error: Pip not found. Please install it."
exit 1
fi
# Tesseract OCR
echo " Installing Tesseract OCR..."
sudo apt-get update
if ! sudo apt-get install -y tesseract-ocr; then
echo " Error: Failed to install Tesseract OCR."
exit 1
fi
echo " Tesseract OCR installed."
fi
# --- 3. Install and Configure PostgreSQL ---
if [ ${START_STEP} -le 3 ]; then
echo "Step 3: Installing and configuring PostgreSQL..."
sudo apt-get update
if ! sudo apt-get install -y postgresql postgresql-contrib; then
echo " Error: Failed to install PostgreSQL."
exit 1
fi
echo " PostgreSQL installed."
fi
# --- 4. Configure Environment Variables (for PostgreSQL) ---
if [ ${START_STEP} -le 4 ]; then
echo "Step 4: Configuring environment variables for PostgreSQL..."
# Merry (load .env first)
cd merry
source .env
# Create PostgreSQL user and database
echo " Creating PostgreSQL user and database..."
if ! sudo -u postgres psql -c "SELECT 1 FROM pg_user WHERE usename = '${POSTGRES_USER}';" &>/dev/null; then
sudo -u postgres psql -c "CREATE USER ${POSTGRES_USER} WITH PASSWORD '${POSTGRES_PASSWORD}';"
echo " PostgreSQL user created."
else
echo " PostgreSQL user already exists."
fi
if ! sudo -u postgres psql -c "SELECT 1 FROM pg_database WHERE datname = '${POSTGRES_DB}';" &>/dev/null; then
sudo -u postgres psql -c "CREATE DATABASE ${POSTGRES_DB} OWNER ${POSTGRES_USER};"
echo " PostgreSQL database created."
else
echo " PostgreSQL database already exists."
fi
cd ..
fi
# --- 5. Install and Configure Redis ---
if [ ${START_STEP} -le 5 ]; then
echo "Step 5: Installing and configuring Redis..."
sudo apt-get update
if ! sudo apt-get install -y redis-server; then
echo " Error: Failed to install Redis."
exit 1
fi
echo " Redis installed."
# Start Redis
echo " Starting Redis service..."
sudo systemctl enable redis-server
if ! sudo systemctl start redis-server; then
echo " Error: Failed to start Redis service."
exit 1
fi
echo " Redis service started."
fi
# --- 6. Install Chromium ---
if [ ${START_STEP} -le 6 ]; then
echo "Step 6: Installing Chromium..."
sudo apt-get update
if ! sudo apt-get install -y chromium-browser; then
echo " Error: Failed to install Chromium."
exit 1
fi
echo " Chromium installed."
fi
# --- 7. Download and Configure ChromeDriver ---
if [ ${START_STEP} -le 7 ]; then
echo "Step 7: Downloading and configuring ChromeDriver..."
# Get the Chromium version
CHROME_VERSION=$(chromium-browser --version | awk '{print $3}' | sed 's/\./_/g')
echo " Detected Chromium version: ${CHROME_VERSION}"
CHROME_DRIVER_VERSION="110.0.5481.77" # Replace with a compatible version
wget -nv https://chromedriver.storage.googleapis.com/${CHROME_DRIVER_VERSION}/chromedriver_linux64.zip
if [ $? -ne 0 ]; then
echo " Error: Failed to download ChromeDriver."
exit 1
fi
echo " ChromeDriver downloaded."
unzip chromedriver_linux64.zip
if [ $? -ne 0 ]; then
echo " Error: Failed to unzip ChromeDriver."
exit 1
fi
echo " ChromeDriver unzipped."
# Place ChromeDriver in your PATH
sudo mv chromedriver /usr/local/bin/
echo " ChromeDriver moved to /usr/local/bin."
fi
# --- 8. Install Flower ---
if [ ${START_STEP} -le 8 ]; then
echo "Step 8: Installing Flower..."
cd merry
python3 -m venv venv
source venv/bin/activate
if ! pip install flower; then
echo " Error: Failed to install Flower."
exit 1
fi
echo " Flower installed."
deactivate
cd ..
fi
# --- 9. Install Dependencies for Merry and Olop Price Scraping ---
if [ ${START_STEP} -le 9 ]; then
echo "Step 9: Installing dependencies for Merry and Olop Price Scraping..."
# Merry
cd merry
python3 -m venv venv
source venv/bin/activate
if ! pip install poetry; then
echo " Error: Failed to install Poetry."
exit 1
fi
echo " Poetry installed."
if ! poetry install --no-root -C src/config/; then
echo " Error: Failed to install dependencies for Merry."
exit 1
fi
echo " Dependencies for Merry installed."
deactivate
cd ..
# Olop Price Scraping
cd olop-price-scraping
python3 -m venv venv
source venv/bin/activate
if ! pip install -r requirements.txt; then
echo " Error: Failed to install dependencies for Olop Price Scraping."
exit 1
fi
echo " Dependencies for Olop Price Scraping installed."
deactivate
cd ..
fi
# --- 10. Configure Environment Variables ---
if [ ${START_STEP} -le 10 ]; then
echo "Step 10: Configuring environment variables for Redis and Selenium..."
# Merge environment variables from both projects
echo "Merging environment variables from olop-price-scraping and merry..."
cat olop-price-scraping/linux.env merry/src/config/.env.example >merged.env
sed -i 's/DB_HOST=.*/DB_HOST=localhost/' merged.env
sed -i 's/POSTGRES_HOST=.*/POSTGRES_HOST=localhost/' merged.env
export $(grep -v '^#' merged.env | xargs)
echo "Environment variables configured."
fi
# --- 11. Initialize Merry ---
if [ ${START_STEP} -le 11 ]; then
echo "Step 11: Initializing Merry..."
cd merry
source venv/bin/activate
if ! python src/manage.py migrate; then
echo " Error: Failed to migrate Merry database."
exit 1
fi
echo " Merry database migrated."
if ! python src/manage.py collectstatic --noinput; then
echo " Error: Failed to collect static files for Merry."
exit 1
fi
echo " Merry static files collected."
deactivate
cd ..
fi
# --- 12. Install screen ---
if [ ${START_STEP} -le 12 ]; then
echo "Step 12: Installing screen..."
sudo apt-get update
if ! sudo apt-get install -y screen; then
echo " Error: Failed to install screen."
exit 1
fi
echo " Screen installed."
fi
# --- 13. Start Services in Screen Sessions ---
if [ ${START_STEP} -le 13 ]; then
echo "Step 13: Starting services in screen sessions..."
# Merry (with Flower)
screen -dmS merry bash -c "cd merry && source venv/bin/activate && python src/manage.py runserver"
# Flower
screen -dmS flower bash -c "cd merry && source venv/bin/activate && flower"
# Olop Price Scraping (with Celery)
screen -dmS olop bash -c "cd olop-price-scraping && source venv/bin/activate && celery -A olop worker --loglevel=INFO"
echo " Services started in screen sessions."
fi
echo "Installation and setup completed successfully."