~launchpad-pqm/launchpad/devel

8687.15.9 by Karl Fogel
Add the copyright header block to more files (everything under database/).
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
3
#
4
# This makefile is used for two task:
5
#
6
#  - Build a replicated development environment
7
#  - Build and replicate the staging database
8
#
9
# To build a replicated development environment:
10
#
11
#  $ make -C database/replication devsetup
12
#  $ make run
13
#
14
# To test the staging rebuild script:
15
#
7675.395.163 by Stuart Bishop
Store and report database patch application times
16
#  $ mkdir -p /srv/staging.launchpad.net/staging-logs
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
17
#  $ cd database/replication
7675.395.170 by Stuart Bishop
Revert to using builtin compression for production dumps so we can use the parallel restore option when rebuilding staging
18
#  $ pg_dump --format=c launchpad_dev > launchpad.dump
7675.395.130 by Stuart Bishop
Specify replication log destination in config
19
#  $ make stagingsetup \
7675.395.170 by Stuart Bishop
Revert to using builtin compression for production dumps so we can use the parallel restore option when rebuilding staging
20
#        STAGING_CONFIG=dev-staging STAGING_DUMP=launchpad.dump
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
21
#  $ make stagingswitch STAGING_CONFIG=dev-staging
22
#
9795.4.8 by Stuart Bishop
Fix staging db restore and add dogfood db restoration process
23
# To restore a dogfood database:
24
#
25
#  $ cd database/replication
26
#  $ make dogfood DOGFOOD_DBNAME=launchpad_dogfood DOGFOOD_DUMP=launchpad.dump
27
#
8635.1.1 by Stuart Bishop
Turn off staging replication lag so DBLoopTuner does not always block
28
29
# This used to be 10 seconds, so we always ran staging lagged to detect
30
# replication glitches more easily. However, this does not play well
31
# with DBLoopTuner, as it correctly blocks when it detects lag.
32
# We should put this back when DBLoopTuner is more intelligent, perhaps
33
# pulling the allowable lag setting from a config file.
34
LAG=0 seconds
5799.1.57 by Stuart Bishop
developer setup tool
35
7675.395.170 by Stuart Bishop
Revert to using builtin compression for production dumps so we can use the parallel restore option when rebuilding staging
36
# For real restores, this is being run on an 8 core system. Let pg_restore
37
# run multiprocess.
38
MULTIPROC=-j 4
39
7178.4.4 by Stuart Bishop
Most of staging db build
40
DEV_CONFIG=replicated-development
41
NEW_STAGING_CONFIG=staging-setup # For building the db with a different name.
13465.2.1 by Stuart Bishop
Use staging-db config for staging db updates
42
STAGING_CONFIG=staging-db # For swapping fresh db into place.
7178.4.4 by Stuart Bishop
Most of staging db build
43
STAGING_DUMP=launchpad.dump # Dumpfile to build new staging from.
7872.1.1 by Stuart Bishop
Staging config changes for new server
44
STAGING_TABLESPACE=pg_default # 'pg_default' for default
7675.395.168 by Stuart Bishop
Log staging database update log to a separate file during a full restore
45
STAGING_LOGDIR=/srv/staging.launchpad.net/staging-logs
9795.4.9 by Stuart Bishop
Make repair-restored-db.py more robust and fix database restore ordering for staging and dogfood
46
DOGFOOD_DBNAME=launchpad_dogfood
47
DOGFOOD_DUMP=launchpad.dump
7178.4.4 by Stuart Bishop
Most of staging db build
48
49
_CONFIG=overridden-on-command-line
50
_SLAVE_TABLESPACE=pg_default
51
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
52
PGMASSACRE=../../utilities/pgmassacre.py
53
7675.395.70 by Stuart Bishop
Remove authdb logic from staging Makefile and prepare for PostgreSQL 8.4
54
CREATEDB_83=createdb --encoding=UTF8
55
CREATEDB_84=createdb --encoding=UTF8 --locale=C --template=template0
7675.395.134 by Stuart Bishop
Switch staging rebuilds to pg 8.4
56
CREATEDB=${CREATEDB_84}
7675.395.70 by Stuart Bishop
Remove authdb logic from staging Makefile and prepare for PostgreSQL 8.4
57
7675.395.135 by Stuart Bishop
Work around glitches restoring a PG 8.3 database dump into a PG 8.4 system
58
# Set this to --exit-on-error once our dumps are coming from a PG 8.4
59
# source. Currently, the PG 8.3 dumps generate some spurious errors
60
# when being restored into a PG 8.4 database.
7675.395.172 by Stuart Bishop
Enable --exit-on-error when rebuilding staging
61
EXIT_ON_ERROR=--exit-on-error
7675.395.135 by Stuart Bishop
Work around glitches restoring a PG 8.3 database dump into a PG 8.4 system
62
7675.395.75 by Stuart Bishop
Review feedback
63
# Turn off output silencing so we can see details of staging deployments.
64
# Without the timestamps, we are unable to estimate production deployment
65
# times.
7675.395.59 by Stuart Bishop
Turn off silencing for now so we details on staging deployments
66
#SHHH=../../utilities/shhh.py
67
SHHH=
9795.4.1 by Stuart Bishop
Fix new-slave.py to work with split authdb/lpmain masters
68
5799.1.52 by Stuart Bishop
Commands to start/stop local slony daemons
69
default:
70
	echo Usage: make [start|stop|restart]
