-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabc
More file actions
259 lines (173 loc) · 7.13 KB
/
abc
File metadata and controls
259 lines (173 loc) · 7.13 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
#!/bin/bash
# Sources the common functions script to use shared utilities.
source ./common.sh
# Sets the application name to "frontend" for consistent logging and validation.
app_name="frontend"
#Disabling of pre-verson of nodejs ,enabling and installing required version of nginx
dnf module disable nginx -y &>>$LOG_FILE
VALIDATE $? "Disabling nginx"
dnf module enable nginx:1.24 -y &>>$LOG_FILE
VALIDATE $? "enabling nginx:1.24"
dnf install nginx -y &>>$LOG_FILE
VALIDATE $? "instaling nginx:1.24"
# Enables and starts the given system service, with validation
systemctl enable nginx &>>$LOG_FILE
systemctl start nginx
VALIDATE $? "Starting Nginx"
#Remove the default content that web server is serving.
rm -rf /usr/share/nginx/html/* &>>$LOG_FILE
VALIDATE $? "Removing the default content that web server is serving."
#Download the frontend content
curl -o /tmp/frontend.zip https://roboshop-artifacts.s3.amazonaws.com/frontend-v3.zip &>>$LOG_FILE
VALIDATE $? "Downloding frotend code to temp dir"
#Extract the frontend content.
cd /usr/share/nginx/html
unzip /tmp/frontend.zip &>>$LOG_FILE
VALIDATE $? "extractin ziped dir"
rm -rf /etc/nginx/nginx.conf &>>$LOG_FILE
VALIDATE $? "Remove default nginx conf"
cp $SCRIPT_DIR/nginx.conf /etc/nginx/nginx.conf
VALIDATE $? "Copying nginx.conf"
#Restart Nginx Service to load the changes of the configuration.
systemctl restart nginx
VALIDATE $? "Restarting Nginx Service to load the changes of the configuration"
print_time
#!/bin/bash
START_TIME=$(date +%s)
USERID=$(id -u)
R="\e[31m"
G="\e[32m"
Y="\e[33m"
B="\e[34m"
N="\e[0m"
LOG_FOLDER="/var/log/roboshop-logs"
SCRIPT_NAME=$(echo $0 | cut -d "." -f 1)
LOG_FILE="$LOG_FOLDER/$SCRIPT_NAME.log"
SCRIPT_DIR=$PWD #To know the current directory usefull when copy file
mkdir -p $LOG_FOLDER
echo "Script started executing at: $(date)" | tee -a $LOG_FILE
setup_python(){
dnf install python3 gcc python3-devel -y &>>$LOG_FILE
VALIDATE $? "Installing python3 ......."
cd /app
pip3 install -r requirements.txt &>>$LOG_FILE
VALIDATE $? "Installing requirements ......."
}
setup_maven(){
dnf install maven -y &>>$LOG_FILE
VALIDATE $? "Installing maven ......"
cd /app
mvn clean package &>>$LOG_FILE
VALIDATE $? "cleaning maven package ......"
mv target/shipping-1.0.jar shipping.jar &>>$LOG_FILE
VALIDATE $? "Moving and renaming file from target/shipping-1.0.jar to shipping.jar ......"
}
setup_systemd(){
# Sets up systemd service by copying, reloading daemon, and validating.
cp $SCRIPT_DIR/$app_name.service /etc/systemd/system/$app_name.service &>>$LOG_FILE
VALIDATE $? "Copying $app_name service file to /etc/systemd dir "
systemctl daemon-reload &>>$LOG_FILE
VALIDATE $? "Service daemon-reloaded "
}
setup_nodejs(){
# Installs Node.js by enabling the correct module and validating the installation.
dnf module disable nodejs -y &>>$LOG_FILE
VALIDATE $? "Disabling nodejs"
dnf module enable nodejs:20 -y &>>$LOG_FILE
VALIDATE $? "Enabling nodejs:20 version"
dnf install nodejs -y &>>$LOG_FILE
VALIDATE $? "Installing nodejs:20 version"
npm install &>>$LOG_FILE
VALIDATE $? "Installing Dependencies"
}
setup_app(){
# Sets up the roboshop application by creating a user, preparing the directory, downloading, and extracting the application files.
id roboshop &>>LOG_FILE
if [ $? -ne 0 ]
then
useradd --system --home /app --shell /sbin/nologin --comment "roboshop system user" roboshop &>>$LOG_FILE
VALIDATE $? "$B Creating roboshop system user$N"
else
echo -e " $B appuser already created ... $Y SKIPPING$N"
fi
mkdir -p /app
VALIDATE $? "Creating app directory "
curl -o /tmp/$app_name.zip https://roboshop-artifacts.s3.amazonaws.com/catalogue-v3.zip
VALIDATE $? "Downloading $app_name zip to tmp dir "
rm -rf /app/*
cd /app
unzip /tmp/$app_name.zip
VALIDATE $? "unzipping $app_name"
}
setup_service(){
# Enables and starts the given system service, with validation
systemctl enable $1 &>>$LOG_FILE
VALIDATE $? "System service enable for $1"
systemctl start $1 &>>$LOG_FILE
VALIDATE $? "System service start for $1"
}
VALIDATE(){
# Validates the success or failure of the previous command and logs the result.
if [ $1 -ne 0 ]
then
echo -e " $2 is ...$R FAILURE: $B check once using $Ycat /var/log/roboshop-logs/$app_name.log $N " | tee -a $LOG_FILE
exit 1 #give other than 0 upto 127
else
echo -e "$2 is ... $G SUCCESS $N " | tee -a $LOG_FILE
fi
}
# check the user has root priveleges or not
check_root(){
# Checks if the script is being run with root privileges, exits if not.
if [ $USERID -ne 0 ]
then
echo -e "$R ERROR:: Please run this script with root access $N " | tee -a $LOG_FILE
exit 1 #give other than 0 upto 127
else
echo -e "You are running with $G root access $N " | tee -a $LOG_FILE
fi
}
print_time(){
# Calculates and prints the total time taken to execute the script.
END_TIME=$(date +%s)
TOTAL_TIME=$((END_TIME-START_TIME))
echo -e "Script executed successfully, $Y Time taken: $TOTAL_TIME seconds $N"
}
#!/bin/bash
# Sources the common functions script to use shared utilities.
source ./common.sh
# Sets the application name to "catalogue" for consistent logging and validation.
app_name="catalogue"
# Checks if the script is being run with root privileges, exits if not.
check_root
# Sets up the roboshop application by creating a user, preparing the directory, downloading, and extracting the application files.
setup_app
# Installs Node.js by enabling the correct module and validating the installation.
setup_nodejs
# Sets up systemd service by copying, reloading daemon, and validating.
setup_systemd
# Enables and starts the given system service, with validation.
systemctl enable catalogue &>>$LOG_FILE
VALIDATE $? "System service enable for catalogue"
systemctl start catalogue &>>$LOG_FILE
VALIDATE $? "System service start for catalogue"
# Copies the MongoDB repository configuration file to the YUM repositories directory.
cp $SCRIPT_DIR/mongod.repo /etc/yum.repos.d/mongo.repo &>>$LOG_FILE
VALIDATE $? "Copying mongoDB repo"
# Installs the MongoDB client (mongosh) and validates the installation.
dnf install mongodb-mongosh -y &>>$LOG_FILE
VALIDATE $? "Installing mongodb-client"
# Checks if the "catalogue" database exists on the MongoDB server.
STATUS=$(mongosh --host mongodb.srivenkata.shop --eval 'db.getMongo().getDBNames().indexOf("catalogue")')
# If the "catalogue" database does not exist, load the master data from the script.
if [ $STATUS -lt 0 ]
then
# Loads the master data for the list of products into the MongoDB database.
mongosh --host mongodb.srivenkata.shop </app/db/master-data.js &>>$LOG_FILE
VALIDATE $? "Loading Master Data of the List of products"
else
# Prints a message indicating that the master data is already loaded and skips the process.
echo -e "$B Already Loaded Master Data of the List of products ..... $Y SKIPPING $N"
fi
# Prints the total execution time of the script.
print_time