2
0

provision.sh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/bin/bash
  2. PROJECT_NAME=$1
  3. : ${PROJECT_DIR:=/vagrant}
  4. : ${DEV_USER:=vagrant}
  5. VIRTUALENV_DIR=/home/$DEV_USER/.virtualenvs/$PROJECT_NAME
  6. PYTHON=$VIRTUALENV_DIR/bin/python
  7. PIP=$VIRTUALENV_DIR/bin/pip
  8. # Virtualenv setup for project
  9. su - $DEV_USER -c "virtualenv --python=python3 $VIRTUALENV_DIR"
  10. # Replace previous line with this if you are using Python 2
  11. # su - $DEV_USER -c "virtualenv --python=python2 $VIRTUALENV_DIR"
  12. su - $DEV_USER -c "echo $PROJECT_DIR > $VIRTUALENV_DIR/.project"
  13. # Upgrade PIP
  14. su - $DEV_USER -c "$PIP install --upgrade pip"
  15. # Install PIP requirements
  16. su - $DEV_USER -c "cd $PROJECT_DIR && $PIP install -r requirements/base.txt"
  17. # Set execute permissions on manage.py as they get lost if we build from a zip file
  18. chmod a+x $PROJECT_DIR/manage.py
  19. # copy local settings file
  20. cp $PROJECT_DIR/bakerydemo/settings/local.py.example $PROJECT_DIR/bakerydemo/settings/local.py
  21. # add .env file for django-dotenv environment variable definitions
  22. echo DJANGO_SETTINGS_MODULE=$PROJECT_NAME.settings.dev > $PROJECT_DIR/.env
  23. if [ -n "$USE_POSTGRESQL" ]
  24. then
  25. echo Creating database.....
  26. DB_EXISTS=$(
  27. su - $DEV_USER -c \
  28. "psql -lqt | cut -d \| -f 1 | grep -q '^ $PROJECT_NAME $' && echo yes || echo no"
  29. )
  30. if [[ "$DB_EXISTS" == "no" ]]; then
  31. echo Database does not exist, creating...
  32. su - $DEV_USER -c "createdb $PROJECT_NAME"
  33. else
  34. echo Database already exists, skipping...
  35. fi
  36. su - $DEV_USER -c "$PIP install \"psycopg2-binary>=2.7,<3\""
  37. cat << EOF >> $PROJECT_DIR/bakerydemo/settings/local.py
  38. DATABASES = {
  39. 'default': {
  40. 'ENGINE': 'django.db.backends.postgresql',
  41. 'NAME': '$PROJECT_NAME',
  42. }
  43. }
  44. EOF
  45. fi
  46. # Run syncdb/migrate/load_initial_data/update_index
  47. su - $DEV_USER -c "$PYTHON $PROJECT_DIR/manage.py migrate --noinput && \
  48. $PYTHON $PROJECT_DIR/manage.py load_initial_data && \
  49. $PYTHON $PROJECT_DIR/manage.py update_index"
  50. # Add a couple of aliases to manage.py into .bashrc
  51. BASHRC=/home/$DEV_USER/.bashrc
  52. BASHRC_BACKUP="${BASHRC}__pre_provision_backup"
  53. if [[ ! -f "$BASHRC_BACKUP" ]];
  54. then
  55. # On the first provision run:
  56. # Create backup of .bashrc if it doesn't exist
  57. cp -f $BASHRC $BASHRC_BACKUP
  58. else
  59. # If provision runs twice or more
  60. # restore "pure" .bashrc from the backup
  61. cp -f $BASHRC_BACKUP $BASHRC
  62. fi
  63. cat << EOF >> /home/$DEV_USER/.bashrc
  64. export PYTHONPATH=$PROJECT_DIR
  65. alias dj="$PROJECT_DIR/manage.py"
  66. alias djrun="dj runserver 0.0.0.0:8000"
  67. source $VIRTUALENV_DIR/bin/activate
  68. export PS1="[$PROJECT_NAME \W]\\$ "
  69. cd $PROJECT_DIR
  70. EOF