Skip to content

Instantly share code, notes, and snippets.

@AlexandroPerez
Created March 17, 2017 08:44
Show Gist options
  • Save AlexandroPerez/ed43f75ee3bad8f422c5939b2c2b4eb5 to your computer and use it in GitHub Desktop.
Save AlexandroPerez/ed43f75ee3bad8f422c5939b2c2b4eb5 to your computer and use it in GitHub Desktop.
Add a new project to your Google Cloud Platform in its own www folder, and ready for automatic deployment using git. Just provide the name of your new project, and this script will take care of the rest.
#!/bin/sh
# Make sure this script is not run using sudo privileges
if [ "$USER" = "root" ]; then
echo "\033[0;31mERROR: You shouldn't run this shell script as root..."
exit
fi
NGINXREADY=true
if [ ! -d "/etc/nginx/conf.d/projects" ]; then
echo "\033[0;31mERROR: /etc/nginx/conf.d/projects doesn't exist..."
echo "\033[0;33mDid you run the ./prepare-auto-project-deployment script as sudo?"
echo "^^^ You can also fix this error by running the following commands:\033[0;37m\n"
echo " sudo mkdir /etc/nginx/conf.d/projects"
echo " sudo chown -R \$USER:\$USER /etc/nginx/conf.d/projects\n"
NGINXREADY=false
fi
if ! grep -Fq "include /etc/nginx/conf.d/projects/*.conf;" /etc/nginx/conf.d/default.conf; then
echo "\033[0;31mERROR: /etc/nginx/conf.d/default.conf is not configured to add projects automatically..."
echo "\033[0;33mDid you run the ./prepare-auto-project-deployment script as sudo?"
echo "^^^ File seems to be missing the following line inside server block.\033[0;37m\n"
echo " include /etc/nginx/conf.d/projects/*.conf;\n"
echo " \033[0;33mYou can try adding the line to the server block yourself to fix this problem"
NGINXREADY=false
fi
if [ "$NGINXREADY" = false ]; then
echo "\033[0;31m\nSome errors need to be fixed before proceeding. \033[0;33m"
echo "You can try fixing them yourself as indicated in the error messages, or make sure"
echo "you dowloaded the prepare-auto-project-deployment script and executed as sudo as follows:\033[0;37m\n"
echo " wget \"https://github.com/link\""
echo " chmod +x prepare-auto-project-deployment"
echo " sudo ./prepare-auto-project-deployment\n"
exit
fi
# Make sure /projects folder in /etc/nginx/conf.d is owned by current user
PROJECTOWNER=$(stat -c '%U' /etc/nginx/conf.d/projects)
if [ ! "$PROJECTOWNER" = "$USER" ]; then
echo "\033[0;31mERROR: The projects folder in /etc/nginx/conf.d/ is owned by $PROJECTOWNER.\nYou ($USER) must be made the owner before proceeding.\n\033[0;33m\nYou can run the following command to change ownership:\033[0;37m\n"
echo "sudo chown -R \$USER:\$USER /etc/nginx/conf.d/projects\n"
exit
fi
PROJECTNAME=$1
while [ -z $PROJECTNAME ]
do
echo -n '\033[0;33mWrite the project name as it will appear on your site (i.e. mysite.com/project-name)\nUse all lowercase and no spaces (you can use hyphens to separate words )\033[0;37m\nproject-name: '
read PROJECTNAME
done
if [ -d "$PROJECTNAME.git" ]; then
echo "\033[0;31mERROR: A project with that name already exits."
exit
fi
echo "\n\033[0;32mCreating bare repository..."
sleep 3
echo "Done\033[0;37m"
git init --bare $PROJECTNAME.git
echo "\n\033[0;32mCreating git hook..."
echo "#!/bin/sh" >> $PROJECTNAME.git/hooks/post-receive
echo "git --work-tree=/var/www/$PROJECTNAME --git-dir=/home/$USER/$PROJECTNAME.git checkout -f" >> $PROJECTNAME.git/hooks/post-receive
chmod +x $PROJECTNAME.git/hooks/post-receive
sleep 3
echo "Done"
echo "\n\033[0;37mpost-receive hook created at ~/$PROJECTNAME.git/hooks/ with the following content\033[0;37m"
cat $PROJECTNAME.git/hooks/post-receive
echo "\n\033[0;32mCreating project's folder in /var/www/..."
mkdir /var/www/$PROJECTNAME
sleep 3
echo "Done"
echo "\n\033[0;37mfolder /var/www/$PROJECTNAME created."
echo "\n\033[0;32mCreating nginx configuration file..."
echo "# Project $PROJECTNAME" >> /etc/nginx/conf.d/projects/$PROJECTNAME.conf
echo "location /$PROJECTNAME {" >> /etc/nginx/conf.d/projects/$PROJECTNAME.conf
echo " root /var/www;" >> /etc/nginx/conf.d/projects/$PROJECTNAME.conf
echo " index index.html index.htm;" >> /etc/nginx/conf.d/projects/$PROJECTNAME.conf
echo "}" >> /etc/nginx/conf.d/projects/$PROJECTNAME.conf
sleep 3
echo "Done"
echo "$PROJECTNAME.conf file created at /etc/nginx/conf.d/projects/ with the following content:\033[0;37m"
cat /etc/nginx/conf.d/projects/$PROJECTNAME.conf
echo "\n\033[0;33mALL DONE!"
echo "Now all that's left to do is to run the following commands as sudo. If the first command\nsays the syntax is ok, and the test was successful, proceed with the second command\n\033[0;37m"
echo " sudo nginx -t"
echo " sudo systemctl restart nginx"
echo "\n\033[0;33mUse the following command on your local machine's repository to add this\n project as a remote with name \"live\":\n"
echo "\n\033[0;37m git remote add live ssh://<user>@<vm-ip-address>/home/$USER/$PROJECTNAME.git"
echo "\n\033[0;33mreplace <user> with your google cloud username, and <vm-ip-address> with your\nvm instance IP address"
echo "\nNow every time you want to deploy your site execute the following commands after\nmaking changes to your project:"
echo "\n\033[0;37m git add ."
echo " git commit -m\"<type a custom message for your changes here>\""
echo " git push live master\n\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment