-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-create-project
executable file
·158 lines (120 loc) · 3.83 KB
/
1-create-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
#!/bin/bash
# set -x
usage() {
PROGNAME=$0
cat <<- EOF
This script saves the logs collection for a given container
Missing arguments
$PROGNAME <project_name> dev|prod
NB: do not add the -dev or -prod extension to the name of the project
For example:
$PROGNAME test1 dev
EOF
exit 1
}
init(){
GREEN='\033[0;32m'
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
. ./config.ini
}
clearmsg(){
msg=$1
printf "${CYAN}${msg}${NC}\n"
}
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
}
generate_new_credentials(){
export new_username=$new_project-$(bw generate -p)
export new_password=$(bw generate)
}
create_db_user(){
#command="CREATE DATABASE \`$new_project\`; SHOW DATABASES; CREATE USER \"$new_username\" IDENTIFIED BY \"$new_password\"; SELECT User FROM mysql.user; GRANT ALL PRIVILEGES ON \`$new_project.*\` TO \"$new_username\"; FLUSH PRIVILEGES; SHOW GRANTS FOR \"$new_username\";"
command="CREATE DATABASE \`$new_project\`; SHOW DATABASES; CREATE USER \"$new_username\" IDENTIFIED BY \"$new_password\"; SELECT User FROM mysql.user; GRANT ALL PRIVILEGES ON \`$new_project\`.* TO \"$new_username\"; FLUSH PRIVILEGES; SHOW GRANTS FOR \"$new_username\";"
docker exec -it $generaldb_container_name mysql -u "$username" -p$password -e"$command"
}
create_files(){
mkdir -p /var/www/$new_project/wordpress
chown 1001 /var/www/$new_project/wordpress
chmod o-rws /var/www/$new_project/wordpress
mkdir /projects/$new_project/
cp $SCRIPTPATH/sources/docker-compose_wordpress.yml /projects/$new_project/docker-compose.yml
sed -i -e "s/{{new_project}}/$new_project/g" -e "s/{{database}}/$generaldb_container_name/g" /projects/$new_project/docker-compose.yml
# export new_project=$new_project
# export new_username=$new_username
# export new_password=$new_password
docker compose -f $project_path/$new_project/docker-compose.yml up -d
}
main(){
init
mode=$2
new_project=$1-$mode
export new_project
generaldb_container_name=${mode}_generaldb_container_name
generaldb_container_name=${!generaldb_container_name}
generaldb_credentials_file $mode
generate_new_credentials
create_db_user
create_files
clearmsg "\nSave this info (in Bitwarden?):"
cat <<-EOF
New database in $generaldb_container_name: $new_project
New user: $new_username
New password: $new_password
EOF
clearmsg "\nIf you ever need to recreate the wordpress container:"
cat <<-EOF
NB: always export the variables before creating the container
export new_project=$new_project; export new_username=$new_username; export new_password=$new_password
docker compose -f $project_path/$new_project/docker-compose.yml up -d
EOF
clearmsg "\nTo create backup:"
cat <<-EOF
$credentials_path/.$new_project.cnf
[mysqldump]
user=$new_username
password=$new_password
EOF
}
if [[ $# -ne 2 ]]
then
init
usage
fi
main $@