-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.sh
More file actions
146 lines (103 loc) · 3.85 KB
/
common.sh
File metadata and controls
146 lines (103 loc) · 3.85 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
#!/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
VALIDATE $? "Installing requirements ......."
cp $SCRIPT_DIR/payment.service /etc/systemd/system/payment.service &>>$LOG_FILE
VALIDATE $? "Copying payment service"
}
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/$app_name-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"
}