From b40151f158f27172e08ae23f79fb18198562d4fb Mon Sep 17 00:00:00 2001 From: Yonguk Jeong Date: Wed, 25 Mar 2020 08:39:29 -0400 Subject: [PATCH 1/5] Revise environment variables regarding which database to use `.env` file is affected. --- mysite/settings.py | 24 +++++++++++++++++------- scripts/env.sample | 9 +++++++++ scripts/setup.sh | 20 ++++++++++++++++++-- 3 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 scripts/env.sample diff --git a/mysite/settings.py b/mysite/settings.py index 60386865..04b13612 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -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): + dotenv.load_dotenv(dotenv_file) # pragma: no cover + DEBUG = True if "TRAVIS" in os.environ: # pragma: no cover DEBUG = True @@ -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': + 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: + DATABASES = {"default": dj_database_url.config(conn_max_age=600)} # Password validation diff --git a/scripts/env.sample b/scripts/env.sample new file mode 100644 index 00000000..b2c39ac6 --- /dev/null +++ b/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 diff --git a/scripts/setup.sh b/scripts/setup.sh index 36cd3aff..00abf7b1 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,3 +1,4 @@ +#!/bin/bash if [[ ! -d ".git" ]]; then echo "You must run this script from the top-level (root) directory of this repository." @@ -5,10 +6,25 @@ if [[ ! -d ".git" ]]; then 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 + +ENV_SAMPLE=scripts/env.sample +if [[ ! -f ".env" ]]; then + cp $ENV_SAMPLE .env +elif ! diff .env scripts/env.sample > /dev/null; then + echo "You already have '.env'. Do you want to overwrite it?" + select yn in "Yes" "No" "See diff"; do + case $yn in + Yes ) cp $ENV_SAMPLE .env; break;; + No ) break;; + "See diff" ) diff .env $ENV_SAMPLE;; + q ) break;; + esac + done +fi + 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." \ No newline at end of file +echo "Run 'python manage.py runserver' to start the Django webserver." From 8d4a1bc9e59cae51dcf471d1cc9a5205dc1a33dd Mon Sep 17 00:00:00 2001 From: Jeong Yonguk Date: Fri, 27 Mar 2020 11:34:49 -0400 Subject: [PATCH 2/5] Revise scripts/setup.sh --- scripts/setup.sh | 111 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 89 insertions(+), 22 deletions(-) diff --git a/scripts/setup.sh b/scripts/setup.sh index 00abf7b1..9703261d 100755 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,30 +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 -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." -ENV_SAMPLE=scripts/env.sample +__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 -elif ! diff .env scripts/env.sample > /dev/null; then - echo "You already have '.env'. Do you want to overwrite it?" - select yn in "Yes" "No" "See diff"; do - case $yn in - Yes ) cp $ENV_SAMPLE .env; break;; - No ) break;; - "See diff" ) diff .env $ENV_SAMPLE;; - q ) break;; - esac - done + __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 "===> 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." +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 +__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." From 7961e9128f379a8bc2c3f220d52851708371434c Mon Sep 17 00:00:00 2001 From: Jeong Yonguk Date: Fri, 27 Mar 2020 11:34:55 -0400 Subject: [PATCH 3/5] Fix black errors --- mysite/settings.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mysite/settings.py b/mysite/settings.py index 04b13612..c0ef5cd4 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -109,15 +109,15 @@ "PORT": "5432", } } -elif 'DB' in os.environ and os.environ['DB'] == 'postgres': +elif "DB" in os.environ and os.environ["DB"] == "postgres": 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', ''), + "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: From 132ae6f31ce7fefe03d7c5d5109ed1ba94621268 Mon Sep 17 00:00:00 2001 From: Yonguk Jeong Date: Sun, 29 Mar 2020 22:07:42 -0400 Subject: [PATCH 4/5] Put 'no cover' for lines of database cofiguration The execution of these code is highly dependent on the configuration. One is never executed whereas the other is always executed. --- mysite/settings.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mysite/settings.py b/mysite/settings.py index c0ef5cd4..3b4dbccc 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -93,8 +93,8 @@ # https://docs.djangoproject.com/en/dev/ref/settings/#databases dotenv_file = os.path.join(BASE_DIR, ".env") -if os.path.isfile(dotenv_file): - dotenv.load_dotenv(dotenv_file) # pragma: no cover +if os.path.isfile(dotenv_file): # pragma: no cover + dotenv.load_dotenv(dotenv_file) DEBUG = True if "TRAVIS" in os.environ: # pragma: no cover @@ -109,7 +109,7 @@ "PORT": "5432", } } -elif "DB" in os.environ and os.environ["DB"] == "postgres": +elif "DB" in os.environ and os.environ["DB"] == "postgres": # pragma: nocover DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql_psycopg2", @@ -120,7 +120,7 @@ "PORT": os.environ.get("DB_PORT", ""), } } -else: +else: # pragma: no cover DATABASES = {"default": dj_database_url.config(conn_max_age=600)} From 632a6b7e21cd28d0d829253b0160ff382f529040 Mon Sep 17 00:00:00 2001 From: Yonguk Jeong Date: Sun, 29 Mar 2020 22:28:47 -0400 Subject: [PATCH 5/5] Fix black errors --- mysite/settings.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mysite/settings.py b/mysite/settings.py index 3b4dbccc..b139f33d 100644 --- a/mysite/settings.py +++ b/mysite/settings.py @@ -93,7 +93,7 @@ # https://docs.djangoproject.com/en/dev/ref/settings/#databases dotenv_file = os.path.join(BASE_DIR, ".env") -if os.path.isfile(dotenv_file): # pragma: no cover +if os.path.isfile(dotenv_file): # pragma: no cover dotenv.load_dotenv(dotenv_file) DEBUG = True @@ -109,7 +109,7 @@ "PORT": "5432", } } -elif "DB" in os.environ and os.environ["DB"] == "postgres": # pragma: nocover +elif "DB" in os.environ and os.environ["DB"] == "postgres": # pragma: nocover DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql_psycopg2", @@ -120,7 +120,7 @@ "PORT": os.environ.get("DB_PORT", ""), } } -else: # pragma: no cover +else: # pragma: no cover DATABASES = {"default": dj_database_url.config(conn_max_age=600)}