You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/08/02 16:14:29 UTC

[trafficcontrol] 02/02: add initialization and loading from dump file for trafficops db

This is an automated email from the ASF dual-hosted git repository.

dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git

commit 0558d7f7e7085b4ed8bfbb538cf12126068da292
Author: Dan Kirkwood <da...@apache.org>
AuthorDate: Wed Aug 1 14:42:23 2018 -0700

    add initialization and loading from dump file for trafficops db
---
 infrastructure/cdn-in-a-box/docker-compose.yml     |  4 ++-
 .../{variables.env => traffic_ops/Dockerfile-db}   | 31 ++++++----------------
 infrastructure/cdn-in-a-box/traffic_ops/adduser.pl | 12 ++++-----
 .../cdn-in-a-box/traffic_ops/initdb.d/.gitignore   |  1 +
 .../traffic_ops/initdb.d/01-turn-on-logging.sh     |  8 ++++++
 .../traffic_ops/initdb.d/10-create-roles.sh        |  8 ++++++
 .../traffic_ops/initdb.d/90-load-dumps.sh          | 13 +++++++++
 infrastructure/cdn-in-a-box/variables.env          |  2 ++
 8 files changed, 49 insertions(+), 30 deletions(-)

diff --git a/infrastructure/cdn-in-a-box/docker-compose.yml b/infrastructure/cdn-in-a-box/docker-compose.yml
index 1038539..6252319 100644
--- a/infrastructure/cdn-in-a-box/docker-compose.yml
+++ b/infrastructure/cdn-in-a-box/docker-compose.yml
@@ -45,9 +45,11 @@ networks:
 
 services:
   db:
+    build:
+      context: traffic_ops
+      dockerfile: Dockerfile-db
     hostname: db
     domainname: cdn.local
-    image: postgres:9.6.6
     networks:
       - tcnet
     env_file:
diff --git a/infrastructure/cdn-in-a-box/variables.env b/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile-db
similarity index 64%
copy from infrastructure/cdn-in-a-box/variables.env
copy to infrastructure/cdn-in-a-box/traffic_ops/Dockerfile-db
index 7a8d24f..a7620fe 100644
--- a/infrastructure/cdn-in-a-box/variables.env
+++ b/infrastructure/cdn-in-a-box/traffic_ops/Dockerfile-db
@@ -14,26 +14,11 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-CERT_CITY=Denver
-CERT_COMPANY=NotComcast
-CERT_COUNTRY=US
-CERT_STATE=CO
-DB_NAME=traffic_ops
-DB_PORT=5432
-DB_ROOT_PASS=twelve
-DB_SERVER=db
-DB_USER=traffic_ops
-DB_USER_PASS=twelve
-DOMAIN=cdn.local
-PGPASSWORD=twelve
-POSTGRES_PASSWORD=twelve
-TM_PASSWORD=twelve
-TM_USER=tmonitor
-TO_ADMIN_PASSWORD=twelve
-TO_ADMIN_USER=admin
-TO_EMAIL=cdnadmin@example.com
-TO_HOST=trafficops
-TO_PORT=6443
-TO_SECRET=blahblah
-TP_EMAIL=none
-TP_HOST=trafficportal
+
+############################################################
+# Dockerfile for trafficops db
+############################################################
+
+FROM postgres:9.6.6
+
+COPY initdb.d /docker-entrypoint-initdb.d
diff --git a/infrastructure/cdn-in-a-box/traffic_ops/adduser.pl b/infrastructure/cdn-in-a-box/traffic_ops/adduser.pl
index 0e41b8e..31503c8 100755
--- a/infrastructure/cdn-in-a-box/traffic_ops/adduser.pl
+++ b/infrastructure/cdn-in-a-box/traffic_ops/adduser.pl
@@ -34,15 +34,15 @@ my $tenant = shift // 'root';
 
 # Skip the insert if the admin 'username' is already there.
 my $hashed_passwd = hash_pass( $password );
-print <<"ADMIN";
-insert into tm_user (username, role, local_passwd, confirm_local_passwd, tenant_id)
-    values  ('$username',
-            (select id from role where name = '$role'),
+print <<"EOSQL";
+INSERT INTO tm_user (username, role, local_passwd, confirm_local_passwd, tenant_id)
+    VALUES  ('$username',
+            (SELECT id FROM role WHERE name = '$role'),
             '$hashed_passwd',
             '$hashed_passwd',
             (SELECT id FROM tenant WHERE name='$tenant'))
-    ON CONFLICT (username) DO NOTHING;
-ADMIN
+    ON CONFLICT (username) DO UPDATE SET local_passwd='$hashed_passwd', confirm_local_passwd='$hashed_passwd';
+EOSQL
 
 sub hash_pass {
     my $pass = shift;
diff --git a/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/.gitignore b/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/.gitignore
new file mode 100644
index 0000000..2232829
--- /dev/null
+++ b/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/.gitignore
@@ -0,0 +1 @@
+*.dump
diff --git a/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/01-turn-on-logging.sh b/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/01-turn-on-logging.sh
new file mode 100755
index 0000000..2f890e4
--- /dev/null
+++ b/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/01-turn-on-logging.sh
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+cd "$PGDATA"
+mkdir -p pg_log
+T=`mktemp`
+sed "s/#logging_collector = off/logging_collector = on/; s/#log_dest/log_dest/; s/#log_statement = 'none'/log_statement = 'all'/ " <postgresql.conf >"$T"
+
+mv "$T" postgresql.conf
diff --git a/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/10-create-roles.sh b/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/10-create-roles.sh
new file mode 100755
index 0000000..dd68b75
--- /dev/null
+++ b/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/10-create-roles.sh
@@ -0,0 +1,8 @@
+#!/bin/bash
+set -e
+
+psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
+    CREATE USER telegraf;
+    CREATE USER grafana;
+EOSQL
+
diff --git a/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/90-load-dumps.sh b/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/90-load-dumps.sh
new file mode 100755
index 0000000..c931693
--- /dev/null
+++ b/infrastructure/cdn-in-a-box/traffic_ops/initdb.d/90-load-dumps.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+set -ex
+
+d=/docker-entrypoint-initdb.d
+for dump in "$d"/*.dump; do
+    [[ -f $dump ]] || break
+    t=$(mktemp XXX.sql)
+    # convert to sql -- can't load a dump until db initialized,  but sql works
+    echo "Restoring from $dump"
+    pg_restore -f "$t" $dump
+    psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <"$t"
+done
diff --git a/infrastructure/cdn-in-a-box/variables.env b/infrastructure/cdn-in-a-box/variables.env
index 7a8d24f..778ad54 100644
--- a/infrastructure/cdn-in-a-box/variables.env
+++ b/infrastructure/cdn-in-a-box/variables.env
@@ -24,8 +24,10 @@ DB_ROOT_PASS=twelve
 DB_SERVER=db
 DB_USER=traffic_ops
 DB_USER_PASS=twelve
+DBIC_TRACE=0
 DOMAIN=cdn.local
 PGPASSWORD=twelve
+POSTGRES_USER=traffic_ops
 POSTGRES_PASSWORD=twelve
 TM_PASSWORD=twelve
 TM_USER=tmonitor