| 
									
										
										
										
											2017-06-14 11:48:01 +05:30
										 |  |  | #!/bin/bash
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # deploy.sh | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # This script made to deploy dynamicweb project. | 
					
						
							|  |  |  | # Please run this script as app user. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | APP_HOME_DIR=~/app | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | echo "" > $APP_HOME_DIR/deploy.log | 
					
						
							|  |  |  | while true; do | 
					
						
							|  |  |  |   case "$1" in | 
					
						
							|  |  |  |     -h | --help ) HELP=true; shift ;; | 
					
						
							|  |  |  |     -v | --verbose ) VERBOSE=true; shift ;; | 
					
						
							| 
									
										
										
										
											2017-08-19 18:28:17 +05:30
										 |  |  |     -D | --dbmakemigrations ) DB_MAKE_MIGRATIONS=true; shift ;; | 
					
						
							| 
									
										
										
										
											2017-06-14 11:48:01 +05:30
										 |  |  |     -d | --dbmigrate ) DB_MIGRATE=true; shift ;; | 
					
						
							|  |  |  |     -n | --nogit ) NO_GIT=true; shift ;; | 
					
						
							|  |  |  |     -b | --branch ) BRANCH="$2"; shift 2 ;; | 
					
						
							|  |  |  |     -- ) shift; break ;; | 
					
						
							|  |  |  |     * ) break ;; | 
					
						
							|  |  |  |   esac | 
					
						
							|  |  |  | done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if [ "$BRANCH" == "" ]; then | 
					
						
							|  |  |  |     BRANCH="master" | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if [ "$HELP" == "true" ]; then | 
					
						
							|  |  |  |     echo "./deploy.sh <options>" | 
					
						
							|  |  |  |     echo "                     " | 
					
						
							|  |  |  |     echo "options are : " | 
					
						
							|  |  |  |     echo "        -h, --help: Print this help message" | 
					
						
							|  |  |  |     echo "        -v, --verbose: Show verbose output to stdout. Without this a deploy.log is written to ~/app folder" | 
					
						
							| 
									
										
										
										
											2017-08-19 18:28:17 +05:30
										 |  |  |     echo "        -D, --dbmakemigrations: Do DB makemigrations" | 
					
						
							|  |  |  |     echo "        -d, --dbmigrate: Do DB migrate. To do both makemigrations and migrate, supply both switches -D and -d" | 
					
						
							|  |  |  |     echo "        -n, --nogit: Don't execute git commands. This is used to deploy the current code in the project repo. With this --branch has no effect." | 
					
						
							| 
									
										
										
										
											2017-06-14 11:48:01 +05:30
										 |  |  |     echo "        -b, --branch: The branch to pull from origin repo." | 
					
						
							|  |  |  |     exit | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | echo "BRANCH="$BRANCH | 
					
						
							| 
									
										
										
										
											2017-08-19 18:28:17 +05:30
										 |  |  | echo "DB_MAKE_MIGRATIONS="$DB_MAKE_MIGRATIONS | 
					
						
							| 
									
										
										
										
											2017-06-14 11:48:01 +05:30
										 |  |  | echo "DB_MIGRATE="$DB_MIGRATE | 
					
						
							|  |  |  | echo "NO_GIT="$NO_GIT | 
					
						
							|  |  |  | echo "VERBOSE="$VERBOSE | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # The project directory exists, we pull the specified branch | 
					
						
							|  |  |  | cd $APP_HOME_DIR | 
					
						
							|  |  |  | if [ -z "$NO_GIT" ]; then | 
					
						
							| 
									
										
										
										
											2017-08-19 18:28:17 +05:30
										 |  |  |     echo 'We are executing default git commands. Please add --nogit to not do this.' | 
					
						
							| 
									
										
										
										
											2017-06-14 11:48:01 +05:30
										 |  |  |     # Save any modified changes before git pulling | 
					
						
							|  |  |  |     git stash | 
					
						
							|  |  |  |     # Fetch all branches/tags | 
					
						
							|  |  |  |     git fetch -a | 
					
						
							|  |  |  |     git checkout $BRANCH | 
					
						
							|  |  |  |     git pull origin $BRANCH     | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |     echo 'Not using git commands.' | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | source ~/pyvenv/bin/activate | 
					
						
							|  |  |  | pip install -r requirements.txt > deploy.log 2>&1 | 
					
						
							|  |  |  | echo "###" >> deploy.log | 
					
						
							| 
									
										
										
										
											2017-08-19 18:28:17 +05:30
										 |  |  | if [ -z "$DB_MAKE_MIGRATIONS" ]; then | 
					
						
							|  |  |  |     echo 'We are not doing DB makemigrations' | 
					
						
							| 
									
										
										
										
											2017-06-14 11:48:01 +05:30
										 |  |  | else | 
					
						
							| 
									
										
										
										
											2017-08-19 18:28:17 +05:30
										 |  |  |     echo 'Doing DB makemigrations' | 
					
						
							| 
									
										
										
										
											2017-06-14 11:48:01 +05:30
										 |  |  |     ./manage.py makemigrations >> deploy.log 2>&1 | 
					
						
							|  |  |  |     echo "###" >> deploy.log | 
					
						
							| 
									
										
										
										
											2017-08-19 18:28:17 +05:30
										 |  |  | fi | 
					
						
							|  |  |  | if [ -z "$DB_MIGRATE" ]; then | 
					
						
							| 
									
										
										
										
											2017-08-19 18:32:58 +05:30
										 |  |  |     echo 'We are not doing DB migrate' | 
					
						
							| 
									
										
										
										
											2017-08-19 18:28:17 +05:30
										 |  |  | else | 
					
						
							|  |  |  |     echo 'Doing DB migrate' | 
					
						
							| 
									
										
										
										
											2017-06-14 11:48:01 +05:30
										 |  |  |     ./manage.py migrate >> deploy.log 2>&1 | 
					
						
							|  |  |  |     echo "###" >> deploy.log | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | printf 'yes' | ./manage.py collectstatic >> deploy.log 2>&1 | 
					
						
							|  |  |  | echo "###" >> deploy.log | 
					
						
							|  |  |  | django-admin compilemessages | 
					
						
							| 
									
										
										
										
											2017-08-19 18:28:17 +05:30
										 |  |  | sudo systemctl restart celery.service | 
					
						
							| 
									
										
										
										
											2017-06-14 11:48:01 +05:30
										 |  |  | sudo systemctl restart uwsgi | 
					
						
							|  |  |  | 
 |