Project

General

Profile

1 5459 aaronmk
root := .
2 3762 aaronmk
include lib/common.Makefile
3
4
5 784 aaronmk
##### Vars/functions
6
7 244 aaronmk
# Make
8 247 aaronmk
SHELL := /bin/bash
9 625 aaronmk
subMake = $(MAKE) $(call subPath,$@) --directory=$(call topDir,$@)
10 244 aaronmk
11 4607 aaronmk
# Paths
12
bin := bin
13
14 784 aaronmk
##### Environment
15
16 225 aaronmk
export PGOPTIONS = --client-min-messages=WARNING
17
18 784 aaronmk
##### General targets
19 225 aaronmk
20 1274 aaronmk
remake: _always clean
21
	$(MAKE)
22
# re-run make so that cache of existing files is reset
23
24 1288 aaronmk
%/remake: _always
25
	$(MAKE) $(@D)/clean
26
	$(MAKE) $(@D)/
27
# re-run make so that cache of existing files is reset
28
29 1813 aaronmk
%-remake: _always
30
	rm -f $*
31
	$(MAKE) $*
32
# re-run make so that cache of existing files is reset
33
34 2981 aaronmk
%/reinstall: _always %/uninstall %/install ;
35
36 784 aaronmk
##### Installation
37 241 aaronmk
38 6102 aaronmk
# public must be installed *after* inputs because some views depend on inputs
39
install: _always core mysql inputs/download inputs/install schemas/install test
40 241 aaronmk
	@$(done)
41 202 aaronmk
42 6200 aaronmk
uninstall: _always rm_mysql rm_core ;
43 241 aaronmk
44 418 aaronmk
reinstall: _always uninstall install ;
45 225 aaronmk
46 784 aaronmk
##### Core: VegBIEN DB and dependencies
47 202 aaronmk
48 1746 aaronmk
core: _always $(call forOs,python php postgres) db
49 241 aaronmk
	@$(done)
50 235 aaronmk
51 1746 aaronmk
rm_core: _always rm_db ;
52 241 aaronmk
53 418 aaronmk
reinstall_core: _always rm_core core ;
54 241 aaronmk
55 784 aaronmk
##### Python
56
57 383 aaronmk
python-Linux: _always
58 6544 aaronmk
	-sudo apt-get --yes install python
59
	-sudo apt-get --yes install python3
60
	-sudo apt-get --yes install python-dev
61
	-sudo apt-get --yes install python-dateutil
62
	-sudo apt-get --yes install python-parallel
63
	-sudo apt-get --yes install python-profiler
64
	-sudo apt-get --yes install python-pip
65
	-sudo apt-get --yes install pymetrics
66 3156 aaronmk
	-sudo pip install ordereddict
67 1845 aaronmk
	-sudo pip install pp
68 284 aaronmk
69 383 aaronmk
python-Darwin: _always
70 6190 aaronmk
	@echo $(emph)'Installing Python 3.2 on Mac OS X:'$(endEmph)
71
	@echo 'Download it: http://www.python.org/ftp/python/3.2.3/python-3.2.3-macosx10.6.dmg'
72
	@echo 'Open the disk image'
73
	@echo 'Run the installer in it'
74
	@$(wait)
75 284 aaronmk
	@echo $(emph)'Installing python-dateutil on Mac OS X:'$(endEmph)
76
	@echo 'Download it: http://labix.org/download/python-dateutil/python-dateutil-1.5.tar.gz'
77
	@echo 'Extract the archive'
78
	@echo 'Change to the directory of the extracted files'
79
	@echo 'Run python setup.py build'
80
	@echo 'Run sudo python setup.py install'
81
	@echo "Run python -c 'import dateutil'"
82
	@$(wait)
83 3156 aaronmk
	@echo $(emph)'Installing pip on Mac OS X:'$(endEmph)
84
	@echo 'Follow the instructions at http://www.pip-installer.org/en/latest/installing.html'
85
	@$(wait)
86 284 aaronmk
87 1599 aaronmk
##### PHP
88
89
php-Linux: _always
90 6544 aaronmk
	-sudo apt-get --yes install php-http-request
91 1599 aaronmk
92
php-Darwin: _always
93 1600 aaronmk
	@echo $(emph)'Installing PHP PEAR and HTTP_Request on Mac OS X:'$(endEmph)
94
	@echo 'Download http://pear.php.net/go-pear.phar'
95
	@echo 'Change to the directory of the downloaded file'
96
	@echo 'Run php -d detect_unicode=0 go-pear.phar'
97
	@echo 'Whenever prompted, press Enter to select default options'
98
	@echo 'Add $$HOME/pear/bin to your $$PATH'
99
	@echo 'Add $(HOME)/pear/share/pear to your php.ini include_path'
100 1665 aaronmk
	@echo 'If needed, set $$PHPRC to your php.ini'
101 1600 aaronmk
	@echo "Run pear install HTTP_Request"
102
	@echo 'To run a php command: php -c <php.ini-path> ...'
103 1599 aaronmk
	@$(wait)
104
105 784 aaronmk
##### PostgreSQL
106
107 6824 aaronmk
define editPhppgadmin
108 6853 aaronmk
-sudo patch /etc/phppgadmin/config.inc.php lib/phpPgAdmin.config.inc.php.diff
109 6824 aaronmk
sudo cp -n /usr/share/phppgadmin/login.php /usr/share/phppgadmin/login.php.old
110 6853 aaronmk
-sudo patch /usr/share/phppgadmin/login.php lib/phpPgAdmin.login.php.diff
111 6824 aaronmk
endef
112 6853 aaronmk
# ignore errors if patch has already been applied
113 6824 aaronmk
114 1970 aaronmk
editPhppgadminApacheConf = echo $$'1\n,s/deny from all/allow from all/\nwq'|\
115 336 aaronmk
sudo ed --loose-exit-status --verbose /etc/phppgadmin/apache.conf
116 334 aaronmk
117
apacheConf := /etc/apache2/apache2.conf
118
apacheConfPhppgadminLine := Include /etc/phppgadmin/apache.conf
119
editApacheConfForPhppgadmin =\
120
$(if $(shell grep -F "$(apacheConfPhppgadminLine)" $(apacheConf)),,\
121
$(call sudoAppend,$(apacheConf),\n$(apacheConfPhppgadminLine)))
122
123 335 aaronmk
nonApacheOnPort80 = $(shell sudo lsof -i :80|tail -n +2|grep -v -F apache2)
124 336 aaronmk
editApachePortsConf = echo $$'1\n,s/\\b80\\b/8080/g\nwq'|\
125
sudo ed --loose-exit-status --verbose /etc/apache2/ports.conf
126 334 aaronmk
127 6197 aaronmk
asAdmin := sudo -E -u postgres
128 6337 aaronmk
asBien := sudo -E -u bien
129 4752 aaronmk
pgVersion = "$$(env minor=1 bin/pg_version)"
130
131 6198 aaronmk
linuxPostgresDir := /etc/postgresql/9.1/main
132 6201 aaronmk
define pg_ctl-Linux
133
$(asAdmin) /etc/init.d/postgresql $(1)
134
sleep 2
135
endef
136 6198 aaronmk
137 6196 aaronmk
macPostgresDir := /Library/PostgreSQL/9.1
138 6187 aaronmk
define macUsePostgresLib
139
sudo mv $(libDest) $(libDest).old||sudo rm -f $(libDest)
140 6196 aaronmk
sudo ln -s $(macPostgresDir)/lib/$(1) $(libDest)
141 6187 aaronmk
142
endef
143
libDest = /usr/lib/$(1)
144
145
macPostgresLibs := libcrypto libssl
146
macPostgresLibs := $(macPostgresLibs) $(macPostgresLibs:%=%.1.0.0)
147
macPostgresLibs := $(macPostgresLibs:%=%.dylib)
148
149 6201 aaronmk
define pg_ctl-Darwin
150
$(asAdmin) pg_ctl -D $(macPostgresDir)/data -l $(macPostgresDir)/postgres.log $(1)
151
sleep 2
152
endef
153 6197 aaronmk
154 6503 aaronmk
confirmPostgresReload = $(call confirm,Modifying postgresql.conf and pg_hba.conf)
155
156 6333 aaronmk
define postgresReload-Linux
157 6503 aaronmk
@$(confirmPostgresReload)
158
@echo $(emph)'kernel.shmmax must be set to at least 4GB minus 1 byte:'$(endEmph)
159
@echo 'Append the following to /etc/sysctl.conf:'
160
@echo 'kernel.shmmax=4294967295'
161
@$(wait)
162 6447 aaronmk
chmod a+r schemas/*.conf || $(asBien) chmod a+r schemas/*.conf
163 6823 aaronmk
-$(asAdmin) mv -n $(linuxPostgresDir)/postgresql.conf $(linuxPostgresDir)/postgresql.conf.old
164 6333 aaronmk
$(asAdmin) cp --preserve=timestamps schemas/postgresql.conf $(linuxPostgresDir)/postgresql.conf
165 6823 aaronmk
-$(asAdmin) mv -n $(linuxPostgresDir)/pg_hba.conf $(linuxPostgresDir)/pg_hba.conf.old
166 6335 aaronmk
$(asAdmin) cp --preserve=timestamps schemas/pg_hba.conf $(linuxPostgresDir)/pg_hba.conf
167 6333 aaronmk
$(call pg_ctl-Linux,restart)
168
169
endef
170
171
define postgresReload-Darwin
172 6503 aaronmk
@$(confirmPostgresReload)
173 6336 aaronmk
chmod a+r schemas/*.conf
174 6823 aaronmk
-$(asAdmin) mv -n $(macPostgresDir)/data/postgresql.conf $(macPostgresDir)/data/postgresql.conf.old
175 6333 aaronmk
$(asAdmin) cp schemas/postgresql.Mac.conf $(macPostgresDir)/data/postgresql.conf
176 6823 aaronmk
-$(asAdmin) mv -n $(macPostgresDir)/data/pg_hba.conf $(macPostgresDir)/data/pg_hba.conf.old
177 6335 aaronmk
$(asAdmin) cp schemas/pg_hba.Mac.conf $(macPostgresDir)/data/pg_hba.conf
178 6333 aaronmk
$(call pg_ctl-Darwin,restart)
179
180
endef
181
182 6334 aaronmk
postgres_reload: _always
183
	$($(call forOs,postgresReload))
184
185 6333 aaronmk
postgres-Linux: _always
186 6544 aaronmk
	-sudo apt-get --yes install postgresql
187
	-sudo apt-get --yes install postgresql-contrib
188
	-sudo apt-get --yes install postgresql-plpython3-$(pgVersion)
189
	-sudo apt-get --yes install libpq-dev
190
	-sudo apt-get --yes install postgresql-$(pgVersion)-postgis
191
	-sudo apt-get --yes install phppgadmin
192 6824 aaronmk
	$(editPhppgadmin)
193 6333 aaronmk
	$(editPhppgadminApacheConf)
194
	$(editApacheConfForPhppgadmin)
195
	$(if $(nonApacheOnPort80),$(editApachePortsConf))
196
	-sudo apache2ctl restart
197
	-sudo pip install psycopg2
198 6503 aaronmk
	-$(MAKE) $(postgresReload-Linux)
199 6333 aaronmk
# run pg_version inline because it needs postgresql to be installed first
200
# ignore errors if conf files already edited
201
202 383 aaronmk
postgres-Darwin: _always
203 254 aaronmk
	@echo $(emph)'Installing PostgreSQL on Mac OS X:'$(endEmph)
204
	@echo 'Download it using the topmost "Mac OS X" link at http://http://www.enterprisedb.com/products-services-training/pgdownload'
205
	@echo 'Open the disk image'
206
	@echo 'Run the installer in it'
207
	@$(wait)
208 6187 aaronmk
	$(foreach lib,$(macPostgresLibs),$(call macUsePostgresLib,$(lib)))
209 6503 aaronmk
	-$(MAKE) $(postgresReload-Darwin)
210 235 aaronmk
211 418 aaronmk
postgres-: _always ; # other OSes
212 241 aaronmk
213 247 aaronmk
psqlOpts := --set ON_ERROR_STOP=1 --quiet
214 6193 aaronmk
psqlAsAdmin := $(asAdmin) psql $(psqlOpts)
215 1980 aaronmk
    # -E preserves env vars so PGOPTIONS is passed to psql
216 2624 aaronmk
psqlAsAdminVegbien := $(psqlAsAdmin) vegbien
217 6914 aaronmk
psqlNoSearchPath := env no_search_path=1 bin/psql_script_vegbien
218 272 aaronmk
bienPassword := $(shell cat config/bien_password)
219 6331 aaronmk
bienReadPassword := $(shell cat config/bien_read_password)
220 241 aaronmk
221 784 aaronmk
##### VegBIEN DB
222 241 aaronmk
223 6331 aaronmk
#### DB and users
224 1967 aaronmk
225 2090 aaronmk
confirmRmDb = $(call confirm,WARNING: This will delete your entire VegBIEN\
226 3374 aaronmk
DB!,This includes all archived imports and staging tables.)
227 2089 aaronmk
228 2633 aaronmk
confirmRmPublicSchema = $(call confirm,WARNING: This will delete the current\
229
public schema of your VegBIEN DB!,To save it: make schemas/rotate)
230 2089 aaronmk
231 2091 aaronmk
rmPublicSchema := $(call rmSchemaCmd,public)
232 1967 aaronmk
233 6102 aaronmk
db: mk_db rm_initial_public _always ;
234 1967 aaronmk
235
mk_db: _always
236 6822 aaronmk
	-echo "CREATE USER public_;"|$(psqlAsAdmin)
237 1745 aaronmk
	-echo "CREATE USER bien PASSWORD '$(bienPassword)';"|$(psqlAsAdmin)
238 6331 aaronmk
	-echo "CREATE USER bien_read PASSWORD '$(bienReadPassword)' IN ROLE bien \
239
NOINHERIT;"|$(psqlAsAdmin)
240 2037 aaronmk
	-echo "CREATE DATABASE vegbien WITH OWNER bien TEMPLATE template1 \
241 1940 aaronmk
ENCODING 'UTF8' LC_COLLATE 'en_US.UTF-8' LC_CTYPE 'en_US.UTF-8';"|$(psqlAsAdmin)
242 6221 aaronmk
	-echo "CREATE EXTENSION hstore SCHEMA pg_catalog;"|$(psqlAsAdminVegbien)
243 6189 aaronmk
	-$(asAdmin) createlang plpython3u vegbien
244 2627 aaronmk
# ignore errors if user/database/etc. exists
245 2091 aaronmk
246
rm_initial_public: _always
247 2089 aaronmk
	@$(confirmRmPublicSchema)
248 2624 aaronmk
	echo $(rmPublicSchema)|$(psqlAsAdminVegbien)
249 1967 aaronmk
# drop public schema *as admin* because it starts out owned by postgres
250 241 aaronmk
251 383 aaronmk
rm_db: _always
252 1748 aaronmk
	@$(confirmRmDb)
253
	echo "DROP DATABASE IF EXISTS vegbien;"|$(psqlAsAdmin)
254 6331 aaronmk
	echo "DROP USER IF EXISTS bien_read;"|$(psqlAsAdmin)
255 1748 aaronmk
	echo "DROP USER IF EXISTS bien;"|$(psqlAsAdmin)
256 6822 aaronmk
	echo "DROP USER IF EXISTS public_;"|$(psqlAsAdmin)
257 241 aaronmk
258 418 aaronmk
reinstall_db: _always rm_db db ;
259 241 aaronmk
260 2631 aaronmk
#### Schemas
261 1967 aaronmk
262 2980 aaronmk
schemas := temp functions py_functions public
263 2631 aaronmk
schemas/install: $(schemas:%=schemas/%/install) ;
264
265 2980 aaronmk
schemasReversed := public py_functions functions temp
266 2631 aaronmk
schemas/uninstall: $(schemasReversed:%=schemas/%/uninstall) ;
267
268
### public
269
270
schemas/public/install: schemas/vegbien.sql _always
271 6098 aaronmk
	-echo $(call mkSchemaCmd,public)|$(psqlNoSearchPath)
272 6192 aaronmk
	<$< $(psqlNoSearchPath)
273 1967 aaronmk
# ignore errors if schema exists
274
# public schema will be owned by bien
275 6088 aaronmk
# don't include public in the search_path
276 1967 aaronmk
277 2631 aaronmk
schemas/public/uninstall: _always
278 2089 aaronmk
	@$(confirmRmPublicSchema)
279 6098 aaronmk
	echo $(rmPublicSchema)|$(psqlNoSearchPath)
280 1967 aaronmk
281 5678 aaronmk
schemas/rename/%: _always
282 6098 aaronmk
	echo 'ALTER SCHEMA public RENAME TO "$*";'|$(psqlNoSearchPath)
283 5678 aaronmk
	$(MAKE) schemas/public/install
284
285 5679 aaronmk
schemas/rotate: _always schemas/rename/public.$(version) ;
286 2034 aaronmk
287 2631 aaronmk
### py_functions
288 2091 aaronmk
289 2631 aaronmk
schemas/py_functions/install: schemas/py_functions.sql _always
290 6098 aaronmk
	-<$< env public= $(psqlAsAdminVegbien)
291 3376 aaronmk
# ignore errors if schema exists
292 2631 aaronmk
293
### Others
294
295
schemas/%/install: schemas/%.sql _always
296 6098 aaronmk
	-<$< $(psqlNoSearchPath)
297 3376 aaronmk
# ignore errors if schema exists
298 2091 aaronmk
299 3393 aaronmk
schemas/%/uninstall: _always
300 6098 aaronmk
	echo $(call rmSchemaCmd,$*)|$(psqlNoSearchPath)
301 2624 aaronmk
302 2983 aaronmk
# Needed on Ubuntu 12.04 (also other Linuxes?) because %/reinstall is ignored.
303
schemas/temp/reinstall: _always schemas/temp/uninstall schemas/temp/install ;
304
305 784 aaronmk
##### MySQL
306 241 aaronmk
307 383 aaronmk
mysql: _always $(call forOs,mysql) mysql_user
308 241 aaronmk
	@$(done)
309
310 418 aaronmk
rm_mysql: _always rm_mysql_user ;
311 241 aaronmk
312 383 aaronmk
mysql-Linux: _always
313 244 aaronmk
	@echo $(emph)"If asked for MySQL root password, enter $(bienPassword)"$(endEmph)
314 238 aaronmk
	@$(wait)
315 6544 aaronmk
	-sudo apt-get --yes install mysql-server mysql-client python-mysqldb
316 235 aaronmk
317 383 aaronmk
mysql-Darwin: _always
318 244 aaronmk
	@echo $(emph)'Installing MySQLdb Python driver on Mac OS X 10.7 (may work on other versions):'$(endEmph)
319 235 aaronmk
	@echo 'Download it using "latest version" link at http://sourceforge.net/projects/mysql-python/files/'
320
	@echo 'Extract the archive'
321
	@echo '(From http://www.rustyrazorblade.com/2011/11/installing-mysqldb-on-macos-lion/:)'
322 284 aaronmk
	@echo 'Run ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib'
323
	@echo 'Run ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql'
324 235 aaronmk
	@echo '(From http://blog.infoentropy.com/MySQL-python_EnvironmentError_mysql_config_not_found:)'
325
	@echo 'Edit site.cfg and change the line like "mysql_config = " to "mysql_config = /usr/local/mysql/bin/mysql_config"'
326
	@echo '(From http://www.mangoorange.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/:)'
327
	@echo 'Change to the directory of the extracted files'
328 284 aaronmk
	@echo 'Run python setup.py clean'
329
	@echo 'Run python setup.py build'
330 235 aaronmk
	@echo 'Run sudo python setup.py install'
331
	@echo "Run python -c 'import MySQLdb'"
332 238 aaronmk
	@$(wait)
333 235 aaronmk
334 383 aaronmk
mysql-: _always # other OSes
335 235 aaronmk
336 247 aaronmk
mysqlAsRoot := mysql --user=root --password='$(bienPassword)'
337 235 aaronmk
338 383 aaronmk
mysql_user: _always
339 241 aaronmk
	-$($@_cmd)
340 364 aaronmk
# ignore errors if user exists
341 244 aaronmk
mysql_user_cmd = echo "CREATE USER 'bien'@'localhost' IDENTIFIED BY \
342 245 aaronmk
'$(bienPassword)';"|$(mysqlAsRoot)
343 202 aaronmk
344 383 aaronmk
rm_mysql_user: _always
345 245 aaronmk
	-echo "DROP USER 'bien'@'localhost';"|$(mysqlAsRoot)
346 364 aaronmk
# ignore errors if user exists
347 202 aaronmk
348 784 aaronmk
##### Datasources
349 202 aaronmk
350 418 aaronmk
inputs: _always inputs/all ;
351 245 aaronmk
352 6191 aaronmk
inputs/install: _always
353 6194 aaronmk
	echo '. bin/reinstall_all; wait'|bash -s
354 6191 aaronmk
355 1542 aaronmk
import: _always import-msg inputs/import ;
356
import-msg: _always
357 1550 aaronmk
	@echo $(emph)"To import all inputs at once:"$(endEmph) . bin/import_all
358 1542 aaronmk
	@echo
359 250 aaronmk
360 1458 aaronmk
verify: _always inputs/verify
361 1241 aaronmk
	@$(done)
362 386 aaronmk
363 1458 aaronmk
test: _always inputs/test
364 397 aaronmk
	@$(done)
365 1458 aaronmk
366
##### Testing
367
368 1743 aaronmk
test-all: _always remake test missing_mappings
369 1458 aaronmk
	@$(done)
370 1967 aaronmk
371
##### Subdir forwarding
372
373
# Must come last so overridden targets are not forwarded
374
375
define subdirTargets
376
$(subdir): _always
377
	+$$(subMake)
378
379
$(subdir)%: _always
380
	+$$(subMake)
381
.PRECIOUS: $(subdir)% # let subdir's Makefile decide whether to delete on error
382
383
$(subdir)reinstall: _always $(subdir)uninstall $(subdir)install ;
384
endef
385
$(foreach subdir,$(wildcard */),$(eval $(subdirTargets)))
386
387
Makefile: ;
388
389
%: $(addsuffix %,$(dir $(shell echo */Makefile))) _always ;
390 1999 aaronmk
391
#### Maps validation
392
393 4607 aaronmk
termsSubdirs := $(call no/,$(call wildcard/,inputs/*/))
394
395 3764 aaronmk
include lib/mappings.Makefile