From a6ca88f6e12a8477580ae035c6385572e9756e05 Mon Sep 17 00:00:00 2001 From: Ryan Northey Date: Wed, 11 Apr 2018 18:08:29 +0100 Subject: [PATCH] Add db-demo recipe --- docker/db-demo/Dockerfile | 10 ++ docker/demo-postgres/bin/await-postgres | 13 ++ docker/demo-postgres/bin/build-travis-images | 37 ----- docker/demo-postgres/bin/run_db | 149 ------------------- docker/demo-postgres/bin/run_db_mariadb | 57 ------- 5 files changed, 23 insertions(+), 243 deletions(-) create mode 100644 docker/db-demo/Dockerfile create mode 100755 docker/demo-postgres/bin/await-postgres delete mode 100755 docker/demo-postgres/bin/build-travis-images delete mode 100755 docker/demo-postgres/bin/run_db delete mode 100644 docker/demo-postgres/bin/run_db_mariadb diff --git a/docker/db-demo/Dockerfile b/docker/db-demo/Dockerfile new file mode 100644 index 00000000000..1223b4a1ab8 --- /dev/null +++ b/docker/db-demo/Dockerfile @@ -0,0 +1,10 @@ +# translate/pootle:db-demo +# +# VERSION 0.0.1 + +# Build stage +FROM postgres:9.4-alpine + +ENV POSTGRES_USER=pootle \ + POSTGRES_PASSWORD=CHANGEME \ + POSTGRES_DB=pootledb diff --git a/docker/demo-postgres/bin/await-postgres b/docker/demo-postgres/bin/await-postgres new file mode 100755 index 00000000000..4a3bb3c9b97 --- /dev/null +++ b/docker/demo-postgres/bin/await-postgres @@ -0,0 +1,13 @@ +#!/bin/bash + +RETRIES=10 + +POSTGRES_PASSWORD=CHANGEME +PG_HOST=postgres +PG_USER=pootle +PG_DATABASE=pootledb + +until psql -h $PG_HOST -U $PG_USER -d $PG_DATABASE -c "select 1" > /dev/null 2>&1 || [ $RETRIES -eq 0 ]; do + echo "Waiting for postgres server, $((RETRIES--)) remaining attempts..." + sleep 2 +done diff --git a/docker/demo-postgres/bin/build-travis-images b/docker/demo-postgres/bin/build-travis-images deleted file mode 100755 index eff5b1059dc..00000000000 --- a/docker/demo-postgres/bin/build-travis-images +++ /dev/null @@ -1,37 +0,0 @@ -#!/bin/bash - -set -e - -MASTER=$(git ls-remote https://github.com/translate/pootle | grep HEAD | awk '{ print $1}') - -function docker_tag_exists() { - curl --silent -f -lSL https://index.docker.io/v1/repositories/$1/tags/$2 > /dev/null -} - -function tag_and_push () { - docker tag translate/pootle:$1 $DOCKER_USERNAME/pootle:$2 - docker push $DOCKER_USERNAME/pootle:$2 -} - -if [ "$FORCE_REBUILD" != true ] && docker_tag_exists $DOCKER_USERNAME/pootle $MASTER; then - echo 'Master image already exists' -else - if [ "$FORCE_REBUILD" = true ]; then - echo 'Rebuilding due to FORCE_REBUILD' - else - echo 'Master image does not exist, building test images'; - fi - - # this should exit if DOCKER_PASSWORD is not set - - pip install -qr requirements/host.txt - export LOCAL_USER_ID=$UID - makey build-all - - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin - tag_and_push dev-root $MASTER - tag_and_push dev-postgres dev-postgres - tag_and_push dev-sqlite dev-sqlite - tag_and_push dev-mariadb dev-mariadb - echo 'Pushed images, now we should delete old images' -fi diff --git a/docker/demo-postgres/bin/run_db b/docker/demo-postgres/bin/run_db deleted file mode 100755 index 9bd63f709cb..00000000000 --- a/docker/demo-postgres/bin/run_db +++ /dev/null @@ -1,149 +0,0 @@ -#!/bin/bash - - -function run_in_env() { - cd /home/pootle/pootle_env \ - && . bin/activate \ - && cd src/pootle \ - && pip install -qe . \ - && pip install -qr requirements/dev.txt \ - && pip install -qr requirements/_es_5.txt \ - && $@ -} - -function pg_drop () { - psql -h $dbhost -w postgres -c "drop database $dbname"; -} - - -function mariadb_drop () { - mysql -h $dbhost -se "drop database $dbname"; -} - - -function drop () { - db_status - if [[ $dbstatus = 1 ]]; then - echo "Dropping database"; - case "$dbtype" in - "django.db.backends.postgresql") - pg_drop;; - "django.db.backends.mysql") - mariadb_drop;; - esac - else - echo "Database does not exist"; - fi -} - - -function create () { - dbinfo - db_status - if [[ $dbstatus = 1 ]]; then - echo "Database exists"; - else - case "$dbtype" in - "django.db.backends.postgresql") - pg_create;; - "django.db.backends.mysql") - mariadb_create;; - esac - fi -} - - -function pg_create () { - psql -h $dbhost -w postgres -c "create database pootledb encoding 'utf8' lc_collate='en_US.utf8' lc_ctype='en_US.utf8' template template0 owner pootle" - -} - - -function mariadb_create () { - mysql -h $dbhost -se "CREATE DATABASE $dbname CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;"; -} - - -function dbinfo () { - dbtype=$(pootle shell -c "from django.conf import settings; print settings.DATABASES['default']['ENGINE']"); - dbuser=$(pootle shell -c "from django.conf import settings; print settings.DATABASES['default']['USER']"); - dbname=$(pootle shell -c "from django.conf import settings; print settings.DATABASES['default']['NAME']"); - dbhost=$(pootle shell -c "from django.conf import settings; print settings.DATABASES['default']['HOST']"); - dbpassword=$(pootle shell -c "from django.conf import settings; print settings.DATABASES['default']['PASSWORD']"); -} - - -function status () { - dbinfo - echo 'DB status...' - echo "host: $dbhost" - echo "type: $dbtype" - echo "name: $dbname" - db_status - [[ $dbstatus = 1 ]] && echo "Database exists" || echo "Database does not exist"; -} - - -function install () { - echo 'Installing db' \ - && pootle migrate \ - && pootle initdb -} - - -function load () { - echo "Loading db from file: $1" - dbinfo - export MYSQL_PWD=$dbpassword - mysql -h mariadb -u root $dbname < "/home/pootle/pootle_env/src/pootle/$1" -} - - -function reload () { - drop - create - install -} - -function pg_status () { - dbinfo; - export PGPASSWORD=$dbpassword - dbstatus=$(psql -h $dbhost -w $dbname -qtAX -c "SELECT 1 from pg_database WHERE datname='$dbname'"); -} - -function mariadb_status () { - dbinfo; - export MYSQL_PWD=$dbpassword - dbstatus=$(mysql -h $dbhost INFORMATION_SCHEMA -se "SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = \"$dbname\""); -} - - -function db_status () { - dbinfo; - case "$dbtype" in - "django.db.backends.postgresql") - pg_status;; - "django.db.backends.mysql") - mariadb_status;; - *) - echo "Unrecognized db $dbtype" - esac -} - - -case "$1" in - "drop") - run_in_env drop;; - "create") - run_in_env create;; - "status") - run_in_env status;; - "install") - run_in_env install;; - "load") - run_in_env load "$2";; - "reload") - run_in_env reload "$2";; - *) - echo "db command not recognized";; -esac diff --git a/docker/demo-postgres/bin/run_db_mariadb b/docker/demo-postgres/bin/run_db_mariadb deleted file mode 100644 index 7a24e4df47b..00000000000 --- a/docker/demo-postgres/bin/run_db_mariadb +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash - - -function run_in_env() { - cd /home/pootle/pootle_env \ - && . bin/activate \ - && cd src/pootle \ - && pip install -e . \ - && $@ -} - - -function drop () { - echo 'Dropping pootle db' \ - && echo 'drop database pootledb' | pootle dbshell; -} - - -function create () { - echo 'Creating pootle db' \ - && echo 'CREATE DATABASE pootledb CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;' | mysql -h mariadb -u root -p; -} - - -function status () { - echo 'Listing dbs' \ - && echo 'show databases;' | pootle dbshell; -} - - -function install () { - echo 'Installing db' \ - && pootle migrate \ - && pootle initdb -} - - -function load () { - echo "Loading db from file: $1" \ - && mysql -h mariadb -u root -p pootledb < "/home/pootle/pootle_env/src/pootle/$1" -} - - -case "$1" in - "drop") - run_in_env drop;; - "create") - run_in_env create;; - "show") - run_in_env show;; - "install") - run_in_env install;; - "load") - run_in_env load "$2";; - *) - echo "db command not recognized";; -esac