#!/bin/bash # TUTORIAL: https://www.webune.com/forums/api.html # The purpose of this script is to install and run a Lavarel API app with token authentication # Usage: To use this script, in Ubuntu terminal type the following command and follow the prompts: # curl -s https://www.webune.com/forums/web/img/webune-com/202106/06-p4925-apiv2.txt | bash clear echo "Stopping All Running Containers.." docker stop $(docker ps -a -q) clear docker info > /dev/null 2>&1 set -e if [ $? -ne 0 ]; then echo "1. Docker is not running." exit 1 fi echo "2. [OK] Docker is running.." # REPO TO CLONE REPOT_DEFAULT="https://github.com/edwinaquino/laravel-backend-api.git" read -p "Enter the repository you want to clone: [$REPOT_DEFAULT] " REPO if [[ $REPO == "" ]]; then REPO=$REPOT_DEFAULT set -e fi #APPNAME_DEFAULT="laravel-backend-api" # Epoch on Linux, Bash date example: 1623008881 APPNAME_DEFAULT="api$(date +%s)" read -p "Enter The App you want to Create: [$APPNAME_DEFAULT/] " APPNAME if [[ $APPNAME == "" ]]; then APPNAME=$APPNAME_DEFAULT set -e fi # check if directory exists if [ -d "$APPNAME" ]; then echo "3. FATAL FERROR: $APPNAME ALREADY EXIST. Try Again." set -e exit fi echo "4. [OK] App does not exist. Creating APP: $APPNAME" # make a database with the app name instead of prompting user DBNAME=$APPNAME # DBNAME_DEFAULT=$APPNAME # read -p "6. Enter Database to create for this project: [$DBNAME_DEFAULT/] " DBNAME # if [[ $DBNAME == "" ]]; then # DBNAME=$DBNAME_DEFAULT # set -e # fi # CLONE THE LATEST ONLY git clone --depth=1 $REPO $APPNAME set -e cd $APPNAME set -e echo "5. Building Laravel Project..." docker run --rm -v $(pwd):/opt -w /opt laravelsail/php80-composer:latest composer install set -e echo "Provide Sudo Password to Change you as the Owner of this directory" sudo chown -R $USER: . set -e echo "l RUNNING SAIL" vendor/bin/sail up -d set -e echo "CREATING .ENV" cp .env.example .env set -e sed -i 's/DB_HOST=127.0.0.1/DB_HOST=mysql/g' .env set -e echo "GENERATE KEY FOR .ENV FILE" vendor/bin/sail artisan key:generate set -e #vendor/bin/sail bash #set -e sed -i 's/DB_DATABASE=laravel_backend_api/DB_DATABASE='$DBNAME'/g' .env set -e code .env set -e #mysql --password= --execute="create database $DBNAME" # service docker status # docker-compose exec mysql sh echo "CREATING DATABASE NAME $DBNAME ..." #sleep 20 # wait wile containers are fully up # THIS WORKS ONLY WHEN I HUMAN EXECUTE IT: vendor/bin/sail exec -d mysql mysql --password= --execute="create database $DBNAME" echo "[IMPORTANT] Copy and paste the following command and then type exit to exit container and continue with installation" echo 'mysql --password= --execute="create database '$DBNAME';"' #tput setaf 1; echo 'mysql --password= --execute="create database `'$DBNAME'`"' # DOES NOT WORK: docker exec -it $DBNAME"_mysql_1" mysql --password= --execute="create database $DBNAME" vendor/bin/sail exec mysql sh set -e echo "[OK] DATABASE NAME $DBNAME CREATED SUCCESSFULY" #exit #set -e echo "MIGRATING SEED..." vendor/bin/sail artisan migrate:fresh --seed set -e echo "INSTALLING PASSPORT" vendor/bin/sail artisan passport:install --force set -e echo "###################### COMPLETED #####################" echo "Visit https://www.webune.com/forums/api.html for next steps"