forked from justinmajetich/AirBnB_clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc6.sh
executable file
·189 lines (183 loc) · 6.28 KB
/
c6.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
clear
echo ""
echo "----------------------"
echo "--- Database Setup ---"
echo "----------------------"
echo ""
if sudo service mysql status >/dev/null 2>&1; then
echo "#--| "
echo "#--> MySQL is already running."
echo "#--| "
else
echo "#--| "
echo "#--> MySQL is not running. Starting MySQL..."
echo "#--| "
echo ""
sudo service mysql start
if sudo service mysql status >/dev/null 2>&1; then
echo "#--| "
echo "#--> MySQL is now running."
echo "#--| "
else
echo "#--| "
echo "#-->Failed to start MySQL. Please check your MySQL installation."
echo "#--| "
fi
fi
current_path=$(pwd)
config="$current_path/config.txt"
if [ ! -f "$config" ]; then
echo "#--| "
echo "#--> Error: 'config.txt' not found in '$current_path'."
echo "#--> when you did clone repo there was file config.txt"
echo "#--> make sure to copy it to where console.py"
echo "#--> also you can put your config there if you dont use default"
echo "#--| "
exit 1
fi
source $config
console_path="$current_path/console.py"
if [ ! -f "$console_path" ]; then
echo "#--| "
echo "#--> Error: 'console.py' not found in '$current_path'."
echo "#--> make sure you put this script in same path of console.py"
echo "#--| "
exit 1
fi
run_mysql_command() {
command="$1"
shift # Shift to remove the first argument (the command)
# Check if additional arguments are provided
if [ $# -ge 1 ]; then
mysql_command="sudo mysql"
while [ $# -ge 1 ]; do
mysql_command="$mysql_command $1"
shift
done
result=$($mysql_command <<< "$command" 2>&1 | grep -v "Using a password on the command line interface can be insecure")
else
result=$(sudo mysql -h"$HBNB_MYSQL_HOST" -u"$YOUR_USER_MYSQL" -p"$YOUR_PASSWORD_MYSQL" <<< "$command" 2>&1 | grep -v "Using a password on the command line interface can be insecure")
fi
if [[ $result == *"Access denied"* ]]; then
echo "$result"
echo ""
echo "Check config.txt and make sure you have"
echo " correct settings for your MySQL database."
echo ""
echo "It's possible that you changed the default password"
echo " when you installed MySQL."
echo ""
echo "If you've updated the password, update config.txt accordingly."
echo ""
exit 1
fi
if [[ -n "$result" ]]; then
echo "$result"
fi
}
run_console_command() {
command="$1"
shift # Shift to remove the first argument (the command)
# Check if additional arguments are provided
if [ $# -ge 1 ]; then
full_command="echo '$command' | sudo -E HBNB_MYSQL_USER=$HBNB_MYSQL_USER HBNB_MYSQL_PWD=$HBNB_MYSQL_PWD HBNB_MYSQL_HOST=$HBNB_MYSQL_HOST HBNB_MYSQL_DB=$HBNB_MYSQL_DB HBNB_TYPE_STORAGE=$HBNB_TYPE_STORAGE $console_path $@"
else
full_command="echo '$command' | sudo -E HBNB_MYSQL_USER=$HBNB_MYSQL_USER HBNB_MYSQL_PWD=$HBNB_MYSQL_PWD HBNB_MYSQL_HOST=$HBNB_MYSQL_HOST HBNB_MYSQL_DB=$HBNB_MYSQL_DB HBNB_TYPE_STORAGE=$HBNB_TYPE_STORAGE $console_path"
fi
result=$(eval "$full_command" 2>&1 | grep -v "MYSQL_OPT_RECONNECT is deprecated and will be removed in a future version")
if [[ -n "$result" ]]; then
echo "$result"
fi
}
echo "#--| "
echo "#--> Dropping hbnb_dev_db database if it exists"
echo "#--| "
run_mysql_command "DROP DATABASE IF EXISTS $HBNB_MYSQL_DB;"
echo "#--| "
# Execute MySQL setup script
echo "#--> Executing MySQL setup script to make database ready"
echo "#--| "
run_mysql_command "CREATE DATABASE IF NOT EXISTS $HBNB_MYSQL_DB;"
run_mysql_command "CREATE USER IF NOT EXISTS '"$HBNB_MYSQL_USER"'@'"$HBNB_MYSQL_HOST"' IDENTIFIED BY '"$HBNB_MYSQL_PWD"';"
run_mysql_command "GRANT SELECT ON performance_schema.* TO '"$HBNB_MYSQL_USER"'@'"$HBNB_MYSQL_HOST"';"
run_mysql_command "GRANT ALL PRIVILEGES ON $HBNB_MYSQL_DB.* TO '"$HBNB_MYSQL_USER"'@'"$HBNB_MYSQL_HOST"';"
echo ""
echo "--------------"
echo "--- TASK6 ---"
echo "--------------"
echo ""
echo "#--| "
echo "#--> Creating a new State:"
echo "#--| "
echo ""
state_output=$(run_console_command 'create State name="California"' | tee /dev/tty)
state_id=$(echo "$state_output" | grep -E "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$" | tail -1)
echo ""
if [ -z "$state_id" ]; then
echo "#--| "
echo "#--> Couldn't find a valid id state id"
echo "#--> Your console is not printing state id"
echo "#--> Check do_create"
echo "#--| "
exit 1
fi
echo "#--| "
echo "#--> State ID: $state_id"
echo "#--| "
echo "#--> Listing all States:"
echo "#--| "
echo ""
run_console_command 'all State'
echo ""
echo "#--| "
echo "#--> Executing SQL query to list all States:"
echo "#--| "
echo ""
run_mysql_command 'SELECT * FROM states\G' -h"$HBNB_MYSQL_HOST" -u"$HBNB_MYSQL_USER" -p"$HBNB_MYSQL_PWD" "$HBNB_MYSQL_DB"
echo ""
echo "#--| "
echo "#--> Creating a new City:"
echo "#--> To create City we need valid State ID"
echo "#--> Creating a new City with previous State ID: $state_id"
echo "#--| "
echo ""
city_output=$(run_console_command 'create City state_id="'$state_id'" name="San_Francisco"' | tee /dev/tty)
city_id=$(echo "$city_output" | grep -E "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$" | tail -1)
echo ""
if [ -z "$city_id" ]; then
echo "#--> Couldn't find a valid id city id"
echo "#--> Your console is not printing city id"
echo "#--> Check do_create"
exit 1
fi
echo "#--| "
echo "#--> City ID: $city_id"
echo "#--| "
echo "#--> Listing all Cities:"
echo "#--| "
echo ""
run_console_command 'all City'
echo ""
echo "#--| "
echo "#--> Creating another new City:"
echo "#--| "
echo ""
city_output=$(run_console_command 'create City state_id="'$state_id'" name="San_Jose"' | tee /dev/tty)
city_id=$(echo "$city_output" | grep -E "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$" | tail -1)
echo ""
if [ -z "$city_id" ]; then
echo "#--> Couldn't find a valid id city id"
echo "#--> Your console is not printing city id"
echo "#--> Check do_create"
exit 1
fi
echo "#--| "
echo "#--> City ID: $city_id"
echo "#--| "
echo "#--> Executing SQL query to list all Cities:"
echo "#--| "
echo ""
run_mysql_command 'SELECT * FROM cities\G' -h"$HBNB_MYSQL_HOST" -u"$HBNB_MYSQL_USER" -p"$HBNB_MYSQL_PWD" "$HBNB_MYSQL_DB"
echo ""
echo "#--| "
echo ""