From 6e4b3e830af6869d309b87044098006f0628a693 Mon Sep 17 00:00:00 2001 From: Joe Prochazka Date: Fri, 29 Apr 2016 23:25:14 -0400 Subject: [PATCH] Allow remote MySQL setup. --- bash/image.sh | 169 +++++++++++++++++++++++++++----------------------- 1 file changed, 91 insertions(+), 78 deletions(-) diff --git a/bash/image.sh b/bash/image.sh index 488fe8e..0776f0e 100644 --- a/bash/image.sh +++ b/bash/image.sh @@ -159,7 +159,8 @@ if [[ $ADVANCED =~ ^[yY]$ ]]; then fi fi -# Check for prerequisite packages. +## CHECK FOR PREREQUISITE PACKAGES + echo -e "\033[33m" echo "Installing packages needed to build and fulfill dependencies..." echo -e "\033[37m" @@ -172,84 +173,95 @@ if [[ $ADVANCED =~ ^[yY]$ ]]; then if [[ $LOCALDATABASE != 2 ]]; then # Install MySQL locally. CheckPackage mysql-server - CheckPackage mysql-client fi + CheckPackage mysql-client CheckPackage php5-mysql CheckPackage python-mysqldb fi fi -# Create the database If advanced features was selected -if [[ $ADVANCED =~ ^[yY]$ ]]; then - if [[ $DATABASEENGINE != 2 ]]; then - echo -e "\033[31m" - echo "Gathering Database Information" - echo -e "\033[33m" - echo "Please supply the information pertaining to the new password when asked." - echo "" - echo "If the database will be hosted locally on this device a database will be" - echo "created automatically for you. If you are hosting your database remotely" - echo "you will need to manually create the database and user on the remote device." - echo -e "\033[37m" - if [[ $LOCALDATABASE == 2 ]]; then - # Ask for remote MySQL address if the database is hosted remotely. - read -p "Remote MySQL Server Address: " DATABASEHOST - else +if [[ $INSTALLED == "n" ]]; then + + ## CREATE THE DATABASE IF ADVANCED FEATURES WAS SELECTED + + if [[ $ADVANCED =~ ^[yY]$ ]]; then + if [[ $DATABASEENGINE != 2 ]]; then + echo -e "\033[31m" + echo "Gathering Database Information" + echo -e "\033[33m" + echo "Please supply the information pertaining to the new password when asked." + echo "" + echo "If the database will be hosted locally on this device a database will be" + echo "created automatically for you. If you are hosting your database remotely" + echo "you will need to manually create the database and user on the remote device." + echo -e "\033[37m" + DATABASEHOST="localhost" - fi - read -p "Password for MySQL root user: " MYSQLROOTPASSWORD - read -p "New Database Name: " DATABASENAME - read -p "New Database User Name: " DATABASEUSER - read -p "New Database User Password: " DATABASEPASSWORD - - # Database creation can only be handled locally. - if [[ $LOCALDATABASE != 2 ]]; then - if [[ $DATABASEENGINE == 1 ]] || [[ $DATABASEENGINE == "" ]]; then - echo -e "\033[33m" - echo -e "Creating MySQL database and user...\033[37m" - mysql -uroot -p${MYSQLROOTPASSWORD} -e "CREATE DATABASE ${DATABASENAME};" - mysql -uroot -p${MYSQLROOTPASSWORD} -e "CREATE USER '${DATABASEUSER}'@'localhost' IDENTIFIED BY \"${DATABASEPASSWORD}\";"; - mysql -uroot -p${MYSQLROOTPASSWORD} -e "GRANT ALL PRIVILEGES ON ${DATABASENAME}.* TO '${DATABASEUSER}'@'localhost';" - mysql -uroot -p${MYSQLROOTPASSWORD} -e "FLUSH PRIVILEGES;" + if [[ $LOCALDATABASE == 2 ]]; then + # Ask for remote MySQL address if the database is hosted remotely. + read -p "Remote MySQL Server Address: " DATABASEHOST fi - fi + read -p "Password for MySQL root user: " MYSQLROOTPASSWORD - echo -e "\033[31m" - echo "BE SURE TO WRITE THIS INFORMATION DOWN." - echo -e "\033[33m" - echo "This information will be needed in order to complete the installation of the portal." - echo "" - if [[ $LOCALDATABASE == 2 ]]; then - echo -e "\033[31mNOTE:" - echo "Being you are hosting your database remotely you will need this information to create" - echo "both the database and database user on your remote database server." + # Check that the supplied password is correct. + while ! mysql -u root -p$MYSQLROOTPASSWORD -h $DATABASEHOST -e ";" ; do + echo -e "\033[31m" + echo -e "Unable to connect to the MySQL server using the supplied password.\033[37m" + read -p "Password for MySQL root user: " MYSQLROOTPASSWORD + done + + read -p "New Database Name: " DATABASENAME + read -p "New Database User Name: " DATABASEUSER + read -p "New Database User Password: " DATABASEPASSWORD + + # Create the database and user as well as assign permissions. + if [[ $DATABASEENGINE == 1 ]] || [[ $DATABASEENGINE == "" ]]; then + echo -e "\033[33m" + echo -e "Creating MySQL database and user...\033[37m" + mysql -uroot -p${MYSQLROOTPASSWORD} -h $DATABASEHOST -e "CREATE DATABASE ${DATABASENAME};" + mysql -uroot -p${MYSQLROOTPASSWORD} -h $DATABASEHOST -e "CREATE USER '${DATABASEUSER}'@'localhost' IDENTIFIED BY \"${DATABASEPASSWORD}\";"; + mysql -uroot -p${MYSQLROOTPASSWORD} -h $DATABASEHOST -e "GRANT ALL PRIVILEGES ON ${DATABASENAME}.* TO '${DATABASEUSER}'@'localhost';" + mysql -uroot -p${MYSQLROOTPASSWORD} -h $DATABASEHOST -e "FLUSH PRIVILEGES;" + fi + + echo -e "\033[31m" + echo "BE SURE TO WRITE THIS INFORMATION DOWN." echo -e "\033[33m" - fi - echo "Database Server: ${DATABASEHOST}" - echo "Database User: ${DATABASEUSER}" - echo "Database Password: ${DATABASEPASSWORD}" - echo "Database Name: ${DATABASENAME}" - echo -e "\033[37m" - read -p "Press enter to continue..." CONTINUE - fi + echo "This information will be needed in order to complete the installation of the portal." + echo "" + if [[ $LOCALDATABASE == 2 ]]; then + echo -e "\033[31mNOTE:" + echo "Being you are hosting your database remotely you will need this information to create" + echo "both the database and database user on your remote database server." + echo -e "\033[33m" + fi + echo "Database Server: ${DATABASEHOST}" + echo "Database User: ${DATABASEUSER}" + echo "Database Password: ${DATABASEPASSWORD}" + echo "Database Name: ${DATABASENAME}" + echo -e "\033[37m" + read -p "Press enter to continue..." CONTINUE - # Setup the flight logging script. - echo -e "\033[33m" - echo -e "Creating configuration file...\033[37m" - case $DATABASEENGINE in - "2") - tee ~/adsb-receiver/build/portal/logging/config.json > /dev/null < /dev/null < /dev/null < /dev/null < /dev/null < /dev/null <0)) && sudo sed -i "${lnum[$((${#lnum[@]}-1))]}i ${BUILDDIR}/portal/logging/flights-maint.sh &\n" /etc/rc.local + fi + + # Start flight logging. echo -e "\033[33m" - echo -e "Adding startup line to rc.local...\033[37m" - lnum=($(sed -n '/exit 0/=' /etc/rc.local)) - ((lnum>0)) && sudo sed -i "${lnum[$((${#lnum[@]}-1))]}i ${BUILDDIR}/portal/logging/flights-maint.sh &\n" /etc/rc.local + echo -e "Starting flight logging...\033[37m" + nohup ${BUILDDIR}/portal/logging/flights-maint.sh > /dev/null 2>&1 & fi - - # Start flight logging. - echo -e "\033[33m" - echo -e "Starting flight logging...\033[37m" - ~/adsb-receiver/build/portal/logging/flights-maint.sh & fi ## FINISH CONFIGURATION