From 26eb4f6e5c6e9e816ce155f1a23451c0c3c326bb Mon Sep 17 00:00:00 2001 From: v-aisac Date: Fri, 21 Jan 2022 13:21:26 +0200 Subject: [PATCH 1/7] Added dbass credentials parsing --- .../lib/cloud/scripts/per-instance/001_onboot | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot b/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot index da0d7af..f69f06d 100755 --- a/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot +++ b/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot @@ -14,8 +14,11 @@ SETTINGS_FILE="${SETTINGS_DIR}/settings.py" cat > /root/.digitalocean_passwords < Date: Fri, 21 Jan 2022 13:21:56 +0200 Subject: [PATCH 2/7] Added port and host variables --- django-20-04/files/var/lib/digitalocean/settings.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/django-20-04/files/var/lib/digitalocean/settings.py b/django-20-04/files/var/lib/digitalocean/settings.py index d99801e..85de174 100644 --- a/django-20-04/files/var/lib/digitalocean/settings.py +++ b/django-20-04/files/var/lib/digitalocean/settings.py @@ -91,8 +91,8 @@ def ip_addresses(): 'NAME': 'django', 'USER': 'django', 'PASSWORD': '@DBPASSWORD@', - 'HOST': 'localhost', - 'PORT': '', + 'HOST': '@DBHOST@', + 'PORT': '@DBPORT@', } } From b4fa5b6f61ff4623aa29485b3a8d7f49df3faaaa Mon Sep 17 00:00:00 2001 From: v-aisac Date: Fri, 21 Jan 2022 13:22:30 +0200 Subject: [PATCH 3/7] Added wait for dbaas --- django-20-04/scripts/012-django.sh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/django-20-04/scripts/012-django.sh b/django-20-04/scripts/012-django.sh index 7e965f9..f42949e 100755 --- a/django-20-04/scripts/012-django.sh +++ b/django-20-04/scripts/012-django.sh @@ -9,3 +9,25 @@ useradd --home-dir /home/django \ # Setup the home directory chown -R django: /home/django + +if [ -f "/root/.digitalocean_dbaas_credentials" ]; then + # grab host & port to block until database connection is ready + host=$(sed -n "s/^db_host=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials) + port=$(sed -n "s/^db_port=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials) + + # wait for db to become available + echo -e "\nWaiting for your database to become available (this may take a few minutes)" + while ! pg_isready -h "$host" -p "$port"; do + printf . + sleep 2 + done + echo -e "\nDatabase available!\n" + + # disable the local Postgresql instance + systemctl stop postgresql.service + systemctl disable postgresql.service + + # cleanup + unset host port + rm -rf /etc/postgresql + fi \ No newline at end of file From 03210b8279621832579c2c913cab7228d122e65a Mon Sep 17 00:00:00 2001 From: v-aisac Date: Fri, 21 Jan 2022 18:42:30 +0200 Subject: [PATCH 4/7] Added separate local and dbaas db init scripts --- .../lib/cloud/scripts/per-instance/001_onboot | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot b/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot index f69f06d..b67d475 100755 --- a/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot +++ b/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot @@ -10,15 +10,19 @@ SETTINGS_DIR="${PROJECT_DIR}/django_project" STATIC_DIR="${SETTINGS_DIR}/static" SETTINGS_FILE="${SETTINGS_DIR}/settings.py" +PG_PASS="" +PG_USER=postgres +PG_DB=postgres + +DJANGO_POSTGRESS_HOST=localhost +DJANGO_POSTGRESS_PORT=5432 + # Generate some passwords cat > /root/.digitalocean_passwords < Date: Fri, 21 Jan 2022 18:43:18 +0200 Subject: [PATCH 5/7] Sslmode required for postgres --- django-20-04/files/var/lib/digitalocean/settings.py | 1 + 1 file changed, 1 insertion(+) diff --git a/django-20-04/files/var/lib/digitalocean/settings.py b/django-20-04/files/var/lib/digitalocean/settings.py index 85de174..11eabea 100644 --- a/django-20-04/files/var/lib/digitalocean/settings.py +++ b/django-20-04/files/var/lib/digitalocean/settings.py @@ -93,6 +93,7 @@ def ip_addresses(): 'PASSWORD': '@DBPASSWORD@', 'HOST': '@DBHOST@', 'PORT': '@DBPORT@', + 'OPTIONS': {'sslmode': 'require'}, } } From 2084edfb4deb11b5d985bc506e660ada5cabcad8 Mon Sep 17 00:00:00 2001 From: v-aisac Date: Wed, 26 Jan 2022 12:49:26 +0200 Subject: [PATCH 6/7] Added newline at the end of 012-django.sh --- django-20-04/scripts/012-django.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django-20-04/scripts/012-django.sh b/django-20-04/scripts/012-django.sh index f42949e..8ee48bd 100755 --- a/django-20-04/scripts/012-django.sh +++ b/django-20-04/scripts/012-django.sh @@ -30,4 +30,4 @@ if [ -f "/root/.digitalocean_dbaas_credentials" ]; then # cleanup unset host port rm -rf /etc/postgresql - fi \ No newline at end of file + fi From 757edca8bcfd05459e6911b299878c624805b787 Mon Sep 17 00:00:00 2001 From: v-aisac Date: Wed, 26 Jan 2022 14:27:34 +0200 Subject: [PATCH 7/7] Added check for protocol --- .../files/var/lib/cloud/scripts/per-instance/001_onboot | 2 +- django-20-04/scripts/012-django.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot b/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot index b67d475..048e3d7 100755 --- a/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot +++ b/django-20-04/files/var/lib/cloud/scripts/per-instance/001_onboot @@ -53,7 +53,7 @@ done echo "${DJANGO_USER}:${DJANGO_USER_PASSWORD}" | chpasswd - # Set up postgres user and database -if [ -f "/root/.digitalocean_dbaas_credentials" ]; +if [ -f "/root/.digitalocean_dbaas_credentials" ] && [ "$(sed -n "s/^db_protocol=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials)" = "postgresql" ]; then # grab all the data from the dbaas credentials file DJANGO_POSTGRESS_HOST=$(sed -n "s/^db_host=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials) diff --git a/django-20-04/scripts/012-django.sh b/django-20-04/scripts/012-django.sh index 8ee48bd..9921204 100755 --- a/django-20-04/scripts/012-django.sh +++ b/django-20-04/scripts/012-django.sh @@ -10,7 +10,7 @@ useradd --home-dir /home/django \ # Setup the home directory chown -R django: /home/django -if [ -f "/root/.digitalocean_dbaas_credentials" ]; then +if [ -f "/root/.digitalocean_dbaas_credentials" ] && [ "$(sed -n "s/^db_protocol=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials)" = "postgresql" ]; then # grab host & port to block until database connection is ready host=$(sed -n "s/^db_host=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials) port=$(sed -n "s/^db_port=\"\(.*\)\"$/\1/p" /root/.digitalocean_dbaas_credentials)