Created
June 20, 2015 13:18
-
-
Save dtateii/340623adf021fc141206 to your computer and use it in GitHub Desktop.
Interactive prompt for setting up directories on existing Vagrant boxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
################################################ | |
# Vagrant Local Site Configurator # | |
# Written by Brendan Corcoran, Dave Tate for NJI Media # | |
# # | |
# Usage: ./newsite.sh -OR- sh newsite.sh # | |
################################################ | |
# Configure Vagrant/VM Sites directories | |
# $HOME/$VMROOT/$VENV/$VHOSTS_DIR/$DOMAIN_PRIMARY/$DOMAIN_SUB/base/web/ | |
# VM/Sites Root | |
VMROOT="Vagrant" | |
# Virtual Hosts Dirname | |
VHOSTS_DIR="vhosts" | |
############################## | |
# Helper Functions # | |
############################## | |
check_sys() { | |
# Check absolute path configuration is OK | |
VM_PATH="$HOME/$VMROOT"; | |
if [[ -d $VM_PATH ]]; then | |
echo "...system config check OK."; echo; | |
else | |
echo "Error. Can't create new site on this machine."; | |
echo "${VM_PATH} does not exist." | |
echo "Aborting." | |
exit; | |
fi | |
} | |
############################## | |
# Creation Functions # | |
############################## | |
create_dirs () { | |
# Shortcuts | |
ABS_PATH="$HOME/$VMROOT/$VENV/$VHOSTS_DIR" | |
DOMAIN_PATH="$ABS_PATH/$DOMAIN_PRIMARY" | |
SITE_PATH="$ABS_PATH/$DOMAIN_PRIMARY/$DOMAIN_SUB" | |
BASE_PATH="$ABS_PATH/$DOMAIN_PRIMARY/$DOMAIN_SUB/base" | |
WEB_ROOT="$ABS_PATH/$DOMAIN_PRIMARY/$DOMAIN_SUB/base/web" | |
# Check VM Env and VHOSTS dirs OK | |
if [[ ! -d $ABS_PATH ]]; then | |
echo "Checking configured paths failed. Inspect your system or script configuration."; | |
echo "${ABS_PATH} needs to exist." | |
echo "Aborting." | |
exit; | |
fi | |
# Check site doesn't already exist | |
if [[ -d $SITE_PATH ]]; then | |
echo "You already have this site, fool!" | |
echo $SITE_PATH | |
echo "Aborting." | |
exit; | |
else | |
# Create new or skip existing domain directory | |
if [[ ! -d $DOMAIN_PATH ]]; then | |
echo "Creating domain $DOMAIN_PRIMARY directory..." | |
mkdir $DOMAIN_PATH | |
else | |
echo "Domain $DOMAIN_PRIMARY directory already exists, not creating..." | |
fi | |
# Create new subdomain directory | |
echo "Creating site directory, $DOMAIN_PRIMARY/$DOMAIN_SUB..." | |
mkdir $SITE_PATH | |
# Create backup directory | |
echo "Creating directory for backups..." | |
mkdir "$SITE_PATH/archive" | |
# Create site/app base directory and webroot directory | |
echo "Creating directory for application base and webroot..." | |
mkdir $BASE_PATH | |
mkdir $WEB_ROOT | |
# Create data directories | |
echo "Creating directories for data exports..." | |
mkdir -p "$SITE_PATH/data/"{dev,loc,prd,stg} | |
# Create logs directory | |
echo "Creating directory for error and access logs..." | |
mkdir "$SITE_PATH/logs" | |
# Create Certs directory | |
echo "Creating directory for SSL certificates..." | |
mkdir -p "$SITE_PATH/secret/ssl" | |
fi | |
} | |
############################## | |
# User Input Functions # | |
############################## | |
## | |
# Vagrant Environment Select | |
## | |
get_venvs () { | |
for f in ~/Vagrant/*; do | |
if [[ -d $f ]]; then | |
echo $f | xargs -n 1 basename | |
fi | |
done | |
} | |
select_vagrant () { | |
echo "Which Vagrant Stack is this for?" | |
select VENV in $(get_venvs); do | |
if [[ -n "$VENV" ]]; then | |
break | |
else | |
echo "Pick a number from the list, weirdo." | |
fi | |
done | |
} | |
## | |
# Domain Prompts | |
## | |
read_domain_primary () { | |
echo "Primary Domain: " | |
read DOMAIN_PRIMARY | |
# Strip any spaces from input | |
DOMAIN_PRIMARY=`echo ${DOMAIN_PRIMARY} | sed 's/ /''/g'` | |
} | |
read_domain_sub () { | |
echo "Sub Domain: " | |
read DOMAIN_SUB | |
# Strip any spaces from input | |
DOMAIN_SUB=`echo ${DOMAIN_SUB} | sed 's/ /''/g'` | |
} | |
name_domain_primary () { | |
echo " " | |
echo "What is the primary domain?" | |
read_domain_primary | |
while [ ! -n "${DOMAIN_PRIMARY}" ] ; do | |
echo 'Primary domain can not be empty, try again.' | |
read_domain_primary | |
done | |
} | |
name_domain_sub () { | |
echo " " | |
echo "What is the sub domain? If none, you must use 'www'." | |
read_domain_sub | |
while [ ! -n "${DOMAIN_SUB}" ] ; do | |
echo 'Subdomain can not be empty, try again.' | |
read_domain_sub | |
done | |
} | |
############################## | |
# Process | |
############################## | |
# | |
echo '########################################################' | |
echo '# Vagrant Local Site Configurator #' | |
echo '########################################################' | |
echo; | |
check_sys | |
select_vagrant | |
name_domain_primary | |
name_domain_sub | |
create_dirs | |
echo "Done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment