Skip to content

Instantly share code, notes, and snippets.

@bvandreunen
Last active March 24, 2019 20:40
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 bvandreunen/3ae5bd353d8b2160991a345f1a4319b0 to your computer and use it in GitHub Desktop.
Save bvandreunen/3ae5bd353d8b2160991a345f1a4319b0 to your computer and use it in GitHub Desktop.
Create new vhost/project in Homestead
#!/bin/bash
CONFIGFILE=~/Homestead/Homestead.yaml
HOSTSFILE=/etc/hosts
VAGRANTID=1fbc8f8
if ! [ $(id -u) = 0 ]; then
echo "Please run script with sudo to allow for hosts file changes." >&2
exit 1
fi
# Get real user for operations where root is not necessary
REAL_USER=$SUDO_USER
# Get Project name from input
read -p 'Project name : ' NAME
read -p 'Git repo SSH address : ' REPO
echo "Adding project vhost"
# Add Project to vhosts list in Homestead confi
SITE=" - map: $NAME\\
to: \/home\/vagrant\/Projects\/$NAME\/public\\
#-SITES"
sudo -u $REAL_USER sed "s/#-SITES/$SITE/g" $CONFIGFILE > ~/config_temp
sudo -u $REAL_USER mv -f ~/config_temp $CONFIGFILE
# Add Project to database list in Homestead confi
echo "Adding project database"
DATABASE=" - $NAME\\
#-DATABASES"
sudo -u $REAL_USER sed "s/#-DATABASES/$DATABASE/g" $CONFIGFILE > ~/config_temp
sudo -u $REAL_USER mv -f ~/config_temp $CONFIGFILE
# Reset the original permissions to Homestead config file
chown $REAL_USER:staff $CONFIGFILE
# Add host to /etc/hosts file
echo "Adding project host and IP to hosts file"
HOST="192.168.10.10 $NAME\\
#-HOSTS"
sed "s/#-HOSTS/$HOST/g" $HOSTSFILE > ~/hosts_temp
mv -f ~/hosts_temp $HOSTSFILE
# Create directory in Projects
sudo -u $REAL_USER mkdir ~/Projects/$NAME
if [ $REPO != "" ]
then
echo "Cloning repo"
git clone $REPO ~/Projects/$NAME/
chown -R $REAL_USER:staff ~/Projects/$NAME/
fi
echo "Provisioning vagrant"
sudo -u $REAL_USER vagrant reload --provision $VAGRANTID
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment