~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
13465.2.40 by Stuart Bishop
Update staging rebuild to newer and suckier restore process
18
#  $ pg_dump --exclude-schema=_sl --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
13465.2.38 by Stuart Bishop
Dumps now using --exclude-schema in preparation for Slony-I 2.0 migration, causing spurious restore errors
58
# Set this to --exit-on-error once we have migrated Launchpad production
59
# to Slony-I 2.0.x.
60
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
61
7675.395.75 by Stuart Bishop
Review feedback
62
# Turn off output silencing so we can see details of staging deployments.
63
# Without the timestamps, we are unable to estimate production deployment
64
# times.
7675.395.59 by Stuart Bishop
Turn off silencing for now so we details on staging deployments
65
#SHHH=../../utilities/shhh.py
66
SHHH=
9795.4.1 by Stuart Bishop
Fix new-slave.py to work with split authdb/lpmain masters
67
5799.1.52 by Stuart Bishop
Commands to start/stop local slony daemons
68
default:
69
	echo Usage: make [start|stop|restart]
70
5799.1.56 by Stuart Bishop
Start/stop script for dev setup
71
start:
7178.4.4 by Stuart Bishop
Most of staging db build
72
	./slon_ctl.py --lag="${LAG}" start
5799.1.52 by Stuart Bishop
Commands to start/stop local slony daemons
73
5799.1.56 by Stuart Bishop
Start/stop script for dev setup
74
stop:
7675.58.10 by Stuart Bishop
Tidy and fix devsetup in Makefile
75
	./slon_ctl.py stop
5799.1.52 by Stuart Bishop
Commands to start/stop local slony daemons
76
5799.1.55 by Stuart Bishop
Improve initialize, less magic dev setup
77
restart: stop start
5799.1.52 by Stuart Bishop
Commands to start/stop local slony daemons
78
5799.1.57 by Stuart Bishop
developer setup tool
79
devsetup:
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
80
	make _prelim LPCONFIG=${DEV_CONFIG} \
81
	    _MASTER=launchpad_dev _SLAVE=launchpad_dev_slave LAG="0 seconds"
82
	
7178.4.4 by Stuart Bishop
Most of staging db build
83
	# Build the master database
84
	LPCONFIG=${DEV_CONFIG} make -C ../schema
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
85
	
86
	# Replicate it
87
	make _replicate LPCONFIG=${DEV_CONFIG} \
88
	    _MASTER=launchpad_dev _SLAVE=launchpad_dev_slave LAG="0 seconds"
7675.395.171 by Stuart Bishop
Revert unnecessary whitespace changes
89
	
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
90
	# Replicate it again, so we can test with multiple slaves.
91
	-${PGMASSACRE} launchpad_dev_slave2
7675.395.70 by Stuart Bishop
Remove authdb logic from staging Makefile and prepare for PostgreSQL 8.4
92
	${CREATEDB} launchpad_dev_slave2
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
93
	LPCONFIG=${DEV_CONFIG} ./slon_ctl.py start \
94
		 node3_node 'dbname=launchpad_dev_slave2 user=slony'
95
	LPCONFIG=${DEV_CONFIG} ./new-slave.py 3 launchpad_dev_slave2
96
	LPCONFIG=${DEV_CONFIG} ../schema/security.py -d launchpad_dev_slave2
97
	
98
	# Regenerate the preamble for manual slonik(1) usage.
99
	LPCONFIG=${DEV_CONFIG} ./preamble.py > preamble.sk
100
101
	# Restart slon daemons with default lag setting.
102
	LPCONFIG=${DEV_CONFIG} ./slon_ctl.py stop
103
	LPCONFIG=${DEV_CONFIG} ./slon_ctl.py --lag="${LAG}" start
104
7178.4.4 by Stuart Bishop
Most of staging db build
105
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
106
# Build _new staging databases from a production dump.
13465.2.40 by Stuart Bishop
Update staging rebuild to newer and suckier restore process
107
stagingsetup: DUMPLIST:=$(shell tempfile --suffix=.lst --prefix=lpstag)
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.
13465.2.40 by Stuart Bishop
Update staging rebuild to newer and suckier restore process
117
	# Unfortunately, we need to dump production databases using
118
	# --exclude-schema=_sl, or the read locks opened by pg_dump will
119
	# cause the replication to appear lagged and set off alerts.
120
	# This makes the restoration process more complex - a human being
121
	# can just restore the dump, scan the errors and confirm it worked.
122
	# For this automation, we need to massage the list of data to
123
	# restore from the dump to avoid the spurious errors and get a
124
	# valid error code.
125
	pg_restore --list ${STAGING_DUMP} | grep -v 'TRIGGER public _sl_' \
126
	    > ${DUMPLIST}
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
127
	pg_restore --dbname=lpmain_staging_new --no-owner ${EXIT_ON_ERROR} \
13465.2.40 by Stuart Bishop
Update staging rebuild to newer and suckier restore process
128
	    --use-list=${DUMPLIST} ${MULTIPROC} ${STAGING_DUMP}
129
	rm ${DUMPLIST}
7178.5.6 by Stuart Bishop
Ensure analyze starts *after* subscription by using a time delay
130
	# Uninstall Slony-I if it is installed - a pg_dump of a DB with
13465.2.40 by Stuart Bishop
Update staging rebuild to newer and suckier restore process
131
	# Slony-I installed isn't usable without this step. This is likely
132
	# irrelevant, as if our dump was created with --exclude-schema=_sl
133
	# it will not contain anything to remove.
7178.5.6 by Stuart Bishop
Ensure analyze starts *after* subscription by using a time delay
134
	LPCONFIG=${NEW_STAGING_CONFIG} ./repair-restored-db.py
7178.4.4 by Stuart Bishop
Most of staging db build
135
	# Setup replication
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
136
	make _replicate LPCONFIG=${NEW_STAGING_CONFIG} LAG="0 seconds" \
7178.4.4 by Stuart Bishop
Most of staging db build
137
	    _MASTER=lpmain_staging_new _SLAVE=lpmain_staging_slave_new \
138
	    _SLAVE_TABLESPACE=${STAGING_TABLESPACE}
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
139
140
# Switch the _new staging databases into place.
141
stagingswitch:
142
	# Stop Slony-I daemons - don't confuse the poor darlings.
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
143
	-LPCONFIG=${NEW_STAGING_CONFIG} ./slon_ctl.py stop
144
	-LPCONFIG=${STAGING_CONFIG} ./slon_ctl.py stop
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
145
	# Kill the existing staging database if it exists.
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
146
	-${PGMASSACRE} lpmain_staging
147
	-${PGMASSACRE} lpmain_staging_slave
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
148
	# Rename the newly build staging databases.
149
	psql -d template1 -c \
150
	    "ALTER DATABASE lpmain_staging_new RENAME TO lpmain_staging;"
151
	psql -d template1 -c "\
152
	    ALTER DATABASE lpmain_staging_slave_new \
153
	    RENAME TO lpmain_staging_slave;"
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
154
	# Fix the paths to match.
155
	psql -d lpmain_staging -U slony -c \
156
	"UPDATE _sl.sl_path SET pa_conninfo=replace(pa_conninfo, '_new', '')"
157
	psql -d lpmain_staging_slave -U slony -c \
158
	"UPDATE _sl.sl_path SET pa_conninfo=replace(pa_conninfo, '_new', '')"
159
	# Start the slon daemons, with requested lag.
160
	LPCONFIG=${STAGING_CONFIG} ./slon_ctl.py --lag="${LAG}" start
7178.4.4 by Stuart Bishop
Most of staging db build
161
13465.2.40 by Stuart Bishop
Update staging rebuild to newer and suckier restore process
162
dogfood: DUMPLIST:=$(shell tempfile --suffix=.lst --prefix=lpdogf)
9795.4.8 by Stuart Bishop
Fix staging db restore and add dogfood db restoration process
163
dogfood:
7675.395.70 by Stuart Bishop
Remove authdb logic from staging Makefile and prepare for PostgreSQL 8.4
164
	${CREATEDB} ${DOGFOOD_DBNAME}
13465.2.40 by Stuart Bishop
Update staging rebuild to newer and suckier restore process
165
	pg_restore --list ${DOGFOOD_DUMP} | grep -v 'TRIGGER public _sl_' \
166
	    > ${DUMPLIST}
167
	pg_restore --dbname=${DOGFOOD_DBNAME} --no-acl --no-owner \
168
	    --use-list=${DUMPLIST} ${EXIT_ON_ERROR} ${DOGFOOD_DUMP}
169
	rm ${DUMPLIST}
9795.4.9 by Stuart Bishop
Make repair-restored-db.py more robust and fix database restore ordering for staging and dogfood
170
	./repair-restored-db.py -d ${DOGFOOD_DBNAME}
9795.4.8 by Stuart Bishop
Fix staging db restore and add dogfood db restoration process
171
	../schema/upgrade.py -d ${DOGFOOD_DBNAME}
172
	../schema/security.py -d ${DOGFOOD_DBNAME}
173
7178.4.4 by Stuart Bishop
Most of staging db build
174
_prelim:
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
175
	@echo LPCONFIG currently ${LPCONFIG}
5799.1.57 by Stuart Bishop
developer setup tool
176
	# Create the slony PostgreSQL superuser if necessary.
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
177
	-createuser --superuser slony
5799.1.57 by Stuart Bishop
developer setup tool
178
	# 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.
179
	-./slon_ctl.py stop
5799.1.57 by Stuart Bishop
developer setup tool
180
	sleep 5
181
	# Drop any existing databases if they exist
7178.4.9 by Stuart Bishop
Staging setup procedure and fixes to db maintenance scripts
182
	${PGMASSACRE} ${_MASTER}
183
	${PGMASSACRE} ${_SLAVE}
7178.4.4 by Stuart Bishop
Most of staging db build
184
185
_replicate:
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
186
	@echo LPCONFIG currently ${LPCONFIG}
187
	# 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
188
	./slon_ctl.py --lag="0 seconds" start \
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
189
		 node1_node "dbname=${_MASTER} user=slony"
190
	# 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
191
	./initialize.py \
192
	    --log-file=INFO:${STAGING_LOGDIR}/dbupgrade.log
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
193
	# 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
194
	${CREATEDB} --tablespace=${_SLAVE_TABLESPACE} ${_SLAVE}
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
195
	# 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
196
	./slon_ctl.py --lag="0 seconds" start \
197
	    node2_node "dbname=${_SLAVE} user=slony"
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
198
	# Setup the slave
7675.395.168 by Stuart Bishop
Log staging database update log to a separate file during a full restore
199
	./new-slave.py 2 "dbname=${_SLAVE}" \
200
	    --log-file=INFO:${STAGING_LOGDIR}/dbupgrade.log
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
201
	# Upgrade all databases in the cluster and reset security.
7675.395.23 by Stuart Bishop
Add timestamps to staging rebuild steps
202
	@echo Running upgrade.py `date`
7675.395.168 by Stuart Bishop
Log staging database update log to a separate file during a full restore
203
	${SHHH} ../schema/upgrade.py \
204
	    --log-file=INFO:${STAGING_LOGDIR}/dbupgrade.log
7675.395.23 by Stuart Bishop
Add timestamps to staging rebuild steps
205
	@echo Running security.py `date`
9795.4.17 by Stuart Bishop
Handle deadlock that can occur when doing scripted updates (race condition)
206
	./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
207
	${SHHH} ../schema/security.py --cluster -U slony \
208
	    --log-file=INFO:${STAGING_LOGDIR}/dbupgrade.log
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
209
	# Restart slon daemons with default lag setting.
210
	./slon_ctl.py --lag="${LAG}" start
7675.58.6 by Stuart Bishop
Script to create new slave databases
211
	# Generate a preamble for manual slonik(1) usage.
7675.85.2 by Jonathan Lange
Undo revision generated by step 2 of process.
212
	./preamble.py > preamble.sk
7675.58.9 by Stuart Bishop
Rebuild permissions on entire cluster only on request
213