From 5f96740851cf110a73f1225e3233a0f93c4bcfd0 Mon Sep 17 00:00:00 2001 From: Ka-Ping Yee Date: Sun, 1 Feb 2015 17:06:22 +0000 Subject: [PATCH] Add db-init package. --- .gitmodules | 3 + db-snapshot | 1 + packages/buendia-backup/Makefile | 6 -- .../buendia-backup/control/control.template | 2 +- packages/buendia-db-init/Makefile | 7 ++ .../buendia-db-init/control/control.template | 6 ++ packages/buendia-db-init/control/postinst | 18 +++++ packages/buendia-db-init/control/preinst | 16 +++++ .../root/usr/share/buendia/db/site-dev.sql | 67 +++++++++++++++++++ .../root/usr/share/buendia/db/site-kh.sql | 61 +++++++++++++++++ packages/buendia-mysql/Makefile | 12 ++++ packages/buendia-mysql/control/postinst | 3 +- packages/buendia-mysql/control/postrm | 4 -- packages/buendia-mysql/control/prerm | 6 ++ packages/buendia-mysql/root/etc/my.cnf | 2 + packages/buendia-pkgserver/Makefile | 6 +- 16 files changed, 205 insertions(+), 15 deletions(-) create mode 160000 db-snapshot create mode 100644 packages/buendia-db-init/Makefile create mode 100644 packages/buendia-db-init/control/control.template create mode 100644 packages/buendia-db-init/control/postinst create mode 100644 packages/buendia-db-init/control/preinst create mode 100644 packages/buendia-db-init/root/usr/share/buendia/db/site-dev.sql create mode 100644 packages/buendia-db-init/root/usr/share/buendia/db/site-kh.sql delete mode 100644 packages/buendia-mysql/control/postrm create mode 100644 packages/buendia-mysql/control/prerm create mode 100644 packages/buendia-mysql/root/etc/my.cnf diff --git a/.gitmodules b/.gitmodules index 0b32f21c..b6e3098b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "buendia-libs"] path = buendia-libs url = ../buendia-libs +[submodule "db-snapshot"] + path = db-snapshot + url = ../db-snapshot diff --git a/db-snapshot b/db-snapshot new file mode 160000 index 00000000..163fbc41 --- /dev/null +++ b/db-snapshot @@ -0,0 +1 @@ +Subproject commit 163fbc418084fe6c790cb2bc457a3520d703e452 diff --git a/packages/buendia-backup/Makefile b/packages/buendia-backup/Makefile index 22d9026f..f77e46b3 100644 --- a/packages/buendia-backup/Makefile +++ b/packages/buendia-backup/Makefile @@ -1,7 +1 @@ include ../Makefile.inc - -$(EXTRA_ROOT): $(EXTRA_ROOT)/usr/bin/buendia-mysql-dump - -$(EXTRA_ROOT)/usr/bin/buendia-mysql-dump: - mkdir -p $$(dirname $@) - cp -p $(TOOLS)/openmrs_dump $@ diff --git a/packages/buendia-backup/control/control.template b/packages/buendia-backup/control/control.template index ed297d0d..cffe45ff 100644 --- a/packages/buendia-backup/control/control.template +++ b/packages/buendia-backup/control/control.template @@ -1,6 +1,6 @@ Package: ${PACKAGE_NAME} Version: ${PACKAGE_VERSION} -Depends: buendia-mysql, buendia-utils +Depends: buendia-mysql Architecture: all Description: Backup and restore for Buendia. Maintainer: projectbuendia.org diff --git a/packages/buendia-db-init/Makefile b/packages/buendia-db-init/Makefile new file mode 100644 index 00000000..e077ef7c --- /dev/null +++ b/packages/buendia-db-init/Makefile @@ -0,0 +1,7 @@ +include ../Makefile.inc + +$(EXTRA_ROOT): $(EXTRA_ROOT)/usr/share/buendia/db/init.zip + +$(EXTRA_ROOT)/usr/share/buendia/db/init.zip: $(TOP)/db-snapshot/ID $(TOP)/db-snapshot/*.sql $(TOP)/db-snapshot/*.txt + mkdir -p $$(dirname $@) + zip -j $@ $^ diff --git a/packages/buendia-db-init/control/control.template b/packages/buendia-db-init/control/control.template new file mode 100644 index 00000000..6522d10e --- /dev/null +++ b/packages/buendia-db-init/control/control.template @@ -0,0 +1,6 @@ +Package: ${PACKAGE_NAME} +Version: ${PACKAGE_VERSION} +Depends: buendia-mysql, buendia-site +Architecture: all +Description: Initial data for the Buendia database. +Maintainer: projectbuendia.org diff --git a/packages/buendia-db-init/control/postinst b/packages/buendia-db-init/control/postinst new file mode 100644 index 00000000..a925b122 --- /dev/null +++ b/packages/buendia-db-init/control/postinst @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e +. /usr/share/buendia/site/id +. /usr/share/buendia/site/mysql + +# Create a fresh new database populated with the initial data. +export MYSQL_USER=root +export MYSQL_PASSWORD=$MYSQL_ROOT_PASSWORD +buendia-mysql-load openmrs /usr/share/buendia/db/init.zip + +# Add site-specific initial data. +site_sql=/usr/share/buendia/db/site-${SITE_ID}.sql +[ -f $site_sql ] && mysql -uroot -p$MYSQL_ROOT_PASSWORD openmrs < $site_sql + +# Reapply the OpenMRS user/password/person setup used by the Buendia server. +config_server=/usr/share/buendia/config.d/server +[ -f $config_server ] && $config_server diff --git a/packages/buendia-db-init/control/preinst b/packages/buendia-db-init/control/preinst new file mode 100644 index 00000000..2995db18 --- /dev/null +++ b/packages/buendia-db-init/control/preinst @@ -0,0 +1,16 @@ +#!/bin/bash + +set -e +service mysql start +. /usr/share/buendia/site/mysql + +if ! mysql -u$MYSQL_ROOT_USER -p$MYSQL_ROOT_PASSWORD -e ''; then + echo "MySQL configuration problem: cannot connect as root." + exit 1 +fi + +if mysql -u$MYSQL_ROOT_USER -p$MYSQL_ROOT_PASSWORD -e '' openmrs; then + echo "Installing buendia-db-init would destroy all data in the existing" + echo "MySQL database 'openmrs'! Not installing." + exit 1 +fi diff --git a/packages/buendia-db-init/root/usr/share/buendia/db/site-dev.sql b/packages/buendia-db-init/root/usr/share/buendia/db/site-dev.sql new file mode 100644 index 00000000..7a5a4469 --- /dev/null +++ b/packages/buendia-db-init/root/usr/share/buendia/db/site-dev.sql @@ -0,0 +1,67 @@ +/* + * This script takes a database cleared by clear_server.sql and sets + * up initial data that will work for most EMCs. + * It also double-checks some data that should already be present + * and adds it if missing. + * + * Bear in mind this assume you are working from a snapshot of our + * GCE instance, and so doesn't add the whole concept dictionary etc, + * as keeping a script up to date is harder than keeping the real database + * up to date. + */ + +-- System Users: make sure Android is present +-- This is surprisingly hard as there is not a unique constraint on username +-- or system_id, so the normal tricks for INSERT IF NOT EXISTS don't work. +-- Ignoring for now. + +-- Buendia users - make sure user, provider and person information present +-- for Guest User + +-- Program. Make sure Ebola program is present. + + +-- Location make sure Camp, and zones are present. Create tents. +-- ON DUPLICATE IGNORE is safe as there is a unique index on uuid +SELECT @android := user_id FROM users WHERE username='android' LIMIT 1; +INSERT INTO location (name, creator, date_created, uuid) VALUES + ('Facility', @android, NOW(), 'e20f2bf8-8650-11e4-9aee-040ccecfdba4') + ON DUPLICATE KEY UPDATE uuid=uuid; +SELECT @emc_id := location_id FROM location WHERE uuid='e20f2bf8-8650-11e4-9aee-040ccecfdba4' LIMIT 1; +-- insert the zones +INSERT INTO location (name, creator, date_created, uuid, parent_location) VALUES + ('Triage', @android, NOW(), '3f75ca61-ec1a-4739-af09-25a84e3dd237', @emc_id), + ('Suspected Zone', @android, NOW(), '2f1e2418-ede6-481a-ad80-b9939a7fde8e', @emc_id), + ('Probable Zone', @android, NOW(), '3b11e7c8-a68a-4a5f-afb3-a4a053592d0e', @emc_id), + ('Confirmed Zone', @android, NOW(), 'b9038895-9c9d-4908-9e0d-51fd535ddd3c', @emc_id), + ('Morgue', @android, NOW(), '4ef642b9-9843-4d0d-9b2b-84fe1984801f', @emc_id), + ('Discharged', @android, NOW(), 'd7ca63c3-6ea0-4357-82fd-0910cc17a2cb', @emc_id) + ON DUPLICATE KEY UPDATE uuid=uuid; + +-- Make sure the allowed locales is correct +UPDATE global_property SET property_value="en, en_GB_client" WHERE property = "locale.allowed.list"; + +-- Insert the tents (more than enough in each zone to work at most EMCs). +SELECT @confirmed_id := location_id FROM location WHERE uuid='b9038895-9c9d-4908-9e0d-51fd535ddd3c' LIMIT 1; +SELECT @suspect_id := location_id FROM location WHERE uuid='2f1e2418-ede6-481a-ad80-b9939a7fde8e' LIMIT 1; +SELECT @probable_id := location_id FROM location WHERE uuid='3b11e7c8-a68a-4a5f-afb3-a4a053592d0e' LIMIT 1; +INSERT INTO location (name, creator, date_created, uuid, parent_location) VALUES + ('S1', @android, NOW(), 'a72f944b-cb50-4bc5-9ac0-f93c44d71b10', @suspect_id), + ('S2', @android, NOW(), 'd81a33d9-2711-47e2-9d47-77e32e0281b9', @suspect_id), + ('S3', @android, NOW(), 'c7a69e19-8650-11e4-9cc2-040ccecfdba4', @suspect_id), + ('S4', @android, NOW(), 'c87ad5ab-8650-11e4-afd6-040ccecfdba4', @suspect_id), + ('P1', @android, NOW(), '0d36bdce-7f0a-11e4-88ec-42010af084c0', @probable_id), + ('P2', @android, NOW(), '0d36beb7-7f0a-11e4-88ec-42010af084c0', @probable_id), + ('P3', @android, NOW(), 'c89d00a1-8650-11e4-a82d-040ccecfdba4', @probable_id), + ('P4', @android, NOW(), 'c8ba4385-8650-11e4-8641-040ccecfdba4', @probable_id), + ('C1', @android, NOW(), '46a8cb21-d9eb-416d-86ee-90a018122859', @confirmed_id), + ('C2', @android, NOW(), '0a49d383-7019-4f1f-bf4b-875f2cd58964', @confirmed_id), + ('C3', @android, NOW(), '4443985e-adbc-4c90-aaac-b27635cb73ac', @confirmed_id), + ('C4', @android, NOW(), '3ca154be-afd1-4074-893d-596bcb423a54', @confirmed_id), + ('C5', @android, NOW(), '6b993dab-7f0a-11e4-88ec-42010af084c0', @confirmed_id), + ('C6', @android, NOW(), '0cce735e-a0c8-4b21-a05e-539b6bb93441', @confirmed_id), + ('C7', @android, NOW(), '5542080a-45db-435e-8505-8e65309ae9d5', @confirmed_id), + ('C8', @android, NOW(), '87233c64-125a-4e8e-b292-f866a8ecb2b4', @confirmed_id), + ('C9', @android, NOW(), 'c8d6607a-8650-11e4-9dfb-040ccecfdba4', @confirmed_id), + ('C10', @android, NOW(), 'c8f5a8d7-8650-11e4-81c0-040ccecfdba4', @confirmed_id) + ON DUPLICATE KEY UPDATE uuid=uuid; diff --git a/packages/buendia-db-init/root/usr/share/buendia/db/site-kh.sql b/packages/buendia-db-init/root/usr/share/buendia/db/site-kh.sql new file mode 100644 index 00000000..f2132e3a --- /dev/null +++ b/packages/buendia-db-init/root/usr/share/buendia/db/site-kh.sql @@ -0,0 +1,61 @@ +/* + * This script takes a database cleared by clear_server.sql and sets + * up initial data required for a start at Kailahun. + * It will also double checks some data that should already be present + * and adds it if missing. + * + * Bear in mind this assume you are working from a snapshot of our + * GCE instance, and so doesn't add the whole concept dictionary etc, + * as keeping a script up to date is harder than keeping the real database + * up to date. + */ + +-- System Users: make sure Android is present +-- This is surprisingly hard as there is not a unique constraint on username +-- or system_id, so the normal tricks for INSERT IF NOT EXISTS don't work. +-- Ignoring for now. + +-- Buendia users - make sure user, provider and person information present +-- for Guest User + +-- Program. Make sure Ebola program is present. + + +-- Location make sure Camp, and zones are present. Create tents. +-- ON DUPLICATE IGNORE is safe as there is a unique index on uuid +SELECT @android := user_id FROM users WHERE username='android' LIMIT 1; +INSERT INTO location (name, creator, date_created, uuid) VALUES + ('Facility Kailahun', @android, NOW(), '3449f5fe-8e6b-4250-bcaa-fca5df28ddbf') + ON DUPLICATE KEY UPDATE uuid=uuid; +SELECT @emc_id := location_id FROM location WHERE uuid='3449f5fe-8e6b-4250-bcaa-fca5df28ddbf' LIMIT 1; +-- insert the zones +INSERT INTO location (name, creator, date_created, uuid, parent_location) VALUES + ('Triage', @android, NOW(), '3f75ca61-ec1a-4739-af09-25a84e3dd237', @emc_id), + ('Suspected Zone', @android, NOW(), '2f1e2418-ede6-481a-ad80-b9939a7fde8e', @emc_id), + ('Probable Zone', @android, NOW(), '3b11e7c8-a68a-4a5f-afb3-a4a053592d0e', @emc_id), + ('Confirmed Zone', @android, NOW(), 'b9038895-9c9d-4908-9e0d-51fd535ddd3c', @emc_id), + ('Morgue', @android, NOW(), '4ef642b9-9843-4d0d-9b2b-84fe1984801f', @emc_id), + ('Discharged', @android, NOW(), 'd7ca63c3-6ea0-4357-82fd-0910cc17a2cb', @emc_id) + ON DUPLICATE KEY UPDATE uuid=uuid; + +-- Make sure the allowed locales is correct +UPDATE global_property SET property_value="en, en_GB_client" WHERE property = "locale.allowed.list"; + +-- Kailahun specific. Insert the tents. +SELECT @confirmed_id := location_id FROM location WHERE uuid='b9038895-9c9d-4908-9e0d-51fd535ddd3c' LIMIT 1; +SELECT @suspect_id := location_id FROM location WHERE uuid='2f1e2418-ede6-481a-ad80-b9939a7fde8e' LIMIT 1; +SELECT @probable_id := location_id FROM location WHERE uuid='3b11e7c8-a68a-4a5f-afb3-a4a053592d0e' LIMIT 1; +INSERT INTO location (name, creator, date_created, uuid, parent_location) VALUES + ('S1', @android, NOW(), 'a72f944b-cb50-4bc5-9ac0-f93c44d71b10', @suspect_id), + ('S2', @android, NOW(), 'd81a33d9-2711-47e2-9d47-77e32e0281b9', @suspect_id), + ('P1', @android, NOW(), '0d36bdce-7f0a-11e4-88ec-42010af084c0', @probable_id), + ('P2', @android, NOW(), '0d36beb7-7f0a-11e4-88ec-42010af084c0', @probable_id), + ('C1', @android, NOW(), '46a8cb21-d9eb-416d-86ee-90a018122859', @confirmed_id), + ('C2', @android, NOW(), '0a49d383-7019-4f1f-bf4b-875f2cd58964', @confirmed_id), + ('C3', @android, NOW(), '4443985e-adbc-4c90-aaac-b27635cb73ac', @confirmed_id), + ('C4', @android, NOW(), '3ca154be-afd1-4074-893d-596bcb423a54', @confirmed_id), + ('C5', @android, NOW(), '6b993dab-7f0a-11e4-88ec-42010af084c0', @confirmed_id), + ('C6', @android, NOW(), '0cce735e-a0c8-4b21-a05e-539b6bb93441', @confirmed_id), + ('C7', @android, NOW(), '5542080a-45db-435e-8505-8e65309ae9d5', @confirmed_id), + ('C8', @android, NOW(), '87233c64-125a-4e8e-b292-f866a8ecb2b4', @confirmed_id) + ON DUPLICATE KEY UPDATE uuid=uuid; diff --git a/packages/buendia-mysql/Makefile b/packages/buendia-mysql/Makefile index f77e46b3..51122d69 100644 --- a/packages/buendia-mysql/Makefile +++ b/packages/buendia-mysql/Makefile @@ -1 +1,13 @@ include ../Makefile.inc + +$(EXTRA_ROOT): $(EXTRA_ROOT)/usr/bin/buendia-mysql-dump $(EXTRA_ROOT)/usr/bin/buendia-mysql-load + +$(EXTRA_ROOT)/usr/bin/buendia-mysql-dump: + mkdir -p $$(dirname $@) + cp -p $(TOOLS)/openmrs_dump $@ + chmod 755 $@ + +$(EXTRA_ROOT)/usr/bin/buendia-mysql-load: + mkdir -p $$(dirname $@) + cp -p $(TOOLS)/openmrs_load $@ + chmod 755 $@ diff --git a/packages/buendia-mysql/control/postinst b/packages/buendia-mysql/control/postinst index ce4773f7..2d93b6da 100644 --- a/packages/buendia-mysql/control/postinst +++ b/packages/buendia-mysql/control/postinst @@ -1,5 +1,6 @@ #!/bin/bash set -e +buendia-divert $1 /etc/my.cnf +service mysql restart /usr/share/buendia/config.d/mysql -service mysql start diff --git a/packages/buendia-mysql/control/postrm b/packages/buendia-mysql/control/postrm deleted file mode 100644 index 57439f06..00000000 --- a/packages/buendia-mysql/control/postrm +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash - -set -e -service mysql start diff --git a/packages/buendia-mysql/control/prerm b/packages/buendia-mysql/control/prerm new file mode 100644 index 00000000..2d93b6da --- /dev/null +++ b/packages/buendia-mysql/control/prerm @@ -0,0 +1,6 @@ +#!/bin/bash + +set -e +buendia-divert $1 /etc/my.cnf +service mysql restart +/usr/share/buendia/config.d/mysql diff --git a/packages/buendia-mysql/root/etc/my.cnf b/packages/buendia-mysql/root/etc/my.cnf new file mode 100644 index 00000000..4e7dbbda --- /dev/null +++ b/packages/buendia-mysql/root/etc/my.cnf @@ -0,0 +1,2 @@ +[mysqld] +innodb_file_per_table=true diff --git a/packages/buendia-pkgserver/Makefile b/packages/buendia-pkgserver/Makefile index 9bd05d3b..63a6099c 100644 --- a/packages/buendia-pkgserver/Makefile +++ b/packages/buendia-pkgserver/Makefile @@ -7,15 +7,15 @@ $(EXTRA_ROOT): $(TARGET_DIR)/buendia-package-apt-indexer $(TARGET_DIR)/buendia-p $(TARGET_DIR)/buendia-package-apt-indexer: $(SOURCE_DIR)/buendia-package-apt-indexer mkdir -p $$(dirname $@) - cp -p $< $@ + cp -p $^ $@ $(TARGET_DIR)/buendia-package-indexer.py: $(SOURCE_DIR)/buendia-package-indexer.py mkdir -p $$(dirname $@) - cp -p $< $@ + cp -p $^ $@ $(TARGET_DIR)/buendia-package-usb-import: $(SOURCE_DIR)/buendia-package-usb-import mkdir -p $$(dirname $@) - cp -p $< $@ + cp -p $^ $@ docker: $(PACKAGE) docker build .