Skip to content

Commit

Permalink
update test/demodata (#65)
Browse files Browse the repository at this point in the history
* update test/demodata

* fix version number

* use pg_dump 9.6 to produce a proper version

* try to upgrade postgresql

* run as sudo

* update client only

* try with postgis 2.4

* take 2 at postgis 2.4

* upgrade client again

* remove older pg versions

* fix role in dump

* fix owner take 2

* complete dump

* demodata restored on freshly created db

* add submodule with demodata

* remove bad submodule folder

* fix submodule

* do not check public schema in pum

* avoid restoring public schema

* fix var in string

* avoid drop/recreating roles

* remove public schema (postgis) from demo data

* fix pum new arguments

* remove demodata submodule

* add deploy script
  • Loading branch information
3nids authored Apr 14, 2018
1 parent d199cda commit 6899408
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 34 deletions.
247 changes: 247 additions & 0 deletions .deploy/create-release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
***************************************************************************
create_release.py
---------------------
Date : April 2018
Copyright : (C) 2018 by Denis Rouzaud
Email : [email protected]
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************
"""

__author__ = 'Denis Rouzaud'
__date__ = 'April 2018'
__copyright__ = '(C) 2018,Denis Rouzaud'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'


import http.client
import os
import json
import subprocess


def create_dumps():
files = []

# Create data-only dumps (with sample data)

dump = 'qgep_v{version}_data_only_sample.backup'.format(
version=os.environ['TRAVIS_TAG'])
print('travis_fold:start:{}'.format(dump))
print('Creating dump {}'.format(dump))
dumpfile = '/tmp/{dump}'.format(dump=dump)
subprocess.call(['pg_dump',
'--format', 'custom',
'--blobs',
'--section', 'data',
'--compress', '5',
'--verbose',
'--file', dumpfile,
'--schema', 'qgep_od',
'qgep_prod']
)
files.append(dumpfile)
print('travis_fold:end:{}'.format(dump))

dump='qgep_v{version}_data_only_sample.sql'.format(
version=os.environ['TRAVIS_TAG'])
print('travis_fold:start:{}'.format(dump))
print('Creating dump {}'.format(dump))
dumpfile='/tmp/{dump}'.format(dump=dump)

subprocess.call(['pg_dump',
'--format', 'plain',
'--blobs',
'--section', 'data',
'--verbose',
'--file', dumpfile,
'--schema', 'qgep_od',
'qgep_prod']
)
files.append(dumpfile)
print('travis_fold:end:{}'.format(dump))

# Create data + structure dumps (with sample data)

dump='qgep_v{version}_data_and_structure_sample.backup'.format(
version=os.environ['TRAVIS_TAG'])
print('travis_fold:start:{}'.format(dump))
print('Creating dump {}'.format(dump))
dumpfile='/tmp/{dump}'.format(dump=dump)

subprocess.call(['pg_dump',
'--format', 'custom',
'--blobs',
'--compress', '5',
'--verbose',
'--file', dumpfile,
'-N', 'public',
'qgep_prod']
)
files.append(dumpfile)
print('travis_fold:end:{}'.format(dump))

dump='qgep_v{version}_data_and_structure_sample.sql'.format(
version=os.environ['TRAVIS_TAG'])
print('travis_fold:start:{}'.format(dump))
print('Creating dump {}'.format(dump))
dumpfile='/tmp/{dump}'.format(dump=dump)

subprocess.call(['pg_dump',
'--format', 'plain',
'--blobs',
'--verbose',
'--file', dumpfile,
'-N', 'public',
'qgep_prod']
)
files.append(dumpfile)
print('travis_fold:end:{}'.format(dump))

# Create structure-only dumps

dump='qgep_v{version}_structure_only.backup'.format(
version=os.environ['TRAVIS_TAG'])
print('travis_fold:start:{}'.format(dump))
print('Creating dump {}'.format(dump))
dumpfile='/tmp/{dump}'.format(dump=dump)

subprocess.call(['pg_dump',
'--format', 'custom',
'--schema-only',
'--verbose',
'--file', dumpfile,
'-N', 'public',
'qgep_prod']
)
files.append(dumpfile)
print('travis_fold:end:{}'.format(dump))

dump='qgep_v{version}_structure_only.sql'.format(
version=os.environ['TRAVIS_TAG'])
print('travis_fold:start:{}'.format(dump))
print('Creating dump {}'.format(dump))
dumpfile='/tmp/{dump}'.format(dump=dump)

subprocess.call(['pg_dump',
'--format', 'plain',
'--schema-only',
'--verbose',
'--file', dumpfile,
'-N', 'public',
'qgep_prod']
)
files.append(dumpfile)
print('travis_fold:end:{}'.format(dump))

# Create value-list data only dumps (the qgep_vl schema)

dump = 'qgep_v{version}_value_list_data_only.backup'.format(
version=os.environ['TRAVIS_TAG'])
print('travis_fold:start:{}'.format(dump))
print('Creating dump {}'.format(dump))
dumpfile = '/tmp/{dump}'.format(dump=dump)
subprocess.call(['pg_dump',
'--format', 'custom',
'--blobs',
'--compress', '5',
'--data-only',
'--verbose',
'--file', dumpfile,
'--schema', 'qgep_vl',
'qgep_prod']
)
files.append(dumpfile)
print('travis_fold:end:{}'.format(dump))

dump='qgep_v{version}_value_list_data_only.sql'.format(
version=os.environ['TRAVIS_TAG'])
print('travis_fold:start:{}'.format(dump))
print('Creating dump {}'.format(dump))
dumpfile='/tmp/{dump}'.format(dump=dump)

subprocess.call(['pg_dump',
'--format', 'plain',
'--blobs',
'--data-only',
'--verbose',
'--file', dumpfile,
'--schema', 'qgep_vl',
'qgep_prod']
)
files.append(dumpfile)
print('travis_fold:end:{}'.format(dump))

return files


def main():
if 'TRAVIS_TAG' not in os.environ or not os.environ['TRAVIS_TAG']:
print('No git tag: not deploying anything')
return
elif os.environ['TRAVIS_SECURE_ENV_VARS'] != 'true':
print('No secure environment variables: not deploying anything')
return
else:
print('Creating release from tag {}'.format(os.environ['TRAVIS_TAG']))

release_files=create_dumps()

conn=http.client.HTTPSConnection('api.github.com')

headers={
'User-Agent': 'Deploy-Script',
'Authorization': 'token {}'.format(os.environ['GH_TOKEN'])
}

raw_data={
"tag_name": os.environ['TRAVIS_TAG']
}

data=json.dumps(raw_data)
url='/repos/{repo_slug}/releases'.format(
repo_slug=os.environ['TRAVIS_REPO_SLUG'])
conn.request('POST', url, body=data, headers=headers)
response=conn.getresponse()
release=json.loads(response.read().decode())

if 'upload_url' not in release:
print('Failed to create release!')
print('Github API replied:')
print('{} {}'.format(response.status, response.reason))
print(repr(release))
exit(-1)

conn=http.client.HTTPSConnection('uploads.github.com')
for release_file in release_files:
_, filename=os.path.split(release_file)
headers['Content-Type']='text/plain'
# headers['Transfer-Encoding']='gzip'
url='{release_url}?name={filename}'.format(release_url=release['upload_url'][:-13], filename=filename)
print('Upload to {}'.format(url))

with open(release_file, 'rb') as f:
conn.request('POST', url, f, headers)

response = conn.getresponse()
result = response.read()
if response.status != 201:
print('Failed to upload filename {filename}'.format(filename=filename))
print('Github API replied:')
print('{} {}'.format(response.status, response.reason))
print(repr(json.loads(result.decode())))


if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.noseids
nosetests.xml
*.orig
.DS_Store
41 changes: 18 additions & 23 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ python:

addons:
postgresql: "9.6"
apt:
packages:
- postgresql-9.6-postgis-2.3

install:
- pip install -r requirements.txt
Expand All @@ -25,22 +22,20 @@ before_install:
- git submodule update --init --recursive

before_script:
- printf "[pg_qgep]\nhost=localhost\ndbname=qgep\nuser=postgres\n" > ~/.pg_service.conf
- psql -c 'CREATE DATABASE qgep;' -U postgres
- psql -c 'CREATE EXTENSION postgis;' -U postgres -d qgep
- psql -c 'CREATE EXTENSION hstore;' -U postgres -d qgep

# Append the 3 services used for pum's checks to pg_service.conf
- printf "[qgep_prod]\nhost=localhost\ndbname=qgep_prod\nuser=postgres\n\n[qgep_test]\nhost=localhost\ndbname=qgep_test\nuser=postgres\n\n[qgep_comp]\nhost=localhost\ndbname=qgep_comp\nuser=postgres\n" >> ~/.pg_service.conf

# Create the 3 databases used for pum's check
- psql -c 'CREATE DATABASE qgep_prod;' -U postgres
- psql -c 'CREATE DATABASE qgep_test;' -U postgres
- psql -c 'CREATE DATABASE qgep_comp;' -U postgres

- psql -c 'CREATE GROUP qgep;' -U postgres
- psql -c 'CREATE ROLE qgepuser LOGIN;' -U postgres
- psql -c 'GRANT qgep TO qgepuser;' -U postgres
- sudo apt-get -qq update
- sudo apt-get autoremove -y postgresql-9.2 postgresql-9.3 postgresql-9.4 postgresql-9.5
- sudo apt-get install -y postgresql-9.6-postgis-2.4
- sudo apt-get upgrade postgresql-client-9.6
- |
for pgsrv in pg_qgep qgep_prod qgep_test qgep_comp; do
printf "[${pgsrv}]\nhost=localhost\ndbname=${pgsrv}\nuser=postgres\n" >> ~/.pg_service.conf
psql -c "CREATE DATABASE ${pgsrv};" -U postgres
psql -c "CREATE EXTENSION postgis;" -U postgres -d ${pgsrv}
psql -c "CREATE EXTENSION hstore;" -U postgres -d ${pgsrv}
done
psql -c "CREATE GROUP qgep;" -U postgres
psql -c "CREATE ROLE qgepuser LOGIN;" -U postgres
psql -c "GRANT qgep TO qgepuser;" -U postgres
script:
- $TRAVIS_BUILD_DIR/scripts/db_setup.sh -r > /dev/null
Expand All @@ -50,17 +45,17 @@ script:
- export VERSION=$(sed 'r' "$TRAVIS_BUILD_DIR/system/CURRENT_VERSION.txt")

# Create a db from a dump file. This simulate the prod db
- pum restore -p qgep_prod -x $TRAVIS_BUILD_DIR/test_data/qgep_dump_20180201.dump
- pum baseline -p qgep_prod -t qgep_sys.pum_info -d $TRAVIS_BUILD_DIR/delta/ -b 2018.02.01
- pum restore -p qgep_prod -x --exclude-schema public -- $TRAVIS_BUILD_DIR/test_data/qgep_demodata_1.0.0.dump
- pum baseline -p qgep_prod -t qgep_sys.pum_info -d $TRAVIS_BUILD_DIR/delta/ -b 1.0.0

# Create last version of qgep db using db_setup.sh script as comp db
- export PGSERVICE=qgep_comp
- $TRAVIS_BUILD_DIR/scripts/db_setup.sh > /dev/null
- pum baseline -p qgep_comp -t qgep_sys.pum_info -d $TRAVIS_BUILD_DIR/delta/ -b $VERSION

# Run pum's test and upgrade
- yes | pum test-and-upgrade -pp qgep_prod -pt qgep_test -pc qgep_comp -t qgep_sys.pum_info -d $TRAVIS_BUILD_DIR/delta/ -f /tmp/qwat_dump -i constraints views
- yes | pum test-and-upgrade -pp qgep_prod -pt qgep_test -pc qgep_comp -t qgep_sys.pum_info -d $TRAVIS_BUILD_DIR/delta/ -f /tmp/qwat_dump -i constraints views --exclude-schema public

after_success:
- $TRAVIS_BUILD_DIR/scripts/make_pull_request.sh
#- $TRAVIS_BUILD_DIR/scripts/make_pull_request.sh
- python $TRAVIS_BUILD_DIR/scripts/release_db_template.py
20 changes: 11 additions & 9 deletions 12_roles.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/* create roles */
DROP ROLE IF EXISTS qgep_viewer;
DROP ROLE IF EXISTS qgep_user;
DROP ROLE IF EXISTS qgep_manager;
DROP ROLE IF EXISTS qgep_sysadmin;

CREATE ROLE qgep_viewer NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
CREATE ROLE qgep_user NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
CREATE ROLE qgep_manager NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
CREATE ROLE qgep_sysadmin NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
DO $$
DECLARE
role text;
BEGIN
FOREACH role IN ARRAY ARRAY['qgep_viewer', 'qgep_user', 'qgep_manager', 'qgep_sysadmin'] LOOP
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = role) THEN
EXECUTE format('CREATE ROLE %1$I NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION', role);
END IF;
END LOOP;
END
$$;

/* Viewer */
GRANT USAGE ON SCHEMA qgep_od TO qgep_viewer;
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion metaproject
2 changes: 1 addition & 1 deletion system/CURRENT_VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2018.02.02
1.0.1
Binary file added test_data/qgep_demodata_1.0.0.dump
Binary file not shown.
Binary file removed test_data/qgep_dump_20180201.dump
Binary file not shown.

0 comments on commit 6899408

Please sign in to comment.