-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-delete-project
executable file
·172 lines (135 loc) · 3.49 KB
/
1-delete-project
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
#!/bin/bash
# set -x
usage() {
PROGNAME=$0
cat <<- EOF
Missing arguments
$PROGNAME <project_name> <username> dev|prod
NB: do not add the -dev or -prod extension to the name of the project
For example:
$PROGNAME test1 test1-dev-clasp-mustiness-retiring dev
EOF
exit 1
}
init(){
. ./config.ini
}
generaldb_credential(){
unset login
prompt="Enter generaldb_mariadb ADMIN username: "
read -p "$prompt" username
unset password
prompt="Enter generaldb_mariadb ADMIN password: "
while IFS= read -p "$prompt" -r -s -n 1 char
do
if [[ $char == $'\0' ]]
then
break
fi
prompt='*'
password+="$char"
done
echo
}
generaldb_credentials_file(){
mode=$1
unset username
unset password
if [[ ! -f .${mode}_generaldb_creds ]]; then
echo "Credentials file cannot be found (.generaldb_creds)"
exit 1
fi
. ./.${mode}_generaldb_creds
username=${mode}_username
username=${!username}
password=${mode}_password
password=${!password}
if [[ -z $username ]] || [[ -z password ]]; then
echo "Credentials are empty (in .generaldb_creds)"
exit 1
fi
}
show_dbs(){
echo "Showing Databases"
command="show databases;"
docker exec -it $generaldb_container_name mysql -u "$username" -p$password -e"$command"
echo
}
delete_db_user(){
command="REVOKE ALL PRIVILEGES, GRANT OPTION FROM \"$username_to_delete\"; DROP USER \"$username_to_delete\";"
docker exec -it $generaldb_container_name mysql -u "$username" -p$password -e"$command"
}
delete_dbs(){
command="DROP DATABASE \`$project_to_delete\`;"
docker exec -it $generaldb_container_name mysql -u "$username" -p$password -e"$command"
}
delete_files(){
if [[ -z $project_to_delete ]]; then
"Variable \$project_to_delete is empty, cannot continue"
fi
rm -r /projects/$project_to_delete/*
rmdir /projects/$project_to_delete/
echo "About to remove /var/www/$project_to_delete/"
rm -r /var/www/$project_to_delete/*
rmdir /var/www/$project_to_delete/
}
show_db_users(){
echo "Showing Users"
command="SELECT User FROM mysql.user;"
docker exec -it $generaldb_container_name mysql -u "$username" -p$password -e"$command"
echo
}
main(){
init
username_to_delete=$2
#export $project_to_delete
#export $username_to_delete
mode=$3
project_to_delete=$1-$mode
generaldb_container_name=${mode}_generaldb_container_name
generaldb_container_name=${!generaldb_container_name}
generaldb_credentials_file $mode
### while true; do
### echo
### prompt="Other things to delete? (yes/no) "
### read -p "$prompt" answer
### if [[ $answer =~ ^(yes|y|YES|Y)$ ]]; then
### execution
### else
### break
### fi
### done
### echo
### show_dbs
###
### echo "Content of /var/www"
### ls -1A /var/www
### echo
###
### echo "Content of /projects"
### ls -1A /projects
### echo
###
### prompt="Enter name of project to delete: "
### read -p "$prompt" project_to_delete
if [[ -z $project_to_delete ]]; then
echo "Project name cannot be empty"
exit 1
fi
# show_db_users
# prompt="Enter username to delete: "
# read -p "$prompt" username_to_delete
delete_db_user
show_db_users
delete_dbs
show_dbs
docker stop $project_to_delete
docker rm $project_to_delete
delete_files
}
if [[ $# -ne 3 ]]
then
init
usage
fi
main $@