71
5799.1.56 by Stuart Bishop
Start/stop script for dev setup
72
start:
7178.4.4 by Stuart Bishop
Most of staging db build
73
	./slon_ctl.py --lag="${LAG}" start
5799.1.52 by Stuart Bishop
Commands to start/stop local slony daemons
74
5799.1.56 by Stuart Bishop
Start/stop script for dev setup
75
stop:
7675.58.10 by Stuart Bishop
Tidy and fix devsetup in Makefile
76
	./slon_ctl.py stop
5799.1.52 by Stuart Bishop
Commands to start/stop local slony daemons
77
5799.1.55 by Stuart Bishop
Improve initialize, less magic dev setup
78
restart: stop start
5799.1.52 by Stuart Bishop
Commands to start/stop local slony daemons
79
5799.1.57 by Stuart Bishop
developer setup tool
80
devsetup:
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
81
	make _prelim LPCONFIG=${DEV_CONFIG} \
82
	    _MASTER=launchpad_dev _SLAVE=launchpad_dev_slave LAG="0 seconds"
83
	
7178.4.4 by Stuart Bishop
Most of staging db build
84
	# Build the master database
85
	LPCONFIG=${DEV_CONFIG} make -C ../schema
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
86
	
87
	# Replicate it
88
	make _replicate LPCONFIG=${DEV_CONFIG} \
89
	    _MASTER=launchpad_dev _SLAVE=launchpad_dev_slave LAG="0 seconds"
7675.395.171 by Stuart Bishop
Revert unnecessary whitespace changes
90
	
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
91
	# Replicate it again, so we can test with multiple slaves.
92
	-${PGMASSACRE} launchpad_dev_slave2
7675.395.70 by Stuart Bishop
Remove authdb logic from staging Makefile and prepare for PostgreSQL 8.4
93
	${CREATEDB} launchpad_dev_slave2
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
94
	LPCONFIG=${DEV_CONFIG} ./slon_ctl.py start \
95
		 node3_node 'dbname=launchpad_dev_slave2 user=slony'
96
	LPCONFIG=${DEV_CONFIG} ./new-slave.py 3 launchpad_dev_slave2
97
	LPCONFIG=${DEV_CONFIG} ../schema/security.py -d launchpad_dev_slave2
98
	
99
	# Regenerate the preamble for manual slonik(1) usage.
100
	LPCONFIG=${DEV_CONFIG} ./preamble.py > preamble.sk
101
102
	# Restart slon daemons with default lag setting.
103
	LPCONFIG=${DEV_CONFIG} ./slon_ctl.py stop
104
	LPCONFIG=${DEV_CONFIG} ./slon_ctl.py --lag="${LAG}" start
105
7178.4.4 by Stuart Bishop
Most of staging db build
106
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
107
# Build _new staging databases from a production dump.
7178.4.4 by Stuart Bishop
Most of staging db build
108
stagingsetup:
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
109
	make _prelim LPCONFIG=${NEW_STAGING_CONFIG} \
110
	    _MASTER=lpmain_staging_new _SLAVE=lpmain_staging_slave_new \
111
	    LAG="0 seconds"
