You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
3.4 KiB

5 years ago
#!/usr/bin/env bash
source .env
5 years ago
5 years ago
DOMAIN=''
SQL_DB=''
SQL_USER=''
SQL_PASS=''
5 years ago
ANY="'%'"
5 years ago
SET_OK=0
5 years ago
EPACE=' '
echow(){
FLAG=${1}
shift
echo -e "\033[1m${EPACE}${FLAG}\033[0m${@}"
}
5 years ago
help_message(){
5 years ago
echo -e "\033[1mOPTIONS\033[0m"
echow '-D, --domain [DOMAIN_NAME]'
echo "${EPACE}${EPACE}Example: database.sh -D example.com"
echo "${EPACE}${EPACE}Will auto generate Database/username/password for the domain"
echow '-D, --domain [DOMAIN_NAME] -U, --user [xxx] -P, --password [xxx] -DB, --database [xxx]'
echo "${EPACE}${EPACE}Example: database.sh -D example.com -U USERNAME -P PASSWORD -DB DATABASENAME"
echo "${EPACE}${EPACE}Will create Database/username/password by given"
echow '-H, --help'
echo "${EPACE}${EPACE}Display help and exit."
exit 0
5 years ago
}
check_input(){
if [ -z "${1}" ]; then
help_message
exit 1
fi
}
specify_name(){
check_input ${SQL_USER}
check_input ${SQL_PASS}
check_input ${SQL_DB}
}
auto_name(){
5 years ago
SQL_DB="${TRANSNAME}"
SQL_USER="${TRANSNAME}"
SQL_PASS="'${RANDOM_PASS}'"
5 years ago
}
gen_pass(){
RANDOM_PASS="$(openssl rand -base64 12)"
}
trans_name(){
TRANSNAME=$(echo ${1} | tr -d '.&&-')
}
display_credential(){
5 years ago
if [ ${SET_OK} = 0 ]; then
echo "Database: ${SQL_DB}"
echo "Username: ${SQL_USER}"
echo "Password: $(echo ${SQL_PASS} | tr -d "'")"
fi
5 years ago
}
store_credential(){
if [ -d "./sites/${1}" ]; then
if [ -f ./sites/${1}/.db_pass ]; then
mv ./sites/${1}/.db_pass ./sites/${1}/.db_pass.bk
fi
cat > "./sites/${1}/.db_pass" << EOT
5 years ago
"Database":"${SQL_DB}"
5 years ago
"Username":"${SQL_USER}"
"Password":"$(echo ${SQL_PASS} | tr -d "'")"
EOT
else
echo "./sites/${1} not found, abort credential store!"
fi
5 years ago
}
add_sql_client(){
docker-compose exec mysql su -c 'apk add mysql-client'
}
check_db_access(){
5 years ago
#add_sql_client
5 years ago
docker-compose exec mysql su -c "mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e 'status'" >/dev/null 2>&1
5 years ago
if [ ${?} != 0 ]; then
echo "DB access failed, please check!"
exit 1
fi
}
db_setup(){
5 years ago
docker-compose exec mysql su -c 'mysql -uroot -p${MYSQL_ROOT_PASSWORD} \
-e "CREATE DATABASE '${SQL_DB}';" \
-e "GRANT ALL PRIVILEGES ON '${SQL_DB}'.* TO '${SQL_USER}'@'${ANY}' IDENTIFIED BY '${SQL_PASS}';" \
-e "FLUSH PRIVILEGES;"'
5 years ago
SET_OK=${?}
5 years ago
}
auto_setup_main(){
check_input ${DOMAIN}
gen_pass
trans_name ${DOMAIN}
auto_name
5 years ago
check_db_access
5 years ago
db_setup
display_credential
5 years ago
store_credential ${DOMAIN}
5 years ago
}
specify_setup_main(){
specify_name
5 years ago
check_db_access
5 years ago
db_setup
display_credential
5 years ago
store_credential ${DOMAIN}
5 years ago
}
5 years ago
main(){
if [ "${SQL_USER}" != '' ] && [ "${SQL_PASS}" != '' ] && [ "${SQL_DB}" != '' ]; then
specify_setup_main
else
auto_setup_main
fi
}
5 years ago
check_input ${1}
while [ ! -z "${1}" ]; do
case ${1} in
-[hH] | -help | --help)
help_message
;;
5 years ago
-[dD] | -domain| --domain) shift
5 years ago
DOMAIN="${1}"
;;
5 years ago
-[uU] | -user | --user) shift
5 years ago
SQL_USER="${1}"
;;
5 years ago
-[pP] | -password| --password) shift
5 years ago
SQL_PASS="'${1}'"
5 years ago
;;
5 years ago
-db | -DB | -database| --database) shift
5 years ago
SQL_DB="${1}"
5 years ago
;;
*)
help_message
;;
esac
shift
done
5 years ago
main