-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy-ecommerce-application.sh
185 lines (140 loc) · 4.92 KB
/
deploy-ecommerce-application.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
#!/bin/bash
#
# Automate ECommerce Application Deployment
# Author: Mumshad Mannambeth
#######################################
# Print a message in a given color.
# Arguments:
# Color. eg: green, red
#######################################
function print_color(){
NC='\033[0m' # No Color
case $1 in
"green") COLOR='\033[0;32m' ;;
"red") COLOR='\033[0;31m' ;;
"*") COLOR='\033[0m' ;;
esac
echo -e "${COLOR} $2 ${NC}"
}
#######################################
# Check the status of a given service. If not active exit script
# Arguments:
# Service Name. eg: firewalld, mariadb
#######################################
function check_service_status(){
service_is_active=$(sudo systemctl is-active $1)
if [ $service_is_active = "active" ]
then
echo "$1 is active and running"
else
echo "$1 is not active/running"
exit 1
fi
}
#######################################
# Check the status of a firewalld rule. If not configured exit.
# Arguments:
# Port Number. eg: 3306, 80
#######################################
function is_firewalld_rule_configured(){
firewalld_ports=$(sudo firewall-cmd --list-all --zone=public | grep ports)
if [[ $firewalld_ports == *$1* ]]
then
echo "FirewallD has port $1 configured"
else
echo "FirewallD port $1 is not configured"
exit 1
fi
}
#######################################
# Check if a given item is present in an output
# Arguments:
# 1 - Output
# 2 - Item
#######################################
function check_item(){
if [[ $1 = *$2* ]]
then
print_color "green" "Item $2 is present on the web page"
else
print_color "red" "Item $2 is not present on the web page"
fi
}
echo "---------------- Setup Database Server ------------------"
# Install and configure firewalld
print_color "green" "Installing FirewallD.. "
sudo yum install -y firewalld
print_color "green" "Installing FirewallD.. "
sudo service firewalld start
sudo systemctl enable firewalld
# Check FirewallD Service is running
check_service_status firewalld
# Install and configure Maria-DB
print_color "green" "Installing MariaDB Server.."
sudo yum install -y mariadb-server
print_color "green" "Starting MariaDB Server.."
sudo service mariadb start
sudo systemctl enable mariadb
# Check FirewallD Service is running
check_service_status mariadb
# Configure Firewall rules for Database
print_color "green" "Configuring FirewallD rules for database.."
sudo firewall-cmd --permanent --zone=public --add-port=3306/tcp
sudo firewall-cmd --reload
is_firewalld_rule_configured 3306
# Configuring Database
print_color "green" "Setting up database.."
cat > setup-db.sql <<-EOF
CREATE DATABASE ecomdb;
CREATE USER 'ecomuser'@'localhost' IDENTIFIED BY 'ecompassword';
GRANT ALL PRIVILEGES ON *.* TO 'ecomuser'@'localhost';
FLUSH PRIVILEGES;
EOF
sudo mysql < setup-db.sql
# Loading inventory into Database
print_color "green" "Loading inventory data into database"
cat > db-load-script.sql <<-EOF
USE ecomdb;
CREATE TABLE products (id mediumint(8) unsigned NOT NULL auto_increment,Name varchar(255) default NULL,Price varchar(255) default NULL, ImageUrl varchar(255) default NULL,PRIMARY KEY (id)) AUTO_INCREMENT=1;
INSERT INTO products (Name,Price,ImageUrl) VALUES ("Laptop","100","c-1.png"),("Drone","200","c-2.png"),("VR","300","c-3.png"),("Tablet","50","c-5.png"),("Watch","90","c-6.png"),("Phone Covers","20","c-7.png"),("Phone","80","c-8.png"),("Laptop","150","c-4.png");
EOF
sudo mysql < db-load-script.sql
mysql_db_results=$(sudo mysql -e "use ecomdb; select * from products;")
if [[ $mysql_db_results == *Laptop* ]]
then
print_color "green" "Inventory data loaded into MySQl"
else
print_color "green" "Inventory data not loaded into MySQl"
exit 1
fi
print_color "green" "---------------- Setup Database Server - Finished ------------------"
print_color "green" "---------------- Setup Web Server ------------------"
# Install web server packages
print_color "green" "Installing Web Server Packages .."
sudo yum install -y httpd php php-mysql
# Configure firewalld rules
print_color "green" "Configuring FirewallD rules.."
sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
sudo firewall-cmd --reload
is_firewalld_rule_configured 80
# Update index.php
sudo sed -i 's/index.html/index.php/g' /etc/httpd/conf/httpd.conf
# Start httpd service
print_color "green" "Start httpd service.."
sudo service httpd start
sudo systemctl enable httpd
# Check FirewallD Service is running
check_service_status httpd
# Download code
print_color "green" "Install GIT.."
sudo yum install -y git
sudo git clone https://github.com/kodekloudhub/learning-app-ecommerce.git /var/www/html/
print_color "green" "Updating index.php.."
sudo sed -i 's/172.20.1.101/localhost/g' /var/www/html/index.php
print_color "green" "---------------- Setup Web Server - Finished ------------------"
# Test Script
web_page=$(curl http://localhost)
for item in Laptop Drone VR Watch Phone
do
check_item "$web_page" $item
done