7178.4.4 by Stuart Bishop
Most of staging db build
112
	# Create the DB with the desired default tablespace.
7675.395.70 by Stuart Bishop
Remove authdb logic from staging Makefile and prepare for PostgreSQL 8.4
113
	${CREATEDB} --tablespace ${STAGING_TABLESPACE} lpmain_staging_new
114
	# Restore the database. We need to restore permissions, despite
9795.4.24 by Stuart Bishop
Fix a comment to match current reality
115
	# later running security.py, to pull in permissions granted on
116
	# production to users not maintained by security.py.
7675.395.170 by Stuart Bishop
Revert to using builtin compression for production dumps so we can use the parallel restore option when rebuilding staging
117
	pg_restore --dbname=lpmain_staging_new --no-owner ${EXIT_ON_ERROR} \
118
	    ${MULTIPROC} ${STAGING_DUMP}
7178.5.6 by Stuart Bishop
Ensure analyze starts *after* subscription by using a time delay
119
	# Uninstall Slony-I if it is installed - a pg_dump of a DB with
120
	# Slony-I installed isn't usable without this step.
121
	LPCONFIG=${NEW_STAGING_CONFIG} ./repair-restored-db.py
7178.4.4 by Stuart Bishop
Most of staging db build
122
	# Setup replication
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
123
	make _replicate LPCONFIG=${NEW_STAGING_CONFIG} LAG="0 seconds" \
7178.4.4 by Stuart Bishop
Most of staging db build
124
	    _MASTER=lpmain_staging_new _SLAVE=lpmain_staging_slave_new \
125
	    _SLAVE_TABLESPACE=${STAGING_TABLESPACE}
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
126
127
# Switch the _new staging databases into place.
128
stagingswitch:
129
	# Stop Slony-I daemons - don't confuse the poor darlings.
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
130
	-LPCONFIG=${NEW_STAGING_CONFIG} ./slon_ctl.py stop
131
	-LPCONFIG=${STAGING_CONFIG} ./slon_ctl.py stop
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
132
	# Kill the existing staging database if it exists.
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
133
	-${PGMASSACRE} lpmain_staging
134
	-${PGMASSACRE} lpmain_staging_slave
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
135
	# Rename the newly build staging databases.
136
	psql -d template1 -c \
137
	    "ALTER DATABASE lpmain_staging_new RENAME TO lpmain_staging;"
138
	psql -d template1 -c "\
139
	    ALTER DATABASE lpmain_staging_slave_new \
140
	    RENAME TO lpmain_staging_slave;"
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
141
	# Fix the paths to match.
142
	psql -d lpmain_staging -U slony -c \
143
	"UPDATE _sl.sl_path SET pa_conninfo=replace(pa_conninfo, '_new', '')"
144
	psql -d lpmain_staging_slave -U slony -c \
145
	"UPDATE _sl.sl_path SET pa_conninfo=replace(pa_conninfo, '_new', '')"
146
	# Start the slon daemons, with requested lag.
147
	LPCONFIG=${STAGING_CONFIG} ./slon_ctl.py --lag="${LAG}" start
7178.4.4 by Stuart Bishop
Most of staging db build
148
9795.4.8 by Stuart Bishop
Fix staging db restore and add dogfood db restoration process
149
dogfood:
7675.395.70 by Stuart Bishop
Remove authdb logic from staging Makefile and prepare for PostgreSQL 8.4
150
	${CREATEDB} ${DOGFOOD_DBNAME}
7675.395.136 by Stuart Bishop
Ignore return value of pg_restore until our dumps come from a PG 8.4 system
151
	# Stop ignoring error code after are dumps come from an 8.4 system.
152
	-pg_restore --dbname=${DOGFOOD_DBNAME} --no-acl --no-owner \
7675.395.135 by Stuart Bishop
Work around glitches restoring a PG 8.3 database dump into a PG 8.4 system
153
	    ${EXIT_ON_ERROR} ${DOGFOOD_DUMP}
9795.4.9 by Stuart Bishop
Make repair-restored-db.py more robust and fix database restore ordering for staging and dogfood
154
	./repair-restored-db.py -d ${DOGFOOD_DBNAME}
9795.4.8 by Stuart Bishop
Fix staging db restore and add dogfood db restoration process
155
	../schema/upgrade.py -d ${DOGFOOD_DBNAME}
