2017-02-09 12:20:21 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
PROJECT_NAME=$1
|
|
|
|
|
2017-10-10 14:50:48 +00:00
|
|
|
: ${PROJECT_DIR:=/vagrant}
|
2017-11-06 21:08:03 +00:00
|
|
|
: ${DEV_USER:=vagrant}
|
|
|
|
VIRTUALENV_DIR=/home/$DEV_USER/.virtualenvs/$PROJECT_NAME
|
2017-02-09 12:20:21 +00:00
|
|
|
|
|
|
|
PYTHON=$VIRTUALENV_DIR/bin/python
|
|
|
|
PIP=$VIRTUALENV_DIR/bin/pip
|
|
|
|
|
|
|
|
|
2024-08-07 11:36:49 +00:00
|
|
|
# Update APT database
|
|
|
|
apt-get update -y
|
|
|
|
|
|
|
|
apt install -y python3-pip
|
|
|
|
pip3 install virtualenv
|
|
|
|
|
2017-02-09 12:20:21 +00:00
|
|
|
# Virtualenv setup for project
|
2017-11-06 21:08:03 +00:00
|
|
|
su - $DEV_USER -c "virtualenv --python=python3 $VIRTUALENV_DIR"
|
2017-02-09 12:20:21 +00:00
|
|
|
# Replace previous line with this if you are using Python 2
|
2017-11-06 21:08:03 +00:00
|
|
|
# su - $DEV_USER -c "virtualenv --python=python2 $VIRTUALENV_DIR"
|
2017-02-09 12:20:21 +00:00
|
|
|
|
2017-11-06 21:08:03 +00:00
|
|
|
su - $DEV_USER -c "echo $PROJECT_DIR > $VIRTUALENV_DIR/.project"
|
2017-02-09 12:20:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Upgrade PIP
|
2017-11-06 21:08:03 +00:00
|
|
|
su - $DEV_USER -c "$PIP install --upgrade pip"
|
2017-02-09 12:20:21 +00:00
|
|
|
|
|
|
|
# Install PIP requirements
|
2019-11-01 09:28:37 +00:00
|
|
|
su - $DEV_USER -c "cd $PROJECT_DIR && $PIP install -r requirements/base.txt"
|
2017-02-09 12:20:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Set execute permissions on manage.py as they get lost if we build from a zip file
|
|
|
|
chmod a+x $PROJECT_DIR/manage.py
|
|
|
|
|
2017-02-12 00:42:48 +00:00
|
|
|
# copy local settings file
|
|
|
|
cp $PROJECT_DIR/bakerydemo/settings/local.py.example $PROJECT_DIR/bakerydemo/settings/local.py
|
|
|
|
# add .env file for django-dotenv environment variable definitions
|
2023-02-20 16:56:45 +00:00
|
|
|
echo DJANGO_SETTINGS_MODULE=$PROJECT_NAME.settings.dev > $PROJECT_DIR/.env
|
2017-02-09 12:20:21 +00:00
|
|
|
|
2017-10-11 12:32:28 +00:00
|
|
|
if [ -n "$USE_POSTGRESQL" ]
|
|
|
|
then
|
2018-10-28 14:20:03 +00:00
|
|
|
echo Creating database.....
|
|
|
|
DB_EXISTS=$(
|
|
|
|
su - $DEV_USER -c \
|
|
|
|
"psql -lqt | cut -d \| -f 1 | grep -q '^ $PROJECT_NAME $' && echo yes || echo no"
|
|
|
|
)
|
|
|
|
if [[ "$DB_EXISTS" == "no" ]]; then
|
|
|
|
echo Database does not exist, creating...
|
|
|
|
su - $DEV_USER -c "createdb $PROJECT_NAME"
|
|
|
|
else
|
|
|
|
echo Database already exists, skipping...
|
|
|
|
fi
|
2018-07-31 11:43:17 +00:00
|
|
|
su - $DEV_USER -c "$PIP install \"psycopg2-binary>=2.7,<3\""
|
2017-10-11 12:32:28 +00:00
|
|
|
cat << EOF >> $PROJECT_DIR/bakerydemo/settings/local.py
|
|
|
|
DATABASES = {
|
|
|
|
'default': {
|
2018-07-31 11:43:17 +00:00
|
|
|
'ENGINE': 'django.db.backends.postgresql',
|
2017-10-11 12:32:28 +00:00
|
|
|
'NAME': '$PROJECT_NAME',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
fi
|
|
|
|
|
2017-02-10 13:12:37 +00:00
|
|
|
# Run syncdb/migrate/load_initial_data/update_index
|
2017-11-06 21:08:03 +00:00
|
|
|
su - $DEV_USER -c "$PYTHON $PROJECT_DIR/manage.py migrate --noinput && \
|
|
|
|
$PYTHON $PROJECT_DIR/manage.py load_initial_data && \
|
|
|
|
$PYTHON $PROJECT_DIR/manage.py update_index"
|
2017-02-09 12:20:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Add a couple of aliases to manage.py into .bashrc
|
|
|
|
|
2020-04-14 17:06:43 +00:00
|
|
|
BASHRC=/home/$DEV_USER/.bashrc
|
|
|
|
BASHRC_BACKUP="${BASHRC}__pre_provision_backup"
|
2017-02-09 12:20:21 +00:00
|
|
|
|
2020-04-14 17:06:43 +00:00
|
|
|
if [[ ! -f "$BASHRC_BACKUP" ]];
|
2018-10-28 14:20:03 +00:00
|
|
|
then
|
2020-04-14 17:06:43 +00:00
|
|
|
# On the first provision run:
|
|
|
|
# Create backup of .bashrc if it doesn't exist
|
|
|
|
cp -f $BASHRC $BASHRC_BACKUP
|
|
|
|
else
|
|
|
|
# If provision runs twice or more
|
|
|
|
# restore "pure" .bashrc from the backup
|
|
|
|
cp -f $BASHRC_BACKUP $BASHRC
|
2018-10-28 14:20:03 +00:00
|
|
|
fi
|
2020-04-14 17:06:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
cat << EOF >> /home/$DEV_USER/.bashrc
|
|
|
|
export PYTHONPATH=$PROJECT_DIR
|
|
|
|
|
|
|
|
alias dj="$PROJECT_DIR/manage.py"
|
|
|
|
alias djrun="dj runserver 0.0.0.0:8000"
|
|
|
|
|
|
|
|
source $VIRTUALENV_DIR/bin/activate
|
|
|
|
export PS1="[$PROJECT_NAME \W]\\$ "
|
|
|
|
cd $PROJECT_DIR
|
|
|
|
EOF
|