Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise setup script #210

Merged
merged 5 commits into from Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 17 additions & 7 deletions mysite/settings.py
Expand Up @@ -92,8 +92,10 @@
# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases

DATABASES = {}
DATABASES["default"] = dj_database_url.config(conn_max_age=600)
dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file): # pragma: no cover
dotenv.load_dotenv(dotenv_file)
DEBUG = True

if "TRAVIS" in os.environ: # pragma: no cover
DEBUG = True
Expand All @@ -107,11 +109,19 @@
"PORT": "5432",
}
}

dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):
dotenv.load_dotenv(dotenv_file) # pragma: no cover
DEBUG = True
elif "DB" in os.environ and os.environ["DB"] == "postgres": # pragma: nocover
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "mercury",
"USER": os.environ.get("DB_USER", "postgres"),
"PASSWORD": os.environ.get("DB_PASSWORD", ""),
"HOST": os.environ.get("DB_HOST", "localhost"),
"PORT": os.environ.get("DB_PORT", ""),
}
}
else: # pragma: no cover
DATABASES = {"default": dj_database_url.config(conn_max_age=600)}


# Password validation
Expand Down
9 changes: 9 additions & 0 deletions scripts/env.sample
@@ -0,0 +1,9 @@
# For more information, open mysite/settings.py and look up 'os.environ'

DB=postgres
# DB_USER=mercury
# DB_PASSWORD=mercury

# localhost and 5432 by default. Uncomment if you want another setup
# DB_HOST=1.2.3.4
# DB_PORT=12345
107 changes: 95 additions & 12 deletions scripts/setup.sh
@@ -1,14 +1,97 @@
#!/bin/bash

if [[ ! -d ".git" ]]; then
echo "You must run this script from the top-level (root) directory of this repository."
exit 1
RED='\033[0;31m' # Red
GREEN='\033[0;32m' # Green
RESET='\033[0m' # Text Reset
function __system {
echo -e $GREEN$*$RESET
}
function __error {
echo -e $RED$*$RESET
}
function __success {
echo -e $GREEN"[OK] "$*$RESET
}
function __assert_exist {
if command -v $1 > /dev/null; then
__success $1
else
__error "[ERR] "$1
echo ""
__system "Please install "$1" first"
__system $2
exit 1
fi
}

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd $SCRIPT_DIR/..

if [[ -z $VIRTUAL_ENV ]]; then
printf $RED
read -n 1 -p "You haven't activated virtualenv. Do you want to proceed? (yN)? " yn
echo -e $RESET
case $yn in
y ) ;;
* ) exit 0 ;;
esac
else
__success "virtualenv activated"
fi

__assert_exist psql "https://github.com/gcivil-nyu-org/spring2020-cs-gy-9223-class/wiki/PostgreSQL-Setup-Guide#install"

ENV_SAMPLE=$SCRIPT_DIR/env.sample
if [[ ! -f ".env" ]]; then
cp $ENV_SAMPLE .env
__success "Copy .env at the project root"
elif diff .env scripts/env.sample > /dev/null; then
__success "Skip copying .env"
else
printf $GREEN
read -n 1 -p "You already have '.env'. Do you want to overwrite it? (yN)" yn
echo -e $RESET
case $yn in
y ) cp $ENV_SAMPLE .env ;;
q ) exit 0 ;;
esac
fi

echo ""
__system "Running pip3 install -r requirements.txt..."
pip3 install -r requirements.txt || exit 1
__success "pip install -r requirements.txt"

echo ""
printf $GREEN
read -n 1 -p "Do you also want to install test requirements? (black, flake8, coveralls, etc.) (Yn)" yn
echo -e $RESET
case $yn in
n ) ;;
* )
__assert_exist geckodriver "https://github.com/gcivil-nyu-org/spring2020-cs-gy-9223-class/wiki/Geckodriver---Install-instructions"
pip3 install -r test-requirements.txt || exit 1
__success "pip install -r test-requirements.txt"
;;
esac

echo ""
if ! psql -c "CREATE DATABASE mercury;" -U postgres 2> /dev/null; then # if it already exists, an error occurs. ignore it
__system "mercury database exists. Skip creating it"
fi
echo "===> Setting up repo..."
read -p "About to install Python requirements. Press Ctrl+C if you need to enter a virtualenv first, or press return to continue."
echo "DATABASE_URL=sqlite:///db.sqlite3" > .env
echo "===> Running pip3 install -r requirements.txt"
pip3 install -r requirements.txt
echo "===> Creating migration files and running database migrations..."
python manage.py makemigrations
python manage.py migrate
echo "Run 'python manage.py runserver' to start the Django webserver."
__success "mercury database"

echo ""
__system "Creating migration files and running database migrations..."
python manage.py makemigrations || exit 1
python manage.py migrate || exit 1
__success "django migration"

echo ""
__system "Collecting static files..."
python manage.py collectstatic --noinput || exit 1
__success "python manage.py collectstatic --noinput"

echo ""
__system "Done!"
__system "Run 'python manage.py runserver' to start the Django webserver."