156
	../schema/fti.py -d ${DOGFOOD_DBNAME}
157
	../schema/security.py -d ${DOGFOOD_DBNAME}
158
7178.4.4 by Stuart Bishop
Most of staging db build
159
_prelim:
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
160
	@echo LPCONFIG currently ${LPCONFIG}
5799.1.57 by Stuart Bishop
developer setup tool
161
	# Create the slony PostgreSQL superuser if necessary.
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
162
	-createuser --superuser slony
5799.1.57 by Stuart Bishop
developer setup tool
163
	# Stop the slon daemons and wait a bit for connections to drop.
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
164
	-./slon_ctl.py stop
5799.1.57 by Stuart Bishop
developer setup tool
165
	sleep 5
166
	# Drop any existing databases if they exist
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
167
	${PGMASSACRE} ${_MASTER}
168
	${PGMASSACRE} ${_SLAVE}
7178.4.4 by Stuart Bishop
Most of staging db build
169
170
_replicate:
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
171
	@echo LPCONFIG currently ${LPCONFIG}
172
	# Start the slon daemon for the master.
7675.395.70 by Stuart Bishop
Remove authdb logic from staging Makefile and prepare for PostgreSQL 8.4
173
	./slon_ctl.py --lag="0 seconds" start \
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
174
		 node1_node "dbname=${_MASTER} user=slony"
175
	# Initialize the cluster and create replication sets.
7675.395.168 by Stuart Bishop
Log staging database update log to a separate file during a full restore
176
	./initialize.py \
177
	    --log-file=INFO:${STAGING_LOGDIR}/dbupgrade.log
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
178
	# Create the soon-to-be-slave database, empty at this point.
7675.395.70 by Stuart Bishop
Remove authdb logic from staging Makefile and prepare for PostgreSQL 8.4
179
	${CREATEDB} --tablespace=${_SLAVE_TABLESPACE} ${_SLAVE}
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
180
	# Start the slon daemon for the slave
7675.395.70 by Stuart Bishop
Remove authdb logic from staging Makefile and prepare for PostgreSQL 8.4
181
	./slon_ctl.py --lag="0 seconds" start \
182
	    node2_node "dbname=${_SLAVE} user=slony"
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
183
	# Setup the slave
7675.395.168 by Stuart Bishop
Log staging database update log to a separate file during a full restore
184
	./new-slave.py 2 "dbname=${_SLAVE}" \
185
	    --log-file=INFO:${STAGING_LOGDIR}/dbupgrade.log
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
186
	# Upgrade all databases in the cluster and reset security.
7675.395.23 by Stuart Bishop
Add timestamps to staging rebuild steps
187
	@echo Running upgrade.py `date`
7675.395.168 by Stuart Bishop
Log staging database update log to a separate file during a full restore
188
	${SHHH} ../schema/upgrade.py \
189
	    --log-file=INFO:${STAGING_LOGDIR}/dbupgrade.log
7675.395.23 by Stuart Bishop
Add timestamps to staging rebuild steps
190
	@echo Running fti.py `date`
7675.395.168 by Stuart Bishop
Log staging database update log to a separate file during a full restore
191
	${SHHH} ../schema/fti.py \
192
	    --log-file=INFO:${STAGING_LOGDIR}/dbupgrade.log
7675.395.23 by Stuart Bishop
Add timestamps to staging rebuild steps
193
	@echo Running security.py `date`
9795.4.17 by Stuart Bishop
Handle deadlock that can occur when doing scripted updates (race condition)
194
	./slon_ctl.py stop # security.py can deadlock with slony
7675.395.168 by Stuart Bishop
Log staging database update log to a separate file during a full restore
195
	${SHHH} ../schema/security.py --cluster -U slony \
196
	    --log-file=INFO:${STAGING_LOGDIR}/dbupgrade.log
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
197
	# Restart slon daemons with default lag setting.
198
	./slon_ctl.py --lag="${LAG}" start
7675.58.6 by Stuart Bishop
Script to create new slave databases
199
	# Generate a preamble for manual slonik(1) usage.
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
200
	./preamble.py > preamble.sk
7675.58.9 by Stuart Bishop
Rebuild permissions on entire cluster only on request
201