Skip to content

Instantly share code, notes, and snippets.

@JonTheNiceGuy
Created December 14, 2018 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonTheNiceGuy/b8e0cd569ef02f77df64ab1f09ed0353 to your computer and use it in GitHub Desktop.
Save JonTheNiceGuy/b8e0cd569ef02f77df64ab1f09ed0353 to your computer and use it in GitHub Desktop.
#! /bin/bash
# This script is based on these instructions:
# https://medium.com/@shakyShane/laravel-docker-part-1-setup-for-development-e3daaefaf3c
# It may also later include details from here
# https://medium.com/@shakyShane/laravel-docker-part-2-preparing-for-production-9c6a024e9797
################ Settings
# "latest" values set at time of writing: 2018-12-14
# You are encouraged to get your own values to update this!
laravel_latest=5.7.15
# https://hub.docker.com/_/php/
php_latest=7.3.0
# https://hub.docker.com/_/nginx/
nginx_latest=1.15.7
# https://hub.docker.com/_/mysql/
mysql_latest=8.0.13
debug=true
################ Set version to obtain
if [ -z "$laravel_version" ] ; then
laravel_version="$laravel_latest"
fi
if [ -z "$php_version" ] ; then
php_version="$php_latest"
fi
if [ -z "$nginx_version" ] ; then
nginx_version="$nginx_latest"
fi
if [ -z "$mysql_version" ] ; then
mysql_version="$mysql_latest"
fi
# Set path defaults
if [ -z "$baseDir" ] ; then
baseDir="$(pwd)"
fi
if [ -z "$sourcesDir" ] ; then
sourcesDir="${baseDir}/sources"
fi
if [ -z "$targetDir" ] ; then
targetDir="site"
fi
if [ -z "$sourceFileName" ] ; then
sourceFileName="v${laravel_version}.tar.gz"
fi
if [ -z "$sourceBaseUrl" ] ; then
sourceBaseUrl="https://github.com/laravel/laravel/archive"
fi
if [ -z "$sourceFullUrl" ] ; then
sourceFullUrl="${sourceBaseUrl}/${sourceFileName}"
fi
if [ -z "$sourceFileType" ] ; then
sourceFileType="gzip compressed"
fi
################ Colours
# Based on https://stackoverflow.com/a/20983251
MESSAGE_PREFIX="$(tput setab 4 ; tput setaf 7 ; tput smso)LOG:$(tput sgr0 ; tput setaf 4)"
FAIL_PREFIX="$(tput setab 7 ; tput setaf 1)FAIL:$(tput sgr0 ; tput setaf 1)"
DEBUG_PREFIX="$(tput setab 2 ; tput setaf 0)DEBUG:$(tput sgr0 ; tput setaf 2)"
RESET_SUFFIX="$(tput sgr0)"
################ Output messages
log() {
echo "${MESSAGE_PREFIX} $1${RESET_SUFFIX}"
}
debug() {
if [ -n "$debug" ] ; then
echo "${DEBUG_PREFIX} $1${RESET_SUFFIX}"
fi
}
fail() {
debug "FAILURE"
echo "${FAIL_PREFIX} $1${RESET_SUFFIX}"
exit 1
}
################# Worker Functions
addLine() {
if [ -n "$3" ]; then
rm "$1"
fi
echo "$2" >> "$1"
}
checkSystemStatus() {
docker info >/dev/null 2>/dev/null || fail "Unable to run Docker - are you authorized?"
debug "Docker OK"
docker-compose version >/dev/null 2>/dev/null || fail "Unable to run docker-compose - is it installed?"
debug "Docker-compose OK"
git version >/dev/null 2>/dev/null || fail "Unable to run git - is it installed?"
debug "git OK"
}
getSource() {
if [ -z "$tries" ] ; then
tries=3
else
((tries--))
if [ $tries -le 0 ] ; then
fail "Failed to get the file"
fi
debug "Trying again... ($tries left)"
fi
debug "Creating sources path"
mkdir -p "$sourcesDir" >/dev/null 2>/dev/null || fail "Could not create sources path"
cd "$sourcesDir" >/dev/null 2>/dev/null || fail "Could not enter the sources path"
if [ ! -e "$sourceFileName" ] ; then
debug "Getting laravel-${laravel_version} source"
wget -q "$sourceFullUrl" || fail "Could not retrieve source: $sourceFullUrl"
log "Laravel source obtained"
fi
if [ -e "$sourceFileName" ] ; then
debug "Checking the source is suitable"
if file "$sourceFileName" | grep "$sourceFileType" >/dev/null 2>/dev/null ; then
debug "All good"
else
debug "Not good. Removing."
rm "$sourceFileName"
getSource
fi
fi
cd "${baseDir}"
}
unpackSource() {
if [ -d "${baseDir}/${targetDir}" ] ; then
archiveDate="$(date +%Y%m%d%H%M%S)"
log "Target directory already exists. Archiving to OLD_${archiveDate}_${targetDir}"
mv "${baseDir}/${targetDir}" "${baseDir}/OLD_${archiveDate}_${targetDir}" >/dev/null 2>/dev/null || fail "Unable to archive old target dir"
fi
cd "${sourcesDir}"
if [ ! -d "${sourcesDir}/laravel-${laravel_version}" ] ; then
debug "Unpacking Source to Target via laravel-${laravel_version}"
tar -xzf "${sourcesDir}/${sourceFileName}" || fail "Unable to unpack laravel source files"
log "Source unpacked. Getting dependencies."
cd "laravel-${laravel_version}"
docker run --rm -v $(pwd):/app composer install >/dev/null 2>&1 || fail "Unable to obtain dependencies from composer"
log "Dependencies obtained"
cd ..
fi
mkdir -p "${baseDir}/${targetDir}"
cp -Rf "${sourcesDir}/laravel-${laravel_version}" "${baseDir}/${targetDir}/www"
cd "${baseDir}/${targetDir}"
log "Target created from laravel source"
}
makeDockerConfigFiles() {
dockerFiles="${baseDir}/${targetDir}/Docker"
mkdir -p "$dockerFiles"
debug "Building the Docker Config Files: docker-compose.yml"
file="${dockerFiles}/docker-compose.yml"
rm -f "$file"
addLine "$file" "version: '2'"
addLine "$file" 'services:'
addLine "$file" ' app:'
addLine "$file" ' build:'
addLine "$file" ' context: ./'
addLine "$file" ' dockerfile: app.dockerfile'
addLine "$file" ' working_dir: /var/www'
addLine "$file" ' volumes:'
addLine "$file" ' - ../www/:/var/www'
addLine "$file" ' environment:'
addLine "$file" ' - "DB_PORT=3306"'
addLine "$file" ' - "DB_HOST=database"'
addLine "$file" ' web:'
addLine "$file" ' build:'
addLine "$file" ' context: ./'
addLine "$file" ' dockerfile: web.dockerfile'
addLine "$file" ' working_dir: /var/www'
addLine "$file" ' volumes_from:'
addLine "$file" ' - app'
addLine "$file" ' ports:'
addLine "$file" ' - 8080:80'
addLine "$file" ' database:'
addLine "$file" " image: mysql:${mysql_version}"
addLine "$file" ' volumes:'
addLine "$file" ' - dbdata:/var/lib/mysql'
addLine "$file" ' environment:'
addLine "$file" ' - "MYSQL_DATABASE=homestead"'
addLine "$file" ' - "MYSQL_USER=homestead"'
addLine "$file" ' - "MYSQL_PASSWORD=secret"'
addLine "$file" ' - "MYSQL_ROOT_PASSWORD=secret"'
addLine "$file" ' ports:'
addLine "$file" ' - "33061:3306"'
addLine "$file" 'volumes:'
addLine "$file" ' dbdata:'
debug "Building the Docker Config Files: app"
file="${dockerFiles}/app.dockerfile"
rm -f "$file"
addLine "$file" "FROM php:${php_version}-fpm"
addLine "$file" 'RUN apt-get update && apt-get install -y libmcrypt-dev mysql-client --no-install-recommends'
debug "Building the Docker Config Files: web"
file="${dockerFiles}/web.dockerfile"
rm -f "$file"
addLine "$file" "FROM nginx:${nginx_version}"
addLine "$file" 'ADD vhost.conf /etc/nginx/conf.d/default.conf'
debug "Building the Docker Config Files: vhost"
file="${dockerFiles}/vhost.conf"
rm -f "$file"
addLine "$file" 'server {'
addLine "$file" ' listen 80;'
addLine "$file" ' index index.php index.html;'
addLine "$file" ' root /var/www/public;'
addLine "$file" ' location / {'
addLine "$file" ' try_files $uri /index.php?$args;'
addLine "$file" ' }'
addLine "$file" ' location ~ \.php$ {'
addLine "$file" ' fastcgi_split_path_info ^(.+\.php)(/.+)$;'
addLine "$file" ' fastcgi_pass app:9000;'
addLine "$file" ' fastcgi_index index.php;'
addLine "$file" ' include fastcgi_params;'
addLine "$file" ' fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;'
addLine "$file" ' fastcgi_param PATH_INFO $fastcgi_path_info;'
addLine "$file" ' }'
addLine "$file" '}'
}
makeLaravelEnvFile() {
webFiles="${baseDir}/${targetDir}/www"
cp "${webFiles}/.env.example" "${webFiles}/.env"
chmod -R 777 "${webFiles}/storage"
}
gitInitAndCommit() {
cd "${baseDir}/${targetDir}"
git init >/dev/null 2>/dev/null || fail "Unable to perform git-init"
git config user.name "Script" || fail "Unable to perform git-config user.name"
git config user.email "script@local" || fail "Unable to perform git-config user.email"
git add Docker www || fail "Unable to add missing files"
file="commit-msg"
rm -f "$file"
addLine "$file" "Initial commit completed"
addLine "$file" "Laravel: $laravel_version PHP: $php_version NGINX: $nginx_version MYSQL: $mysql_version"
addLine "$file" "Based on instructions from: https://medium.com/@shakyShane/laravel-docker-part-1-setup-for-development-e3daaefaf3c"
addLine "$file" "Updated by [Jon "TheNiceGuy" Spriggs](https://jon.sprig.gs) [(github)](https://github.com/JonTheNiceGuy)"
git commit -F "$file" >/dev/null 2>/dev/null || fail "Unable to perform git-commit"
rm -f "$file"
git config --unset user.name || fail "Unable to perform git-config user.name"
git config --unset user.email || fail "Unable to perform git-config user.email"
log "Made git init and git commit"
}
startDockerInstances() {
cd "${baseDir}/${targetDir}/Docker"
log "Starting docker instances"
docker-compose up -d || fail "Unable to bring up the docker containers"
log "Post-installing the laravel environment"
docker-compose exec app php artisan key:generate || fail "Unable to generate a new Laravel Key"
}
log "Building with Laravel: $laravel_version PHP: $php_version NGINX: $nginx_version MYSQL: $mysql_version"
checkSystemStatus
getSource
unpackSource
makeDockerConfigFiles
makeLaravelEnvFile
gitInitAndCommit
startDockerInstances
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment